Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

minim and brigtest x

$
0
0
Hi i am working on an instrument that triggers a sound when the brightest pixel reaches certain area, my problem is that every time ths happens processing triggers the audio several times per second so you can´t even listen to the full sound. I would love to get jus one trigger per zone every 2 or 3 seconds or when the sound finishes playing.


I am not very experienced programming so any help would be grate. thanks.

To test it you may need an audio file called notes 1.wav   notes 2.wav , etc...

thanks again!!!


  1.  import processing.video.*;


  2. import ddf.minim.*;

  3. Minim minim;
  4. AudioSample kick;
  5. AudioSample snare;
  6. AudioSample chick;
  7. AudioSample chack;
  8. AudioSample chuck;
  9. AudioSample check;
  10. AudioSample tac;
  11. AudioSample tuc;
  12. AudioSample tic;
  13. AudioSample toc;


  14. Capture video;

  15. float boxSize = 80;
  16. float boxSize2 = 40;
  17. float boxX, boxY;
  18. float boxX1, boxY1;
  19. float boxX2, boxY2;
  20. float boxX3, boxY3;
  21. float boxX4, boxY4;
  22. float boxX5, boxY5;
  23. float boxX6, boxY6;
  24. float boxX7, boxY7;
  25. float boxX8, boxY8;
  26. float boxX9, boxY9;

  27. void setup() {
  28.     size(1024, 768, P3D);
  29.     
  30.     boxX = width/2;
  31.   boxY = height/2;
  32.   boxX1 = 230;
  33.   boxY1 = 300;
  34.   boxX2= 220;
  35.   boxY2= 500;
  36.   boxX3= 600;
  37.   boxY3= 50;
  38.   boxX4 = 700;
  39.   boxY4 = 600;
  40.   boxX5 = 900;
  41.   boxY5 = 200;
  42.    boxX6 = 170;
  43.   boxY6 = 120;
  44.    boxX7 = 280;
  45.   boxY7 = 120;
  46.   boxX8 = 700;
  47.   boxY8 =400;
  48.   boxX9 = 700;
  49.   boxY9 = 300;
  50.   rectMode(RADIUS);
  51.     video = new Capture(this, width, height);
  52.     
  53.   video.start();  
  54.   noStroke();
  55.   smooth();
  56.     minim = new Minim(this);
  57.     
  58.   kick = minim.loadSample( "notes 1.wav", // filename
  59.                             512      // buffer size
  60.                          );
  61.                          
  62.   snare = minim.loadSample("notes 2.wav", 512);
  63.   if ( snare == null ) println("Didn't get snare!");
  64.   
  65.                         
  66.  chick = minim.loadSample("notes 3.wav", 512);
  67.   if ( chick == null ) println("Didn't get chick!");
  68.   
  69.               
  70.  chack = minim.loadSample("notes 4.wav", 512);
  71.   if ( chack == null ) println("Didn't get chack!");
  72.   
  73.    chuck = minim.loadSample("notes 5.wav", 512);
  74.   if ( chuck == null ) println("Didn't get chuck!");
  75.   
  76.   
  77.    check = minim.loadSample("notes 6.wav", 512);
  78.   if ( check == null ) println("Didn't get check!");
  79.   
  80.     tac = minim.loadSample("notes 7.wav", 512);
  81.   if ( tac == null ) println("Didn't get tac!");
  82.   
  83.      tuc = minim.loadSample("notes 8.wav", 512);
  84.   if ( tuc == null ) println("Didn't get tuc!");
  85.   
  86.     tic = minim.loadSample("notes 9.wav", 512);
  87.   if ( tic == null ) println("Didn't get tic!");
  88.   
  89.      toc = minim.loadSample("notes 10.wav", 512);
  90.   if ( toc == null ) println("Didn't get toc!");
  91. }

  92.   
  93.   // Uses the default video input, see the reference if this causes an error
  94.   


  95. void draw() {
  96.   if (video.available()) {
  97.     video.read();
  98.    
  99.    
  100.     image(video, 0, 0, width, height); // Draw the webcam video onto the screen
  101.     int brightestX = 0; // X-coordinate of the brightest video pixel
  102.     int brightestY = 0; // Y-coordinate of the brightest video pixel
  103.     float brightestValue = 0; // Brightness of the brightest video pixel
  104.     // Search for the brightest pixel: For each row of pixels in the video image and
  105.     // for each pixel in the yth row, compute each pixel's index in the video
  106.     video.loadPixels();
  107.     int index = 0;
  108.     for (int y = 0; y < video.height; y++) {

  109.       for (int x = 0; x < video.width; x++) {
  110.         // Get the color stored in the pixel
  111.         int pixelValue = video.pixels[index];
  112.         // Determine the brightness of the pixel
  113.         float pixelBrightness = brightness(pixelValue);
  114.         // If that value is brighter than any previous, then store the
  115.         // brightness of that pixel, as well as its (x,y) location
  116.         if (pixelBrightness > brightestValue) {
  117.           brightestValue = pixelBrightness;
  118.           brightestY = y;
  119.           brightestX = x;

  120.         }
  121.         index++;
  122.       }
  123.     }
  124.     // Draw a large, yellow circle at the brightest pixel
  125.     
  126.     
  127.                   

  128.   if(brightestX>boxX-boxSize && brightestX<boxX+boxSize &&
  129.   brightestY>boxY-boxSize && brightestY<boxY+boxSize) snare.trigger(); 
  130.   
  131.    if(brightestX>boxX-boxSize && brightestX<boxX+boxSize &&
  132.   brightestY>boxY-boxSize && brightestY<boxY+boxSize){
  133.     fill(200,200);
  134.   } else {
  135.     noFill();
  136.   }
  137.       rect(boxX, boxY, boxSize, boxSize);

  138.   
  139.    noFill();

  140.    if(brightestX>boxX1-boxSize && brightestX<boxX1+boxSize &&
  141.   brightestY>boxY1-boxSize && brightestY<boxY1+boxSize) kick.trigger();
  142.   
  143.    if(brightestX>boxX1-boxSize && brightestX<boxX1+boxSize &&
  144.   brightestY>boxY1-boxSize && brightestY<boxY1+boxSize){
  145.     fill(200,200);
  146.   } else {
  147.     noFill();
  148.   } 
  149.   
  150.   
  151.     rect(boxX1, boxY1, boxSize, boxSize);

  152.   
  153. noFill();
  154.        if(brightestX>boxX2-boxSize && brightestX<boxX2+boxSize &&
  155.   brightestY>boxY2-boxSize && brightestY<boxY2+boxSize) chick.trigger();
  156.   
  157.   if(brightestX>boxX2-boxSize && brightestX<boxX2+boxSize &&
  158.   brightestY>boxY2-boxSize && brightestY<boxY2+boxSize) {
  159.     fill(200,200);
  160.   } else {
  161.     noFill();
  162.   } 
  163.     rect(boxX2,boxY2, boxSize, boxSize);

  164.     
  165. noFill();
  166.          if(brightestX>boxX3-boxSize && brightestX<boxX3+boxSize &&
  167.   brightestY>boxY3-boxSize && brightestY<boxY3+boxSize) chack.trigger();
  168.   
  169.          if(brightestX>boxX3-boxSize && brightestX<boxX3+boxSize &&
  170.   brightestY>boxY3-boxSize && brightestY<boxY3+boxSize) {
  171.     fill(200,200);
  172.   } else {
  173.     noFill();
  174.   } 
  175.     rect(boxX3, boxY3, boxSize, boxSize);
  176.     
  177.     
  178.     noFill();
  179.          if(brightestX>boxX4-boxSize && brightestX<boxX4+boxSize &&
  180.   brightestY>boxY4-boxSize && brightestY<boxY4+boxSize) chuck.trigger();
  181.   
  182.          if(brightestX>boxX4-boxSize && brightestX<boxX4+boxSize &&
  183.   brightestY>boxY4-boxSize && brightestY<boxY4+boxSize) {
  184.     fill(200,200);
  185.   } else {
  186.     noFill();
  187.   } 
  188.     rect(boxX4, boxY4, boxSize, boxSize);
  189.     
  190.      noFill();
  191.          if(brightestX>boxX5-boxSize && brightestX<boxX5+boxSize &&
  192.   brightestY>boxY5-boxSize && brightestY<boxY5+boxSize) check.trigger();
  193.   
  194.          if(brightestX>boxX5-boxSize && brightestX<boxX5+boxSize &&
  195.   brightestY>boxY5-boxSize && brightestY<boxY5+boxSize) {
  196.     fill(200,200);
  197.   } else {
  198.     noFill();
  199.   } 
  200.     rect(boxX5, boxY5, boxSize, boxSize);
  201.     
  202.     
  203.      noFill();
  204.          if(brightestX>boxX6-boxSize && brightestX<boxX6+boxSize &&
  205.   brightestY>boxY6-boxSize && brightestY<boxY6+boxSize) tac.trigger();
  206.   
  207.          if(brightestX>boxX6-boxSize && brightestX<boxX6+boxSize &&
  208.   brightestY>boxY6-boxSize && brightestY<boxY6+boxSize) {
  209.     fill(200,200);
  210.   } else {
  211.     noFill();
  212.   } 
  213.     rect(boxX6, boxY6, boxSize2, boxSize2);
  214.     
  215.     
  216.      noFill();
  217.          if(brightestX>boxX7-boxSize && brightestX<boxX7+boxSize &&
  218.   brightestY>boxY7-boxSize && brightestY<boxY7+boxSize) tuc.trigger();
  219.   
  220.          if(brightestX>boxX7-boxSize && brightestX<boxX7+boxSize &&
  221.   brightestY>boxY7-boxSize && brightestY<boxY7+boxSize) {
  222.     fill(200,200);
  223.   } else {
  224.     noFill();
  225.   } 
  226.     rect(boxX7, boxY7, boxSize2, boxSize2);
  227.     
  228.     
  229.      noFill();
  230.          if(brightestX>boxX8-boxSize && brightestX<boxX8+boxSize &&
  231.   brightestY>boxY8-boxSize && brightestY<boxY8+boxSize) tic.trigger();
  232.   
  233.          if(brightestX>boxX8-boxSize && brightestX<boxX8+boxSize &&
  234.   brightestY>boxY8-boxSize && brightestY<boxY8+boxSize) {
  235.     fill(200,200);
  236.   } else {
  237.     noFill();
  238.   } 
  239.     rect(boxX8, boxY8, boxSize2, boxSize2);
  240.     
  241.     
  242.      noFill();
  243.          if(brightestX>boxX9-boxSize && brightestX<boxX9+boxSize &&
  244.   brightestY>boxY9-boxSize && brightestY<boxY9+boxSize) toc.trigger();
  245.   
  246.          if(brightestX>boxX9-boxSize && brightestX<boxX9+boxSize &&
  247.   brightestY>boxY9-boxSize && brightestY<boxY9+boxSize) {
  248.     fill(200,200);
  249.   } else {
  250.     noFill();
  251.   } 
  252.     rect(boxX9, boxY9, boxSize2, boxSize2);
  253.     
  254.     ellipse(brightestX, brightestY, 20, 30);
  255.                   stroke(100);
  256.                   noFill();




  257.     
  258.  }
  259.  


  260.  }
  261.   
  262.   


  263. {

  264.    if ( mouseX == 300 ) snare.trigger();
  265.   if ( mouseX  == 500 ) kick.trigger();
  266.   }



Viewing all articles
Browse latest Browse all 1768

Trending Articles