File size: 6,193 Bytes
665769e 404fa6a 665769e 404fa6a 665769e 404fa6a 665769e 404fa6a 665769e |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
function freeConfig(config, Module) {
if ('buffer' in config) {
Module._free(config.buffer);
}
if ('sileroVad' in config) {
freeConfig(config.sileroVad, Module)
}
Module._free(config.ptr);
}
// The user should free the returned pointers
function initSherpaOnnxSileroVadModelConfig(config, Module) {
const modelLen = Module.lengthBytesUTF8(config.model || '') + 1;
const n = modelLen;
const buffer = Module._malloc(n);
const len = 6 * 4;
const ptr = Module._malloc(len);
Module.stringToUTF8(config.model || '', buffer, modelLen);
offset = 0;
Module.setValue(ptr, buffer, 'i8*');
offset += 4;
Module.setValue(ptr + offset, config.threshold || 0.5, 'float');
offset += 4;
Module.setValue(ptr + offset, config.minSilenceDuration || 0.5, 'float');
offset += 4;
Module.setValue(ptr + offset, config.minSpeechDuration || 0.25, 'float');
offset += 4;
Module.setValue(ptr + offset, config.windowSize || 512, 'i32');
offset += 4;
Module.setValue(ptr + offset, config.maxSpeechDuration || 20, 'float');
offset += 4;
return {
buffer: buffer, ptr: ptr, len: len,
}
}
function initSherpaOnnxVadModelConfig(config, Module) {
if (!('sileroVad' in config)) {
config.sileroVad = {
model: '',
threshold: 0.50,
minSilenceDuration: 0.50,
minSpeechDuration: 0.25,
windowSize: 512,
maxSpeechDuration: 20,
};
}
const sileroVad =
initSherpaOnnxSileroVadModelConfig(config.sileroVad, Module);
const len = sileroVad.len + 4 * 4;
const ptr = Module._malloc(len);
const providerLen = Module.lengthBytesUTF8(config.provider || 'cpu') + 1;
const buffer = Module._malloc(providerLen);
Module.stringToUTF8(config.provider || 'cpu', buffer, providerLen);
let offset = 0;
Module._CopyHeap(sileroVad.ptr, sileroVad.len, ptr + offset);
offset += sileroVad.len;
Module.setValue(ptr + offset, config.sampleRate || 16000, 'i32');
offset += 4;
Module.setValue(ptr + offset, config.numThreads || 1, 'i32');
offset += 4;
Module.setValue(ptr + offset, buffer, 'i8*'); // provider
offset += 4;
Module.setValue(ptr + offset, config.debug || 0, 'i32');
offset += 4;
return {
buffer: buffer, ptr: ptr, len: len, sileroVad: sileroVad,
}
}
function createVad(Module, myConfig) {
const sileroVad = {
model: './silero_vad.onnx',
threshold: 0.50,
minSilenceDuration: 0.50,
minSpeechDuration: 0.25,
maxSpeechDuration: 20,
windowSize: 512,
};
let config = {
sileroVad: sileroVad,
sampleRate: 16000,
numThreads: 1,
provider: 'cpu',
debug: 1,
bufferSizeInSeconds: 30,
};
if (myConfig) {
config = myConfig;
}
return new Vad(config, Module);
}
class CircularBuffer {
constructor(capacity, Module) {
this.handle = Module._SherpaOnnxCreateCircularBuffer(capacity);
this.Module = Module;
}
free() {
this.Module._SherpaOnnxDestroyCircularBuffer(this.handle);
this.handle = 0
}
/**
* @param samples {Float32Array}
*/
push(samples) {
const pointer =
this.Module._malloc(samples.length * samples.BYTES_PER_ELEMENT);
this.Module.HEAPF32.set(samples, pointer / samples.BYTES_PER_ELEMENT);
this.Module._SherpaOnnxCircularBufferPush(
this.handle, pointer, samples.length);
this.Module._free(pointer);
}
get(startIndex, n) {
const p =
this.Module._SherpaOnnxCircularBufferGet(this.handle, startIndex, n);
const samplesPtr = p / 4;
const samples = new Float32Array(n);
for (let i = 0; i < n; i++) {
samples[i] = this.Module.HEAPF32[samplesPtr + i];
}
this.Module._SherpaOnnxCircularBufferFree(p);
return samples;
}
pop(n) {
this.Module._SherpaOnnxCircularBufferPop(this.handle, n);
}
size() {
return this.Module._SherpaOnnxCircularBufferSize(this.handle);
}
head() {
return this.Module._SherpaOnnxCircularBufferHead(this.handle);
}
reset() {
this.Module._SherpaOnnxCircularBufferReset(this.handle);
}
}
class Vad {
constructor(configObj, Module) {
this.config = configObj;
const config = initSherpaOnnxVadModelConfig(configObj, Module);
const handle = Module._SherpaOnnxCreateVoiceActivityDetector(
config.ptr, configObj.bufferSizeInSeconds || 30);
freeConfig(config, Module);
this.handle = handle;
this.Module = Module;
}
free() {
this.Module._SherpaOnnxDestroyVoiceActivityDetector(this.handle);
this.handle = 0
}
// samples is a float32 array
acceptWaveform(samples) {
const pointer =
this.Module._malloc(samples.length * samples.BYTES_PER_ELEMENT);
this.Module.HEAPF32.set(samples, pointer / samples.BYTES_PER_ELEMENT);
this.Module._SherpaOnnxVoiceActivityDetectorAcceptWaveform(
this.handle, pointer, samples.length);
this.Module._free(pointer);
}
isEmpty() {
return this.Module._SherpaOnnxVoiceActivityDetectorEmpty(this.handle) == 1;
}
isDetected() {
return this.Module._SherpaOnnxVoiceActivityDetectorDetected(this.handle) ==
1;
}
pop() {
this.Module._SherpaOnnxVoiceActivityDetectorPop(this.handle);
}
clear() {
this.Module._SherpaOnnxVoiceActivityDetectorClear(this.handle);
}
/*
{
samples: a 1-d float32 array,
start: an int32
}
*/
front() {
const h = this.Module._SherpaOnnxVoiceActivityDetectorFront(this.handle);
const start = this.Module.HEAP32[h / 4];
const samplesPtr = this.Module.HEAP32[h / 4 + 1] / 4;
const numSamples = this.Module.HEAP32[h / 4 + 2];
const samples = new Float32Array(numSamples);
for (let i = 0; i < numSamples; i++) {
samples[i] = this.Module.HEAPF32[samplesPtr + i];
}
this.Module._SherpaOnnxDestroySpeechSegment(h);
return {samples: samples, start: start};
}
reset() {
this.Module._SherpaOnnxVoiceActivityDetectorReset(this.handle);
}
flush() {
this.Module._SherpaOnnxVoiceActivityDetectorFlush(this.handle);
}
};
if (typeof process == 'object' && typeof process.versions == 'object' &&
typeof process.versions.node == 'string') {
module.exports = {
createVad,
CircularBuffer,
};
}
|