2

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

  • Is there a particular issue you are looking to solve, or are you just looking for recommendations? – Lanny Dec 20 '17 at 16:26
  • Thanks @Lanny. Probably a bit of recommendation seeking but the issue is the erratic rhythm of the ticks when trying to play a sound repeatedly very fast. Do you think it's possible to play my sound every 100ms? Even if I have a framerate of 60 (~17ms) and play it every 6th tick, it sounds pretty erratic. – Philip Sole Dec 21 '17 at 07:05

1 Answers1

0

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;
}
Lanny
  • 11,244
  • 1
  • 22
  • 30
  • I think I got a bit excited before testing. Perhaps it is a Cordova thing... but I've implemented your code and am still getting the erratic timing. One console error is popping up. Uncaught TypeError: e.setPlaybackResource is not a function. I'm using createJS 1.0.0 – Philip Sole Dec 21 '17 at 20:51
  • I think we solved error in the latest version (the function was incorrectly deprecated) -- https://github.com/CreateJS/SoundJS/commit/33b1303425da20770688455c46aab28c6dab4cdc. You could also use a faster tick for more accuracy (like using RAF). Any latency in your tick could definitely prevent it from playing reliably -- my demo does nothing else, so there is nothing to impact its performance. – Lanny Dec 21 '17 at 22:17
  • Cordova could also be adding an inconsistent delay between your call, and the actual sound ... :( – Lanny Dec 21 '17 at 22:31
  • Thanks Lanny. I did a console.log of the tick delta and it seemed to be pretty close to your jsfiddle. Not different enough to account for the large delays I'm hearing in the playback. So I guess it must be a Cordova issue I'll have to work around somehow. – Philip Sole Jan 02 '18 at 23:55