User Tools

Site Tools


courses:intro.prototyping.fall.2017.oct10

Sound in Processing

Today we worked with a library for sound called Minim. Minim documentation

Free sound for downloading

http://www.mediacollege.com/downloads/sound-effects/

http://www.freesoundeffects.com/

There are also a whole heap of sounds on the server under the “public” folder.

We moved on to a brief look into object oriented programming and on to play with sound using the Minim library. The full documentation is here.

Two of the classes we played around with was audioSample and audioPlayer.

Audiosample - short samples
import ddf.minim.*;
 
Minim minim;
AudioSample money, gun;
 
void setup()
{
  size(640, 480);
  // always start Minim before you do anything with it
  minim = new Minim(this);
  // load soundfile from the data folder
  money = minim.loadSample("CASH2.WAV");
  gun = minim.loadSample("SHOTGUN-X3.WAV");
 
}
 
void draw()
{
  //nothing to actually draw
}
 
void keyPressed()
{  
  if ( key == 'y' ) 
  {
    money.trigger();
  }
 
  if ( key == 'p' ) 
  {
    gun.trigger();
  }
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  money.close();
  gun.close();
  minim.stop();
}
AudioPlayer - longer audio files
import ddf.minim.*;
 
Minim minim;
AudioPlayer soundtrack;
 
void setup()
{
  size(640, 480);
  // always start Minim before you do anything with it
  minim = new Minim(this);
  //load a file, 
  soundtrack = minim.loadFile("song2.mp3");
}
 
void draw()
{
  //nothing to actually draw
}
 
void keyPressed()
{
  if ( key == 'q' ) 
  {
    if(soundtrack.isPlaying()) //checking if the song is playing to choose play/pause
     soundtrack.pause();
    else
     soundtrack.play();
  }
 
  if ( key == 'w')
  {
    soundtrack.skip(3000); //skipping three seconds
  }
 
  if( key == 'e')
  {
    soundtrack.rewind(); //well...yeah, guess. :-)
  }
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  soundtrack.close();
  minim.stop(); 
}
 
Concepts we worked with
audioPlayer
play()
pause()
rewind()
isPlaying()
position()
skip()
cue()
 
 
audioSample
trigger()
length()
stop()
 
setGain() and shiftGain() can be confusing, but quiet is -80 and full volume is 20, 
so if you control between those numbers you should get the right result.

Homework is to have something triggering sound using keyboard or mouse, and preferably with something nice on the screen.

courses/intro.prototyping.fall.2017.oct10.txt · Last modified: 2017/10/10 06:38 by rickard