/** * Arduino Sounds * * Play WAV or MP3 files when piezo knocks from an Arduino running the * "PiezoKnock" sketch or when a computer keyboard key is pressed. * * Taken from the Minim "trigger" sketch: * * This sketch demonstrates how to use the trigger method of an AudioSample.
* AudioSamples can only be triggered, not cue'd and looped * or anything else you might do with an Playable object. The advantage, however, is that * an AudioSample can be retriggered while it is still playing, which will cause the sample to * overlap with itself . */ import ddf.minim.*; import processing.serial.*; String portname = "/dev/tty.usbserial-A4001qa8"; // or "COM8" Serial port; // Create object from Serial class AudioSample sounds[]; String sound_names[] = { "cat.wav", "fx.mp3", "electric_wrench.wav", "wehoa.mp3", "oriental_gong_2.wav", "yipee.wav", "car_brake.wav" // find more wav or mp3 files and put them in the "data" directory }; void setup() { size(400, 400); background(0); stroke(255); // always start Minim before you do anything with it Minim.start(this); Minim.debugOn(); sounds = new AudioSample[sound_names.length]; for( int i=0; i< sound_names.length; i++ ) { sounds[i] = Minim.loadSample(sound_names[i], 512); } // Open the port that the board is connected to and use the same speed (19200 bps) port = new Serial(this, portname, 19200); } void draw() { // do the drawing on events } void soundball() { int r = int(random(sounds.length)); println("picked sound #"+r); sounds[r].trigger(); // play a random sound int x = int(random(0,300)); int y = int(random(0,300)); fill(240,0,0); ellipse(x,y, 40,40); fill(30,0,0); ellipse(x,y, 8,8); } void serialEvent(Serial p) { char inByte = port.readChar(); println("received char: "+ inByte); if( inByte == '!' ) { // '!' is end of "knock!" soundball(); } } void keyPressed() { if(key == ' ') { background(40,40,40); // erase screen } soundball(); } void stop() { // always close Minim audio classes when you are done with them for( int i=0; i