Sunday, February 24, 2013

Playing, Pausing and Restarting Sounds in Three.js

‹prev | My Chain | next›

I had a good time with HTML5 sounds last night. I really had no idea that it was so easy. I am still unsure if I will end up including sound in 3D Game Programming for Kids, but it seems worth building up a simple Sounds.js library.

I spend a good amount of time trolling through the for a selection of sounds. I end up with the following list (sound name, attribution, freesound filename):
bubble.mp3: CGEffex // 89534__cgeffex__very-fast-bubble-pop1.mp3
buzz.mp3: Sikoses // 82756__sikoses__stm1-upbass-5.mp3
click.mp3: // 157871__orginaljun__arcade-button-1-click-sound.mp3
donk.mp3: // 153063__orginaljun__1-bell-sound.mp3
drip.mp3: // 151840__orginaljun__pie-poi-water-dripping-sound.mp3
guitar.mp3: johnnypanic // 158650__johnnypanic__quickpowerchord2.mp3
knock.mp3: blimp66 // 88658__blimp66__knock.mp3
scratch.mp3: // 152844__orginaljun__1-rub-scratch-sound.mp3
snick.mp3: // 151482__orginaljun__weechuuu-high-pitch-sound-version-02.mp3
spring.mp3: // 153490__orginaljun__rusty-spring.mp3
swish.mp3: // 153489__orginaljun__friction-moving-object.mp3
It is not an exhaustive list, but hopefully a good start for kids / beginners building their first games.

I push each of those sounds under the urlspace. With that, I need a Sound class that can play any of them:
  function Sound(name) { = name; = document.createElement('audio');
    var source = document.createElement('source');
    source.src = '/sounds/' + name + '.mp3';;
  } = function() {;
This is just an object representation of last night's work. Also in the Sound prototype, I add the ability to repeat the sound, as well as stopping it:
  Sound.prototype.repeat = function() { = true;;

  Sound.prototype.stop = function() { = false;;
Since the constructor takes the sound name, which is cleverly the basename of the MP3 file containing the sound, I can define the list of all sounds as:
  var Sounds = {
    all: [
And then add each of these sounds to the Sounds list:
  Sounds.all.forEach(function(sound) {
    Sounds[sound] = new Sound(sound);
With that, I can redefine my Three.js / Physijs collision event handler to play a spring noise as:
  player.addEventListener('collision', function(object) {;
I rather like that. Except that it does not always play on collision. In particular, when a collision occurs immediately after another collision, the previous sound is still playing, blocking the new collision sound. To get around this, I stop the sound whenever I start it: = function() {
Furtermore, in the stop() method, I need to reset the current playback time to zero:
  Sound.prototype.stop = function() { = false; = 0;;
And that seems to do the trick.

I close by verifying that repeat mode works:
  setTimeout(function(){Sounds.swish.stop();}, 8000);
And work it does.

If nothing else, this seems worth including in an appendix. I have to think that kids are going to want to include sounds in their games. I must say that I really am surprised at how easy this sound stuff is—I had avoided it so far assuming that it would be much tougher.

(a simple live code sound demo)

Day #672

1 comment:

  1. The 'loop' attribute leaves a gap, so you can't hear it seamlessly.