// sound stretching. // // 8 sounds were added at the last minute so code is a bit longwinded. // an array would probably look better. // // sounds pretty heinous. ;) Playhead playhead; Sample sound1; Sample sound2; Sample sound3; Sample sound4; Sample sound5; Sample sound6; Sample sound7; Sample sound8; Snip snip1; Snip snip2; Snip snip3; Snip snip4; Snip snip5; Snip snip6; Snip snip7; Snip snip8; void setup(){ size(512,100); Sonia.start(this); playhead = new Playhead(); sound1 = new Sample("bjork.aiff"); sound2 = new Sample("bjork.aiff"); sound3 = new Sample("bjork.aiff"); sound4 = new Sample("bjork.aiff"); sound5 = new Sample("bjork.aiff"); sound6 = new Sample("bjork.aiff"); sound7 = new Sample("bjork.aiff"); sound8 = new Sample("bjork.aiff"); snip1 = new Snip(sound1, 29); snip2 = new Snip(sound2, 35); snip3 = new Snip(sound3, 41); snip4 = new Snip(sound4, 47); snip5 = new Snip(sound5, 53); snip6 = new Snip(sound6, 59); snip7 = new Snip(sound7, 65); snip8 = new Snip(sound8, 71); } void loop(){ playhead.exist(); drawBackground(); snip1.exist(); snip2.exist(); snip3.exist(); snip4.exist(); snip5.exist(); snip6.exist(); snip7.exist(); snip8.exist(); } void drawBackground(){ background(80,0,0); fill(51,0,0); noStroke(); rect(playhead.x, 0, width - playhead.x, height); if (playhead.mouseOver || playhead.draggable){ stroke(255,0,0); strokeWeight(4); line(playhead.x, 0, playhead.x, height); } else { stroke(153,0,0); strokeWeight(1); line(playhead.x, 0, playhead.x, height); } } class Playhead { int x = 256; boolean draggable = false; boolean mouseOver = false; Playhead (){} void exist(){ if (mouseX > (x - 5) && mouseX < (x + 5)){ mouseOver = true; } else { mouseOver = false; } if (mouseOver && mousePressed){ draggable = true; } if (draggable){ drag(); } if (!mousePressed){ draggable = false; } } void drag(){ x -= (x - mouseX) * .1f; } } class Snip { Sample forwardSound; Sample reverseSound; float[] forwardArray; float[] reverseArray; int soundLength; // x positions of the endpoints float x1p = random(10, width-10); float x2p = random(10, width-10); float y1p, y2p; // velocities of the endpoints float x1v = random(1.0f, 2.0f); float x2v = random(-2.0f, -1.0f); float dist; // distance between endpoints float newSeg; // distance between x1p and playhead float oldSeg; // previous value of newSeg float speedDist; // float xVar; // float oVar; Snip (Sample mySound, float y){ forwardSound = mySound; soundLength = forwardSound.getNumFrames(); reverseSound = new Sample(soundLength); forwardArray = new float[soundLength]; reverseArray = new float[soundLength]; forwardSound.read(forwardArray); int counter = 0; for (int i = soundLength - 1; i >= 0; i--){ reverseArray[counter] = forwardArray[i]; counter ++; } reverseSound.write(reverseArray); y1p = y; y2p = y; } void exist(){ move(); findPlayFrames(); render(); } void move(){ x1p += x1v; x2p += x2v; if (x1p < 10 || x1p > (width - 10)){ x1v *= -1f; } if (x2p < 10 || x2p > (width - 10)){ x2v *= -1f; } } void findPlayFrames(){ oldSeg = newSeg; if (x1p < playhead.x && x2p > playhead.x){ newSeg = playhead.x - x1p; } else if (x2p < playhead.x && x1p > playhead.x){ newSeg = playhead.x - x2p; } dist = abs(x2p - x1p); int xVar = int((soundLength * newSeg)/dist); int oldxVar = int((soundLength * oldSeg)/dist); speedDist = abs(xVar - oldxVar); forwardSound.setRate(speedDist * 10.0f); reverseSound.setRate(speedDist * 10.0f); if (xVar > oldxVar){ if (x1p < playhead.x){ forwardSound.play(oldxVar,xVar); } else { reverseSound.play(oldxVar,xVar); } } else { if (x1p < playhead.x){ reverseSound.play(xVar,oldxVar); } else { forwardSound.play(xVar,oldxVar); } } } void render(){ // draw connecting line strokeWeight(1); if (x1p < playhead.x && x2p < playhead.x){ stroke(0); line(x1p,y1p,x2p,y2p); } else if (x1p > playhead.x && x2p > playhead.x){ stroke(102,0,0); line(x1p,y1p,x2p,y2p); } else if (x1p < playhead.x && x2p >= playhead.x){ stroke(0); line(x1p,y1p,playhead.x,y2p); stroke(102,0,0); line(playhead.x, y1p, x2p, y2p); stroke(255); point(playhead.x, y1p); } else { stroke(102,0,0); line(x1p,y1p,playhead.x,y2p); stroke(0); line(playhead.x, y1p, x2p, y2p); stroke(255); point(playhead.x, y1p); } //draw left dot stroke(0); fill(150,0,0); strokeWeight(1); rect(x1p - 2, y1p - 2, 4, 4); //draw right dot stroke(102,0,0); fill(0); strokeWeight(1); rect(x2p - 2, y2p - 2, 4, 4); } } // Safely close the sound engine upon Browser shutdown. public void stop(){ Sonia.stop(); super.stop(); }