2014-10-23 15:42:41 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/effect/Effect"], function(Tone){
|
2014-10-22 21:58:03 +00:00
|
|
|
|
2014-10-23 15:42:41 +00:00
|
|
|
"use strict";
|
2014-10-22 21:58:03 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-23 15:42:41 +00:00
|
|
|
* @class Convolver wrapper for reverb and emulation.
|
|
|
|
* NB: currently, this class only supports 1 buffer member.
|
|
|
|
* Future iterations will include a this.buffers collection for multi buffer mode.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone.Effect}
|
2015-01-05 02:46:10 +00:00
|
|
|
* @param {string|Object|AudioBuffer=} url
|
2014-12-02 06:42:08 +00:00
|
|
|
* @param {function=} callback function
|
2014-10-22 21:58:03 +00:00
|
|
|
*/
|
2015-01-06 02:49:21 +00:00
|
|
|
Tone.Convolver = function(url){
|
2014-10-22 21:58:03 +00:00
|
|
|
|
2015-01-06 02:49:21 +00:00
|
|
|
Tone.Effect.apply(this, arguments);
|
2014-10-22 21:58:03 +00:00
|
|
|
|
2014-10-23 15:42:41 +00:00
|
|
|
/**
|
|
|
|
* convolver node
|
|
|
|
* @type {ConvolverNode}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._convolver = this.context.createConvolver();
|
2014-10-22 21:58:03 +00:00
|
|
|
|
2014-10-23 15:42:41 +00:00
|
|
|
/**
|
|
|
|
* the convolution buffer
|
2015-01-05 02:46:10 +00:00
|
|
|
* @type {Tone.Buffer}
|
2014-10-23 15:42:41 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2015-01-06 02:49:21 +00:00
|
|
|
this._buffer = new Tone.Buffer(url, this.setBuffer.bind(this));
|
2014-10-22 21:58:03 +00:00
|
|
|
|
2014-10-23 15:42:41 +00:00
|
|
|
this.connectEffect(this._convolver);
|
|
|
|
};
|
2014-10-22 21:58:03 +00:00
|
|
|
|
2014-10-23 15:42:41 +00:00
|
|
|
Tone.extend(Tone.Convolver, Tone.Effect);
|
2014-10-22 21:58:03 +00:00
|
|
|
|
2014-10-23 15:42:41 +00:00
|
|
|
/**
|
2015-01-06 02:49:21 +00:00
|
|
|
* the default parameters
|
2014-10-23 15:42:41 +00:00
|
|
|
* @static
|
2015-01-06 02:49:21 +00:00
|
|
|
* @const
|
2014-10-23 15:42:41 +00:00
|
|
|
* @type {Object}
|
|
|
|
*/
|
2015-01-06 02:49:21 +00:00
|
|
|
Tone.Convolver.defaults = {};
|
2014-10-23 15:42:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the impulse response url as an audio buffer.
|
|
|
|
* Decodes the audio asynchronously and invokes
|
|
|
|
* the callback once the audio buffer loads.
|
|
|
|
* @param {string} url the url of the buffer to load.
|
|
|
|
* filetype support depends on the
|
|
|
|
* browser.
|
|
|
|
* @param {function(Tone.Convolver)=} callback
|
|
|
|
*/
|
|
|
|
Tone.Convolver.prototype.load = function(url, callback){
|
2015-01-06 02:49:21 +00:00
|
|
|
var self = this;
|
|
|
|
this._buffer.load(url, function(buff){
|
|
|
|
self.setBuffer(buff);
|
|
|
|
if (callback){
|
|
|
|
callback(this);
|
|
|
|
}
|
|
|
|
});
|
2014-10-23 15:42:41 +00:00
|
|
|
};
|
2014-10-22 21:58:03 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-23 15:42:41 +00:00
|
|
|
* set the buffer
|
|
|
|
* @param {AudioBuffer} buffer the impulse response
|
|
|
|
*/
|
|
|
|
Tone.Convolver.prototype.setBuffer = function(buffer){
|
2015-01-05 02:46:10 +00:00
|
|
|
this._buffer.set(buffer);
|
|
|
|
this._convolver.buffer = this._buffer.get();
|
2014-10-23 15:42:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set multiple parameters at once with an object
|
|
|
|
* @param {Object} params the parameters as an object
|
|
|
|
*/
|
|
|
|
Tone.Convolver.prototype.set = function(params){
|
|
|
|
if (!this.isUndef(params.buffer)) this.setBuffer(params.buffer);
|
|
|
|
Tone.Effect.prototype.set.call(this, params);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dispose and disconnect
|
|
|
|
*/
|
|
|
|
Tone.Convolver.prototype.dispose = function(){
|
|
|
|
Tone.Effect.prototype.dispose.call(this);
|
|
|
|
this._convolver.disconnect();
|
|
|
|
this._convolver = null;
|
2015-01-05 02:46:10 +00:00
|
|
|
this._buffer.dispose();
|
2014-10-23 15:42:41 +00:00
|
|
|
this._buffer = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Convolver;
|
2014-10-22 21:58:03 +00:00
|
|
|
});
|