@ai-coustics/aic-sdk 0.6.3 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/binding.cc DELETED
@@ -1,469 +0,0 @@
1
- #include "../sdk/include/aic.h"
2
- #include <napi.h>
3
- #include <string>
4
- #include <vector>
5
-
6
- class ModelWrapper : public Napi::ObjectWrap<ModelWrapper> {
7
- public:
8
- static Napi::Object Init(Napi::Env env, Napi::Object exports);
9
- ModelWrapper(const Napi::CallbackInfo &info);
10
- ~ModelWrapper();
11
-
12
- private:
13
- AicModel *model_;
14
-
15
- Napi::Value Initialize(const Napi::CallbackInfo &info);
16
- Napi::Value Reset(const Napi::CallbackInfo &info);
17
- Napi::Value ProcessInterleaved(const Napi::CallbackInfo &info);
18
- Napi::Value ProcessPlanar(const Napi::CallbackInfo &info);
19
- Napi::Value SetParameter(const Napi::CallbackInfo &info);
20
- Napi::Value GetParameter(const Napi::CallbackInfo &info);
21
- Napi::Value GetOutputDelay(const Napi::CallbackInfo &info);
22
- Napi::Value GetOptimalSampleRate(const Napi::CallbackInfo &info);
23
- Napi::Value GetOptimalNumFrames(const Napi::CallbackInfo &info);
24
- };
25
-
26
- ModelWrapper::ModelWrapper(const Napi::CallbackInfo &info)
27
- : Napi::ObjectWrap<ModelWrapper>(info), model_(nullptr) {
28
- Napi::Env env = info.Env();
29
-
30
- if (info.Length() < 1 || !info[0].IsExternal()) {
31
- Napi::TypeError::New(env, "Expected external model handle")
32
- .ThrowAsJavaScriptException();
33
- return;
34
- }
35
-
36
- model_ = info[0].As<Napi::External<AicModel>>().Data();
37
- }
38
-
39
- ModelWrapper::~ModelWrapper() {
40
- if (model_) {
41
- aic_model_destroy(model_);
42
- model_ = nullptr;
43
- }
44
- }
45
-
46
- Napi::Object ModelWrapper::Init(Napi::Env env, Napi::Object exports) {
47
- Napi::Function func = DefineClass(
48
- env, "ModelWrapper",
49
- {
50
- InstanceMethod("initialize", &ModelWrapper::Initialize),
51
- InstanceMethod("reset", &ModelWrapper::Reset),
52
- InstanceMethod("processInterleaved",
53
- &ModelWrapper::ProcessInterleaved),
54
- InstanceMethod("processPlanar", &ModelWrapper::ProcessPlanar),
55
- InstanceMethod("setParameter", &ModelWrapper::SetParameter),
56
- InstanceMethod("getParameter", &ModelWrapper::GetParameter),
57
- InstanceMethod("getOutputDelay", &ModelWrapper::GetOutputDelay),
58
- InstanceMethod("getOptimalSampleRate",
59
- &ModelWrapper::GetOptimalSampleRate),
60
- InstanceMethod("getOptimalNumFrames",
61
- &ModelWrapper::GetOptimalNumFrames),
62
- });
63
-
64
- Napi::FunctionReference *constructor = new Napi::FunctionReference();
65
- *constructor = Napi::Persistent(func);
66
- env.SetInstanceData(constructor);
67
-
68
- return exports;
69
- }
70
-
71
- Napi::Value ModelWrapper::Initialize(const Napi::CallbackInfo &info) {
72
- Napi::Env env = info.Env();
73
-
74
- if (info.Length() < 3) {
75
- Napi::TypeError::New(env, "Expected 3 arguments")
76
- .ThrowAsJavaScriptException();
77
- return env.Null();
78
- }
79
-
80
- uint32_t sample_rate = info[0].As<Napi::Number>().Uint32Value();
81
- uint16_t num_channels = info[1].As<Napi::Number>().Uint32Value();
82
- size_t num_frames = info[2].As<Napi::Number>().Uint32Value();
83
-
84
- AicErrorCode error =
85
- aic_model_initialize(model_, sample_rate, num_channels, num_frames);
86
- return Napi::Number::New(env, static_cast<int>(error));
87
- }
88
-
89
- Napi::Value ModelWrapper::Reset(const Napi::CallbackInfo &info) {
90
- Napi::Env env = info.Env();
91
- AicErrorCode error = aic_model_reset(model_);
92
- return Napi::Number::New(env, static_cast<int>(error));
93
- }
94
-
95
- Napi::Value ModelWrapper::ProcessInterleaved(const Napi::CallbackInfo &info) {
96
- Napi::Env env = info.Env();
97
-
98
- if (info.Length() < 3 || !info[0].IsTypedArray()) {
99
- Napi::TypeError::New(env, "Expected Float32Array and dimensions")
100
- .ThrowAsJavaScriptException();
101
- return env.Null();
102
- }
103
-
104
- Napi::Float32Array array = info[0].As<Napi::Float32Array>();
105
- uint16_t num_channels = info[1].As<Napi::Number>().Uint32Value();
106
- size_t num_frames = info[2].As<Napi::Number>().Uint32Value();
107
-
108
- float *data = array.Data();
109
- AicErrorCode error =
110
- aic_model_process_interleaved(model_, data, num_channels, num_frames);
111
-
112
- return Napi::Number::New(env, static_cast<int>(error));
113
- }
114
-
115
- Napi::Value ModelWrapper::ProcessPlanar(const Napi::CallbackInfo &info) {
116
- Napi::Env env = info.Env();
117
-
118
- if (info.Length() < 3 || !info[0].IsArray()) {
119
- Napi::TypeError::New(env, "Expected array of Float32Arrays and dimensions")
120
- .ThrowAsJavaScriptException();
121
- return env.Null();
122
- }
123
-
124
- Napi::Array arrays = info[0].As<Napi::Array>();
125
- uint16_t num_channels = info[1].As<Napi::Number>().Uint32Value();
126
- size_t num_frames = info[2].As<Napi::Number>().Uint32Value();
127
-
128
- std::vector<float *> channel_data(num_channels);
129
- for (uint16_t i = 0; i < num_channels; i++) {
130
- Napi::Value val = arrays[i];
131
- if (!val.IsTypedArray()) {
132
- Napi::TypeError::New(env, "Expected Float32Array")
133
- .ThrowAsJavaScriptException();
134
- return env.Null();
135
- }
136
- Napi::Float32Array channel = val.As<Napi::Float32Array>();
137
- channel_data[i] = channel.Data();
138
- }
139
-
140
- AicErrorCode error = aic_model_process_planar(model_, channel_data.data(),
141
- num_channels, num_frames);
142
- return Napi::Number::New(env, static_cast<int>(error));
143
- }
144
-
145
- Napi::Value ModelWrapper::SetParameter(const Napi::CallbackInfo &info) {
146
- Napi::Env env = info.Env();
147
-
148
- if (info.Length() < 2) {
149
- Napi::TypeError::New(env, "Expected parameter and value")
150
- .ThrowAsJavaScriptException();
151
- return env.Null();
152
- }
153
-
154
- AicParameter param =
155
- static_cast<AicParameter>(info[0].As<Napi::Number>().Int32Value());
156
- float value = info[1].As<Napi::Number>().FloatValue();
157
-
158
- AicErrorCode error = aic_model_set_parameter(model_, param, value);
159
- return Napi::Number::New(env, static_cast<int>(error));
160
- }
161
-
162
- Napi::Value ModelWrapper::GetParameter(const Napi::CallbackInfo &info) {
163
- Napi::Env env = info.Env();
164
-
165
- if (info.Length() < 1) {
166
- Napi::TypeError::New(env, "Expected parameter")
167
- .ThrowAsJavaScriptException();
168
- return env.Null();
169
- }
170
-
171
- AicParameter param =
172
- static_cast<AicParameter>(info[0].As<Napi::Number>().Int32Value());
173
- float value = 0.0f;
174
-
175
- AicErrorCode error = aic_model_get_parameter(model_, param, &value);
176
-
177
- Napi::Object result = Napi::Object::New(env);
178
- result.Set("error", Napi::Number::New(env, static_cast<int>(error)));
179
- result.Set("value", Napi::Number::New(env, value));
180
- return result;
181
- }
182
-
183
- Napi::Value ModelWrapper::GetOutputDelay(const Napi::CallbackInfo &info) {
184
- Napi::Env env = info.Env();
185
-
186
- size_t delay = 0;
187
- AicErrorCode error = aic_get_output_delay(model_, &delay);
188
-
189
- Napi::Object result = Napi::Object::New(env);
190
- result.Set("error", Napi::Number::New(env, static_cast<int>(error)));
191
- result.Set("delay", Napi::Number::New(env, delay));
192
- return result;
193
- }
194
-
195
- Napi::Value ModelWrapper::GetOptimalSampleRate(const Napi::CallbackInfo &info) {
196
- Napi::Env env = info.Env();
197
-
198
- uint32_t sample_rate = 0;
199
- AicErrorCode error = aic_get_optimal_sample_rate(model_, &sample_rate);
200
-
201
- Napi::Object result = Napi::Object::New(env);
202
- result.Set("error", Napi::Number::New(env, static_cast<int>(error)));
203
- result.Set("sampleRate", Napi::Number::New(env, sample_rate));
204
- return result;
205
- }
206
-
207
- Napi::Value ModelWrapper::GetOptimalNumFrames(const Napi::CallbackInfo &info) {
208
- Napi::Env env = info.Env();
209
-
210
- size_t num_frames = 0;
211
- AicErrorCode error = aic_get_optimal_num_frames(model_, &num_frames);
212
-
213
- Napi::Object result = Napi::Object::New(env);
214
- result.Set("error", Napi::Number::New(env, static_cast<int>(error)));
215
- result.Set("numFrames", Napi::Number::New(env, num_frames));
216
- return result;
217
- }
218
-
219
- // Module functions
220
- Napi::Value CreateModel(const Napi::CallbackInfo &info) {
221
- Napi::Env env = info.Env();
222
-
223
- if (info.Length() < 2) {
224
- Napi::TypeError::New(env, "Expected model type and license key")
225
- .ThrowAsJavaScriptException();
226
- return env.Null();
227
- }
228
-
229
- AicModelType model_type =
230
- static_cast<AicModelType>(info[0].As<Napi::Number>().Int32Value());
231
- std::string license_key = info[1].As<Napi::String>().Utf8Value();
232
-
233
- AicModel *model = nullptr;
234
- AicErrorCode error =
235
- aic_model_create(&model, model_type, license_key.c_str());
236
-
237
- Napi::Object result = Napi::Object::New(env);
238
- result.Set("error", Napi::Number::New(env, static_cast<int>(error)));
239
-
240
- if (error == AIC_ERROR_CODE_SUCCESS && model != nullptr) {
241
- result.Set("model", Napi::External<AicModel>::New(env, model));
242
- } else {
243
- result.Set("model", env.Null());
244
- }
245
-
246
- return result;
247
- }
248
-
249
- Napi::Value DestroyModel(const Napi::CallbackInfo &info) {
250
- Napi::Env env = info.Env();
251
-
252
- if (info.Length() < 1 || !info[0].IsExternal()) {
253
- return env.Null();
254
- }
255
-
256
- AicModel *model = info[0].As<Napi::External<AicModel>>().Data();
257
- if (model) {
258
- aic_model_destroy(model);
259
- }
260
-
261
- return env.Null();
262
- }
263
-
264
- Napi::Value GetSdkVersion(const Napi::CallbackInfo &info) {
265
- Napi::Env env = info.Env();
266
- const char *version = aic_get_sdk_version();
267
- return Napi::String::New(env, version);
268
- }
269
-
270
- Napi::Object Init(Napi::Env env, Napi::Object exports) {
271
- exports.Set("createModel", Napi::Function::New(env, CreateModel));
272
- exports.Set("destroyModel", Napi::Function::New(env, DestroyModel));
273
- exports.Set("getSdkVersion", Napi::Function::New(env, GetSdkVersion));
274
-
275
- // Helper functions that delegate to ModelWrapper methods
276
- exports.Set(
277
- "initialize",
278
- Napi::Function::New(
279
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
280
- Napi::Env env = info.Env();
281
- if (!info[0].IsExternal()) {
282
- Napi::TypeError::New(env, "Expected model handle")
283
- .ThrowAsJavaScriptException();
284
- return env.Null();
285
- }
286
- AicModel *model = info[0].As<Napi::External<AicModel>>().Data();
287
- uint32_t sample_rate = info[1].As<Napi::Number>().Uint32Value();
288
- uint16_t num_channels = info[2].As<Napi::Number>().Uint32Value();
289
- size_t num_frames = info[3].As<Napi::Number>().Uint32Value();
290
- AicErrorCode error = aic_model_initialize(model, sample_rate,
291
- num_channels, num_frames);
292
- return Napi::Number::New(env, static_cast<int>(error));
293
- }));
294
-
295
- exports.Set("reset",
296
- Napi::Function::New(
297
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
298
- Napi::Env env = info.Env();
299
- if (!info[0].IsExternal()) {
300
- Napi::TypeError::New(env, "Expected model handle")
301
- .ThrowAsJavaScriptException();
302
- return env.Null();
303
- }
304
- AicModel *model =
305
- info[0].As<Napi::External<AicModel>>().Data();
306
- AicErrorCode error = aic_model_reset(model);
307
- return Napi::Number::New(env, static_cast<int>(error));
308
- }));
309
-
310
- exports.Set(
311
- "processInterleaved",
312
- Napi::Function::New(
313
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
314
- Napi::Env env = info.Env();
315
- if (!info[0].IsExternal() || !info[1].IsTypedArray()) {
316
- Napi::TypeError::New(env, "Invalid arguments")
317
- .ThrowAsJavaScriptException();
318
- return env.Null();
319
- }
320
- AicModel *model = info[0].As<Napi::External<AicModel>>().Data();
321
- Napi::Float32Array array = info[1].As<Napi::Float32Array>();
322
- uint16_t num_channels = info[2].As<Napi::Number>().Uint32Value();
323
- size_t num_frames = info[3].As<Napi::Number>().Uint32Value();
324
- float *data = array.Data();
325
- AicErrorCode error = aic_model_process_interleaved(
326
- model, data, num_channels, num_frames);
327
- return Napi::Number::New(env, static_cast<int>(error));
328
- }));
329
-
330
- exports.Set(
331
- "processPlanar",
332
- Napi::Function::New(
333
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
334
- Napi::Env env = info.Env();
335
- if (!info[0].IsExternal() || !info[1].IsArray()) {
336
- Napi::TypeError::New(env, "Invalid arguments")
337
- .ThrowAsJavaScriptException();
338
- return env.Null();
339
- }
340
- AicModel *model = info[0].As<Napi::External<AicModel>>().Data();
341
- Napi::Array arrays = info[1].As<Napi::Array>();
342
- uint16_t num_channels = info[2].As<Napi::Number>().Uint32Value();
343
- size_t num_frames = info[3].As<Napi::Number>().Uint32Value();
344
-
345
- std::vector<float *> channel_data(num_channels);
346
- for (uint16_t i = 0; i < num_channels; i++) {
347
- Napi::Value val = arrays[i];
348
- if (!val.IsTypedArray()) {
349
- Napi::TypeError::New(env, "Expected Float32Array")
350
- .ThrowAsJavaScriptException();
351
- return env.Null();
352
- }
353
- Napi::Float32Array channel = val.As<Napi::Float32Array>();
354
- channel_data[i] = channel.Data();
355
- }
356
-
357
- AicErrorCode error = aic_model_process_planar(
358
- model, channel_data.data(), num_channels, num_frames);
359
- return Napi::Number::New(env, static_cast<int>(error));
360
- }));
361
-
362
- exports.Set("setParameter",
363
- Napi::Function::New(
364
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
365
- Napi::Env env = info.Env();
366
- if (!info[0].IsExternal()) {
367
- Napi::TypeError::New(env, "Expected model handle")
368
- .ThrowAsJavaScriptException();
369
- return env.Null();
370
- }
371
- AicModel *model =
372
- info[0].As<Napi::External<AicModel>>().Data();
373
- AicParameter param = static_cast<AicParameter>(
374
- info[1].As<Napi::Number>().Int32Value());
375
- float value = info[2].As<Napi::Number>().FloatValue();
376
- AicErrorCode error =
377
- aic_model_set_parameter(model, param, value);
378
- return Napi::Number::New(env, static_cast<int>(error));
379
- }));
380
-
381
- exports.Set("getParameter",
382
- Napi::Function::New(
383
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
384
- Napi::Env env = info.Env();
385
- if (!info[0].IsExternal()) {
386
- Napi::TypeError::New(env, "Expected model handle")
387
- .ThrowAsJavaScriptException();
388
- return env.Null();
389
- }
390
- AicModel *model =
391
- info[0].As<Napi::External<AicModel>>().Data();
392
- AicParameter param = static_cast<AicParameter>(
393
- info[1].As<Napi::Number>().Int32Value());
394
- float value = 0.0f;
395
- AicErrorCode error =
396
- aic_model_get_parameter(model, param, &value);
397
- Napi::Object result = Napi::Object::New(env);
398
- result.Set("error",
399
- Napi::Number::New(env, static_cast<int>(error)));
400
- result.Set("value", Napi::Number::New(env, value));
401
- return result;
402
- }));
403
-
404
- exports.Set("getOutputDelay",
405
- Napi::Function::New(
406
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
407
- Napi::Env env = info.Env();
408
- if (!info[0].IsExternal()) {
409
- Napi::TypeError::New(env, "Expected model handle")
410
- .ThrowAsJavaScriptException();
411
- return env.Null();
412
- }
413
- AicModel *model =
414
- info[0].As<Napi::External<AicModel>>().Data();
415
- size_t delay = 0;
416
- AicErrorCode error = aic_get_output_delay(model, &delay);
417
- Napi::Object result = Napi::Object::New(env);
418
- result.Set("error",
419
- Napi::Number::New(env, static_cast<int>(error)));
420
- result.Set("delay", Napi::Number::New(env, delay));
421
- return result;
422
- }));
423
-
424
- exports.Set(
425
- "getOptimalSampleRate",
426
- Napi::Function::New(
427
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
428
- Napi::Env env = info.Env();
429
- if (!info[0].IsExternal()) {
430
- Napi::TypeError::New(env, "Expected model handle")
431
- .ThrowAsJavaScriptException();
432
- return env.Null();
433
- }
434
- AicModel *model = info[0].As<Napi::External<AicModel>>().Data();
435
- uint32_t sample_rate = 0;
436
- AicErrorCode error =
437
- aic_get_optimal_sample_rate(model, &sample_rate);
438
- Napi::Object result = Napi::Object::New(env);
439
- result.Set("error",
440
- Napi::Number::New(env, static_cast<int>(error)));
441
- result.Set("sampleRate", Napi::Number::New(env, sample_rate));
442
- return result;
443
- }));
444
-
445
- exports.Set("getOptimalNumFrames",
446
- Napi::Function::New(
447
- env, [](const Napi::CallbackInfo &info) -> Napi::Value {
448
- Napi::Env env = info.Env();
449
- if (!info[0].IsExternal()) {
450
- Napi::TypeError::New(env, "Expected model handle")
451
- .ThrowAsJavaScriptException();
452
- return env.Null();
453
- }
454
- AicModel *model =
455
- info[0].As<Napi::External<AicModel>>().Data();
456
- size_t num_frames = 0;
457
- AicErrorCode error =
458
- aic_get_optimal_num_frames(model, &num_frames);
459
- Napi::Object result = Napi::Object::New(env);
460
- result.Set("error",
461
- Napi::Number::New(env, static_cast<int>(error)));
462
- result.Set("numFrames", Napi::Number::New(env, num_frames));
463
- return result;
464
- }));
465
-
466
- return exports;
467
- }
468
-
469
- NODE_API_MODULE(aic_binding, Init)