Ability to change playback rate at runtime
Hi,
Why doesn't the playback rate change when set at runtime. Is this possible?
There is no property to change this except on setup, but I can look into that.
I added it to my build of howler.js (stable) using the following code.
Drop this in just below pos3d , and it works as you'd expect, calling howl.rate(0.5) changes it immediately.
At first I thought the rate was read only in the spec, but it says here:
Note: Though the AudioParam returned is read-only, the value it represents is not. so you change the .value property = realtime update. I couldn't find any specifics for the maximum or minimum, so you're mileage may vary.
/**
* Get/set the playback rate.
* NOTE: This only works with Web Audio API, HTML5 Audio playback
* will not be affected.
* @param {Float} rate The rate of the playback from 0.0 to 1000.0
* @param {String} id (optional) The play instance ID.
* @return {Howl/Float} Returns self or the current playback rate
*/
rate: function(value, id) {
var self = this;
// if the sound hasn't been loaded, add it to the event queue
if (!self._loaded) {
self.on('play', function() {
self.playback_rate(value, id);
});
return self;
}
if (value >= 0.0 && value <= 1000.0) {
if (self._webAudio) {
var activeNode = (id) ? self._nodeById(id) : self._activeNode();
if (activeNode) {
self._rate = value;
activeNode.bufferSource.playbackRate.value = value;
}
}
} else {
return self._rate;
}
return self;
},
(you're free to license this under howler/MIT license)
This has been added into the 2.0 branch: https://github.com/goldfire/howler.js/commit/5cb89aae9eaf4b4c4908fa696dcdcce86aaa5b30