User Tools

Site Tools


courses:intro.prototyping.spring.2011.feb04

Part of the course Intro to Prototyping IxD1, Spring 2011

Sound in processing

We used the Minim library and the objects AudioSample and AudioPlayer to play sounds in processing.

AudioSample

The first example of loading an audiosample into the RAM, triggering it with either a key on the keyboard or klicking a mousebutton. Also there's a graph illustrating the waveform of the sound. Try finding a stereo sample and displaying both channels.

import ddf.minim.*;
 
Minim minim;
AudioSample kick;
 
void setup()
{
  size(640, 480, P2D);
  // always start Minim before you do anything with it
  minim = new Minim(this);
  // load Balloon.wav from the data folder, with a 512 sample buffer
  kick = minim.loadSample("Balloon.wav");
}
 
void draw()
{
  background(0);
  stroke(255);
  // use the mix buffer to draw the waveforms.
  // because these are MONO files, we could have used the left or right buffers and got the same data
  for (int i = 0; i < kick.bufferSize() - 1; i++)
  {
    line(i, 200 - kick.left.get(i)*50, i+1, 200 - kick.left.get(i+1)*50);
  }
}
 
void keyPressed()
{
  if ( key == 'k' ) kick.trigger();
}
 
void mouseReleased()
{
    kick.trigger();
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  kick.close();
  minim.stop();
 
  super.stop();
}
AudioPlayer

In the next example we load an image to follow the mousepointer and have it play an AudioSample if it passes a position along the X-axis. We also use an AudioPlayer to load and play a lengthier file as soundtrack in the background.

import ddf.minim.*;
 
PImage myImage;
AudioSample player;
AudioPlayer soundtrack;
Minim minim;
 
 
void setup()
{
  background(0);
  size(640, 480);
  noCursor();
 
  minim = new Minim(this);
  //load a file, give the AudioPlayer buffers that are 2048 samples long
  player = minim.loadSample("Balloon.wav");
  soundtrack = minim.loadFile("song.mp3");
  soundtrack.play();
  myImage = loadImage("RicPic.jpg");
}
 
void draw()
{
 
  background(0);
 
  image(myImage, mouseX, mouseY);
  if(mouseX > 500)
  {
    player.trigger();
  }
}
 
 
void stop()
{
    // always close Minim audio classes when you are done with them
  player.close();
  minim.stop();
 
  super.stop();
}
Interactive sound

Here we draw a square and check if a mouseclick is done on it. If it is, you either start or stop a sound, depending on what it did before the click.

import ddf.minim.*;
String stopnow = "stopped";
AudioSample player;
Minim minim;
 
void setup ()
{  
 
  background (255,204,0);
  size(400,400);
 
  minim= new Minim(this);
  player= minim.loadSample("softecho.wav");
}
 
 
void draw()
{
  rect(50,50,100,100); 
}
 
void mousePressed () 
{
   if (mouseX>50 && mouseX<150 && mouseY>50 && mouseY<150)
  {
    if (stopnow == "stopped")
    { 
      player.trigger();
      stopnow = "playing";
    }
    else 
    { 
      if (stopnow == "playing") 
      {
        player.stop();
        stopnow = "stopped";
      }
    }
  }
}
courses/intro.prototyping.spring.2011.feb04.txt · Last modified: 2011/02/11 16:13 by 131.107.0.111