Skip to content

Commit

Permalink
updated noise capture logic
Browse files Browse the repository at this point in the history
  • Loading branch information
namniak committed Oct 18, 2016
1 parent 9fd12c1 commit 1cf3fcc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
smoothingTimeConstant: 0.2,
minCaptureFreq: 85, // in Hz
maxCaptureFreq: 255, // in Hz
noiseCaptureDuration: 0, // in ms
minNoiseLevel: 0.4, // from 0 to 1
noiseCaptureDuration: 1000, // in ms
minNoiseLevel: 0.3, // from 0 to 1
maxNoiseLevel: 0.7, // from 0 to 1
avgNoiseMultiplier: 1.2,
onVoiceStart: function() {},
Expand All @@ -30,4 +30,4 @@
See [example code](https://github.com/Jam3/voice-activity-detection/blob/master/test/test.js)

## Test
```npm start```
```npm run test```
30 changes: 18 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

var analyserFrequency = require('analyser-frequency-average');

module.exports = function(audioContext, stream, opts) {
Expand All @@ -12,13 +11,16 @@ module.exports = function(audioContext, stream, opts) {
smoothingTimeConstant: 0.2,
minCaptureFreq: 85, // in Hz
maxCaptureFreq: 255, // in Hz
noiseCaptureDuration: 0, // in ms
minNoiseLevel: 0.4, // from 0 to 1
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) {}
onVoiceStart: function() {
},
onVoiceStop: function() {
},
onUpdate: function(val) {
}
};

var options = {};
Expand All @@ -27,6 +29,7 @@ module.exports = function(audioContext, stream, opts) {
}

var baseLevel = 0;
var voiceScale = 1;
var activityCounter = 0;
var activityCounterMin = 0;
var activityCounterMax = 60;
Expand Down Expand Up @@ -56,13 +59,16 @@ module.exports = function(audioContext, stream, opts) {
//console.log('VAD: stop noise capturing');
isNoiseCapturing = false;

var averageEnvFreq = (envFreqRange.reduce(function(p, c) {
return p + c;
}, 0) / envFreqRange.length) || 0;
envFreqRange = envFreqRange.filter(function(val) {
return val;
}).sort();
var averageEnvFreq = envFreqRange.length ? envFreqRange.reduce((p, c) => Math.min(p, c), 1) : (options.minNoiseLevel || 0.1);

baseLevel = averageEnvFreq * options.avgNoiseMultiplier;
if (baseLevel < options.minNoiseLevel) baseLevel = options.minNoiseLevel;
if (baseLevel > options.maxNoiseLevel) baseLevel = options.maxNoiseLevel;
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);
}
Expand Down Expand Up @@ -104,7 +110,7 @@ module.exports = function(audioContext, stream, opts) {
prevVadState = vadState;
}

options.onUpdate(average);
options.onUpdate(Math.max(0, average - baseLevel) / voiceScale);
}

function onVoiceStart() {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "voice-activity-detection",
"version": "0.0.2",
"version": "0.0.3",
"description": "Mic input activity detection",
"main": "index.js",
"license": "MIT",
Expand All @@ -16,7 +16,7 @@
"budo": "^9.2.1"
},
"scripts": {
"start": "budo test/test.js --live"
"test": "budo test/test.js --live"
},
"keywords": [
"voice",
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
var vad = require('../index.js');
var audioContext;

var valueContainer = document.createElement('div');
document.body.appendChild(valueContainer);

var stateContainer = document.createElement('div');
document.body.appendChild(stateContainer);

requestMic();

function requestMic() {
Expand All @@ -27,12 +33,15 @@ function startUserMedia(stream) {
var options = {
onVoiceStart: function() {
console.log('voice start');
stateContainer.innerHTML = 'Voice state: <strong>active</strong>';
},
onVoiceStop: function() {
console.log('voice stop');
stateContainer.innerHTML = 'Voice state: <strong>inactive</strong>';
},
onUpdate: function(val) {
//console.log('curr val:', val);
valueContainer.innerHTML = 'Current voice activity value: <strong>' + val + '</strong>';
}
};
vad(audioContext, stream, options);
Expand Down

0 comments on commit 1cf3fcc

Please sign in to comment.