In these notes you will learn:
As usual, it is wise to think about how we want a program to work before doing any coding. For the sake of concreteness, lets agree to have nine different sound effects that can be played. On the screen we’ll list the names of the effects beside a number. When the user presses the number on the keyboard, the corresponding sound effect is played.
Thinking more about the program, it soon becomes clear that we will need to:
We’ve seen these things before, and so our goal is to combine them into a single program.
We need nine different sound effect files. There are two ways to get such files: borrow them (e.g. from the web), or make your own.
If you search the web for “sound effects”, you will find many websites that provide free sound effect files of varying quality. Be careful not to download files that play for too long — they may take up too much memory when stored in our program.
If you have a computer with a microphone, then it’s not hard to create your own sound files. For instance, a few of the sound files in this note were recorded on a laptop. Some simple edits were made (such as removing leading and trailing silence) using Audacity, a free sound editing program.
For this program, we will use a selection of both borrowed and original WAV files: burp.wav, cough.wav, explosion.wav, elephant.wav, kaboom.wav, pew.wav, gunshot.wav, quack.wav, squeeze-toy.wav.
Our first step is to write the code that loads the sound effect files and to use keyPressed() to play the snippets:
// import Minim
import ddf.minim.*;
// set up the sound variables
Minim minim;
AudioSnippet effect1;
AudioSnippet effect2;
AudioSnippet effect3;
AudioSnippet effect4;
AudioSnippet effect5;
AudioSnippet effect6;
AudioSnippet effect7;
AudioSnippet effect8;
AudioSnippet effect9;
void setup() {
// load the sound effect files
print("Loading sound effects ... ");
minim = new Minim(this); // initialize Minim
effect1 = minim.loadSnippet("burp.wav");
effect2 = minim.loadSnippet("cough.wav");
effect3 = minim.loadSnippet("explosion.wav");
effect4 = minim.loadSnippet("elephant.wav");
effect5 = minim.loadSnippet("kaboom.wav");
effect6 = minim.loadSnippet("pew.wav");
effect7 = minim.loadSnippet("gunshot.wav");
effect8 = minim.loadSnippet("quack.wav");
effect9 = minim.loadSnippet("squeeze-toy.wav");
println("done");
// ...
}
void draw() {
// ... see below ...
}
void keyPressed() {
if (key == '1') {
effect1.play(0);
println("burp");
} else if (key == '2') {
effect2.play(0);
println("cough");
} else if (key == '3') {
effect3.play(0);
println("explosion");
} else if (key == '4') {
effect4.play(0);
println("elephant");
} else if (key == '5') {
effect5.play(0);
println("kaboom");
} else if (key == '6') {
effect6.play(0);
println("pew");
} else if (key == '7') {
effect7.play(0);
println("gunshot");
} else if (key == '8') {
effect8.play(0);
println("quack");
} else if (key == '9') {
effect9.play(0);
println("squeeze toy");
}
}
We use println to write the name of the sound effect to the console. This is useful if for some reason the sound on your computer isn’t working (or the volume is turned too low!): it gives you visual feedback about which effect has been played.
Note
The long if-else-if structure in keyPressed() can be re-written in a slightly simpler way using a switch statement:
switch(key) {
case '1':
effect1.play(0);
println("burp");
break;
case '2':
effect2.play(0);
println("cough");
break;
case '3':
effect3.play(0);
println("explosion");
break;
case '4':
effect4.play(0);
println("elephant");
break;
case '5':
effect5.play(0);
println("kaboom");
break;
case '6':
effect6.play(0);
println("pew");
break;
case '7':
effect7.play(0);
println("gunshot");
break;
case '8':
effect8.play(0);
println("quack");
break;
case '9':
effect9.play(0);
println("squeeze toy");
break;
} // switch
Some programmers find switch statements to be more readable than the equivalent if-statements, and so you will occasionally see them in Processing (and Java) programs.
However, switch does not work like other Processing statements, and this can lead to confusion. For instance, switch does not use code blocks to group the different cases. Instead it uses break to immediately jump out of the switch structure and execute the first statement that comes after it.
We won’t be using switch statements in any of the programs for this course.
The next step is to create an interface on the screen for the sound board. Creating good interfaces is quite challenging, and so we will aim for simplicity and just display a list of options with the number beside them:
void draw() {
background(255);
// draw effect names on the screen
fill(0);
text("1. burp", 10, 20);
text("2. cough", 10, 2*20);
text("3. explosion", 10, 3*20);
text("4. elephant", 10, 4*20);
text("5. kaboom", 10, 5*20);
text("6. pew", 10, 6*20);
text("7. gunshot", 10, 7*20);
text("8. quack", 10, 8*20);
text("9. squeeze toy", 10, 9*20);
}
// import Minim
import ddf.minim.*;
// set up the sound variables
Minim minim;
AudioSnippet effect1;
AudioSnippet effect2;
AudioSnippet effect3;
AudioSnippet effect4;
AudioSnippet effect5;
AudioSnippet effect6;
AudioSnippet effect7;
AudioSnippet effect8;
AudioSnippet effect9;
// fonts
PFont font;
void setup() {
// load the sound effect files
print("Loading sound effects ... ");
minim = new Minim(this); // initialize Minim
effect1 = minim.loadSnippet("burp.wav");
effect2 = minim.loadSnippet("cough.wav");
effect3 = minim.loadSnippet("explosion.wav");
effect4 = minim.loadSnippet("elephant.wav");
effect5 = minim.loadSnippet("kaboom.wav");
effect6 = minim.loadSnippet("pew.wav");
effect7 = minim.loadSnippet("gunshot.wav");
effect8 = minim.loadSnippet("quack.wav");
effect9 = minim.loadSnippet("squeeze-toy.wav");
println("done");
// initialize drawing
size(150, 200);
// fonts
font = loadFont("DejaVuSans-14.vlw");
textFont(font, 14);
}
void draw() {
background(255);
// draw effect names on the screen
fill(0);
text("1. burp", 10, 20);
text("2. cough", 10, 2*20);
text("3. explosion", 10, 3*20);
text("4. elephant", 10, 4*20);
text("5. kaboom", 10, 5*20);
text("6. pew", 10, 6*20);
text("7. gunshot", 10, 7*20);
text("8. quack", 10, 8*20);
text("9. squeeze toy", 10, 9*20);
}
void keyPressed() {
if (key == '1') {
effect1.play(0);
println("burp");
}
else if (key == '2') {
effect2.play(0);
println("cough");
}
else if (key == '3') {
effect3.play(0);
println("explosion");
}
else if (key == '4') {
effect4.play(0);
println("elephant");
}
else if (key == '5') {
effect5.play(0);
println("kaboom");
}
else if (key == '6') {
effect6.play(0);
println("pew");
}
else if (key == '7') {
effect7.play(0);
println("gunshot");
}
else if (key == '8') {
effect8.play(0);
println("quack");
}
else if (key == '9') {
effect9.play(0);
println("squeeze toy");
}
}