2014-06-20 04:25:00 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/source/Source"], function(Tone){
|
|
|
|
|
2014-09-04 04:41:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-06-20 04:25:00 +00:00
|
|
|
/**
|
2015-02-17 19:58:47 +00:00
|
|
|
* @class Noise generator.
|
|
|
|
* Uses looped noise buffers to save on performance.
|
2014-06-20 04:25:00 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone.Source}
|
|
|
|
* @param {string} type the noise type (white|pink|brown)
|
2015-02-27 16:19:45 +00:00
|
|
|
* @example
|
|
|
|
* var noise = new Tone.Noise("pink");
|
2014-06-20 04:25:00 +00:00
|
|
|
*/
|
2014-09-30 04:28:48 +00:00
|
|
|
Tone.Noise = function(){
|
2014-06-20 04:25:00 +00:00
|
|
|
|
2014-10-01 04:55:31 +00:00
|
|
|
var options = this.optionsObject(arguments, ["type"], Tone.Noise.defaults);
|
2015-02-02 01:38:06 +00:00
|
|
|
Tone.Source.call(this, options);
|
2014-06-20 04:25:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {AudioBufferSourceNode}
|
|
|
|
*/
|
|
|
|
this._source = null;
|
2015-02-17 19:58:47 +00:00
|
|
|
|
2014-06-20 04:25:00 +00:00
|
|
|
/**
|
|
|
|
* the buffer
|
|
|
|
* @private
|
|
|
|
* @type {AudioBuffer}
|
|
|
|
*/
|
|
|
|
this._buffer = null;
|
|
|
|
|
2015-02-04 15:10:34 +00:00
|
|
|
this.type = options.type;
|
2014-06-20 04:25:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.Noise, Tone.Source);
|
|
|
|
|
2014-09-30 04:28:48 +00:00
|
|
|
/**
|
|
|
|
* the default parameters
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.Noise.defaults = {
|
|
|
|
"type" : "white",
|
|
|
|
};
|
|
|
|
|
2014-06-20 04:25:00 +00:00
|
|
|
/**
|
2015-02-04 15:10:34 +00:00
|
|
|
* The type of the noise. Can be "white", "brown", or "pink".
|
|
|
|
* @memberOf Tone.Noise#
|
|
|
|
* @type {string}
|
|
|
|
* @name type
|
2015-05-05 20:40:52 +00:00
|
|
|
* @options ["white", "brown", "pink"]
|
2015-02-27 16:19:45 +00:00
|
|
|
* @example
|
|
|
|
* noise.type = "white";
|
2014-06-20 04:25:00 +00:00
|
|
|
*/
|
2015-02-04 15:10:34 +00:00
|
|
|
Object.defineProperty(Tone.Noise.prototype, "type", {
|
|
|
|
get : function(){
|
|
|
|
if (this._buffer === _whiteNoise){
|
|
|
|
return "white";
|
|
|
|
} else if (this._buffer === _brownNoise){
|
|
|
|
return "brown";
|
|
|
|
} else if (this._buffer === _pinkNoise){
|
|
|
|
return "pink";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set : function(type){
|
2015-02-28 23:06:25 +00:00
|
|
|
if (this.type !== type){
|
|
|
|
switch (type){
|
|
|
|
case "white" :
|
|
|
|
this._buffer = _whiteNoise;
|
|
|
|
break;
|
|
|
|
case "pink" :
|
|
|
|
this._buffer = _pinkNoise;
|
|
|
|
break;
|
|
|
|
case "brown" :
|
|
|
|
this._buffer = _brownNoise;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
this._buffer = _whiteNoise;
|
|
|
|
}
|
|
|
|
//if it's playing, stop and restart it
|
2015-05-23 22:57:05 +00:00
|
|
|
if (this.state === Tone.State.Started){
|
2015-02-28 23:06:25 +00:00
|
|
|
var now = this.now() + this.bufferTime;
|
|
|
|
//remove the listener
|
|
|
|
this._source.onended = undefined;
|
|
|
|
this._stop(now);
|
|
|
|
this._start(now);
|
|
|
|
}
|
2015-02-04 15:10:34 +00:00
|
|
|
}
|
2014-10-03 18:54:09 +00:00
|
|
|
}
|
2015-02-04 15:10:34 +00:00
|
|
|
});
|
2014-10-03 18:54:09 +00:00
|
|
|
|
2014-06-20 04:25:00 +00:00
|
|
|
/**
|
|
|
|
* internal start method
|
2015-02-17 19:58:47 +00:00
|
|
|
*
|
2014-06-20 05:46:39 +00:00
|
|
|
* @param {Tone.Time} time
|
2014-06-20 04:25:00 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2015-02-17 19:58:47 +00:00
|
|
|
Tone.Noise.prototype._start = function(time){
|
2014-06-20 04:25:00 +00:00
|
|
|
this._source = this.context.createBufferSource();
|
|
|
|
this._source.buffer = this._buffer;
|
|
|
|
this._source.loop = true;
|
2014-11-30 22:37:29 +00:00
|
|
|
this.connectSeries(this._source, this.output);
|
2014-07-30 17:56:44 +00:00
|
|
|
this._source.start(this.toSeconds(time));
|
2014-10-08 15:59:31 +00:00
|
|
|
this._source.onended = this.onended;
|
2014-06-20 04:25:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* internal stop method
|
2015-02-17 19:58:47 +00:00
|
|
|
*
|
2014-06-20 05:12:15 +00:00
|
|
|
* @param {Tone.Time} time
|
2014-06-20 04:25:00 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.Noise.prototype._stop = function(time){
|
2015-02-02 02:32:07 +00:00
|
|
|
if (this._source){
|
|
|
|
this._source.stop(this.toSeconds(time));
|
2014-06-20 04:25:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dispose all the components
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Noise} `this`
|
2014-06-20 04:25:00 +00:00
|
|
|
*/
|
|
|
|
Tone.Noise.prototype.dispose = function(){
|
2014-08-24 20:24:16 +00:00
|
|
|
Tone.Source.prototype.dispose.call(this);
|
2014-06-20 04:25:00 +00:00
|
|
|
if (this._source !== null){
|
|
|
|
this._source.disconnect();
|
|
|
|
this._source = null;
|
|
|
|
}
|
|
|
|
this._buffer = null;
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
2014-06-20 04:25:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// THE BUFFERS
|
2015-02-17 19:58:47 +00:00
|
|
|
// borrowed heavily from http://noisehack.com/generate-noise-web-audio-api/
|
2014-06-20 04:25:00 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
2014-07-30 17:56:44 +00:00
|
|
|
* static noise buffers
|
2015-02-17 19:58:47 +00:00
|
|
|
*
|
2014-06-20 04:25:00 +00:00
|
|
|
* @static
|
|
|
|
* @private
|
|
|
|
* @type {AudioBuffer}
|
|
|
|
*/
|
2014-07-30 17:56:44 +00:00
|
|
|
var _pinkNoise = null, _brownNoise = null, _whiteNoise = null;
|
2014-06-20 04:25:00 +00:00
|
|
|
|
2014-07-30 17:56:44 +00:00
|
|
|
Tone._initAudioContext(function(audioContext){
|
2014-06-20 04:25:00 +00:00
|
|
|
|
2014-07-30 17:56:44 +00:00
|
|
|
var sampleRate = audioContext.sampleRate;
|
2015-02-17 19:58:47 +00:00
|
|
|
|
2014-07-30 17:56:44 +00:00
|
|
|
//four seconds per buffer
|
|
|
|
var bufferLength = sampleRate * 4;
|
|
|
|
|
|
|
|
//fill the buffers
|
|
|
|
_pinkNoise = (function() {
|
|
|
|
var buffer = audioContext.createBuffer(2, bufferLength, sampleRate);
|
|
|
|
for (var channelNum = 0; channelNum < buffer.numberOfChannels; channelNum++){
|
|
|
|
var channel = buffer.getChannelData(channelNum);
|
|
|
|
var b0, b1, b2, b3, b4, b5, b6;
|
|
|
|
b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0.0;
|
|
|
|
for (var i = 0; i < bufferLength; i++) {
|
|
|
|
var white = Math.random() * 2 - 1;
|
|
|
|
b0 = 0.99886 * b0 + white * 0.0555179;
|
|
|
|
b1 = 0.99332 * b1 + white * 0.0750759;
|
|
|
|
b2 = 0.96900 * b2 + white * 0.1538520;
|
|
|
|
b3 = 0.86650 * b3 + white * 0.3104856;
|
|
|
|
b4 = 0.55000 * b4 + white * 0.5329522;
|
|
|
|
b5 = -0.7616 * b5 - white * 0.0168980;
|
|
|
|
channel[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
|
|
|
|
channel[i] *= 0.11; // (roughly) compensate for gain
|
|
|
|
b6 = white * 0.115926;
|
|
|
|
}
|
2014-06-20 04:25:00 +00:00
|
|
|
}
|
2014-07-30 17:56:44 +00:00
|
|
|
return buffer;
|
|
|
|
}());
|
|
|
|
|
|
|
|
_brownNoise = (function() {
|
|
|
|
var buffer = audioContext.createBuffer(2, bufferLength, sampleRate);
|
|
|
|
for (var channelNum = 0; channelNum < buffer.numberOfChannels; channelNum++){
|
|
|
|
var channel = buffer.getChannelData(channelNum);
|
|
|
|
var lastOut = 0.0;
|
|
|
|
for (var i = 0; i < bufferLength; i++) {
|
|
|
|
var white = Math.random() * 2 - 1;
|
|
|
|
channel[i] = (lastOut + (0.02 * white)) / 1.02;
|
|
|
|
lastOut = channel[i];
|
|
|
|
channel[i] *= 3.5; // (roughly) compensate for gain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
})();
|
|
|
|
|
|
|
|
_whiteNoise = (function(){
|
|
|
|
var buffer = audioContext.createBuffer(2, bufferLength, sampleRate);
|
|
|
|
for (var channelNum = 0; channelNum < buffer.numberOfChannels; channelNum++){
|
|
|
|
var channel = buffer.getChannelData(channelNum);
|
|
|
|
for (var i = 0; i < bufferLength; i++){
|
|
|
|
channel[i] = Math.random() * 2 - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}());
|
|
|
|
});
|
2014-06-20 04:25:00 +00:00
|
|
|
|
|
|
|
return Tone.Noise;
|
2014-04-06 00:47:59 +00:00
|
|
|
});
|