-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 117cdf7
Showing
56 changed files
with
8,161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BasedOnStyle: Google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
build | ||
dist | ||
node_modules | ||
*.log | ||
*.wav | ||
*.raw | ||
*.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
build | ||
*.log | ||
*.raw | ||
*.wav |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2019 Serenade Labs, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# webrtcvad | ||
|
||
webrtcvad is a cross-platform, native [node.js](https://nodejs.org) [addon](http://nodejs.org/api/addons.html) for detecting speech in raw audio by providing node.js bindings to the [WebRTC](https://webrtc.org) voice activity detection library. | ||
|
||
## Installation | ||
|
||
webrtcvad has been tested on Windows 10, macOS 10.14+, and Ubuntu 18.04+ (and may work on other platforms as well). | ||
|
||
To install webrtcvad, run: | ||
|
||
yarn add webrtcvad | ||
|
||
If you're using this library with Electron, you should probably use [electron-rebuild](https://github.com/electron/electron-rebuild). | ||
|
||
## Usage | ||
|
||
webrtcvad works with single-channel, 16-bit, signed-integer linear PCM audio sampled at 8kHz, 16kHz, 32kHz, or 48kHz. | ||
|
||
You can create a voice activity detector (VAD) with: | ||
|
||
import VAD from "webrtcvad"; | ||
|
||
const audio = ...; // Buffer containing exactly 10ms, 20ms, or 30ms of audio data | ||
const vad = new VAD(16000, 3); | ||
vad.process(audio); | ||
|
||
The `VAD` constructor accepts two arguments. The first is the sample rate (which must be one of the above), and the second is a VAD level, with `0` being the least aggressive (i.e., leading to the most false positives) and `3` being the most aggressive (i.e., leading to the least false positives, but possibly missing actual speech). | ||
|
||
The `VAD` object has one method called `process` that returns `true` if the frame contains speech and `false` if not. It takes one argument, a `Buffer` containing exactly 10ms, 20ms, or 30ms of audio. For instance, if you're sampling at 16kHz, then the length of `audio` should be `160`, `320`, or `480`, since the audio must be single-channel, 16-bit. | ||
|
||
If you're looking to apply the VAD to live microphone data, take a look at [speech-recorder](https://github.com/serenadeai/speech-recorder), which uses webrtcvad to capture speech from a device's microphone. | ||
|
||
## Credits | ||
|
||
This module is based on [py-webrtcvad](https://github.com/wiseman/py-webrtcvad). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "vad", | ||
"cflags!": ["-fexceptions"], | ||
"cflags_cc!": ["-fexceptions", "-std=c++11", "-stdlib=libc++"], | ||
"sources": [ | ||
"src/webrtcvad.cc", | ||
"src/vad.cc", | ||
"src/webrtc/common_audio/signal_processing/complex_bit_reverse.c", | ||
"src/webrtc/common_audio/signal_processing/complex_fft.c", | ||
"src/webrtc/common_audio/signal_processing/cross_correlation.c", | ||
"src/webrtc/common_audio/signal_processing/division_operations.c", | ||
"src/webrtc/common_audio/signal_processing/downsample_fast.c", | ||
"src/webrtc/common_audio/signal_processing/energy.c", | ||
"src/webrtc/common_audio/signal_processing/get_scaling_square.c", | ||
"src/webrtc/common_audio/signal_processing/min_max_operations.c", | ||
"src/webrtc/common_audio/signal_processing/resample_48khz.c", | ||
"src/webrtc/common_audio/signal_processing/resample_by_2_internal.c", | ||
"src/webrtc/common_audio/signal_processing/resample_fractional.c", | ||
"src/webrtc/common_audio/signal_processing/spl_init.c", | ||
"src/webrtc/common_audio/signal_processing/spl_inl.c", | ||
"src/webrtc/common_audio/signal_processing/spl_sqrt.c", | ||
"src/webrtc/common_audio/signal_processing/vector_scaling_operations.c", | ||
"src/webrtc/common_audio/third_party/spl_sqrt_floor/spl_sqrt_floor.c", | ||
"src/webrtc/common_audio/vad/vad_core.c", | ||
"src/webrtc/common_audio/vad/vad_filterbank.c", | ||
"src/webrtc/common_audio/vad/vad_gmm.c", | ||
"src/webrtc/common_audio/vad/vad_sp.c", | ||
"src/webrtc/common_audio/vad/webrtc_vad.c", | ||
"src/webrtc/rtc_base/checks.cc" | ||
], | ||
"include_dirs": [ | ||
"<!@(node -p \"require('node-addon-api').include\")", | ||
"src" | ||
], | ||
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"], | ||
"conditions": [ | ||
[ | ||
'OS=="mac"', { | ||
"defines": ["WEBRTC_POSIX"] | ||
} | ||
], | ||
[ | ||
'OS=="win"', { | ||
"defines": ["WEBRTC_WIN"] | ||
} | ||
], | ||
[ | ||
'OS=="linux"', { | ||
"defines": ["WEBRTC_WIN"] | ||
} | ||
], | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import bindings from "bindings"; | ||
const vadBindings = bindings("vad.node"); | ||
|
||
export default class VAD { | ||
private sampleRate: number; | ||
private instance: any; | ||
|
||
constructor(sampleRate: number = 16000, level: number = 3) { | ||
this.sampleRate = sampleRate; | ||
this.instance = new vadBindings.VAD(sampleRate, level); | ||
} | ||
|
||
private valid(audio: Buffer): boolean { | ||
return ( | ||
audio.length / 2 == this.sampleRate / 100 || | ||
audio.length / 2 == (2 * this.sampleRate) / 100 || | ||
audio.length / 2 == (3 * this.sampleRate) / 100 | ||
); | ||
} | ||
|
||
process(audio: Buffer): boolean { | ||
if (!this.valid(audio)) { | ||
throw new Error( | ||
`Invalid audio length. For a sample rate of ${this.sampleRate}, audio length must be ${(2 * | ||
this.sampleRate) / | ||
100}, ${(4 * this.sampleRate) / 100}, or ${(6 * this.sampleRate) / 100}.` | ||
); | ||
} | ||
|
||
return this.instance.process(audio, audio.length / 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "webrtcvad", | ||
"author": "Serenade Labs, Inc. <[email protected]>", | ||
"version": "1.0.0", | ||
"description": "Bindings for the WebRTC VAD.", | ||
"license": "MIT", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"keywords": ["webrtc", "vad", "audio", "pcm"], | ||
"bugs": { | ||
"url": "https://github.com/serenadeai/webrtcvad/issues" | ||
}, | ||
"repository": "git+https://github.com/serenadeai/webrtcvad.git", | ||
"dependencies": { | ||
"bindings": "^1.3.0", | ||
"node-addon-api": "^1.7.1" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"clean": "rm -rf dist", | ||
"prepublish": "tsc", | ||
"install": "node-gyp rebuild" | ||
}, | ||
"devDependencies": { | ||
"@types/bindings": "^1.3.0", | ||
"@types/node": "^12.11.5", | ||
"typescript": "^3.6.4" | ||
}, | ||
"gypfile": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "vad.h" | ||
|
||
Napi::FunctionReference VAD::constructor; | ||
|
||
Napi::Object VAD::Init(Napi::Env env, Napi::Object exports) { | ||
Napi::HandleScope scope(env); | ||
Napi::Function func = | ||
DefineClass(env, "VAD", {InstanceMethod("process", &VAD::Process)}); | ||
|
||
constructor = Napi::Persistent(func); | ||
constructor.SuppressDestruct(); | ||
|
||
exports.Set("VAD", func); | ||
return exports; | ||
} | ||
|
||
VAD::VAD(const Napi::CallbackInfo& info) : Napi::ObjectWrap<VAD>(info) { | ||
Napi::Env env = info.Env(); | ||
Napi::HandleScope scope(env); | ||
|
||
if (info.Length() != 2) { | ||
Napi::TypeError::New(env, "Expected two arguments") | ||
.ThrowAsJavaScriptException(); | ||
} | ||
|
||
if (!info[0].IsNumber()) { | ||
Napi::TypeError::New(env, "Expected number for first argument") | ||
.ThrowAsJavaScriptException(); | ||
} | ||
|
||
if (!info[1].IsNumber()) { | ||
Napi::TypeError::New(env, "Expected number for second argument") | ||
.ThrowAsJavaScriptException(); | ||
} | ||
|
||
this->sample_rate_ = info[0].As<Napi::Number>().Int32Value(); | ||
int level = info[1].As<Napi::Number>().Int32Value(); | ||
|
||
this->instance_ = WebRtcVad_Create(); | ||
WebRtcVad_Init(this->instance_); | ||
WebRtcVad_set_mode(this->instance_, level); | ||
} | ||
|
||
VAD::~VAD() { WebRtcVad_Free(this->instance_); } | ||
|
||
Napi::Value VAD::Process(const Napi::CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
Napi::HandleScope scope(env); | ||
|
||
if (info.Length() != 2) { | ||
Napi::TypeError::New(env, "Expected two arguments") | ||
.ThrowAsJavaScriptException(); | ||
} | ||
|
||
if (!info[0].IsBuffer()) { | ||
Napi::TypeError::New(env, "Expected buffer for first argument") | ||
.ThrowAsJavaScriptException(); | ||
} | ||
|
||
if (!info[1].IsNumber()) { | ||
Napi::TypeError::New(env, "Expected number for second argument") | ||
.ThrowAsJavaScriptException(); | ||
} | ||
|
||
Napi::Buffer<int16_t> audio = info[0].As<Napi::Buffer<int16_t>>(); | ||
uint32_t length = info[1].As<Napi::Number>().Uint32Value(); | ||
int result = WebRtcVad_Process(this->instance_, this->sample_rate_, | ||
audio.Data(), length); | ||
|
||
return Napi::Value::From(info.Env(), result == 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef VAD_H | ||
#define VAD_H | ||
|
||
#include <napi.h> | ||
|
||
extern "C" { | ||
#include "webrtc/common_audio/vad/include/webrtc_vad.h" | ||
} | ||
|
||
class VAD : public Napi::ObjectWrap<VAD> { | ||
public: | ||
VAD(const Napi::CallbackInfo& info); | ||
~VAD(); | ||
static Napi::Object Init(Napi::Env env, Napi::Object exports); | ||
|
||
private: | ||
VadInst* instance_; | ||
int sample_rate_; | ||
|
||
Napi::Value Process(const Napi::CallbackInfo& info); | ||
static Napi::FunctionReference constructor; | ||
}; | ||
|
||
#endif |
108 changes: 108 additions & 0 deletions
108
src/webrtc/common_audio/signal_processing/complex_bit_reverse.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by a BSD-style license | ||
* that can be found in the LICENSE file in the root of the source | ||
* tree. An additional intellectual property rights grant can be found | ||
* in the file PATENTS. All contributing project authors may | ||
* be found in the AUTHORS file in the root of the source tree. | ||
*/ | ||
|
||
#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" | ||
|
||
/* Tables for data buffer indexes that are bit reversed and thus need to be | ||
* swapped. Note that, index_7[{0, 2, 4, ...}] are for the left side of the swap | ||
* operations, while index_7[{1, 3, 5, ...}] are for the right side of the | ||
* operation. Same for index_8. | ||
*/ | ||
|
||
/* Indexes for the case of stages == 7. */ | ||
static const int16_t index_7[112] = { | ||
1, 64, 2, 32, 3, 96, 4, 16, 5, 80, 6, 48, 7, 112, 9, 72, 10, 40, 11, 104, | ||
12, 24, 13, 88, 14, 56, 15, 120, 17, 68, 18, 36, 19, 100, 21, 84, 22, 52, | ||
23, 116, 25, 76, 26, 44, 27, 108, 29, 92, 30, 60, 31, 124, 33, 66, 35, 98, | ||
37, 82, 38, 50, 39, 114, 41, 74, 43, 106, 45, 90, 46, 58, 47, 122, 49, 70, | ||
51, 102, 53, 86, 55, 118, 57, 78, 59, 110, 61, 94, 63, 126, 67, 97, 69, | ||
81, 71, 113, 75, 105, 77, 89, 79, 121, 83, 101, 87, 117, 91, 109, 95, 125, | ||
103, 115, 111, 123 | ||
}; | ||
|
||
/* Indexes for the case of stages == 8. */ | ||
static const int16_t index_8[240] = { | ||
1, 128, 2, 64, 3, 192, 4, 32, 5, 160, 6, 96, 7, 224, 8, 16, 9, 144, 10, 80, | ||
11, 208, 12, 48, 13, 176, 14, 112, 15, 240, 17, 136, 18, 72, 19, 200, 20, | ||
40, 21, 168, 22, 104, 23, 232, 25, 152, 26, 88, 27, 216, 28, 56, 29, 184, | ||
30, 120, 31, 248, 33, 132, 34, 68, 35, 196, 37, 164, 38, 100, 39, 228, 41, | ||
148, 42, 84, 43, 212, 44, 52, 45, 180, 46, 116, 47, 244, 49, 140, 50, 76, | ||
51, 204, 53, 172, 54, 108, 55, 236, 57, 156, 58, 92, 59, 220, 61, 188, 62, | ||
124, 63, 252, 65, 130, 67, 194, 69, 162, 70, 98, 71, 226, 73, 146, 74, 82, | ||
75, 210, 77, 178, 78, 114, 79, 242, 81, 138, 83, 202, 85, 170, 86, 106, 87, | ||
234, 89, 154, 91, 218, 93, 186, 94, 122, 95, 250, 97, 134, 99, 198, 101, | ||
166, 103, 230, 105, 150, 107, 214, 109, 182, 110, 118, 111, 246, 113, 142, | ||
115, 206, 117, 174, 119, 238, 121, 158, 123, 222, 125, 190, 127, 254, 131, | ||
193, 133, 161, 135, 225, 137, 145, 139, 209, 141, 177, 143, 241, 147, 201, | ||
149, 169, 151, 233, 155, 217, 157, 185, 159, 249, 163, 197, 167, 229, 171, | ||
213, 173, 181, 175, 245, 179, 205, 183, 237, 187, 221, 191, 253, 199, 227, | ||
203, 211, 207, 243, 215, 235, 223, 251, 239, 247 | ||
}; | ||
|
||
void WebRtcSpl_ComplexBitReverse(int16_t* __restrict complex_data, int stages) { | ||
/* For any specific value of stages, we know exactly the indexes that are | ||
* bit reversed. Currently (Feb. 2012) in WebRTC the only possible values of | ||
* stages are 7 and 8, so we use tables to save unnecessary iterations and | ||
* calculations for these two cases. | ||
*/ | ||
if (stages == 7 || stages == 8) { | ||
int m = 0; | ||
int length = 112; | ||
const int16_t* index = index_7; | ||
|
||
if (stages == 8) { | ||
length = 240; | ||
index = index_8; | ||
} | ||
|
||
/* Decimation in time. Swap the elements with bit-reversed indexes. */ | ||
for (m = 0; m < length; m += 2) { | ||
/* We declare a int32_t* type pointer, to load both the 16-bit real | ||
* and imaginary elements from complex_data in one instruction, reducing | ||
* complexity. | ||
*/ | ||
int32_t* complex_data_ptr = (int32_t*)complex_data; | ||
int32_t temp = 0; | ||
|
||
temp = complex_data_ptr[index[m]]; /* Real and imaginary */ | ||
complex_data_ptr[index[m]] = complex_data_ptr[index[m + 1]]; | ||
complex_data_ptr[index[m + 1]] = temp; | ||
} | ||
} | ||
else { | ||
int m = 0, mr = 0, l = 0; | ||
int n = 1 << stages; | ||
int nn = n - 1; | ||
|
||
/* Decimation in time - re-order data */ | ||
for (m = 1; m <= nn; ++m) { | ||
int32_t* complex_data_ptr = (int32_t*)complex_data; | ||
int32_t temp = 0; | ||
|
||
/* Find out indexes that are bit-reversed. */ | ||
l = n; | ||
do { | ||
l >>= 1; | ||
} while (l > nn - mr); | ||
mr = (mr & (l - 1)) + l; | ||
|
||
if (mr <= m) { | ||
continue; | ||
} | ||
|
||
/* Swap the elements with bit-reversed indexes. | ||
* This is similar to the loop in the stages == 7 or 8 cases. | ||
*/ | ||
temp = complex_data_ptr[m]; /* Real and imaginary */ | ||
complex_data_ptr[m] = complex_data_ptr[mr]; | ||
complex_data_ptr[mr] = temp; | ||
} | ||
} | ||
} |
Oops, something went wrong.