MusicCam + Brightness Tracking
in
Contributed Library Questions
•
2 years ago
Hi guys.....today i was trying to adapt musiccam (it was developed for the shake sensor (mustick), but i used the webcam version, and i was trying to control the music according to the brightest point but the music just keeps playing....any ideas why? Thanks!!!
- import beads.*;
- AudioContext ac;
- Envelope rateEnvelope;
- // ---------------------------------------------------------------
- /*
- Two helper math functions that decrease and increase a variable up until a limit
- */
- float capMax(float current, float maximum, float changeRate)
- {if (current+changeRate<maximum) current+=changeRate;
- else current= maximum;
- return current;}
- float capMin(float current, float minimum, float changeRate)
- {if (current-changeRate>minimum) current-=changeRate;
- else current= minimum;
- return current;}
- // ---------------------------------------------------------------
- /*
- Global variables to coordinate the playback speed - the last time we check the sensor and the current sample speed
- */
- int lastTime;
- float sampleSpeed=1;
- // ---------------------------------------------------------------
- /*
- Initialise the sketch
- */
- void setup() {
- /*
- Set the window size
- */
- size(640,480);
- initVideo();
- /*
- Init the audio context and player with a file (open a file selection box)
- */
- ac = new AudioContext();
- String audioFile = selectInput();
- SamplePlayer player = new SamplePlayer(ac, SampleManager.sample(audioFile));
- player.setLoopType(SamplePlayer.LoopType.LOOP_FORWARDS);
- /*
- Set up the audio envelope that controls the playback rate
- */
- rateEnvelope = new Envelope(ac, 1);
- player.setRateEnvelope(rateEnvelope);
- /*
- Add gain (volume) and start the playboack
- */
- Gain g = new Gain(ac, 2, 0.2);
- g.addInput(player);
- ac.out.addInput(g);
- ac.start();
- }
- // ---------------------------------------------------------------
- /*
- Draw - called each time the screen is drawn (30-60 times per second)
- */
- void draw()
- {
- /*
- Parameters configuring response to sensor input
- */
- int timePeriod = 200; // how long between examining the sensor (ms)
- float changeRate = 0.1; // how rapidly the playback speed will change (device specific units)
- float sensorThreshold = 15; // the value we consider to be 'fast' movement (device specific units)
- float maximum = 1.0; // the maximum sameple speed (1 is normal speed)
- float minimum = 0.2; // the minimum sample speed (before stopping the music)
- /*
- Get the time and figure if we should get more data from the sensor
- */
- int now = millis();
- if (now-timePeriod > lastTime)
- {
- /*
- Get input from the sensor
- */
- int sensorInput = getCameraSensorInput(); // a camera in this case - this method also draws to the screen
- /*
- If sensor input exceeds a threshold, increase the sampleSpeed. Otherwise, decrease it
- */
- if (sensorInput>sensorThreshold)
- sampleSpeed = capMax(sampleSpeed, maximum, changeRate);
- else
- sampleSpeed = capMin(sampleSpeed, minimum, changeRate);
- /*
- When the sample is playing, adjust the speed according to level of movement
- In the case of very slow movement, stop the sample
- */
- if (ac.isRunning())
- {
- if (sampleSpeed <= minimum)
- ac.stop();
- else
- rateEnvelope.addSegment(sampleSpeed, 1);
- }
- /*
- Otherwise, the sample must be stopped, so if speed in above minimum, start it again!
- */
- else if (sampleSpeed > minimum)
- {
- ac.start();
- rateEnvelope.addSegment(sampleSpeed, timePeriod);
- }
- lastTime = now;
- }
- import processing.video.*;
- int numPixels;
- int[] previousFrame;
- Capture video;
- void initVideo()
- {
- /*
- Open the default camera, see the reference manual if this causes an error
- */
- size(640, 480); // 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
- video = new Capture(this, width, height, 30);
- noStroke();
- smooth();
- numPixels = video.width * video.height;
- /*
- Create an array to store the previously captured frame of video
- */
- previousFrame = new int[numPixels];
- loadPixels();
- }
- /*
- Function gets a number for the "activity" in the camera scene.
- This comes straight from the standard processing demo "Frame Differencing"
- */
- int getCameraSensorInput()
- {
- int movementSum = 0; // Amount of movement in the frame
- if (video.available()) {
- video.read();
- image(video, 0, 0, width, height); // Draw the webcam video onto the screen
- int brightestX = 0; // X-coordinate of the brightest video pixel
- int brightestY = 0; // Y-coordinate of the brightest video pixel
- float brightestValue = 0; // Brightness of the brightest video pixel
- // Search for the brightest pixel: For each row of pixels in the video image and
- // for each pixel in the yth row, compute each pixel's index in the video
- video.loadPixels();
- int index = 0;
- for (int y = 0; y < video.height; y++) {
- for (int x = 0; x < video.width; x++) {
- // Get the color stored in the pixel
- int pixelValue = video.pixels[index];
- // Determine the brightness of the pixel
- float pixelBrightness = brightness(pixelValue);
- // If that value is brighter than any previous, then store the
- // brightness of that pixel, as well as its (x,y) location
- if (pixelBrightness > brightestValue) {
- brightestValue = pixelBrightness;
- brightestY = y;
- brightestX = x;
- movementSum = y+x;
- }
- index++;
- }
- }
- // Draw a large, yellow circle at the brightest pixel
- fill(255, 204, 0, 128);
- ellipse(brightestX, brightestY, 100, 100);
- }
- // devisor ascertained by trial and error
- //movementSum = movementSum/400000;
- println (movementSum);
- return movementSum;
1