Tone.js/doc/Noise.js.html
2014-06-23 14:20:20 -04:00

494 lines
11 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tone.js Source: source/Noise.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.flatly.css">
</head>
<body>
<div class="container-fluid">
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<a class="brand" href="index.html">Tone.js</a>
<ul class="nav">
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="Tone.html">Tone</a>
</li>
<li>
<a href="Tone.Add.html">Add</a>
</li>
<li>
<a href="Tone.AutoPanner.html">AutoPanner</a>
</li>
<li>
<a href="Tone.BitCrusher.html">BitCrusher</a>
</li>
<li>
<a href="Tone.DryWet.html">DryWet</a>
</li>
<li>
<a href="Tone.Effect.html">Effect</a>
</li>
<li>
<a href="Tone.Envelope.html">Envelope</a>
</li>
<li>
<a href="Tone.FeedbackDelay.html">FeedbackDelay</a>
</li>
<li>
<a href="Tone.FeedbackEffect.html">FeedbackEffect</a>
</li>
<li>
<a href="Tone.LFO.html">LFO</a>
</li>
<li>
<a href="Tone.Merge.html">Merge</a>
</li>
<li>
<a href="Tone.Meter.html">Meter</a>
</li>
<li>
<a href="Tone.Microphone.html">Microphone</a>
</li>
<li>
<a href="Tone.Multiply.html">Multiply</a>
</li>
<li>
<a href="Tone.Noise.html">Noise</a>
</li>
<li>
<a href="Tone.Oscillator.html">Oscillator</a>
</li>
<li>
<a href="Tone.Panner.html">Panner</a>
</li>
<li>
<a href="Tone.PingPongDelay.html">PingPongDelay</a>
</li>
<li>
<a href="Tone.Player.html">Player</a>
</li>
<li>
<a href="Tone.Recorder.html">Recorder</a>
</li>
<li>
<a href="Tone.Scale.html">Scale</a>
</li>
<li>
<a href="Tone.Signal.html">Signal</a>
</li>
<li>
<a href="Tone.Source.html">Source</a>
</li>
<li>
<a href="Tone.Split.html">Split</a>
</li>
<li>
<a href="Tone.Transport.html">Transport</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="global.html#isFrequency">isFrequency</a>
</li>
<li>
<a href="global.html#isNotation">isNotation</a>
</li>
<li>
<a href="global.html#isTransportTime">isTransportTime</a>
</li>
<li>
<a href="global.html#toTransportTime">toTransportTime</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<h1 class="page-title">Source: source/Noise.js</h1>
<section>
<article>
<pre class="sunlight-highlight-javascript linenums">define(["Tone/core/Tone", "Tone/source/Source"], function(Tone){
var sampleRate = Tone.context.sampleRate;
//two seconds per buffer
var bufferLength = sampleRate * 4;
/**
* Noise generator.
*
* uses looped noise buffers to save on performance.
*
* @constructor
* @extends {Tone.Source}
* @param {string} type the noise type (white|pink|brown)
*/
Tone.Noise = function(type){
Tone.Source.call(this);
/**
* @private
* @type {AudioBufferSourceNode}
*/
this._source = null;
/**
* the buffer
* @private
* @type {AudioBuffer}
*/
this._buffer = null;
/**
* set a callback function to invoke when the sample is over
*
* @type {function}
*/
this.onended = function(){};
this.setType(this.defaultArg(type, "white"));
};
Tone.extend(Tone.Noise, Tone.Source);
/**
* set the noise type
*
* @param {string} type the noise type (white|pink|brown)
* @param {Tone.Time} time (optional) time that the set will occur
*/
Tone.Noise.prototype.setType = function(type, time){
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
if (this.state === Tone.Source.State.STARTED){
time = this.toSeconds(time);
//remove the listener
this._source.onended = undefined;
this._stop(time);
this._start(time);
}
};
/**
* internal start method
*
* @param {Tone.Time} time
* @private
*/
Tone.Noise.prototype._start = function(time){
this._source = this.context.createBufferSource();
this._source.buffer = this._buffer;
this._source.loop = true;
this._source.start(this.toSeconds(time));
this.chain(this._source, this.output);
this._source.onended = this._onended.bind(this);
};
/**
* start the noise at a specific time
*
* @param {Tone.Time} time
*/
Tone.Noise.prototype.start = function(time){
if (this.state === Tone.Source.State.STOPPED){
this.state = Tone.Source.State.STARTED;
//make the source
this._start(time);
}
};
/**
* internal stop method
*
* @param {Tone.Time} time
* @private
*/
Tone.Noise.prototype._stop = function(time){
this._source.stop(this.toSeconds(time));
};
/**
* stop the noise at a specific time
*
* @param {Tone.Time} time
*/
Tone.Noise.prototype.stop = function(time){
if (this.state === Tone.Source.State.STARTED) {
if (this._buffer && this._source){
if (!time){
this.state = Tone.Source.State.STOPPED;
}
this._stop(time);
}
}
};
/**
* internal call when the buffer is done playing
*
* @private
*/
Tone.Noise.prototype._onended = function(){
this.state = Tone.Source.State.STOPPED;
this.onended();
};
/**
* dispose all the components
*/
Tone.Noise.prototype.dispose = function(){
if (this._source !== null){
this._source.disconnect();
this._source = null;
}
this._buffer = null;
this.output.disconnect();
this.output = null;
};
///////////////////////////////////////////////////////////////////////////
// THE BUFFERS
// borred heavily from http://noisehack.com/generate-noise-web-audio-api/
///////////////////////////////////////////////////////////////////////////
/**
* static brown noise buffer
*
* @static
* @private
* @type {AudioBuffer}
*/
var _pinkNoise = (function() {
var buffer = Tone.context.createBuffer(2, bufferLength, sampleRate);
for (var channelNum = 0; channelNum &lt; 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 &lt; 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;
}
}
return buffer;
}());
/**
* static brown noise buffer
*
* @static
* @private
* @type {AudioBuffer}
*/
var _brownNoise = (function() {
var buffer = Tone.context.createBuffer(2, bufferLength, sampleRate);
for (var channelNum = 0; channelNum &lt; buffer.numberOfChannels; channelNum++){
var channel = buffer.getChannelData(channelNum);
var lastOut = 0.0;
for (var i = 0; i &lt; 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;
})();
/**
* static white noise buffer
*
* @static
* @private
* @type {AudioBuffer}
*/
var _whiteNoise = (function(){
var buffer = Tone.context.createBuffer(2, bufferLength, sampleRate);
for (var channelNum = 0; channelNum &lt; buffer.numberOfChannels; channelNum++){
var channel = buffer.getChannelData(channelNum);
for (var i = 0; i &lt; bufferLength; i++){
channel[i] = Math.random() * 2 - 1;
}
}
return buffer;
}());
return Tone.Noise;
});</pre>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="copyright">
Tone No Tone Copyright © 2014.
</span>
<br />
<span class="jsdoc-message">
Documentation generated on Mon Jun 23 2014 14:12:30 GMT-0400 (EDT).
</span>
</footer>
</div>
<br clear="both">
</div>
</div>
<script src="scripts/sunlight.js"></script>
<script src="scripts/sunlight.javascript.js"></script>
<script src="scripts/sunlight-plugin.doclinks.js"></script>
<script src="scripts/sunlight-plugin.linenumbers.js"></script>
<script src="scripts/sunlight-plugin.menu.js"></script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/jquery.scrollTo.js"></script>
<script src="scripts/jquery.localScroll.js"></script>
<script src="scripts/bootstrap-dropdown.js"></script>
<script src="scripts/toc.js"></script>
<script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
<script>
$( function () {
$( "#toc" ).toc( {
selectors : "h1,h2,h3,h4",
showAndHide : false,
scrollTo : 60
} );
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
} );
</script>
<script>
$( function () {
$('#main').localScroll({
offset: { top: 56 } //offset by the height of your header (give or take a few px, see what works for you)
});
$( "dt h4.name" ).each( function () {
var $this = $( this );
var icon = $( "<i/>" ).addClass( "icon-plus-sign" ).addClass( "pull-right" ).addClass( "icon-white" );
var dt = $this.parents( "dt" );
var children = dt.next( "dd" );
$this.append( icon ).css( {cursor : "pointer"} );
$this.addClass( "member-collapsed" ).addClass( "member" );
children.hide();
$this.toggle( function () {
icon.addClass( "icon-minus-sign" ).removeClass( "icon-plus-sign" ).removeClass( "icon-white" );
$this.addClass( "member-open" ).removeClass( "member-collapsed" );
children.slideDown();
}, function () {
icon.addClass( "icon-plus-sign" ).removeClass( "icon-minus-sign" ).addClass( "icon-white" );
$this.addClass( "member-collapsed" ).removeClass( "member-open" );
children.slideUp();
} );
} );
} );
</script>
</body>
</html>