Sound with Arduino and Processing

Since you are all obsessed with making noisy things here are two examples.

The Processing library for sounds is minim. It is included in Processing and you can find examples in the example menu.

Here’s a basic one to play sound files. You need a file in your folder. You can grab this one for testing:

import ddf.minim.*;
Minim minim;
 
//this is the object that plays your file
AudioPlayer player;
 
void setup()
{
  size(300, 300);
 
  //initialize minim
  minim = new Minim(this);
 
  // loadFile will look in all the same places as loadImage does.
  // this means you can find files that are in the data folder and the 
  // sketch folder. you can also pass an absolute path, or a URL.
  // mp3, wav, ogg should all work
  player = minim.loadFile("test.wav");
 
  // play the file
  player.play();
}
 
void draw()
{
 background(0);
 
  //check if the reproduction is in process
  if ( player.isPlaying() )
  {
    text("The player is playing.", 5, 15);
  }
  else
  {
    text("The player is not playing.", 5, 15);
  }
}
 
//replay the file if the mouse is pressed
void mousePressed()
{
  //it's weird but you have to rewind a file to play it
  player.rewind();
  player.play();
}
 
//stop is called when you hit stop on processing. Just leave this here
void stop()
{
  player.close();
  minim.stop();
  super.stop();
}

This is a modification of the above that receives an input from Arduino via Firmata. READ THE POST ARDUINO + PROCESSING FIRST:

//import the libraries for serial communication
//and for arduino/firmata specifically
//and firmata
import ddf.minim.*;
import processing.serial.*;
import cc.arduino.*;
 
//this object represents your board
Arduino arduino;
//this is the minim environment
Minim minim;
 
//this is the object that plays your file
AudioPlayer player;
 
//like in an arduino sketch it's good to
//use variables for pin numbers 
int sensorPin = 0;
 
//this is the minimum value from the sensor that will trigger the sound
int threshold = 360;
 
void setup()
{
  size(300, 300);
 
  //the arduino object needs to be created at the beginning
  arduino = new Arduino(this, Arduino.list()[0], 57600);
 
  //once it's created I set the pin modes  
  arduino.pinMode(sensorPin, Arduino.INPUT);
 
  //initialize minim
  minim = new Minim(this);
 
  // loadFile will look for a file in the data folder
  // mp3, wav, ogg should all work
  player = minim.loadFile("test.wav");
}
 
void draw()
{
  background(0);
 
  //read an analog value from the sensor pin
  int analogValue =  arduino.analogRead(sensorPin);
  println(analogValue); //print it for testing purposes
 
  //check if the reproduction is in process if not don't trigger another sound
  if ( player.isPlaying() == false && analogValue > threshold)
  {
    //it's weird but you have to rewind a file to play it
    player.rewind();
    player.play();
  }
}
 
//stop is called when you hit stop on processing. Just leave this here
void stop()
{
  player.close();
  minim.stop();
  super.stop();
}

Using OSX Text to speech from Arduino + Processing
Here’s a fun feature of Mac OSX. It may change depending on your version.
This sketch works like the one above but synthesize a voice from a String (text).

//import the libraries for serial communication
//and for arduino/firmata specifically
import processing.serial.*;
import cc.arduino.*;
import java.io.IOException;
 
//this object represents your board
Arduino arduino;
 
//like in an arduino sketch it's good to
//use variables for pin numbers 
int sensorPin = 0;
 
//this is the minimum value from the sensor that will trigger the sound
int threshold = 360;
//counter to avoid overlapping
int waitTime = 0;
 
void setup()
{
  size(300, 300);
 
  //the arduino object needs to be created at the beginning
  arduino = new Arduino(this, Arduino.list()[0], 57600);
 
  //once it's created I set the pin modes  
  arduino.pinMode(sensorPin, Arduino.INPUT);
}
 
void draw()
{
  background(0);
 
  //read an analog value from the sensor pin
  int analogValue =  arduino.analogRead(sensorPin);
  println(analogValue); //print it for testing purposes
 
  //if above a certain threshold and wait is over
  if (analogValue > threshold && waitTime <=0)
  {
    //your text
    String text = "Hello World";
    //your speed
    int voiceSpeed = 200;
 
    //reset the wait time according to length of text and speed
    //the 50 is a "magic number" you may need to tweak it according to your needs
    waitTime = text.length() * voiceSpeed / 50;
 
    /*the voice must be put in quotes, these are the available ones:
     Alex, Bruce, Fred, Junior, Ralph, Agnes, Kathy, Princess, Vicki, Victoria
     Albert, "Bad News", Bahh, Bells, Boing, Bubbles, Cellos, Deranged, "Good News",
     Hysterical, Pipe Organ, Trinoids, Whisper, Zarvox
     */
    say(text, "Alex", voiceSpeed);
  }
 
  //wait time is just a counter that lets some time pass between utterances 
  waitTime--;
  println("Waiting "+waitTime);
 
}
 
//this is the function that connects to the OSX text to speech system
void say(String script, String voice, int speed) {
  try {
    Runtime.getRuntime().exec(new String[] {
      "say", "-v", voice, "[[rate " + speed + "]]" + script
    }
    );
  }
  catch (IOException e) {
    System.err.println("IOException");
  }
}
Posted in

Post a comment