Advertisement
Guest User

ArduinoLedMusic

a guest
Oct 23rd, 2011
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import processing.serial.*;
  2. import ddf.minim.*;
  3. import ddf.minim.analysis.*;
  4. import cc.arduino.*;
  5.  
  6. class BeatListener implements AudioListener
  7. {
  8.   private BeatDetect beat;
  9.   private AudioPlayer source;
  10.  
  11.   BeatListener(BeatDetect beat, AudioPlayer source)
  12.   {
  13.     this.source = source;
  14.     this.source.addListener(this);
  15.     this.beat = beat;
  16.   }
  17.  
  18.   void samples(float[] samps)
  19.   {
  20.     beat.detect(source.mix);
  21.   }
  22.  
  23.   void samples(float[] sampsL, float[] sampsR)
  24.   {
  25.     beat.detect(source.mix);
  26.   }
  27. }
  28.  
  29. Minim minim;
  30. AudioPlayer song;
  31. BeatDetect beat;
  32. BeatListener bl;
  33. Arduino arduino;
  34.  
  35. int ledPin =  10;
  36. int ledPin2 =  6;
  37. int ledPin3 =  2;
  38. float kickSize, snareSize, hatSize;
  39.  
  40. void setup() {
  41.   size(512, 200, P3D);
  42.  
  43.   minim = new Minim(this);
  44.   arduino = new Arduino(this, Arduino.list()[1], 57600);
  45.  
  46.   song = minim.loadFile("music.mp3", 2048);
  47.   song.play();
  48.  
  49.   beat = new BeatDetect(song.bufferSize(), song.sampleRate());
  50.   beat.setSensitivity(100);
  51.   kickSize = snareSize = hatSize = 16;
  52.  
  53.   arduino.pinMode(ledPin, Arduino.OUTPUT);
  54.   arduino.pinMode(ledPin2, Arduino.OUTPUT);  
  55.   arduino.pinMode(ledPin3, Arduino.OUTPUT);
  56.  
  57.   bl = new BeatListener(beat, song);  
  58.   textFont(createFont("Helvetica", 16));
  59.   textAlign(CENTER);
  60. }
  61.  
  62. void draw() {
  63.   background(0);
  64.   fill(255);
  65.   if(beat.isKick()) {
  66.       arduino.digitalWrite(ledPin, Arduino.HIGH);   // set the LED on
  67.       kickSize = 32;
  68.   }
  69.   if(beat.isSnare()) {
  70.       arduino.digitalWrite(ledPin2, Arduino.HIGH);   // set the LED on
  71.       snareSize = 32;
  72.   }
  73.   if(beat.isHat()) {
  74.       arduino.digitalWrite(ledPin3, Arduino.HIGH);   // set the LED on
  75.       hatSize = 32;
  76.   }
  77.   arduino.digitalWrite(ledPin, Arduino.LOW);
  78.   arduino.digitalWrite(ledPin2, Arduino.LOW);
  79.   arduino.digitalWrite(ledPin3, Arduino.LOW);
  80.   textSize(kickSize);
  81.   text("KICK", width/4, height/2);
  82.   textSize(snareSize);
  83.   text("SNARE", width/2, height/2);
  84.   textSize(hatSize);
  85.   text("HAT", 3*width/4, height/2);
  86.   kickSize = constrain(kickSize * 0.95, 16, 32);
  87.   snareSize = constrain(snareSize * 0.95, 16, 32);
  88.   hatSize = constrain(hatSize * 0.95, 16, 32);
  89. }
  90.  
  91. void stop() {
  92.   // always close Minim audio classes when you are finished with them
  93.   song.close();
  94.   // always stop Minim before exiting
  95.   minim.stop();
  96.   // this closes the sketch
  97.   super.stop();
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement