How To...

Triggered Sound

triggered_sound1.pde

Using Processing's sound library (see class 12 and 13 for examples) we'll load a sound from the data folder and play it based on the spacebar being pressed. Two tricky bits here. One is rotating the laser cannon barrel with the right and left arrow keys, and the other is triggering the blast sound with the spacebar. There's a common 'gotcha' bug in triggering sounds in a loop, you have to only trigger the sound once, otherwise the sound will be triggered every time you cycle through the loop, resulting in a noisy mess. To do this, we test a boolean that's set to true (fireLaser) when the spacebar is pressed, but then set back to false as soon as the sound is commanded to play (laserSound.play()).

You'll need to put blast.mp3 into your sketch's data folder (unzip it first) for this to work (unzip if first)

PROCESSING

Triggered Sound

// Simple Triggered Sound 
// v1.0
//
// Import Processing's sound library
import processing.sound.*;

// Global Variable
// Make a sound variable
SoundFile laserSound;
// Laser variables
int rotateLaser = 0;
int turretSpeed = 5;
boolean fireLaser = false;

void setup() {
  size(500, 500);
  // Load the sound into the player object
  laserSound = new SoundFile(this, "blast.mp3");
}

void draw() {
  background(128);
  // Provide instructions
  textAlign(CENTER);
  text("SPACEBAR TO FIRE, RIGHT AND LEFT ARROWS TO AIM", width/2, height-15);
  // Center the laser cannon at the bottom of the screen
  translate(width/2, height-50);
  drawCannon();
}

void drawCannon() {
  // Laser
  // Isolate laser rotation from turret
  pushMatrix();
    rotate(radians(rotateLaser)-PI);
    // Laser beam and blast sound
    if (fireLaser) {
      laserSound.play();
      stroke(255, 0, 0);
      strokeWeight(9);
      line(0, 10, 0, 1000);
      fireLaser=false;
    }
    // Laser barrel
    strokeWeight(11);
    stroke(0);
    line(0, 10, 0, 100);
    strokeWeight(1);
    translate(0, 10);
  popMatrix();
  // Turret
  arc(0, 10, 100, 100, PI, TWO_PI, CHORD);
}

void keyPressed() {
  if (keyCode == 37) { // left
    if (rotateLaser > -90) { // limit rotation
      rotateLaser -= turretSpeed;
    }
  }
  if (keyCode == 39) { // right
    if (rotateLaser < 90) { // limit rotation
      rotateLaser += turretSpeed;
    }
  }
  if (keyCode == 32) { // spacebar
    fireLaser=true;
  }
}

A Variation

triggered_sound2.pde

This is essentially the same code except it does a bit of testing to keep the laserbeam on for half a second. In the above version, sometimes the sketch is playing at such a high framerate that you don't see the red line of the laser blast. Here we draw a fancier laser blast and hold in on. To do this, we add another boolean variable, laserBeam, and a variable to track the time, laserBeamTimer. When the spacebar is pressed it sets both the fireLaser and laserBeam booleans to true. fireLaser is immediately turned off like before, but laserBeam stays true for a while (the length of time tested with laserBeamTimer).

PROCESSING

Fancy Triggered Sound

PROCESSING

Example

// Fancy Triggered Sound 
// v1.0
//
// Import Processing's sound library
import processing.sound.*;

// Global Variable
// Make a sound variable
SoundFile laserSound;
// Laser variables
int rotateLaser = 0;
int turretSpeed = 5;
boolean fireLaser = false;
boolean laserBeam = false;
int laserBeamTimer = 0;
int barrelLength = 100;

void setup() {
  size(500, 500);
  // Load the sound into the player object
  laserSound = new SoundFile(this, "blast.mp3");
}

void draw() {
  background(128);
  // Provide instructions
  textAlign(CENTER);
  text("SPACEBAR TO FIRE, RIGHT AND LEFT ARROWS TO AIM", width/2, height-15);
  // Center the laser cannon at the bottom of the screen
  translate(width/2, height-50);
  drawCannon();
}

void drawCannon() {
  // Laser
  // Isolate laser rotation from turret
  pushMatrix();
    rotate(radians(rotateLaser)-PI);
    // Blast sound
    if (fireLaser) {
      laserSound.play();
      fireLaser=false;
    }
    // Laser beam
    if (laserBeam) { // Value set in keyPressed()
      // Draw laserbeam
      noStroke();
      fill(128,0,0);
      ellipse(0, barrelLength+5, 30,20);
      stroke(128, 0, 0);
      strokeWeight(9);
      line(0, 100, 0, height);
      noStroke();
      fill(255, 128, 0);
      ellipse(0, barrelLength+5, 20,10);
      stroke(255, 128, 0);
      strokeWeight(5);
      line(0, 100, 0, height);
      stroke(255, 255, 255);
      strokeWeight(1);
      line(0, 100, 0, height);
      noStroke();
      fill(255, 255, 255);
      ellipse(0, barrelLength+5, 10,10);
      // Fix fill
      fill(255);
      barrelLength = 90;
      // Keep drawing the beam for a 1/4 second
      if(millis() > laserBeamTimer + 250) {
        laserBeam = false;
      } 
    } else {
      barrelLength = 100;
    }
    // Laser barrel
    strokeWeight(11);
    stroke(0);
    line(0, 10, 0, barrelLength);
    strokeWeight(1);
    translate(0, 10);
  popMatrix();
  // Turret
  arc(0, 10, 100, 100, PI, TWO_PI, CHORD);
}

void keyPressed() {
  if (keyCode == 37) { // left
    if (rotateLaser >= -85) { // limit rotation
      rotateLaser -= turretSpeed;
    }
  }
  if (keyCode == 39) { // right
    if (rotateLaser <= 85) { // limit rotation
      rotateLaser += turretSpeed;
    }
  }
  if (keyCode == 32) { // spacebar
    // Trigger the laser sound
    fireLaser = true;
    // Tr1gger the laser beam
    laserBeam = true;
    // Track start time of laser beam
    laserBeamTimer = millis();
  }
}