import processing.video.*; import pitaru.sonia_v2_9.*; int numPixels; int[] previousFrame; Capture cam; Sample mySample0; Sample mySample1; Sample mySample2; Sample mySample3; void setup() { size(160,120); // Change size to 320 x 240 if too slow at 640 x 480 // Uses the default video input, see the reference if this causes an error cam = new Capture(this, width, height, 24); numPixels = cam.width * cam.height; // Create an array to store the previously captured frame previousFrame = new int[numPixels]; loadPixels(); Sonia.start(this); // Start Sonia engine. // create a new sample object. mySample0 = new Sample("2-77 Frog Laughing Frog Common Frog.L.aif"); mySample1 = new Sample("2-74 Wapiti Red Deer A Ruttish Stag Calls.L.aif"); mySample2 = new Sample("2-74 Wapiti Red Deer A Ruttish Stag Calls.R.aif"); mySample3 = new Sample("2-77 Frog Laughing Frog Common Frog.R.aif"); mySample1.repeat(); mySample2.repeat(); mySample0.repeat(); mySample3.repeat(); } void draw(){ setPan(); setRate(); } void setRate(){ cam.read(); cam.loadPixels(); // Make its pixels[] array available int movementSum = 0; // Amount of movement in the frame for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... color currColor = cam.pixels[i]; color prevColor = previousFrame[i]; // Extract the red, green, and blue components from current pixel // int currR = (currColor >> 16) & 0xFF; // Like red(), but faster int currG = (currColor >> 8) & 0xFF; // int currB = currColor & 0xFF; // Extract red, green, and blue components from previous pixel // 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); // Add these differences to the running tally // movementSum += diffR + diffG + diffB; movementSum += diffG; // Render the difference image to the screen // pixels[i] = color(diffR, diffG, diffB); pixels[i] = color(diffG); // The following line is much faster, but more confusing to read //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB; // Save the current color into the 'previous' buffer previousFrame[i] = currColor; } // To prevent flicker from frames that are all black (no movement), // only update the screen if the image has changed. if (movementSum > 0) { updatePixels(); println(movementSum); // Print the total amount of movement to the console // float rate1 = (movementSum); float rate2 =(movementSum-20000); float rate1 =(10000); mySample0.setRate(rate2); mySample3.setRate(rate2); mySample1.setRate(rate1); mySample2.setRate(rate1); } } void setPan(){ // set the pan of the sample object. // Range: float from -1 to 1 .... -1 -> left, 0 -> balanced ,1 -> right // notes: only works with MONO samples. Pan for Stereo support in next version. float pan0 = -1; float pan1 = -1; float pan2 = 1; float pan3 = 1; mySample0.setPan(pan0); mySample1.setPan(pan1); mySample2.setPan(pan2); mySample3.setPan(pan3); } void captureEvent(Capture cam) { cam.read(); cam.loadPixels(); // Make its pixels[] array available int movementSum = 0; // Amount of movement in the frame for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... color currColor = cam.pixels[i]; color prevColor = previousFrame[i]; // Extract the red, green, and blue components from current pixel // int currR = (currColor >> 16) & 0xFF; // Like red(), but faster int currG = (currColor >> 8) & 0xFF; // int currB = currColor & 0xFF; // Extract red, green, and blue components from previous pixel // 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); // Add these differences to the running tally // movementSum += diffR + diffG + diffB; movementSum += diffG; // Render the difference image to the screen // pixels[i] = color(diffR, diffG, diffB); pixels[i] = color(diffG); // The following line is much faster, but more confusing to read //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB; // Save the current color into the 'previous' buffer previousFrame[i] = currColor; } // To prevent flicker from frames that are all black (no movement), // only update the screen if the image has changed. if (movementSum > 0) { updatePixels(); println(movementSum); // Print the total amount of movement to the console // float rate1 = (movementSum); float rate2 =(movementSum-20000); mySample0.setRate(rate2); mySample3.setRate(rate2); } } public void stop(){ Sonia.stop(); super.stop(); }