createjssoundjs

Repeating a short sound very fast with CreateJS


Goal

I am trying to create a fast ticking sound in a Cordova app using Createjs.

The ticking sound speed changes based on user settings. At the moment the timing is erratic

Setup

I have an mp3 audio file of a single tick sound that is 50ms long.

A target speed of repetition could be as fast as 10 times per second.

Question

How can I get the sound to play evenly and consistently at that speed?

More Technical Detail

createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNCHED;
createjs.Ticker.framerate = 30;

Cheers for any help


Solution

  • This should be pretty straightforward. I set up a quick fiddle to play a sound a specific amount of times per second. It seems pretty reliable, even when playing at 60fps.

    https://jsfiddle.net/lannymcnie/ghjejvq9/

    The approach is to just check every Ticker.tick if the amount of time has passed since the last tick sound. The duration is derived by 1000/ticksPerSecond.

    // Every tick
    var d = new Date().getTime();
    if (d > lastTick + 1000/ticksPerSecond) {
      createjs.Sound.play("tick");
      lastTick = d;
    }