Home‎ > ‎Unit 11‎ > ‎

Working with Audio Files


You can have processing grab an audio file and play it. Stick with mp3 or wav files. Lets play with the LoadFile example sketch (File->Examples->Libraries->Minim->LoadFile).

Time to Break it Down:

The first bit imports the Minim Library, Starts Minim, and create an instance of AudioPlayer called player. Again, "player" is just a name, it could be anything.

The comments do a good job of explaining. One thing to know. Processing will look for the audio file in the sketch's "data" folder. The important piece here is player.play(); This is the command that plays the file. If you take it out of the setup() and do something like:

Then the song will wait till you press a key. You could also tie this to an "interesting pushbutton" and plan an alarm sound when someone enters your room.

Player Commands (remember, "player" is just an instance of AudioPlayer you could use any word you want instead of "player"):
 player.play() Plays the audio file loaded into player
 player.pause() Pause the audio file being played by player
 player.loop(##) Play the audio file in player ## times
 player.loop() Replaces player.play() to loop forever
 player.rewind() Start the audio file in player over again
 player.skip(##) Skip ahead ## milliseconds (use negative ## to skip Back)

To learn more check out the Minim Code Library for a more thorough explanation of Playable procedures.


This works just like LoadFile, but is used for short audio samples you will play over and over again. Grab it from File-Examples-Libraries-Minim and take a look. It should look very familiar. In fact, I'm not going to explain it all. You should be able to understand it all without my help. The only new bit is name.trigger() which basically just plays the file.

The main difference between LoadSample and LoadFile is when the audio file is copied into the computer's RAM. If you have short audio files you should use LoadSample. It will take slightly longer for the program to start when you run it, but it will run better.

You have all the same control options in AudioSample as you did with AudioPlayer. Again, your audio files need to be in the data folder so Processing can find them. Things to do:
    • Make a keyboard Drum Machine - Load in some drum sounds (Free Drum Sounds) and set them to be triggered with key presses from your keyboard.
    • Assignment 11.3: Make an Arduino Drum Machine - Build an Arduino circuit with pushbuttons to control your drum machine. Bonus, find a picture of a drum set. Highlight the different drums when they are played.