User Tools

Site Tools


courses:feb04

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

courses:feb04 [2011/02/04 00:45]
130.239.148.196 created
courses:feb04 [2011/02/04 02:45] (current)
130.239.148.196
Line 1: Line 1:
 +=== Sound in processing ===
 +
 +We used the [[http://​www.processing.org/​learning/​library/​|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.
 +
 +<code java>
 +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();​
 +}
 +</​code>​
 +
 +== 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.
 +
 +<code java>
 +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();​
 +}
 +</​code>  ​
 +
 +
 +== 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.
 +
 +<code java>
 +
 +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";​
 +      }
 +    }
 +  }
 +}
 +</​code>​
  
courses/feb04.txt · Last modified: 2011/02/04 02:45 by 130.239.148.196