Tone.js/Tone/source/Noise.js

259 lines
6.3 KiB
JavaScript
Raw Normal View History

define(["../core/Tone", "../source/Source", "../core/Buffer",
"../source/BufferSource"], function(Tone){
2014-06-20 04:25:00 +00:00
"use strict";
2014-06-20 04:25:00 +00:00
/**
* @class Tone.Noise is a noise generator. It uses looped noise buffers to save on performance.
* Tone.Noise supports the noise types: "pink", "white", and "brown". Read more about
2015-07-04 19:25:37 +00:00
* colors of noise on [Wikipedia](https://en.wikipedia.org/wiki/Colors_of_noise).
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
* //initialize the noise and start
* var noise = new Tone.Noise("pink").start();
2017-10-21 23:02:46 +00:00
*
* //make an autofilter to shape the noise
* var autoFilter = new Tone.AutoFilter({
2017-10-21 23:02:46 +00:00
* "frequency" : "8m",
* "min" : 800,
* "max" : 15000
* }).connect(Tone.Master);
2017-10-21 23:02:46 +00:00
*
* //connect the noise
* noise.connect(autoFilter);
* //start the autofilter LFO
* autoFilter.start()
2014-06-20 04:25:00 +00:00
*/
Tone.Noise = function(){
2014-06-20 04:25:00 +00:00
var options = Tone.defaults(arguments, ["type"], Tone.Noise);
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._type = options.type;
2014-06-20 04:25:00 +00:00
/**
* The playback rate of the noise. Affects
* the "frequency" of the noise.
* @type {Positive}
* @signal
*/
this._playbackRate = options.playbackRate;
2014-06-20 04:25:00 +00:00
};
Tone.extend(Tone.Noise, Tone.Source);
/**
* the default parameters
*
* @static
* @const
* @type {Object}
*/
Tone.Noise.defaults = {
"type" : "white",
"playbackRate" : 1
};
2014-06-20 04:25:00 +00:00
/**
2017-10-21 23:02:46 +00:00
* The type of the noise. Can be "white", "brown", or "pink".
2015-02-04 15:10:34 +00:00
* @memberOf Tone.Noise#
* @type {string}
* @name type
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(){
return this._type;
2017-10-21 23:02:46 +00:00
},
2015-02-04 15:10:34 +00:00
set : function(type){
if (this._type !== type){
if (type in _noiseBuffers){
this._type = type;
//if it's playing, stop and restart it
if (this.state === Tone.State.Started){
var now = this.now();
this._stop(now);
this._start(now);
}
} else {
throw new TypeError("Tone.Noise: invalid type: "+type);
}
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
/**
* The playback rate of the noise. Affects
* the "frequency" of the noise.
* @type {Positive}
* @signal
*/
Object.defineProperty(Tone.Noise.prototype, "playbackRate", {
get : function(){
return this._playbackRate;
2017-10-21 23:02:46 +00:00
},
set : function(rate){
this._playbackRate = rate;
2018-03-05 17:25:33 +00:00
if (this._source){
this._source.playbackRate.value = rate;
}
}
});
2014-06-20 04:25:00 +00:00
/**
* internal start method
2015-02-17 19:58:47 +00:00
*
2015-06-14 00:20:36 +00:00
* @param {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){
var buffer = _noiseBuffers[this._type];
this._source = new Tone.BufferSource(buffer).connect(this.output);
2014-06-20 04:25:00 +00:00
this._source.loop = true;
this._source.playbackRate.value = this._playbackRate;
this._source.start(this.toSeconds(time), Math.random() * (buffer.duration - 0.001));
2014-06-20 04:25:00 +00:00
};
/**
* internal stop method
2015-02-17 19:58:47 +00:00
*
2015-06-14 00:20:36 +00:00
* @param {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));
this._source = null;
2014-06-20 04:25:00 +00:00
}
};
/**
* Restarts the noise.
2018-05-19 19:07:58 +00:00
* @param {Time} time When to restart the noise.
* @return {Tone.Noise} this
*/
Tone.Noise.prototype.restart = function(time){
//TODO could be optimized by cancelling the buffer source 'stop'
//stop and restart
this._stop(time);
this._start(time);
2018-05-19 19:07:58 +00:00
return this;
};
2014-06-20 04:25:00 +00:00
/**
2015-06-20 19:50:57 +00:00
* Clean up.
* @returns {Tone.Noise} this
2014-06-20 04:25:00 +00:00
*/
Tone.Noise.prototype.dispose = function(){
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;
return this;
2014-06-20 04:25:00 +00:00
};
///////////////////////////////////////////////////////////////////////////
// THE BUFFERS
///////////////////////////////////////////////////////////////////////////
//Noise buffer stats
var bufferLength = 44100 * 5;
var channels = 2;
2014-06-20 04:25:00 +00:00
/**
2017-04-30 15:16:35 +00:00
* The noise arrays. Generated on initialization.
2017-10-21 23:02:46 +00:00
* borrowed heavily from https://github.com/zacharydenton/noise.js
2017-04-30 15:16:35 +00:00
* (c) 2013 Zach Denton (MIT)
2014-06-20 04:25:00 +00:00
* @static
* @private
* @type {Array}
2014-06-20 04:25:00 +00:00
*/
var _noiseBuffers = {};
var _noiseCache = {};
Object.defineProperty(_noiseBuffers, "pink", {
get : function(){
if (!_noiseCache.pink){
var buffer = [];
for (var channelNum = 0; channelNum < channels; channelNum++){
var channel = new Float32Array(bufferLength);
buffer[channelNum] = channel;
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-07-30 17:56:44 +00:00
}
_noiseCache.pink = new Tone.Buffer().fromArray(buffer);
2014-06-20 04:25:00 +00:00
}
return _noiseCache.pink;
}
});
Object.defineProperty(_noiseBuffers, "brown", {
get : function(){
if (!_noiseCache.brown){
var buffer = [];
for (var channelNum = 0; channelNum < channels; channelNum++){
var channel = new Float32Array(bufferLength);
buffer[channelNum] = channel;
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
}
2014-07-30 17:56:44 +00:00
}
_noiseCache.brown = new Tone.Buffer().fromArray(buffer);
2014-07-30 17:56:44 +00:00
}
return _noiseCache.brown;
}
});
Object.defineProperty(_noiseBuffers, "white", {
get : function(){
if (!_noiseCache.white){
var buffer = [];
for (var channelNum = 0; channelNum < channels; channelNum++){
var channel = new Float32Array(bufferLength);
buffer[channelNum] = channel;
for (var i = 0; i < bufferLength; i++){
channel[i] = Math.random() * 2 - 1;
}
2014-07-30 17:56:44 +00:00
}
_noiseCache.white = new Tone.Buffer().fromArray(buffer);
2014-07-30 17:56:44 +00:00
}
return _noiseCache.white;
}
});
2014-06-20 04:25:00 +00:00
return Tone.Noise;
2017-10-21 23:02:46 +00:00
});