Sample theSound; Sample revSound; float xPlayHead = 256; void setup(){ size(512,100); Sonia.start(this); // Start Sonia engine. // create a new sample object. theSound = new Sample("bjork.aiff"); int sampleLength = theSound.getNumFrames(); revSound = new Sample(sampleLength); float[] data = new float[sampleLength]; float[] other = new float[sampleLength]; theSound.read(data); int counter = 0; for (int i = sampleLength - 1; i>=0; i--){ other[counter] = data[i]; counter ++; } revSound.write(other); } void loop(){ background(80,0,0); fill(51,0,0); noStroke(); rect(width/2, 0, width/2, height); moveMeanderer(); } float x1p = 206f; float x1v = random(-2.0f,-1.0f); float x2p = 306f; float x2v = random(1.0f,2.0f); void moveMeanderer(){ x1p += x1v; x2p += x2v; if (x1p < 10 || x1p > (xPlayHead - 10)){ x1v *= -1f; } if (x2p < (xPlayHead + 10) || x2p > (width - 10)){ x2v *= -1f; } findDistance(x1p,x2p); renderStuff(); } float dist = 100; float oldDist = 100; boolean playing = false; void findDistance(float x1, float x2){ oldDist = dist; dist = x2 - x1; findPlayFrames(dist, oldDist, x1, x2); } float speedDist; float seg, oldSeg; void findPlayFrames(float d, float od, float x1, float x2){ oldSeg = seg; seg = 256 - x1; int xVar = int((88200 * seg)/d); int oldxVar = int((88200 * oldSeg)/d); speedDist = abs(xVar - oldxVar); theSound.setRate(speedDist * 4.0f); revSound.setRate(speedDist * 4.0f); if (xVar > oldxVar){ theSound.play(oldxVar,xVar); } else { revSound.play(xVar,oldxVar); } } void renderStuff(){ // draw connecting line stroke(0); noFill(); strokeWeight(1); line(x1p,50,xPlayHead,50); stroke(102,0,0); line(xPlayHead,50,x2p,50); //draw left dot stroke(0); fill(150,0,0); strokeWeight(1); rect(x1p - 2, 48, 4, 4); //draw right dot stroke(102,0,0); fill(0); strokeWeight(1); rect(x2p - 2, 48, 4, 4); } // Safely close the sound engine upon Browser shutdown. public void stop(){ Sonia.stop(); super.stop(); }