User Tools

Site Tools


courses:intro.prototyping.fall.2012.nov07

Sound in processing

AudioSample
import ddf.minim.*;
 
Minim minim;
AudioSample money, chest;
 
void setup()
{
  // always start Minim before you do anything with it
  minim = new Minim(this);
 
  // load soundfile from the data folder
  money = minim.loadSample("CASH2.WAV");
  chest = minim.loadSample("CHEST-OPEN1.WAV");  
}
 
void draw()
{
  //nothing to actually draw
}
 
//this runs when a key is pressed
void keyPressed()
{  
  if ( key == 'y' ) 
  {
    money.trigger();
  }
 
  if ( key == 'u' ) 
  {
    chest.trigger();
  }
}
 
//this is clean-up when the program quits
void stop()
{
  // always close Minim audio classes when you are done with them
  money.close();
  chest.close();
  minim.stop();
}
AudioPlayer
import ddf.minim.*;
 
Minim minim;
AudioPlayer soundtrack;
 
void setup()
{
  // always start Minim before you do anything with it
  minim = new Minim(this);
 
  //load a file  
  soundtrack = minim.loadFile("song.mp3");
}
 
void draw()
{
  //nothing to actually draw
}
 
//this is run when a key is pressed
void keyPressed()
{
  if ( key == 'q' ) 
  {
    soundtrack.play();
  }
 
  if ( key == 'w')
  {
    soundtrack.pause();
  }
 
  if( key == 'e')
  {
    soundtrack.rewind(); 
  }
}
 
//this is clean-up when the program quits
void stop()
{
  // always close Minim audio classes when you are done with them
  soundtrack.close();
  minim.stop(); 
}
 
New input! Mouseclicked!

Much like keyPressed() is run when a key is pressed, mouseClicked is run when any mousebutton is clicked and like keyPressed() this is written outside of setup() and draw().

void mouseClicked() {
  //you decide what happens
}
courses/intro.prototyping.fall.2012.nov07.txt · Last modified: 2012/11/07 00:00 by 130.239.232.31