Sound-mirror sketch: it works! Any ideas for improvement / faster code?
in
Share your Work
•
3 years ago
http://ickydog.com/code/p5/canyouhearme/
--
/biting at the ankles of giants/
...but I guess the webcam bit won't work over the tubes.
- import processing.video.*;
- import ddf.minim.analysis.*;
- import ddf.minim.*;
- Minim minim;
- AudioInput in;
- float audioAmp = 0.0;
- float sampleAmp;
- float sens = 0.89;
- int vidFactor = 1;
- Capture cam;
- PImage camImg;
- PImage bkgdImg;
- PImage trailsImg;
- PImage tempImg;
- PImage _tempImg;
- int[] previousFrame;
- color currColor;
- color brightred;
- int w,h;
- boolean info = false;
- boolean start = true;
- int counter = 0;
- PFont font;
- void setup() {
- size(640, 480, P2D);
- frame.setBackground(new java.awt.Color(0, 0, 0));
- frame.setTitle("can you hear me | 2010 | charlie williams");
- background(0);
- w = width;
- h = height;
- cam = new Capture(this, w, h, 30);
- minim = new Minim(this);
- in = minim.getLineIn(Minim.STEREO, 64);
- camImg = createImage(w, h, RGB);
- trailsImg = createImage(w, h, RGB);
- bkgdImg = createImage(w, h, RGB);
- tempImg = createImage(w, h, RGB);
- _tempImg = createImage(w, h, HSB);
- previousFrame = new int[w*h];
- font = loadFont("ArialMT-48.vlw");
- color brightred = color(255,0,0,255);
- }
- void draw() {
- doAudio();
- // VIDEO ANALYSIS & DISPLAY
- if (cam.available()) {
- cam.read();
- camImg.copy(cam,0,0,w,h,0,0,w,h);
- flipCam();
- alphize();
- // tint(255, vidFactor);
- }
- blend(0, 0, w, h, 0, 0, w, h, OVERLAY); // LIVE
- fill(0, 255-vidFactor);
- rect(0, 0, w, h); // MASK RECT
- blend(bkgdImg, 0, 0, w, h, 0, 0, w, h, SCREEN); // BACKGROUND
- trails();
- blend(trailsImg, 0, 0, w, h, 0, 0, w, h, LIGHTEST); // TRAILS
- // blend(trailsImg, 0, 0, w, h, 0, 0, w, h, ADD); // ALT TRAILS
- // INFO DISPLAY
- if(info || counter < 150) {
- fill(255);
- textFont(font, 14);
- textAlign(LEFT);
- text(sens + " sens", 20, 20);
- text("fps " + frameRate, width - 95, 20);
- textAlign(CENTER);
- text("type \'i\' to hide or show this info", width/2, 20);
- text("+/- to change audio sensitivity", width/2, 35);
- text("any other key to recapture background", width/2, 50);
- }
- if(counter < 15) {
- textFont(font, 18);
- text("Stand out of the frame for background detection", width/2, height/2 - 30);
- }
- else if(counter < 25) {
- textFont(font, 18);
- text("Stand out of the frame for background detection", width/2, height/2 - 30);
- textFont(font, 32);
- text("- 3 -", width/2, height/2);
- }
- else if (counter < 35) {
- textFont(font, 18);
- text("Stand out of the frame for background detection", width/2, height/2 - 30);
- textFont(font, 32);
- text("- 2 -", width/2, height/2);
- }
- else if(counter < 45) {
- textFont(font, 18);
- text("Stand out of the frame for background detection", width/2, height/2 - 30);
- textFont(font, 32);
- text("- 1 -", width/2, height/2);
- }
- else if (counter > 45 && start == true) {
- bkgdImg.copy(camImg,0,0,w,h,0,0,w,h);
- start = false;
- }
- counter++;
- }
- void alphize() {
- loadPixels();
- for (int i = 0; i < h * w; i++) {
- currColor = camImg.pixels[i];
- //clear alpha channel since it comes in as FF:
- // currColor = 0x00FFFFFF & currColor;
- pixels[i] = 0x00000000 | (vidFactor << 24) | currColor;
- }
- updatePixels();
- // println(hex(pixels[1]) + " " + vidFactor + " " + hex(currColor));
- }
- void doAudio() {
- // AUDIO ANALYSIS
- audioAmp *= sens;
- for(int i = 0; i < in.bufferSize() - 1; i++) {
- sampleAmp = abs(in.mix.get(i));
- audioAmp += sampleAmp;
- }
- vidFactor = int(audioAmp*10);
- constrain(vidFactor, 0, 255);
- }
- void flipCam () {
- // camImg.loadPixels();
- for (int i=0; i<w*h; i++) {
- int posX = i % width; // x coordinate of pixel
- int posY = floor(i / width); // y coordinate of pixel
- int _x = width - posX - 1; // flip x coordinate if we want to flip
- tempImg.pixels[posY * width + _x] = camImg.pixels[i];
- }
- // tempImg.updatePixels();
- camImg.copy(tempImg,0,0,w,h,0,0,w,h);
- // camImg.updatePixels();
- }
- void keyPressed() {
- // println(keyCode);
- switch(keyCode) {
- case 8: // DELTETED!
- bkgdImg.loadPixels();
- for (int i = 0; i < h * w; i++) {
- bkgdImg.pixels[i] = 0;
- }
- bkgdImg.updatePixels();
- break;
- case 32: // SPACEBEAR EATS YOU
- bkgdImg.copy(camImg,0,0,w,h,0,0,w,h);
- break;
- case 73: // 'i' see you
- info = !info;
- break;
- case 157: // APPLE oops I mean command
- break;
- case 27: // ESC
- break;
- case 61:
- sens += 0.01;
- break;
- case 45:
- sens -= 0.01;
- break;
- default:
- start = true;
- counter = 0;
- println(keyCode);
- break;
- }
- }
- void stop() {
- in.close();
- minim.stop();
- super.stop();
- }
- void trails() {
- _tempImg.copy(camImg,0,0,w,h,0,0,w,h);
- int movementSum = 0;
- for (int i = 0; i < w*h; i++) {
- color currColor = _tempImg.pixels[i];
- color prevColor = previousFrame[i];
- int currR = (currColor >> 16) & 0xFF;
- int currG = (currColor >> 8) & 0xFF;
- int currB = currColor & 0xFF;
- int prevR = (prevColor >> 16) & 0xFF;
- int prevG = (prevColor >> 8) & 0xFF;
- int prevB = prevColor & 0xFF;
- // Compute the difference of the red, green, and blue values
- int diffR = abs(currR - prevR);
- int diffG = abs(currG - prevG);
- int diffB = abs(currB - prevB);
- _tempImg.pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
- previousFrame[i] = currColor;
- }
- // ** is this a magically-faster way of calculating difference?
- // function imgDif(int[] img1,int[] img2) {
- // for (int i=pixels.length;--i>-1;pixels[i]=img1[i]^img2[i]) {
- // }
- // }
- _tempImg.filter(GRAY);
- // _tempImg.filter(THRESHOLD, 0.2);
- // _tempImg.filter(BLUR, 1);
- // _tempImg.blend(trailsImg, 0, 0, w, h, 0, 0, w, h, SCREEN);
- trailsImg.copy(_tempImg,0,0,w,h,0,0,w,h);
- }
--
/biting at the ankles of giants/