본문 바로가기

만들기 / making/sensing workshop

audio sensor processing code

audio sensor processing code

import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;


import processing.serial.*;






Serial myPort; // Create object from Serial class
String myString = null;
int adc;
char play_flag = 0;
char stop_flag = 0;
Minim minim;
AudioPlayer in;








void setup()
{


size(512, 200);
//Start Sonia
minim = new Minim(this);
// load a file, default sample buffer size is 1024
in = minim.loadFile("dirty.wav");
// play the file



// List all the available serial ports
println(Serial.list());
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}




void draw()
{
myPort.write('a');
while (myPort.available() > 0)
{
char inByte = myPort.readChar();
adc = int(inByte);
println(adc);
}
// if adc > 125 and play_flag = 0, play the music
if (adc > 125 && play_flag == 0)
{
in.play();
play_flag =1;
}


// if adc < 80 and stop_flag = 0, stop the music
if (adc < 80 && stop_flag == 0)
{
//in.close();
minim.stop();
stop_flag = 1;
}


// if 80 < adc < 125, reset the flags
if (adc > 80 && adc < 125)
{
play_flag = 0;
stop_flag = 0;
}


}




// Safely close the sound engine upon Browser shutdown.
public void stop(){
minim.stop();
super.stop();
}

'만들기 / making > sensing workshop' 카테고리의 다른 글

Audio sensor module on PCB  (0) 2009.08.31
Audio sensor module on Breadboard  (0) 2009.08.31
audio sensor MCU code  (0) 2009.08.31
Day 7: Audio Sensor  (0) 2009.08.31
Day 5: Ultrasonic Sensor  (0) 2009.08.31