Tone.js/Tone/effect/Convolver.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-10-23 15:42:41 +00:00
define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/effect/Effect"], function(Tone){
2014-10-23 15:42:41 +00:00
"use strict";
/**
2017-10-21 23:02:46 +00:00
* @class Tone.Convolver is a wrapper around the Native Web Audio
2015-07-04 19:25:37 +00:00
* [ConvolverNode](http://webaudio.github.io/web-audio-api/#the-convolvernode-interface).
* Convolution is useful for reverb and filter emulation. Read more about convolution reverb on
2015-07-04 19:25:37 +00:00
* [Wikipedia](https://en.wikipedia.org/wiki/Convolution_reverb).
2017-10-21 23:02:46 +00:00
*
2014-10-23 15:42:41 +00:00
* @constructor
* @extends {Tone.Effect}
2015-06-22 05:20:57 +00:00
* @param {string|Tone.Buffer|Object} [url] The URL of the impulse response or the Tone.Buffer
2017-10-21 23:02:46 +00:00
* contianing the impulse response.
* @param {Function=} onload The callback to invoke when the url is loaded.
2015-02-27 21:53:10 +00:00
* @example
* //initializing the convolver with an impulse response
* var convolver = new Tone.Convolver("./path/to/ir.wav").toMaster();
*/
2015-04-24 21:45:12 +00:00
Tone.Convolver = function(){
2017-04-26 03:18:08 +00:00
var options = Tone.defaults(arguments, ["url", "onload"], Tone.Convolver);
2015-04-24 21:45:12 +00:00
Tone.Effect.call(this, options);
2017-10-21 23:02:46 +00:00
/**
2014-10-23 15:42:41 +00:00
* convolver node
* @type {ConvolverNode}
* @private
*/
this._convolver = this.context.createConvolver();
2014-10-23 15:42:41 +00:00
/**
* the convolution buffer
* @type {Tone.Buffer}
2014-10-23 15:42:41 +00:00
* @private
*/
this._buffer = new Tone.Buffer(options.url, function(buffer){
this._convolver.buffer = buffer.get();
2016-11-07 01:13:18 +00:00
options.onload();
}.bind(this));
2014-10-23 15:42:41 +00:00
this.connectEffect(this._convolver);
};
2014-10-23 15:42:41 +00:00
Tone.extend(Tone.Convolver, Tone.Effect);
2015-04-24 21:45:12 +00:00
/**
* @static
* @const
* @type {Object}
*/
Tone.Convolver.defaults = {
2015-06-14 05:54:36 +00:00
"onload" : Tone.noOp
2015-04-24 21:45:12 +00:00
};
2015-02-23 05:28:07 +00:00
/**
* The convolver's buffer
* @memberOf Tone.Convolver#
* @type {AudioBuffer}
* @name buffer
*/
Object.defineProperty(Tone.Convolver.prototype, "buffer", {
get : function(){
return this._buffer.get();
},
set : function(buffer){
this._buffer.set(buffer);
this._convolver.buffer = this._buffer.get();
2015-02-23 05:28:07 +00:00
}
});
2014-10-23 15:42:41 +00:00
/**
2015-02-27 21:53:10 +00:00
* Load an impulse response url as an audio buffer.
2014-10-23 15:42:41 +00:00
* Decodes the audio asynchronously and invokes
* the callback once the audio buffer loads.
2015-06-22 05:20:57 +00:00
* @param {string} url The url of the buffer to load.
2014-10-23 15:42:41 +00:00
* filetype support depends on the
* browser.
2015-02-23 05:28:07 +00:00
* @param {function=} callback
2016-11-22 18:31:06 +00:00
* @returns {Promise}
2014-10-23 15:42:41 +00:00
*/
Tone.Convolver.prototype.load = function(url, callback){
2016-11-22 18:31:06 +00:00
return this._buffer.load(url, function(buff){
2015-02-23 05:28:07 +00:00
this.buffer = buff;
if (callback){
2015-02-23 05:28:07 +00:00
callback();
}
2015-02-23 05:28:07 +00:00
}.bind(this));
2014-10-23 15:42:41 +00:00
};
/**
2017-10-21 23:02:46 +00:00
* Clean up.
* @returns {Tone.Convolver} this
2014-10-23 15:42:41 +00:00
*/
Tone.Convolver.prototype.dispose = function(){
Tone.Effect.prototype.dispose.call(this);
this._convolver.disconnect();
this._convolver = null;
this._buffer.dispose();
2014-10-23 15:42:41 +00:00
this._buffer = null;
2015-02-02 18:22:16 +00:00
return this;
2017-10-21 23:02:46 +00:00
};
2014-10-23 15:42:41 +00:00
return Tone.Convolver;
2017-10-21 23:02:46 +00:00
});