Here we'll build on the tricks about only triggering the sound once in a loop from the Triggered Sound How To. In these examples it's very important because we're not only playing a sound, but we're looping it. Overlapping looping sounds can crash your machine and blow out your headphones. mySound.play() will play a sound until the sound is finished. mySound.stop() will stop it playing, where ever it is in its timeline, but mySound.loop() will play the sound over and over until you tell it to stop. This only works with certain sounds that have been designed to loop, and we'll use one of those here. Also, since the sound is tracking with the motion of a robot, we also have to turn it off when the robot is done moving.
You'll need to put this data folder into your sketch's folder for this to work
PROCESSING
Looping Sound
// Looping Sound
// v1.0
//
// Import Processing's sound library
import processing.sound.*;
// Global Variables
// Sound variables
SoundFile jetsonLoop;
boolean soundOn = false;
// Movement variables
boolean robotOn = false;
boolean goLeft = false;
boolean goRight = false;
int robotWidth = 100;
int newX = 500;
int speed = 5;
void setup() {
size(1000, 250);
jetsonLoop = new SoundFile(this, "jetsonloop.aif");
}
void draw() {
background(128);
// Provide instructions
textAlign(CENTER);
text("RIGHT AND LEFT ARROWS TO MOVE", width/2, height-15);
// Control motion
if (goLeft) {
newX-= speed;
}
if (goRight) {
newX+= speed;
}
// Limit motion
if (newX > width + (robotWidth/2)) {
newX = -robotWidth/2;
}
if (newX < -robotWidth/2) {
newX = width + (robotWidth/2);
}
// Move robot
translate(newX, height/2);
drawRobot();
}
void drawRobot() {
// Control sound
// Only trigger the sound once by setting
// soundOn to true the first time through
// the draw loop
if (robotOn && soundOn == false) {
jetsonLoop.loop();
soundOn = true; // only trigger the sound the first time
}
// When the key is released, set robotOn
// to false and also turn off the soundOn.
// That way, the next time the key is pressed
// the sound can be triggered again
if (!robotOn) {
jetsonLoop.stop(); // Stop the sound
soundOn = false;
}
// Draw robot
ellipse(0,0,100,100);
}
void keyPressed() {
if ( keyCode == 37) { // left
robotOn = true;
goLeft = true;
}
if ( keyCode == 39) { // right
robotOn = true;
goRight = true;
}
}
void keyReleased() {
robotOn = false;
goLeft = false;
goRight = false;
}
In this fancier version, we not only loop the sound when the robot is moving, but we'll attach starting and ending sounds to the movement. We'll use a cascade of tests to make them work in sequence. We need three booleans to track the sequence, soundStartOn, soundLoopOn, and soundEndOn, each to keep track of what's currently playing or looping.
Complex, but it's reliable and sounds kind of cool.
PROCESSING
Buttons Anywhere
// Advanced Looping Sound
// v1.0
//
// Import Processing's sound library
import processing.sound.*;
// Global Variables
// Sound variables
SoundFile jetsonStart;
SoundFile jetsonLoop;
SoundFile jetsonEnd;
boolean soundStartOn = false;
boolean soundLoopOn = false;
boolean soundEndOn = true;
// Movement variables
boolean robotOn = false;
boolean goLeft = false;
boolean goRight = false;
int robotWidth = 100;
int newX = 500;
int speed = 5;
void setup() {
size(1000, 250);
surface.setLocation(100, 900);
jetsonLoop = new SoundFile(this, "jetsonloop.aif");
jetsonStart = new SoundFile(this, "jetsonstart.aif");
jetsonEnd = new SoundFile(this, "jetsonstop.aif");
}
void draw() {
background(128);
// Provide instructions
textAlign(CENTER);
text("RIGHT AND LEFT ARROWS TO MOVE", width/2, height-15);
// Control motion
if (goLeft) {
newX-= speed;
println(newX);
}
if (goRight) {
newX+= speed;
}
// Limit motion
if (newX > width + (robotWidth/2)) {
newX = -robotWidth/2;
}
if (newX < -robotWidth/2) {
newX = width + (robotWidth/2);
}
// Move robot
translate(newX, height/2);
drawRobot();
}
void drawRobot() {
// Control sounds
// play startup sound
if (robotOn && soundStartOn == false) {
jetsonStart.play();
soundStartOn = true; // only trigger the sound the first time
soundEndOn = false; // trigger the looping sound
}
// Check if starup sound is finished, then loop sound
if (robotOn && !jetsonStart.isPlaying() && soundLoopOn == false) {
jetsonLoop.loop();
soundLoopOn = true; // only trigger the sound the first time
}
// Check if the robot isn't on anymore and that
// the end sound hasn't already been triggered
if (!robotOn && soundEndOn == false) {
jetsonEnd.play();
soundEndOn = true; // only trigger the sound the first time
}
// Reset boolean triggers and stop jetsonStart and jetsonStop
// if they're playing
if (!robotOn) {
jetsonStart.stop();
soundStartOn = false;
jetsonLoop.stop();
soundLoopOn = false;
}
// Draw robot
ellipse(0,0,100,100);
}
void keyPressed() {
if ( keyCode == 37) { // left
robotOn = true;
goLeft = true;
}
if ( keyCode == 39) { // right
robotOn = true;
goRight = true;
}
}
void keyReleased() {
robotOn = false;
goLeft = false;
goRight = false;
}