JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>VAD Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div id="voice" class="novoice">
не активен
</div>
</body>
</html>
CSS
DIV {
display: table-cell;
height: 100px;
width: 100px;
border-radius: 50px;
border: 1px solid black;
text-align: center;
vertical-align: middle;
}
.novoice {
background-color: red;
}
.voice {
background-color: green;
}
JavaScript
(function(window) {
var VAD = function(options) {
// Default options
this.options = {
fftSize: 512,
bufferLen: 512,
voice_stop: function() {},
voice_start: function() {},
smoothingTimeConstant: 0.99,
energy_offset: 1e-8, // The initial offset.
energy_threshold_ratio_pos: 2, // Signal must be twice the offset
energy_threshold_ratio_neg: 0.5, // Signal must be half the offset
energy_integration: 1, // Size of integration change compared to the signal per second.
filter: [
{f: 200, v:0}, // 0 -> 200 is 0
{f: 2000, v:1} // 200 -> 2k is 1
],
source: null,
context: null
};
// User options
for(var option in options) {
if(options.hasOwnProperty(option)) {
this.options[option] = options[option];
}
}
// Require source
if(!this.options.source)
throw new Error("The options must specify a MediaStreamAudioSourceNode.");
// Set this.options.context
this.options.context = this.options.source.context;
// Calculate time relationships
this.hertzPerBin = this.options.context.sampleRate / this.options.fftSize;
this.iterationFrequency = this.options.context.sampleRate / this.options.bufferLen;
this.iterationPeriod = 1 / this.iterationFrequency;
var DEBUG = true;
if(DEBUG) console.log(
'Vad' +
' | sampleRate: ' + this.options.context.sampleRate +
' | hertzPerBin: ' + this.hertzPerBin +
' | iterationFrequency: ' + this.iterationFrequency +
' | iterationPeriod: ' + this.iterationPeriod
);
this.setFilter = function(shape) {
this.filter = [];
for(var i = 0, iLen = this.options.fftSize / 2; i < iLen; i++) {
this.filter[i] = 0;
for(var j = 0, jLen = shape.length; j < jLen; j++) {
if(i * this.hertzPerBin < shape[j].f) {
this.filter[i] = shape[j].v;
break; // Exit j loop
}
}
}
...