// applet size variables int xSize = 400; int ySize = 300; int xMid = xSize/2; int yMid = ySize/2; int frameRate = 30; int counter = 0; Camera camera; Sound sound; SpikeBall spikeBall; boolean micInput = true; boolean blur = false; BImage bBlur; Convolver c; float[] clearZ; void setup(){ size(xSize, ySize); //lights(); //smooth(); framerate(frameRate); ellipseMode(CENTER_RADIUS); colorMode(HSB,255); Sonia.start(this); // create camera (distance, lensAngle) camera = new Camera(50.0, 60.0); // create sound (sampleName, bands, history, speed, degrade) sound = new Sound("none", 512, 200, 5, 0.99); if (micInput){ LiveInput.start(sound.bands); LiveInput.useEqualizer(true); LiveInput.useEnvelope(true,1.5); } // create spikeBall (numSpikes, spikeLength, speed) spikeBall = new SpikeBall(50, 200, 5); bBlur = new BImage(xSize, ySize); c = new Convolver(1); clearZ=new float[width*height]; System.arraycopy(g.zbuffer,0,clearZ,0,clearZ.length); } void loop(){ if (blur){ System.arraycopy(clearZ,0,g.zbuffer,0,clearZ.length); c.setRadius(2); System.arraycopy(pixels,0,bBlur.pixels,0,xSize*ySize); c.blur(bBlur,0,0,width,height); image(bBlur,0,0); } else { background(150,50,25); } sound.exist(); camera.exist(); spikeBall.exist(); counter ++; delay(10); } void keyPressed(){ if (key == 'B' || key == 'b'){ if (!blur){ blur = true; } else { blur = false; } } } class Spike{ int index; int spikeLength; int speed; float fillColor; float sineOffset, sine2Offset; float mySize; float r; float g; float b; float bb; float X, Y, Z, x, y, z; float waveOffset; Spike(int sentSpikeLength, int sentIndex, int sentSpeed){ spikeLength = sentSpikeLength; index = sentIndex; speed = sentSpeed; waveOffset = random(TWO_PI); } void undulate(){ beginShape(TRIANGLE_STRIP); noStroke(); for(int i = 0; i <= spikeLength - speed; i += speed){ sineOffset = (sin(i/100.0 - counter/6.0 + waveOffset) * 1.0); sine2Offset = (sin(i/100.0 - counter/10.0 + waveOffset) * 3.0); fillColor = abs(sound.signalHistory[i][index]); bb = abs(sound.signalHistory[i][index] * 2.0) - i/10.0 + sineOffset*50.0; x = X; y = Y; z = Z; X = ((sound.history - i)/500.0 + sound.spectrumHistory[i][index]/100.0) * (sound.history - i)/sound.history; if (index%4 > 0){ Y = (sound.signalHistory[i][index]/25.0 + sineOffset); } else { Y = (sound.signalHistory[i][index]/25.0 + sineOffset); } Z = (i)/speed + sound.spectrumHistory[i][index]/1000.0; if (i < speed * 20.0){ Y *= (i / (speed * 20.0)); Z *= (i / (speed * 20.0)); } stroke(255,255,255); if (i%(speed * 2) > 0){ vertex(-X, Y, Z); fill(125, 255, bb*0.5); vertex(X, Y, Z); fill(150+bb, 255, bb*0.5); } else { vertex(-X, Y, Z); fill(125+bb, 255, bb); vertex(X, Y, Z); fill(150, 255, bb); } noStroke(); } endShape(); noStroke(); push(); translate(X,Y,Z); fill(175,255,bb * 3.0); box(0.05,0.05,0.05); pop(); } } class SpikeBall{ Spike[] spike; int numSpikes; int spikeLength; int speed; float bodyRadius; float xRot, yRot, zRot; SpikeBall(int sentNumSpikes, int sentSpikeLength, int sentSpeed){ numSpikes = sentNumSpikes; spikeLength = sentSpikeLength; speed = sentSpeed; spike = new Spike[numSpikes]; for (int i=0; i 0; i -= speed){ for(int j = 0; j < bands; j ++){ spectrumHistory[i][j] -= (spectrumHistory[i][j] - (spectrumHistory[i-speed][j] * degrade)) * .85; signalHistory[i][j] -= (signalHistory[i][j] - (signalHistory[i-speed][j] * degrade)) * .75; } } for(int j = 0; j < bands; j ++){ spectrumHistory[0][j] -= (spectrumHistory[0][j] - (constrain(LiveInput.spectrum[j]*.25,0,200))) * .999; signalHistory[0][j] -= (signalHistory[0][j] - (constrain(LiveInput.signal[j]*.25,0,200))) * .9; } } void setCounter(){ timeCounter += (1.0f/float(frameRate)); } } class Camera{ // camera variables float xPos, yPos; float rotationVar; float elevation; float azimuth; float twist; float distance; float lensAngle; boolean autoRotate = true; Camera (float sentDistance, float sentLensAngle){ distance = sentDistance; lensAngle = sentLensAngle; } void exist(){ findRotation(); setCamera(); } void findRotation(){ if (autoRotate){ xPos -= (xPos - (cos(counter/80.0) * 65.0)) * .2; yPos -= (yPos - (sin(counter/135.0) * 86.0)) * .2; azimuth = cos(counter/100.0); } else { xPos -= (xPos - (xMid - mouseX)) * .2; yPos -= (yPos - (yMid - mouseY)) * .2; } } void setCamera(){ beginCamera(); perspective(lensAngle, (float)xSize / (float)ySize, 1.0f, 200); translate(cos(counter/100.0) * 3.0, sin(counter/50.0) * 1.5, -distance); twist = radians(xPos); elevation = radians(yPos); rotateY(-twist); rotateX(elevation); rotateZ(-azimuth); endCamera(); } } class Convolver{ int radius; int kernelSize; int[] kernel; int[][] mult; Convolver(int sz){ this.setRadius(sz); } void setRadius(int sz){ int i,j; sz=min(max(1,sz),248); if (radius==sz) return; kernelSize=1+sz*2; radius=sz; kernel= new int[1+sz*2]; mult=new int[1+sz*2][256]; int sum=0; for (i=1;i>16; g[i]=(ri&0x00ff00)>>8; b[i]=(ri&0x0000ff); } int r2[]=new int[wh]; int g2[]=new int[wh]; int b2[]=new int[wh]; x=max(0,x); y=max(0,y); w=x+w-max(0,(x+w)-iw); h=y+h-max(0,(y+h)-img.height); yi=y*iw; for (yl=y;yl=x && read=y){ cr+=mult[i][r2[read]]; cg+=mult[i][g2[read]]; cb+=mult[i][b2[read]]; sum+=kernel[i]; } ri++; read+=iw; } pix[xl+yi]=0xff000000 | (cr/sum)<<16 | (cg/sum)<<8 | (cb/sum); } yi+=iw; } } } // translates the brightness into an alpha channel (code by Ryan Alexander of Motion Theory) void brightToAlpha(BImage b){ b.format = RGBA; for(int i=0; i < b.pixels.length; i++) { b.pixels[i] = color(59,100,0,255 - brightness(b.pixels[i])); } } public void stop(){ Sonia.stop(); super.stop(); } //