Skip to content

DynamicsCompressor.reduction

Hongchan Choi edited this page Apr 19, 2016 · 3 revisions

In WebAudioAPI Issue 243, the reduction parameter for the DynamicsCompressor node changed from an AudioParam to a plain float. As this change gets implemented, developers will need to support both the AudioParam version and the float version.

Polyfill (to float value getter)

!function() {

  var _isCompressorReductionAudioParam = 
    typeof (new OfflineAudioContext(1, 1, 44100)).createDynamicsCompressor().reduction 
    === 'object';
  
  AudioContext.prototype._createDynamicsCompressor =
    AudioContext.prototype.createDynamicsCompressor;
  OfflineAudioContext.prototype._createDynamicsCompressor =
    OfflineAudioContext.prototype.createDynamicsCompressor;

  AudioContext.prototype.createDynamicsCompressor =
  OfflineAudioContext.prototype.createDynamicsCompressor = function () {
    var compressor = this._createDynamicsCompressor();
    if (_isCompressorReductionAudioParam) {
      compressor._reduction = compressor.reduction;
      Object.defineProperty(compressor, 'reduction', {
        get: function () {
          return compressor._reduction.value;
        }
      });
    }
    return compressor;
  };
  
}();

Here's a simple example of how to support both:

var c = new AudioContext();
var dc = c.createDynamicsCompressor();
var reductionValue;
if (typeof dc.reduction === 'float') {
  reductionValue = dc.reduction;
} else {
  reductionValue = dc.reduction.value;
}