Fangjun Kuang
Committed by GitHub

small fixes to wasm kws. (#672)

@@ -4,6 +4,23 @@ function freeConfig(config, Module) { @@ -4,6 +4,23 @@ function freeConfig(config, Module) {
4 if ('buffer' in config) { 4 if ('buffer' in config) {
5 Module._free(config.buffer); 5 Module._free(config.buffer);
6 } 6 }
  7 +
  8 + if ('transducer' in config) {
  9 + freeConfig(config.transducer);
  10 + }
  11 +
  12 + if ('featConfig' in config) {
  13 + freeConfig(config.featConfig);
  14 + }
  15 +
  16 + if ('modelConfig' in config) {
  17 + freeConfig(config.modelConfig);
  18 + }
  19 +
  20 + if ('keywordsBuffer' in config) {
  21 + Module._free(config.keywordsBuffer);
  22 + }
  23 +
7 Module._free(config.ptr); 24 Module._free(config.ptr);
8 } 25 }
9 26
@@ -89,7 +106,7 @@ function initModelConfig(config, Module) { @@ -89,7 +106,7 @@ function initModelConfig(config, Module) {
89 offset += 4; 106 offset += 4;
90 107
91 return { 108 return {
92 - buffer: buffer, ptr: ptr, len: len, 109 + buffer: buffer, ptr: ptr, len: len, transducer: transducer
93 } 110 }
94 } 111 }
95 112
@@ -103,12 +120,10 @@ function initFeatureExtractorConfig(config, Module) { @@ -103,12 +120,10 @@ function initFeatureExtractorConfig(config, Module) {
103 } 120 }
104 121
105 function initKwsConfig(config, Module) { 122 function initKwsConfig(config, Module) {
106 - let featConfig =  
107 - initFeatureExtractorConfig(config.featConfig, Module); 123 + let featConfig = initFeatureExtractorConfig(config.featConfig, Module);
108 124
109 let modelConfig = initModelConfig(config.modelConfig, Module); 125 let modelConfig = initModelConfig(config.modelConfig, Module);
110 - let numBytes =  
111 - featConfig.len + modelConfig.len + 4 * 5; 126 + let numBytes = featConfig.len + modelConfig.len + 4 * 5;
112 127
113 let ptr = Module._malloc(numBytes); 128 let ptr = Module._malloc(numBytes);
114 let offset = 0; 129 let offset = 0;
@@ -118,7 +133,6 @@ function initKwsConfig(config, Module) { @@ -118,7 +133,6 @@ function initKwsConfig(config, Module) {
118 Module._CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset) 133 Module._CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset)
119 offset += modelConfig.len; 134 offset += modelConfig.len;
120 135
121 -  
122 Module.setValue(ptr + offset, config.maxActivePaths, 'i32'); 136 Module.setValue(ptr + offset, config.maxActivePaths, 'i32');
123 offset += 4; 137 offset += 4;
124 138
@@ -138,7 +152,8 @@ function initKwsConfig(config, Module) { @@ -138,7 +152,8 @@ function initKwsConfig(config, Module) {
138 offset += 4; 152 offset += 4;
139 153
140 return { 154 return {
141 - ptr: ptr, len: numBytes, featConfig: featConfig, modelConfig: modelConfig 155 + ptr: ptr, len: numBytes, featConfig: featConfig, modelConfig: modelConfig,
  156 + keywordsBuffer: keywordsBuffer
142 } 157 }
143 } 158 }
144 159
@@ -178,7 +193,7 @@ class Stream { @@ -178,7 +193,7 @@ class Stream {
178 } 193 }
179 194
180 inputFinished() { 195 inputFinished() {
181 - _InputFinished(this.handle); 196 + this.Module._InputFinished(this.handle);
182 } 197 }
183 }; 198 };
184 199
@@ -188,9 +203,6 @@ class Kws { @@ -188,9 +203,6 @@ class Kws {
188 let config = initKwsConfig(configObj, Module) 203 let config = initKwsConfig(configObj, Module)
189 let handle = Module._CreateKeywordSpotter(config.ptr); 204 let handle = Module._CreateKeywordSpotter(config.ptr);
190 205
191 -  
192 - freeConfig(config.featConfig, Module);  
193 - freeConfig(config.modelConfig, Module);  
194 freeConfig(config, Module); 206 freeConfig(config, Module);
195 207
196 this.handle = handle; 208 this.handle = handle;
@@ -211,7 +223,6 @@ class Kws { @@ -211,7 +223,6 @@ class Kws {
211 return this.Module._IsKeywordStreamReady(this.handle, stream.handle) === 1; 223 return this.Module._IsKeywordStreamReady(this.handle, stream.handle) === 1;
212 } 224 }
213 225
214 -  
215 decode(stream) { 226 decode(stream) {
216 return this.Module._DecodeKeywordStream(this.handle, stream.handle); 227 return this.Module._DecodeKeywordStream(this.handle, stream.handle);
217 } 228 }
@@ -230,12 +241,12 @@ function createKws(Module, myConfig) { @@ -230,12 +241,12 @@ function createKws(Module, myConfig) {
230 encoder: './encoder-epoch-12-avg-2-chunk-16-left-64.onnx', 241 encoder: './encoder-epoch-12-avg-2-chunk-16-left-64.onnx',
231 decoder: './decoder-epoch-12-avg-2-chunk-16-left-64.onnx', 242 decoder: './decoder-epoch-12-avg-2-chunk-16-left-64.onnx',
232 joiner: './joiner-epoch-12-avg-2-chunk-16-left-64.onnx', 243 joiner: './joiner-epoch-12-avg-2-chunk-16-left-64.onnx',
233 - } 244 + };
234 let modelConfig = { 245 let modelConfig = {
235 transducer: transducerConfig, 246 transducer: transducerConfig,
236 tokens: './tokens.txt', 247 tokens: './tokens.txt',
237 provider: 'cpu', 248 provider: 'cpu',
238 - modelType: "", 249 + modelType: '',
239 numThreads: 1, 250 numThreads: 1,
240 debug: 1 251 debug: 1
241 }; 252 };
@@ -252,8 +263,8 @@ function createKws(Module, myConfig) { @@ -252,8 +263,8 @@ function createKws(Module, myConfig) {
252 numTrailingBlanks: 1, 263 numTrailingBlanks: 1,
253 keywordsScore: 1.0, 264 keywordsScore: 1.0,
254 keywordsThreshold: 0.25, 265 keywordsThreshold: 0.25,
255 - keywords: "x iǎo ài t óng x ué @小爱同学\n" +  
256 - "j ūn g ē n iú b ī @军哥牛逼" 266 + keywords: 'x iǎo ài t óng x ué @小爱同学\n' +
  267 + 'j ūn g ē n iú b ī @军哥牛逼'
257 }; 268 };
258 269
259 if (myConfig) { 270 if (myConfig) {
@@ -267,4 +278,4 @@ if (typeof process == 'object' && typeof process.versions == 'object' && @@ -267,4 +278,4 @@ if (typeof process == 'object' && typeof process.versions == 'object' &&
267 module.exports = { 278 module.exports = {
268 createKws, 279 createKws,
269 }; 280 };
270 -}  
  281 +}