import ptext.*; import pitaru.sonia_v2_9.*; // Section 1 - Declare Variables ==================================== int SAMPLE_LENGTH_MILLIS = 3250; PFont font; int mode = 5; int timeStamp; int[] alphas = {255,200,150,100,50,15}; PText[] ptexts = new PText[6]; Sample[] samples = new Sample[6]; // Section 2 - Code to run once ===================================== void setup() { // Set the sketch size (in pixels) size(750,250); // Load the font from your sketches data directory font = loadFont("AbadiMT-CondensedExtraBold-32.vlw"); // Set the current font and font-size textFont(font, 32); Sonia.start(this); for (int i = 0;i < stanzas.length; i++) { for (int j = 0;j < samples.length; j++) { String sFile = i+"_"+j+".wav"; stanzas[i].samples[j] = new Sample(sFile); } } nextLine(); } void stop() { Sonia.stop(); } // Section 3 - Code to run each frame =============================== void draw() { background(0); PText.drawAll(PText.LEFT); if (millis() - SAMPLE_LENGTH_MILLIS > timeStamp) { nextLine(); } } void setAlphas() { if (mode == 0) { alphas[0] = 255; alphas[1] = 100; alphas[2] = 75; alphas[3] = 50; alphas[4] = 25; alphas[5] = 15; } else if (mode == 1) { alphas[0] = 50; alphas[1] = 255; alphas[2] = 100; alphas[3] = 75; alphas[4] = 50; alphas[5] = 25; } else if (mode == 2) { alphas[0] = 25; alphas[1] = 50; alphas[2] = 255; alphas[3] = 100; alphas[4] = 75; alphas[5] = 50; } else if (mode == 3) { alphas[0] = 15; alphas[1] = 25; alphas[2] = 50; alphas[3] = 255; alphas[4] = 100; alphas[5] = 75; } else if (mode == 4) { alphas[0] = 10; alphas[1] = 15; alphas[2] = 25; alphas[3] = 50; alphas[4] = 255; alphas[5] = 100; } else if (mode == 5) { alphas[0] = 7; alphas[1] = 10; alphas[2] = 15; alphas[3] = 25; alphas[4] = 50; alphas[5] = 255; } // set the fill color for each ptext for (int i = 0;i < ptexts.length; i++) { ptexts[i].fadeTo(255,255,255,alphas[i], 2.0); } } void nextStanza() { mode = 0; AudibleLine[] lines = getUniqueEndingsStanza(); for (int i = 0;i < lines.length; i++) { if (ptexts[i]==null) ptexts[i] = new PText(this); ptexts[i].setPosition(25, 50+(i*30)); ptexts[i].setText(lines[i].text); if (samples[i] != null) samples[i].stop(); samples[i] = lines[i].sample; samples[i].setVolume(0); } samples[mode].setVolume(1); samples[mode].play(); println("new stanza @ "+timeStamp/1000f+"s"); } void nextLine() { mode++; if (mode == 6) nextStanza(); // start the correct sample for (int i = 0;i < samples.length; i++) { samples[i].setVolume(0); samples[i].play(); } samples[mode].setVolume(1); timeStamp = millis(); if (mode == 5) timeStamp += SAMPLE_LENGTH_MILLIS/3; setAlphas(); } // This is a container for one Stanza class Stanza { int id; String[] lines = new String[6]; Sample[] samples = new Sample[6]; Stanza(int id, String ln1, String ln2, String ln3, String ln4, String ln5, String ln6) { this.id = id; lines[0] = ln1; lines[1] = ln2; lines[2] = ln3; lines[3] = ln4; lines[4] = ln5; lines[5] = ln6; } } // This is a container for 1 line + 1 sample class AudibleLine { String text; Sample sample; AudibleLine(String t, Sample s) { text = t; sample = s; } } AudibleLine[] getUniqueEndingsStanza() { int successes = 0; AudibleLine[] result = new AudibleLine[6]; int[] choices = { 0,1,2,3,4,5,6,7,8 }; while (successes < 6) { int num = choices[(int)random(0, choices.length)]; if (num < 0) continue; Stanza currentStanza = stanzas[num]; result[successes] = randomFromStanza(currentStanza); choices[num] = -1; successes++; } return result; } AudibleLine randomFromStanza(Stanza chosenStanza) { // Pick a random line # from stanza int lineNo = (int)random(chosenStanza.lines.length); return new AudibleLine(chosenStanza.lines[lineNo],chosenStanza.samples[lineNo]); }