2014-03-11 23:27:46 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// METER
|
|
|
|
//
|
|
|
|
// get the rms of the input signal with some averaging
|
|
|
|
//
|
|
|
|
// inspired by https://github.com/cwilso/volume-meter/blob/master/volume-meter.js
|
|
|
|
// The MIT License (MIT) Copyright (c) 2014 Chris Wilson
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-04-06 20:51:30 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/core/Master"], function(Tone){
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
//@param {number=} channels
|
|
|
|
Tone.Meter = function(channels){
|
|
|
|
//extends Unit
|
|
|
|
Tone.call(this);
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
this.channels = this.defaultArg(channels, 1);
|
|
|
|
this.volume = new Array(this.channels);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
this.clipTime = 0;
|
|
|
|
|
|
|
|
//components
|
|
|
|
this.jsNode = this.context.createScriptProcessor(this.bufferSize, this.channels, this.channels);
|
|
|
|
this.jsNode.onaudioprocess = this.onprocess.bind(this);
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
//signal just passes
|
|
|
|
this.input.connect(this.output);
|
|
|
|
this.input.connect(this.jsNode);
|
|
|
|
//so it doesn't get garbage collected
|
|
|
|
this.jsNode.toMaster();
|
2014-06-15 22:19:05 +00:00
|
|
|
};
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.extend(Tone.Meter, Tone);
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
|
|
|
|
//@param {number=} channel
|
|
|
|
//@returns {number}
|
|
|
|
Tone.Meter.prototype.getLevel = function(channel){
|
|
|
|
channel = this.defaultArg(channel, 0);
|
|
|
|
var vol = this.volume[channel];
|
2014-06-15 22:19:05 +00:00
|
|
|
if (vol < 0.00001){
|
2014-04-05 22:05:42 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return vol;
|
|
|
|
}
|
2014-06-15 22:19:05 +00:00
|
|
|
};
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
//@param {number=} channel
|
|
|
|
//@returns {number}
|
|
|
|
Tone.Meter.prototype.getValue = function(channel){
|
|
|
|
channel = this.defaultArg(channel, 0);
|
|
|
|
return this.values[channel];
|
2014-06-15 22:19:05 +00:00
|
|
|
};
|
2014-04-03 21:13:13 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
//@param {number=} channel
|
|
|
|
//@returns {number} the channel volume in decibels
|
|
|
|
Tone.Meter.prototype.getDb = function(channel){
|
|
|
|
return this.gainToDb(this.getLevel(channel));
|
2014-06-15 22:19:05 +00:00
|
|
|
};
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
// @returns {boolean} if the audio has clipped in the last 500ms
|
|
|
|
Tone.Meter.prototype.isClipped = function(){
|
|
|
|
return Date.now() - this.clipTime < 500;
|
2014-06-15 22:19:05 +00:00
|
|
|
};
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
//get the max value
|
|
|
|
Tone.Meter.prototype.onprocess = function(event){
|
|
|
|
var bufferSize = this.jsNode.bufferSize;
|
|
|
|
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];
|
2014-06-15 22:19:05 +00:00
|
|
|
if (!clipped && x > 0.95){
|
2014-04-05 22:05:42 +00:00
|
|
|
clipped = true;
|
|
|
|
this.clipTime = Date.now();
|
|
|
|
}
|
|
|
|
total += x;
|
|
|
|
sum += x * x;
|
2014-03-11 23:27:46 +00:00
|
|
|
}
|
2014-04-05 22:05:42 +00:00
|
|
|
var average = total / bufferSize;
|
|
|
|
var rms = Math.sqrt(sum / bufferSize);
|
2014-06-15 22:19:05 +00:00
|
|
|
this.volume[channel] = Math.max(rms, this.volume[channel] * 0.8);
|
2014-04-05 22:05:42 +00:00
|
|
|
this.values[channel] = average;
|
2014-03-11 23:27:46 +00:00
|
|
|
}
|
2014-06-15 22:19:05 +00:00
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
|
|
|
return Tone.Meter;
|
|
|
|
});
|