-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
128 lines (103 loc) · 3.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
var analyserFrequency = require('analyser-frequency-average');
module.exports = function(audioContext, stream, opts) {
opts = opts || {};
var defaults = {
fftSize: 1024,
bufferLen: 1024,
smoothingTimeConstant: 0.2,
minCaptureFreq: 85, // in Hz
maxCaptureFreq: 255, // in Hz
noiseCaptureDuration: 1000, // in ms
minNoiseLevel: 0.3, // from 0 to 1
maxNoiseLevel: 0.7, // from 0 to 1
avgNoiseMultiplier: 1.2,
onVoiceStart: function() {
},
onVoiceStop: function() {
},
onUpdate: function(val) {
}
};
var options = {};
for (var key in defaults) {
options[key] = opts.hasOwnProperty(key) ? opts[key] : defaults[key];
}
var baseLevel = 0;
var voiceScale = 1;
var activityCounter = 0;
var activityCounterMin = 0;
var activityCounterMax = 60;
var activityCounterThresh = 5;
var envFreqRange = [];
var isNoiseCapturing = true;
var prevVadState = undefined;
var vadState = false;
var captureTimeout = null;
var source = audioContext.createMediaStreamSource(stream);
var analyser = audioContext.createAnalyser();
analyser.smoothingTimeConstant = options.smoothingTimeConstant;
analyser.fftSize = options.fftSize;
var scriptProcessorNode = audioContext.createScriptProcessor(options.bufferLen, 1, 1);
connect();
scriptProcessorNode.onaudioprocess = monitor;
if (isNoiseCapturing) {
//console.log('VAD: start noise capturing');
captureTimeout = setTimeout(init, options.noiseCaptureDuration);
}
function init() {
//console.log('VAD: stop noise capturing');
isNoiseCapturing = false;
envFreqRange = envFreqRange.filter(function(val) {
return val;
}).sort();
var averageEnvFreq = envFreqRange.length ? envFreqRange.reduce(function (p, c) { return Math.min(p, c) }, 1) : (options.minNoiseLevel || 0.1);
baseLevel = averageEnvFreq * options.avgNoiseMultiplier;
if (options.minNoiseLevel && baseLevel < options.minNoiseLevel) baseLevel = options.minNoiseLevel;
if (options.maxNoiseLevel && baseLevel > options.maxNoiseLevel) baseLevel = options.maxNoiseLevel;
voiceScale = 1 - baseLevel;
//console.log('VAD: base level:', baseLevel);
}
function connect() {
source.connect(analyser);
analyser.connect(scriptProcessorNode);
scriptProcessorNode.connect(audioContext.destination);
}
function disconnect() {
scriptProcessorNode.disconnect();
analyser.disconnect();
source.disconnect();
}
function destroy() {
captureTimeout && clearTimeout(captureTimeout);
disconnect();
scriptProcessorNode.onaudioprocess = null;
}
function monitor() {
var frequencies = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(frequencies);
var average = analyserFrequency(analyser, frequencies, options.minCaptureFreq, options.maxCaptureFreq);
if (isNoiseCapturing) {
envFreqRange.push(average);
return;
}
if (average >= baseLevel && activityCounter < activityCounterMax) {
activityCounter++;
} else if (average < baseLevel && activityCounter > activityCounterMin) {
activityCounter--;
}
vadState = activityCounter > activityCounterThresh;
if (prevVadState !== vadState) {
vadState ? onVoiceStart() : onVoiceStop();
prevVadState = vadState;
}
options.onUpdate(Math.max(0, average - baseLevel) / voiceScale);
}
function onVoiceStart() {
options.onVoiceStart();
}
function onVoiceStop() {
options.onVoiceStop();
}
return {connect: connect, disconnect: disconnect, destroy: destroy};
};