<<< Previous Lecture ––––––– Next Lecture >>>
Capture – Using a Live Video Image in Processing
The video library has another class called Capture for displaying the image from your webcam or any other video camera.
Audio in Processing – The minim Library
The library you need for playing back audio files inside Processing is called minim. See the documentation for more information.
See examples below on how to use them. Here is a download link for downloading the files used in these examples
- Download audio samples. (make sure you put them in the data folder of your sketch)
Example 1
The example below show how to load a longer sound file as AudioPlayer and how to load a short audio sample as AudioSample. The longer file is playing as a loop and can be stopped with the keyboard keys. The volume (or Gain) is being controlled by the mouse position (mouseX). Hitting space bar triggers the short horn sample.
import ddf.minim.*; // Import the library
Minim minim; // Declare the Minim variable
AudioPlayer player; // Declare AudioPlayer (for long samples)
AudioSample sample; // Declare AudioSample (for short samples)
void setup(){
size(500,300);
// Instantiate Minim
minim = new Minim(this);
// Load the soundfiles (from the data folder)
player = minim.loadFile("drums.wav");
sample = minim.loadSample("horn.wav");
// Set the gain of the foghorn a bit lower
sample.setGain(-35);
// start playing the long file on a loop
player.loop();
}
void draw(){
// map the mouse x coordinate to a range to be used for the gain
float volume = map(mouseX, 0, width, -20, 0);
// set the gain of the drums
player.setGain(volume);
}
void keyPressed(){
// if p is pressed....
if (key == 'p'){
// ... check if the file is playing
if( player.isPlaying() == true){
// if it is, then pause
player.pause();
}else{
// if it isn't, then play again
player.loop();
}
}
// if r is pressed...
else if(key == 'r'){
// ... rewind the file
player.rewind();
}
// if space is pressed...
else if(key == ' '){
// ... trigger the horn sample
sample.trigger();
}
}
void stop(){
// always close Minim audio classes
player.close();
sample.close();
// always stop Minim before exiting
minim.stop();
// The super.stop() makes sure that all the normal cleanup routines are done
super.stop();
}
Example 2
This is the actual example we worked on during the class. With the controls for changing colors.
import ddf.minim.*; // import the library
Minim minim; // Declare the Minim variable
AudioPlayer player; // Declare the AudioPlayer variable
// these are used for controlling the background color
int red=0;
int green=0;
int blue=0;
void setup(){
size(640,480);
minim = new Minim(this);
player = minim.loadFile("drums.wav");
player.loop();
}
void draw(){
// background color
background(red,green,blue);
// map the mouse coordinates to control the gain
float vol = map(mouseX, 0, width, -45, 0);
// set the gain (volume)
player.setGain(vol);
// display the gain value
text(vol,20,20);
// display the mouse x coordinate
text(mouseX,20,40);
}
void keyPressed() {
if(key == 's'){
player.pause();
}else if(key == 'p'){
player.loop();
}else if(key == 'm'){
// pressing ¨'m' changes the colors
fill(134,65,77);
red = 190;
green = 45;
blue = 176;
}else if(key == 'a'){
// pressing ¨'a' changes the colors
fill(30,200,179);
red = 88;
green = 255;
blue = 169;
}
}
// We need to always have these when using minim
void stop(){
minim.stop();
player.close();
super.stop();
}
Useful Links & Food For Thought
- Today’s artist: Memo Akten
- The mimin Quickstart