Fangjun Kuang
Committed by GitHub

Refactor node-addon C++ code. (#1768)

Use macros to delete pointers.
@@ -82,21 +82,10 @@ static Napi::External<SherpaOnnxAudioTagging> CreateAudioTaggingWrapper( @@ -82,21 +82,10 @@ static Napi::External<SherpaOnnxAudioTagging> CreateAudioTaggingWrapper(
82 82
83 const SherpaOnnxAudioTagging *at = SherpaOnnxCreateAudioTagging(&c); 83 const SherpaOnnxAudioTagging *at = SherpaOnnxCreateAudioTagging(&c);
84 84
85 - if (c.model.zipformer.model) {  
86 - delete[] c.model.zipformer.model;  
87 - }  
88 -  
89 - if (c.model.ced) {  
90 - delete[] c.model.ced;  
91 - }  
92 -  
93 - if (c.model.provider) {  
94 - delete[] c.model.provider;  
95 - }  
96 -  
97 - if (c.labels) {  
98 - delete[] c.labels;  
99 - } 85 + SHERPA_ONNX_DELETE_C_STR(c.model.zipformer.model);
  86 + SHERPA_ONNX_DELETE_C_STR(c.model.ced);
  87 + SHERPA_ONNX_DELETE_C_STR(c.model.provider);
  88 + SHERPA_ONNX_DELETE_C_STR(c.labels);
100 89
101 if (!at) { 90 if (!at) {
102 Napi::TypeError::New(env, "Please check your config!") 91 Napi::TypeError::New(env, "Please check your config!")
@@ -47,50 +47,20 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper( @@ -47,50 +47,20 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
47 SHERPA_ONNX_ASSIGN_ATTR_INT32(keywords_buf_size, keywordsBufSize); 47 SHERPA_ONNX_ASSIGN_ATTR_INT32(keywords_buf_size, keywordsBufSize);
48 48
49 const SherpaOnnxKeywordSpotter *kws = SherpaOnnxCreateKeywordSpotter(&c); 49 const SherpaOnnxKeywordSpotter *kws = SherpaOnnxCreateKeywordSpotter(&c);
  50 + SHERPA_ONNX_DELETE_C_STR(c.model_config.transducer.encoder);
  51 + SHERPA_ONNX_DELETE_C_STR(c.model_config.transducer.decoder);
  52 + SHERPA_ONNX_DELETE_C_STR(c.model_config.transducer.joiner);
50 53
51 - if (c.model_config.transducer.encoder) {  
52 - delete[] c.model_config.transducer.encoder;  
53 - }  
54 -  
55 - if (c.model_config.transducer.decoder) {  
56 - delete[] c.model_config.transducer.decoder;  
57 - }  
58 -  
59 - if (c.model_config.transducer.joiner) {  
60 - delete[] c.model_config.transducer.joiner;  
61 - } 54 + SHERPA_ONNX_DELETE_C_STR(c.model_config.paraformer.encoder);
  55 + SHERPA_ONNX_DELETE_C_STR(c.model_config.paraformer.decoder);
62 56
63 - if (c.model_config.paraformer.encoder) {  
64 - delete[] c.model_config.paraformer.encoder;  
65 - }  
66 -  
67 - if (c.model_config.paraformer.decoder) {  
68 - delete[] c.model_config.paraformer.decoder;  
69 - }  
70 -  
71 - if (c.model_config.zipformer2_ctc.model) {  
72 - delete[] c.model_config.zipformer2_ctc.model;  
73 - } 57 + SHERPA_ONNX_DELETE_C_STR(c.model_config.zipformer2_ctc.model);
74 58
75 - if (c.model_config.tokens) {  
76 - delete[] c.model_config.tokens;  
77 - }  
78 -  
79 - if (c.model_config.provider) {  
80 - delete[] c.model_config.provider;  
81 - }  
82 -  
83 - if (c.model_config.model_type) {  
84 - delete[] c.model_config.model_type;  
85 - }  
86 -  
87 - if (c.keywords_file) {  
88 - delete[] c.keywords_file;  
89 - }  
90 -  
91 - if (c.keywords_buf) {  
92 - delete[] c.keywords_buf;  
93 - } 59 + SHERPA_ONNX_DELETE_C_STR(c.model_config.tokens);
  60 + SHERPA_ONNX_DELETE_C_STR(c.model_config.provider);
  61 + SHERPA_ONNX_DELETE_C_STR(c.model_config.model_type);
  62 + SHERPA_ONNX_DELETE_C_STR(c.keywords_file);
  63 + SHERPA_ONNX_DELETE_C_STR(c.keywords_buf);
94 64
95 if (!kws) { 65 if (!kws) {
96 Napi::TypeError::New(env, "Please check your config!") 66 Napi::TypeError::New(env, "Please check your config!")
@@ -154,21 +154,10 @@ CreateOfflineSpeakerDiarizationWrapper(const Napi::CallbackInfo &info) { @@ -154,21 +154,10 @@ CreateOfflineSpeakerDiarizationWrapper(const Napi::CallbackInfo &info) {
154 SherpaOnnxCreateOfflineSpeakerDiarization(&c); 154 SherpaOnnxCreateOfflineSpeakerDiarization(&c);
155 #endif 155 #endif
156 156
157 - if (c.segmentation.pyannote.model) {  
158 - delete[] c.segmentation.pyannote.model;  
159 - }  
160 -  
161 - if (c.segmentation.provider) {  
162 - delete[] c.segmentation.provider;  
163 - }  
164 -  
165 - if (c.embedding.model) {  
166 - delete[] c.embedding.model;  
167 - }  
168 -  
169 - if (c.embedding.provider) {  
170 - delete[] c.embedding.provider;  
171 - } 157 + SHERPA_ONNX_DELETE_C_STR(c.segmentation.pyannote.model);
  158 + SHERPA_ONNX_DELETE_C_STR(c.segmentation.provider);
  159 + SHERPA_ONNX_DELETE_C_STR(c.embedding.model);
  160 + SHERPA_ONNX_DELETE_C_STR(c.embedding.provider);
172 161
173 if (!sd) { 162 if (!sd) {
174 Napi::TypeError::New(env, "Please check your config!") 163 Napi::TypeError::New(env, "Please check your config!")
@@ -155,78 +155,28 @@ static Napi::External<SherpaOnnxOfflineTts> CreateOfflineTtsWrapper( @@ -155,78 +155,28 @@ static Napi::External<SherpaOnnxOfflineTts> CreateOfflineTtsWrapper(
155 #else 155 #else
156 const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&c); 156 const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&c);
157 #endif 157 #endif
158 -  
159 - if (c.model.vits.model) {  
160 - delete[] c.model.vits.model;  
161 - }  
162 -  
163 - if (c.model.vits.lexicon) {  
164 - delete[] c.model.vits.lexicon;  
165 - }  
166 -  
167 - if (c.model.vits.tokens) {  
168 - delete[] c.model.vits.tokens;  
169 - }  
170 -  
171 - if (c.model.vits.data_dir) {  
172 - delete[] c.model.vits.data_dir;  
173 - }  
174 -  
175 - if (c.model.vits.dict_dir) {  
176 - delete[] c.model.vits.dict_dir;  
177 - }  
178 -  
179 - if (c.model.matcha.acoustic_model) {  
180 - delete[] c.model.matcha.acoustic_model;  
181 - }  
182 -  
183 - if (c.model.matcha.vocoder) {  
184 - delete[] c.model.matcha.vocoder;  
185 - }  
186 -  
187 - if (c.model.matcha.lexicon) {  
188 - delete[] c.model.matcha.lexicon;  
189 - }  
190 -  
191 - if (c.model.matcha.tokens) {  
192 - delete[] c.model.matcha.tokens;  
193 - }  
194 -  
195 - if (c.model.matcha.data_dir) {  
196 - delete[] c.model.matcha.data_dir;  
197 - }  
198 -  
199 - if (c.model.matcha.dict_dir) {  
200 - delete[] c.model.matcha.dict_dir;  
201 - }  
202 -  
203 - if (c.model.kokoro.model) {  
204 - delete[] c.model.kokoro.model;  
205 - }  
206 -  
207 - if (c.model.kokoro.voices) {  
208 - delete[] c.model.kokoro.voices;  
209 - }  
210 -  
211 - if (c.model.kokoro.tokens) {  
212 - delete[] c.model.kokoro.tokens;  
213 - }  
214 -  
215 - if (c.model.kokoro.data_dir) {  
216 - delete[] c.model.kokoro.data_dir;  
217 - }  
218 -  
219 - if (c.model.provider) {  
220 - delete[] c.model.provider;  
221 - }  
222 -  
223 - if (c.rule_fsts) {  
224 - delete[] c.rule_fsts;  
225 - }  
226 -  
227 - if (c.rule_fars) {  
228 - delete[] c.rule_fars;  
229 - } 158 + SHERPA_ONNX_DELETE_C_STR(c.model.vits.model);
  159 + SHERPA_ONNX_DELETE_C_STR(c.model.vits.lexicon);
  160 + SHERPA_ONNX_DELETE_C_STR(c.model.vits.tokens);
  161 + SHERPA_ONNX_DELETE_C_STR(c.model.vits.data_dir);
  162 + SHERPA_ONNX_DELETE_C_STR(c.model.vits.dict_dir);
  163 +
  164 + SHERPA_ONNX_DELETE_C_STR(c.model.matcha.acoustic_model);
  165 + SHERPA_ONNX_DELETE_C_STR(c.model.matcha.vocoder);
  166 + SHERPA_ONNX_DELETE_C_STR(c.model.matcha.lexicon);
  167 + SHERPA_ONNX_DELETE_C_STR(c.model.matcha.tokens);
  168 + SHERPA_ONNX_DELETE_C_STR(c.model.matcha.data_dir);
  169 + SHERPA_ONNX_DELETE_C_STR(c.model.matcha.dict_dir);
  170 +
  171 + SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.model);
  172 + SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.voices);
  173 + SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.tokens);
  174 + SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.data_dir);
  175 +
  176 + SHERPA_ONNX_DELETE_C_STR(c.model.provider);
  177 +
  178 + SHERPA_ONNX_DELETE_C_STR(c.rule_fsts);
  179 + SHERPA_ONNX_DELETE_C_STR(c.rule_fars);
230 180
231 if (!tts) { 181 if (!tts) {
232 Napi::TypeError::New(env, "Please check your config!") 182 Napi::TypeError::New(env, "Please check your config!")
@@ -63,13 +63,8 @@ CreateOfflinePunctuationWrapper(const Napi::CallbackInfo &info) { @@ -63,13 +63,8 @@ CreateOfflinePunctuationWrapper(const Napi::CallbackInfo &info) {
63 const SherpaOnnxOfflinePunctuation *punct = 63 const SherpaOnnxOfflinePunctuation *punct =
64 SherpaOnnxCreateOfflinePunctuation(&c); 64 SherpaOnnxCreateOfflinePunctuation(&c);
65 65
66 - if (c.model.ct_transformer) {  
67 - delete[] c.model.ct_transformer;  
68 - }  
69 -  
70 - if (c.model.provider) {  
71 - delete[] c.model.provider;  
72 - } 66 + SHERPA_ONNX_DELETE_C_STR(c.model.ct_transformer);
  67 + SHERPA_ONNX_DELETE_C_STR(c.model.provider);
73 68
74 if (!punct) { 69 if (!punct) {
75 Napi::TypeError::New(env, "Please check your config!") 70 Napi::TypeError::New(env, "Please check your config!")
@@ -70,14 +70,8 @@ CreateSpeakerEmbeddingExtractorWrapper(const Napi::CallbackInfo &info) { @@ -70,14 +70,8 @@ CreateSpeakerEmbeddingExtractorWrapper(const Napi::CallbackInfo &info) {
70 const SherpaOnnxSpeakerEmbeddingExtractor *extractor = 70 const SherpaOnnxSpeakerEmbeddingExtractor *extractor =
71 SherpaOnnxCreateSpeakerEmbeddingExtractor(&c); 71 SherpaOnnxCreateSpeakerEmbeddingExtractor(&c);
72 #endif 72 #endif
73 -  
74 - if (c.model) {  
75 - delete[] c.model;  
76 - }  
77 -  
78 - if (c.provider) {  
79 - delete[] c.provider;  
80 - } 73 + SHERPA_ONNX_DELETE_C_STR(c.model);
  74 + SHERPA_ONNX_DELETE_C_STR(c.provider);
81 75
82 if (!extractor) { 76 if (!extractor) {
83 Napi::TypeError::New(env, "Please check your config!") 77 Napi::TypeError::New(env, "Please check your config!")
@@ -66,17 +66,9 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) { @@ -66,17 +66,9 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) {
66 const SherpaOnnxSpokenLanguageIdentification *slid = 66 const SherpaOnnxSpokenLanguageIdentification *slid =
67 SherpaOnnxCreateSpokenLanguageIdentification(&c); 67 SherpaOnnxCreateSpokenLanguageIdentification(&c);
68 68
69 - if (c.whisper.encoder) {  
70 - delete[] c.whisper.encoder;  
71 - }  
72 -  
73 - if (c.whisper.decoder) {  
74 - delete[] c.whisper.decoder;  
75 - }  
76 -  
77 - if (c.provider) {  
78 - delete[] c.provider;  
79 - } 69 + SHERPA_ONNX_DELETE_C_STR(c.whisper.encoder);
  70 + SHERPA_ONNX_DELETE_C_STR(c.whisper.decoder);
  71 + SHERPA_ONNX_DELETE_C_STR(c.provider);
80 72
81 if (!slid) { 73 if (!slid) {
82 Napi::TypeError::New(env, "Please check your config!") 74 Napi::TypeError::New(env, "Please check your config!")
@@ -222,78 +222,26 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper( @@ -222,78 +222,26 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
222 const SherpaOnnxOnlineRecognizer *recognizer = 222 const SherpaOnnxOnlineRecognizer *recognizer =
223 SherpaOnnxCreateOnlineRecognizer(&c); 223 SherpaOnnxCreateOnlineRecognizer(&c);
224 #endif 224 #endif
225 -  
226 - if (c.model_config.transducer.encoder) {  
227 - delete[] c.model_config.transducer.encoder;  
228 - }  
229 -  
230 - if (c.model_config.transducer.decoder) {  
231 - delete[] c.model_config.transducer.decoder;  
232 - }  
233 -  
234 - if (c.model_config.transducer.joiner) {  
235 - delete[] c.model_config.transducer.joiner;  
236 - }  
237 -  
238 - if (c.model_config.paraformer.encoder) {  
239 - delete[] c.model_config.paraformer.encoder;  
240 - }  
241 -  
242 - if (c.model_config.paraformer.decoder) {  
243 - delete[] c.model_config.paraformer.decoder;  
244 - }  
245 -  
246 - if (c.model_config.zipformer2_ctc.model) {  
247 - delete[] c.model_config.zipformer2_ctc.model;  
248 - }  
249 -  
250 - if (c.model_config.tokens) {  
251 - delete[] c.model_config.tokens;  
252 - }  
253 -  
254 - if (c.model_config.provider) {  
255 - delete[] c.model_config.provider;  
256 - }  
257 -  
258 - if (c.model_config.model_type) {  
259 - delete[] c.model_config.model_type;  
260 - }  
261 -  
262 - if (c.model_config.modeling_unit) {  
263 - delete[] c.model_config.modeling_unit;  
264 - }  
265 -  
266 - if (c.model_config.bpe_vocab) {  
267 - delete[] c.model_config.bpe_vocab;  
268 - }  
269 -  
270 - if (c.model_config.tokens_buf) {  
271 - delete[] c.model_config.tokens_buf;  
272 - }  
273 -  
274 - if (c.decoding_method) {  
275 - delete[] c.decoding_method;  
276 - }  
277 -  
278 - if (c.hotwords_file) {  
279 - delete[] c.hotwords_file;  
280 - }  
281 -  
282 - if (c.rule_fsts) {  
283 - delete[] c.rule_fsts;  
284 - }  
285 -  
286 - if (c.rule_fars) {  
287 - delete[] c.rule_fars;  
288 - }  
289 -  
290 - if (c.hotwords_buf) {  
291 - delete[] c.hotwords_buf;  
292 - }  
293 -  
294 - if (c.ctc_fst_decoder_config.graph) {  
295 - delete[] c.ctc_fst_decoder_config.graph;  
296 - } 225 + SHERPA_ONNX_DELETE_C_STR(c.model_config.transducer.encoder);
  226 + SHERPA_ONNX_DELETE_C_STR(c.model_config.transducer.decoder);
  227 + SHERPA_ONNX_DELETE_C_STR(c.model_config.transducer.joiner);
  228 +
  229 + SHERPA_ONNX_DELETE_C_STR(c.model_config.paraformer.encoder);
  230 + SHERPA_ONNX_DELETE_C_STR(c.model_config.paraformer.decoder);
  231 +
  232 + SHERPA_ONNX_DELETE_C_STR(c.model_config.zipformer2_ctc.model);
  233 + SHERPA_ONNX_DELETE_C_STR(c.model_config.tokens);
  234 + SHERPA_ONNX_DELETE_C_STR(c.model_config.provider);
  235 + SHERPA_ONNX_DELETE_C_STR(c.model_config.model_type);
  236 + SHERPA_ONNX_DELETE_C_STR(c.model_config.modeling_unit);
  237 + SHERPA_ONNX_DELETE_C_STR(c.model_config.bpe_vocab);
  238 + SHERPA_ONNX_DELETE_C_STR(c.model_config.tokens_buf);
  239 + SHERPA_ONNX_DELETE_C_STR(c.decoding_method);
  240 + SHERPA_ONNX_DELETE_C_STR(c.hotwords_file);
  241 + SHERPA_ONNX_DELETE_C_STR(c.rule_fsts);
  242 + SHERPA_ONNX_DELETE_C_STR(c.rule_fars);
  243 + SHERPA_ONNX_DELETE_C_STR(c.hotwords_buf);
  244 + SHERPA_ONNX_DELETE_C_STR(c.ctc_fst_decoder_config.graph);
297 245
298 if (!recognizer) { 246 if (!recognizer) {
299 Napi::TypeError::New(env, "Please check your config!") 247 Napi::TypeError::New(env, "Please check your config!")
@@ -367,14 +367,8 @@ CreateVoiceActivityDetectorWrapper(const Napi::CallbackInfo &info) { @@ -367,14 +367,8 @@ CreateVoiceActivityDetectorWrapper(const Napi::CallbackInfo &info) {
367 SherpaOnnxVoiceActivityDetector *vad = 367 SherpaOnnxVoiceActivityDetector *vad =
368 SherpaOnnxCreateVoiceActivityDetector(&c, buffer_size_in_seconds); 368 SherpaOnnxCreateVoiceActivityDetector(&c, buffer_size_in_seconds);
369 #endif 369 #endif
370 -  
371 - if (c.silero_vad.model) {  
372 - delete[] c.silero_vad.model;  
373 - }  
374 -  
375 - if (c.provider) {  
376 - delete[] c.provider;  
377 - } 370 + SHERPA_ONNX_DELETE_C_STR(c.silero_vad.model);
  371 + SHERPA_ONNX_DELETE_C_STR(c.provider);
378 372
379 return Napi::External<SherpaOnnxVoiceActivityDetector>::New( 373 return Napi::External<SherpaOnnxVoiceActivityDetector>::New(
380 env, vad, [](Napi::Env env, SherpaOnnxVoiceActivityDetector *p) { 374 env, vad, [](Napi::Env env, SherpaOnnxVoiceActivityDetector *p) {