One of the lesser-used functions of Processing is its ability to work with sound, both in terms of creating sound synthesis, sound Processing, and the exporting and playing of sounds. In this movie, I just want to show you the simplest-possible thing where we actually have just a sound file that has played as part as the Processing sketch. Start by putting a little comment here at the top of my sign. Now, in order to do this, we're going to need to import a library. It's one that comes with Processing, but it needs to be added. So, I come up here to Sketch, to Import Library, and we're going to use this one right here.
It's called minim. I am going to add that, and I will put my little comment back up at the top. And it brings in several blocks of code that it's going to be working with. That's fine. Now all I need to do is I need to create a minim object. That's something that's going to have the actual sound in it. I am going to put soundCode. Minim is like the variable type, and soundCode is what I'm declaring it as. Then we're going to do this little AudioPlayer function that we call birdSong; that will be the variable. Then I do the standard void setup, and I don't need a lot here.
I'm going to make this window a little different. I will do 400 x 250. Then I'm going to bring in a picture. I have a picture of a bird that I'm going to put in here. So, that declares the variable that I have a image, the variable type. Actually, the object is PImage. I am declaring bird, and then I'm going to put bird = loadImage, and then in parentheses and quotes, I need to put the exact name of the file. Although it occurs to me, I actually haven't added this one to the folder yet.
I'm going to scoot this Processing window over for a moment. And these are the files that exist in the Exercise folder, but they're not yet added to the sketch. I have an image file and I have a WAV file. So, I'm going to take just the both of those and drag them onto the Processing sketch. Now, you will see that it says 2 files added to the sketch. In fact, if I click on the Sketch folder, go to data, they've both been copied into there. So, that's exactly what we need. I will come back to the Processing window and open it up again. Then I need to place the image, image(bird and I'll put it on top-left corner.
I'm going to make it go the width of the window, and the height. Great! And now I just need a little bit of code for the sound. So earlier, at the top, I said I have created a minim object called soundCode. So, I'm going to refer back to that one right now, and I have to do new Minim and then I use (this). It's kind of a funny way to do it, but that refers back to the most recently created object. Then I'm going to call it birdSong = soundCode, and then what we're going to do with that object that I've created now is we're going to do the loadFile command, and we're going to load in there ("bird.wav"). That's the name of the file, so quotes and parentheses and a semicolon.
I need to add one line of code here, birdSong.play, so it will actually play on launch. And that finishes that block of code. Now, there is one other block that we're going to need to do that's a little unusual. Interestingly, we have a setup block here, but we're not going to have a draw block because we don't need anything that cycles. But the developers of Minim are very clear that you need to add a special block to stop all the functions. So, I'm going to type in one here called void stop, and I'll put in those curly brackets. This one has three commands. First, there is the birdSong.close is our first one, and then the second one is soundCode.
That's another one of the things I declared. That one I need to do stop. And then the last one goes by the humorous title of the super.stop. This is necessary because the Minim library can't connect with external hardware like MIDI equipment, and it needs to make sure that all the channels are shut off. So this is just something to always add at the end. I'm going to save that, and I'll press Ctrl+R to run on a PC, or Command+R to run on a Mac. (music playing) And there we go! I have a picture of a bird and I have a birdsong.
And now I'm going to stop it all and do my stopping. Let me just mention one thing: Minim is a very well-developed library for sound Processing. Even though that's not something that happens very often with Processing, it is still something very capable of it. I do want to recommend that you take a look at another library that's called Beads, and it's at beadsproject.net. It's also a very well-developed sound library for Processing. And in fact, there is even a book that's available in hard copy or electronically, by Evan Merz, called Sonifying Processing: The Beads Tutorial.
And if you're interested in using Processing with sound, I suggest you take a close look at that one as well.
Get unlimited access to all courses for just $25/month.Become a member