mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 16:48:00 +00:00
421 lines
9.9 KiB
HTML
421 lines
9.9 KiB
HTML
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Tone.js Source: component/Meter.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: component/Meter.js</h1>
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="sunlight-highlight-javascript linenums">define(["Tone/core/Tone", "Tone/core/Master"], function(Tone){
|
|
|
|
/**
|
|
* get the rms of the input signal with some averaging
|
|
* can also just get the value of the signal
|
|
* or the value in dB
|
|
*
|
|
* inspired by https://github.com/cwilso/volume-meter/blob/master/volume-meter.js
|
|
* The MIT License (MIT) Copyright (c) 2014 Chris Wilson
|
|
*
|
|
* @constructor
|
|
* @extends {Tone}
|
|
* @param {number=} channels (optional) number of channels being metered
|
|
* @param {number=} smoothing (optional) amount of smoothing applied to the volume
|
|
* @param {number=} clipMemory (optional) number in ms that a "clip" should be remembered
|
|
*/
|
|
Tone.Meter = function(channels, smoothing, clipMemory){
|
|
//extends Unit
|
|
Tone.call(this);
|
|
|
|
/** @type {number} */
|
|
this.channels = this.defaultArg(channels, 1);
|
|
|
|
/** @type {number} */
|
|
this.smoothing = this.defaultArg(smoothing, 0.8);
|
|
|
|
/** @type {number} */
|
|
this.clipMemory = this.defaultArg(clipMemory, 500);
|
|
|
|
/**
|
|
* the rms for each of the channels
|
|
* @private
|
|
* @type {Array<number>}
|
|
*/
|
|
this._volume = new Array(this.channels);
|
|
|
|
/**
|
|
* the raw values for each of the channels
|
|
* @private
|
|
* @type {Array<number>}
|
|
*/
|
|
this._values = new Array(this.channels);
|
|
|
|
//zero out the volume array
|
|
for (var i = 0; i < this.channels; i++){
|
|
this._volume[i] = 0;
|
|
this._values[i] = 0;
|
|
}
|
|
|
|
/**
|
|
* last time the values clipped
|
|
* @private
|
|
* @type {number}
|
|
*/
|
|
this._lastClip = 0;
|
|
|
|
/**
|
|
* @private
|
|
* @type {ScriptProcessorNode}
|
|
*/
|
|
this._jsNode = this.context.createScriptProcessor(this.bufferSize, this.channels, 1);
|
|
this._jsNode.onaudioprocess = this._onprocess.bind(this);
|
|
//so it doesn't get garbage collected
|
|
this._jsNode.noGC();
|
|
|
|
//signal just passes
|
|
this.input.connect(this.output);
|
|
this.input.connect(this._jsNode);
|
|
};
|
|
|
|
Tone.extend(Tone.Meter);
|
|
|
|
/**
|
|
* called on each processing frame
|
|
* @private
|
|
* @param {AudioProcessingEvent} event
|
|
*/
|
|
Tone.Meter.prototype._onprocess = function(event){
|
|
var bufferSize = this._jsNode.bufferSize;
|
|
var smoothing = this.smoothing;
|
|
for (var channel = 0; channel < this.channels; channel++){
|
|
var input = event.inputBuffer.getChannelData(channel);
|
|
var sum = 0;
|
|
var total = 0;
|
|
var x;
|
|
var clipped = false;
|
|
for (var i = 0; i < bufferSize; i++){
|
|
x = input[i];
|
|
if (!clipped && x > 0.95){
|
|
clipped = true;
|
|
this._lastClip = Date.now();
|
|
}
|
|
total += x;
|
|
sum += x * x;
|
|
}
|
|
var average = total / bufferSize;
|
|
var rms = Math.sqrt(sum / bufferSize);
|
|
this._volume[channel] = Math.max(rms, this._volume[channel] * smoothing);
|
|
this._values[channel] = average;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* get the rms of the signal
|
|
*
|
|
* @param {number=} channel which channel
|
|
* @return {number} the value
|
|
*/
|
|
Tone.Meter.prototype.getLevel = function(channel){
|
|
channel = this.defaultArg(channel, 0);
|
|
var vol = this._volume[channel];
|
|
if (vol < 0.00001){
|
|
return 0;
|
|
} else {
|
|
return vol;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* get the value of the signal
|
|
* @param {number=} channel
|
|
* @return {number}
|
|
*/
|
|
Tone.Meter.prototype.getValue = function(channel){
|
|
channel = this.defaultArg(channel, 0);
|
|
return this._values[channel];
|
|
};
|
|
|
|
/**
|
|
* get the volume of the signal in dB
|
|
* @param {number=} channel
|
|
* @return {number}
|
|
*/
|
|
Tone.Meter.prototype.getDb = function(channel){
|
|
return this.gainToDb(this.getLevel(channel));
|
|
};
|
|
|
|
// @returns {boolean} if the audio has clipped in the last 500ms
|
|
Tone.Meter.prototype.isClipped = function(){
|
|
return Date.now() - this._lastClip < this.clipMemory;
|
|
};
|
|
|
|
/**
|
|
* @override
|
|
*/
|
|
Tone.Meter.prototype.dispose = function(){
|
|
this._jsNode.disconnect();
|
|
this._jsNode.onaudioprocess = null;
|
|
this._volume = null;
|
|
this._values = null;
|
|
this.input.disconnect();
|
|
this.output.disconnect();
|
|
};
|
|
|
|
return Tone.Meter;
|
|
});</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>
|