CDA - Adding Sounds in Programs Lesson

Adding Sounds in Programs

There are many different file formats for storing digital audio. Two of the most common are wav and MP3. Wav is an uncompressed file format and is the default for Windows computers. It takes about 10 megabytes of storage per minute of sound. A compressed MP3 file format is about 10 times smaller than the uncompressed audio.

Processing does not have sound functions built into the programming language but there are different libraries that can be used and imported to use sound in your Processing sketches.        

Minim Library

The Minim library provides a set of classes to use with playing and recording sounds. It is easy to import the library in Processing.  

Playing Sounds in Processing

Sounds in Processing

  1. To import the minim library to a Processing program you will click the minim Library. This will import all the classes that you need to work with sound.
  2. You will need to add the sound files to your program. Click Sketch and add file. Find the wav files on your computer and add them to the program just like you did with images. 

You have previously been provided sites to find free sound files.

  1. Next you need to create a Minim object to use to call the functions/methods of the class. You also need to create an AudioPlayer objects that will store the sound files you want to play. You will have an AudioPlayer for each sound file.  

Minim min;

AudioPlayer sound1;

  1. In the setup() function, you will create initialize the Minim object and load the sound files that were created.

min = new Minim(this);  

sound1 = min.loadFile("thud.wav");  

  1. To play the sound you use the play function:        

sound1.play();

  1. The last thing you must do is to close the audio. This is done in a stop() function.    

public void stop()

{

sound1.close();

min.stop();

super.stop();

}

soundfunctions
load snippet ( string filename)
load sample ( string file name)
loade file ( string filename)

Completed sound program

[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION