File size: 7,876 Bytes
adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 d88250a adba281 |
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
function freeConfig(config, Module) {
if ('buffer' in config) {
Module._free(config.buffer);
}
Module._free(config.ptr);
}
// The user should free the returned pointers
function initSherpaNcnnModelConfig(config, Module) {
let encoderParamLen = Module.lengthBytesUTF8(config.encoderParam) + 1;
let decoderParamLen = Module.lengthBytesUTF8(config.decoderParam) + 1;
let joinerParamLen = Module.lengthBytesUTF8(config.joinerParam) + 1;
let encoderBinLen = Module.lengthBytesUTF8(config.encoderBin) + 1;
let decoderBinLen = Module.lengthBytesUTF8(config.decoderBin) + 1;
let joinerBinLen = Module.lengthBytesUTF8(config.joinerBin) + 1;
let tokensLen = Module.lengthBytesUTF8(config.tokens) + 1;
let n = encoderParamLen + decoderParamLen + joinerParamLen;
n += encoderBinLen + decoderBinLen + joinerBinLen;
n += tokensLen;
let buffer = Module._malloc(n);
let ptr = Module._malloc(4 * 9);
let offset = 0;
Module.stringToUTF8(config.encoderParam, buffer + offset, encoderParamLen);
offset += encoderParamLen;
Module.stringToUTF8(config.encoderBin, buffer + offset, encoderBinLen);
offset += encoderBinLen;
Module.stringToUTF8(config.decoderParam, buffer + offset, decoderParamLen);
offset += decoderParamLen;
Module.stringToUTF8(config.decoderBin, buffer + offset, decoderBinLen);
offset += decoderBinLen;
Module.stringToUTF8(config.joinerParam, buffer + offset, joinerParamLen);
offset += joinerParamLen;
Module.stringToUTF8(config.joinerBin, buffer + offset, joinerBinLen);
offset += joinerBinLen;
Module.stringToUTF8(config.tokens, buffer + offset, tokensLen);
offset += tokensLen;
offset = 0;
Module.setValue(ptr, buffer + offset, 'i8*'); // encoderParam
offset += encoderParamLen;
Module.setValue(ptr + 4, buffer + offset, 'i8*'); // encoderBin
offset += encoderBinLen;
Module.setValue(ptr + 8, buffer + offset, 'i8*'); // decoderParam
offset += decoderParamLen;
Module.setValue(ptr + 12, buffer + offset, 'i8*'); // decoderBin
offset += decoderBinLen;
Module.setValue(ptr + 16, buffer + offset, 'i8*'); // joinerParam
offset += joinerParamLen;
Module.setValue(ptr + 20, buffer + offset, 'i8*'); // joinerBin
offset += joinerBinLen;
Module.setValue(ptr + 24, buffer + offset, 'i8*'); // tokens
offset += tokensLen;
Module.setValue(ptr + 28, config.useVulkanCompute, 'i32');
Module.setValue(ptr + 32, config.numThreads, 'i32');
return {
buffer: buffer, ptr: ptr, len: 36,
}
}
function initSherpaNcnnDecoderConfig(config, Module) {
let n = Module.lengthBytesUTF8(config.decodingMethod) + 1;
let buffer = Module._malloc(n);
let ptr = Module._malloc(4 * 2);
Module.stringToUTF8(config.decodingMethod, buffer, n);
Module.setValue(ptr, buffer, 'i8*');
Module.setValue(ptr + 4, config.numActivePaths, 'i32');
return {
buffer: buffer, ptr: ptr, len: 8,
}
}
function initSherpaNcnnFeatureExtractorConfig(config, Module) {
let ptr = Module._malloc(4 * 2);
Module.setValue(ptr, config.samplingRate, 'float');
Module.setValue(ptr + 4, config.featureDim, 'i32');
return {
ptr: ptr, len: 8,
}
}
function initSherpaNcnnRecognizerConfig(config, Module) {
let featConfig =
initSherpaNcnnFeatureExtractorConfig(config.featConfig, Module);
let modelConfig = initSherpaNcnnModelConfig(config.modelConfig, Module);
let decoderConfig = initSherpaNcnnDecoderConfig(config.decoderConfig, Module);
let numBytes =
featConfig.len + modelConfig.len + decoderConfig.len + 4 * 4 + 4 * 2;
let ptr = Module._malloc(numBytes);
let offset = 0;
Module._CopyHeap(featConfig.ptr, featConfig.len, ptr + offset);
offset += featConfig.len;
Module._CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset)
offset += modelConfig.len;
Module._CopyHeap(decoderConfig.ptr, decoderConfig.len, ptr + offset)
offset += decoderConfig.len;
Module.setValue(ptr + offset, config.enableEndpoint, 'i32');
offset += 4;
Module.setValue(ptr + offset, config.rule1MinTrailingSilence, 'float');
offset += 4;
Module.setValue(ptr + offset, config.rule2MinTrailingSilence, 'float');
offset += 4;
Module.setValue(ptr + offset, config.rule3MinUtternceLength, 'float');
offset += 4;
Module.setValue(ptr + offset, 0, 'i32'); // hotwords file
offset += 4;
Module.setValue(ptr + offset, 0.5, 'float'); // hotwords_score
offset += 4;
return {
ptr: ptr, len: numBytes, featConfig: featConfig, modelConfig: modelConfig,
decoderConfig: decoderConfig,
}
}
class Stream {
constructor(handle, Module) {
this.handle = handle;
this.pointer = null;
this.n = 0;
this.Module = Module;
}
free() {
if (this.handle) {
this.Module._DestroyStream(this.handle);
this.handle = null;
this.Module._free(this.pointer);
this.pointer = null;
this.n = 0;
}
}
/**
* @param sampleRate {Number}
* @param samples {Float32Array} Containing samples in the range [-1, 1]
*/
acceptWaveform(sampleRate, samples) {
if (this.n < samples.length) {
this.Module._free(this.pointer);
this.pointer =
this.Module._malloc(samples.length * samples.BYTES_PER_ELEMENT);
this.n = samples.length
}
this.Module.HEAPF32.set(samples, this.pointer / samples.BYTES_PER_ELEMENT);
this.Module._AcceptWaveform(
this.handle, sampleRate, this.pointer, samples.length);
}
inputFinished() {
_InputFinished(this.handle);
}
};
class Recognizer {
constructor(configObj, Module) {
this.config = configObj;
let config = initSherpaNcnnRecognizerConfig(configObj, Module)
let handle = Module._CreateRecognizer(config.ptr);
freeConfig(config.featConfig, Module);
freeConfig(config.modelConfig, Module);
freeConfig(config.decoderConfig, Module);
freeConfig(config, Module);
this.handle = handle;
this.Module = Module;
}
free() {
this.Module._DestroyRecognizer(this.handle);
this.handle = 0
}
createStream() {
let handle = this.Module._CreateStream(this.handle);
return new Stream(handle, this.Module);
}
isReady(stream) {
return this.Module._IsReady(this.handle, stream.handle) == 1;
}
isEndpoint(stream) {
return this.Module._IsEndpoint(this.handle, stream.handle) == 1;
}
decode(stream) {
return this.Module._Decode(this.handle, stream.handle);
}
reset(stream) {
this.Module._Reset(this.handle, stream.handle);
}
getResult(stream) {
let r = this.Module._GetResult(this.handle, stream.handle);
let textPtr = this.Module.getValue(r, 'i8*');
let text = this.Module.UTF8ToString(textPtr);
this.Module._DestroyResult(r);
return text;
}
}
function createRecognizer(Module, myConfig) {
let modelConfig = {
encoderParam: './encoder_jit_trace-pnnx.ncnn.param',
encoderBin: './encoder_jit_trace-pnnx.ncnn.bin',
decoderParam: './decoder_jit_trace-pnnx.ncnn.param',
decoderBin: './decoder_jit_trace-pnnx.ncnn.bin',
joinerParam: './joiner_jit_trace-pnnx.ncnn.param',
joinerBin: './joiner_jit_trace-pnnx.ncnn.bin',
tokens: './tokens.txt',
useVulkanCompute: 0,
numThreads: 1,
};
let decoderConfig = {
decodingMethod: 'greedy_search',
numActivePaths: 4,
};
let featConfig = {
samplingRate: 16000,
featureDim: 80,
};
let configObj = {
featConfig: featConfig,
modelConfig: modelConfig,
decoderConfig: decoderConfig,
enableEndpoint: 1,
rule1MinTrailingSilence: 1.2,
rule2MinTrailingSilence: 2.4,
rule3MinUtternceLength: 20,
};
if (myConfig) {
configObj = myConfig;
}
return new Recognizer(configObj, Module);
}
if (typeof process == 'object' && typeof process.versions == 'object' &&
typeof process.versions.node == 'string') {
module.exports = {
createRecognizer,
};
}
|