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.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone.Effect}
|
2015-02-23 05:28:07 +00:00
|
|
|
* @param {string|AudioBuffer=} url
|
2015-02-27 21:53:10 +00:00
|
|
|
* @example
|
|
|
|
* var convolver = new Tone.Convolver("./path/to/ir.wav");
|
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-02-23 05:28:07 +00:00
|
|
|
this._buffer = new Tone.Buffer(url, function(buffer){
|
|
|
|
this.buffer = buffer;
|
|
|
|
}.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
|
|
|
|
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);
|
2015-03-10 18:08:16 +00:00
|
|
|
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.
|
|
|
|
* @param {string} url the url of the buffer to load.
|
|
|
|
* filetype support depends on the
|
|
|
|
* browser.
|
2015-02-23 05:28:07 +00:00
|
|
|
* @param {function=} callback
|
2015-02-02 18:22:16 +00:00
|
|
|
* @returns {Tone.Convolver} `this`
|
2014-10-23 15:42:41 +00:00
|
|
|
*/
|
|
|
|
Tone.Convolver.prototype.load = function(url, callback){
|
2015-01-06 02:49:21 +00:00
|
|
|
this._buffer.load(url, function(buff){
|
2015-02-23 05:28:07 +00:00
|
|
|
this.buffer = buff;
|
2015-01-06 02:49:21 +00:00
|
|
|
if (callback){
|
2015-02-23 05:28:07 +00:00
|
|
|
callback();
|
2015-01-06 02:49:21 +00:00
|
|
|
}
|
2015-02-23 05:28:07 +00:00
|
|
|
}.bind(this));
|
2015-02-02 18:22:16 +00:00
|
|
|
return this;
|
2014-10-23 15:42:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dispose and disconnect
|
2015-02-02 18:22:16 +00:00
|
|
|
* @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;
|
2015-01-05 02:46:10 +00:00
|
|
|
this._buffer.dispose();
|
2014-10-23 15:42:41 +00:00
|
|
|
this._buffer = null;
|
2015-02-02 18:22:16 +00:00
|
|
|
return this;
|
2014-10-23 15:42:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Convolver;
|
2014-10-22 21:58:03 +00:00
|
|
|
});
|