Friday, July 13, 2012

EL Wire Beat Detector with Processing on an Arduino

So the hardware here is an Arduino Duemilanove with an EL Escudo sequencer and 3 lengths of 3m ELWire. I'm controlling the Arduino with a Processing sketch because I wanted to use the Sonia audio library to process audio input on the laptop and send pinOut commands to the sequencer. I read a little more on that and it looks like the Minim audio library may be a better choice with more options for isolating pitch frequencies I think. You can see it starts responding to MJ's voice and other sounds. Ultimately I plan to have some students work on programming EL Wire sequences for a student dance performance where the EL Wire will be woven into some costumes. I have to figure out if I can integrate an XBee into the works to send the commands to multiple units over wifi so the costumes can be centrally controlled.
Here's the code:



import processing.serial.*;
import cc.arduino.*;
import pitaru.sonia_v2_9.*;
Arduino arduino;
int[] ledPin = {2,3,4}; // EL Escudo pinOuts start at A=2, B=3, etc.
float beat;


void setup() {
  //println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[1], 57600);
  for(int i = 0;i<3;i++) {
    arduino.pinMode(ledPin[i], Arduino.OUTPUT);
  }
  
  size(200,200);
  frameRate(30);
  // change beat value or input volume if beat isn't accurate, or maybe your sample does not have a clear enough beat
  beat = 0.03; 
  
  Sonia.start(this);
  // Start listening to the microphone
  LiveInput.start(); 
}


void draw() {
  float level = LiveInput.getLevel();
  if(level > beat) {
     blink();
  }
}


void blink() {
  for(int i = 0;i<ledPin.length;i++) {
    arduino.digitalWrite(ledPin[i], Arduino.HIGH);
  }
  delay(100);
  for(int i = 0;i<ledPin.length;i++) {
    arduino.digitalWrite(ledPin[i], Arduino.LOW);
  }
  delay(100);
}


// Close the sound engine
public void stop() {
  Sonia.stop();
  super.stop();
}

1 comment :

Erik N. said...

I tried the Minim library which even has a beatDetect class and just couldn't get it to do any better than the Sonia library. Oh well, the main thing I'm looking for is an application that can return a tempo in milliseconds, which the Sonia does pretty well.