howler.js icon indicating copy to clipboard operation
howler.js copied to clipboard

Ability to change playback rate at runtime

Open davidpaulrosser opened this issue 11 years ago • 3 comments

Hi,

Why doesn't the playback rate change when set at runtime. Is this possible?

davidpaulrosser avatar Jan 18 '15 17:01 davidpaulrosser

There is no property to change this except on setup, but I can look into that.

goldfire avatar Feb 03 '15 17:02 goldfire

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)

ruby0x1 avatar Apr 18 '15 05:04 ruby0x1

This has been added into the 2.0 branch: https://github.com/goldfire/howler.js/commit/5cb89aae9eaf4b4c4908fa696dcdcce86aaa5b30

goldfire avatar Sep 29 '15 20:09 goldfire