react-native-wakeword-sid 1.1.100 → 1.1.101

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.
@@ -18,6 +18,13 @@ import java.io.File;
18
18
  import java.util.HashMap;
19
19
  import java.util.Map;
20
20
  import java.util.concurrent.*;
21
+ import android.content.res.AssetManager;
22
+ import java.io.InputStream;
23
+ import java.io.BufferedInputStream;
24
+ import java.io.ByteArrayOutputStream;
25
+ import java.io.IOException;
26
+ import java.util.Locale;
27
+
21
28
 
22
29
  /**
23
30
  * React Native bridge for the speaker-id Android library.
@@ -106,6 +113,8 @@ public class SpeakerIdRNBridge extends ReactContextBaseJavaModule {
106
113
 
107
114
  @ReactMethod
108
115
  public void initVerificationUsingCurrentConfig(Promise promise) {
116
+ SpeakerIdApi api = instances.get(instanceId);
117
+ if (api == null) { promise.reject("InstanceNotFound", instanceId); return; }
109
118
  try {
110
119
  boolean ok = api.initVerificationUsingCurrentConfig();
111
120
  promise.resolve(ok);
@@ -114,6 +123,59 @@ public class SpeakerIdRNBridge extends ReactContextBaseJavaModule {
114
123
  }
115
124
  }
116
125
 
126
+ // ======= External-audio cluster API (RN) =======
127
+
128
+ @ReactMethod
129
+ public void initCluster(String instanceId, int numOfEmb, Promise promise) {
130
+ SpeakerIdApi api = instances.get(instanceId);
131
+ if (api == null) { promise.reject("InstanceNotFound", instanceId); return; }
132
+ exec.submit(() -> {
133
+ try {
134
+ int cid = api.initCluster(numOfEmb);
135
+ main.post(() -> promise.resolve(cid));
136
+ } catch (Throwable t) {
137
+ main.post(() -> promise.reject("InitClusterError", t.getMessage(), t));
138
+ }
139
+ });
140
+ }
141
+
142
+ @ReactMethod
143
+ public void createAndPushEmbeddingsToCluster(String instanceId, int clusterId, ReadableArray pcmI16, int length, Promise promise) {
144
+ SpeakerIdApi api = instances.get(instanceId);
145
+ if (api == null) { promise.reject("InstanceNotFound", instanceId); return; }
146
+ // Convert RN int[] -> short[]
147
+ int n = Math.min(length, pcmI16.size());
148
+ final short[] block = new short[n];
149
+ for (int i = 0; i < n; i++) block[i] = (short) pcmI16.getInt(i);
150
+
151
+ exec.submit(() -> {
152
+ try {
153
+ api.createAndPushEmbeddingsToCluster(clusterId, block, n);
154
+ main.post(() -> promise.resolve(null));
155
+ } catch (Throwable t) {
156
+ main.post(() -> promise.reject("PushClusterError", t.getMessage(), t));
157
+ }
158
+ });
159
+ }
160
+
161
+ @ReactMethod
162
+ public void createAndVerifyEmbeddingsFromCluster(String instanceId, int clusterId, ReadableArray pcmI16, int length, Promise promise) {
163
+ SpeakerIdApi api = instances.get(instanceId);
164
+ if (api == null) { promise.reject("InstanceNotFound", instanceId); return; }
165
+ int n = Math.min(length, pcmI16.size());
166
+ final short[] block = new short[n];
167
+ for (int i = 0; i < n; i++) block[i] = (short) pcmI16.getInt(i);
168
+
169
+ exec.submit(() -> {
170
+ try {
171
+ float score = api.createAndVerifyEmbeddingsFromCluster(clusterId, block, n);
172
+ main.post(() -> promise.resolve(score));
173
+ } catch (Throwable t) {
174
+ main.post(() -> promise.reject("VerifyClusterError", t.getMessage(), t));
175
+ }
176
+ });
177
+ }
178
+
117
179
  @ReactMethod
118
180
  public void destroyInstance(String instanceId, Promise promise) {
119
181
  SpeakerIdApi api;
@@ -319,6 +381,9 @@ public class SpeakerIdRNBridge extends ReactContextBaseJavaModule {
319
381
  }
320
382
 
321
383
  // ======= WWD: create instance tuned for wake-word =======
384
+
385
+ private static final boolean DEBUG_SID_ASSETS = true; // set false for production
386
+
322
387
  @ReactMethod
323
388
  public void createInstanceWWD(String instanceId, Promise promise) {
324
389
  if (instances.containsKey(instanceId)) {
@@ -330,6 +395,11 @@ public class SpeakerIdRNBridge extends ReactContextBaseJavaModule {
330
395
  SpeakerIdApi api = SpeakerIdApi.createWWD(reactContext);
331
396
  synchronized (instances) { instances.put(instanceId, api); }
332
397
  main.post(() -> promise.resolve(true));
398
+ // Then run debug async if enabled
399
+ if (DEBUG_SID_ASSETS) {
400
+ exec.submit(() -> debugOnAssetsWWD(instanceId));
401
+ }
402
+
333
403
  } catch (Exception e) {
334
404
  main.post(() -> promise.reject("CreateWWDError", e.getMessage(), e));
335
405
  }
@@ -434,4 +504,78 @@ public class SpeakerIdRNBridge extends ReactContextBaseJavaModule {
434
504
  exec.shutdownNow();
435
505
  super.onCatalystInstanceDestroy();
436
506
  }
507
+
508
+ // ======= DEBUG harness (assets) =======
509
+
510
+ private void emitDebug(String msg) {
511
+ WritableMap m = Arguments.createMap();
512
+ m.putString("line", msg);
513
+ sendEvent("SpeakerIdDebug", m);
514
+ }
515
+
516
+ private void debugOnAssetsWWD(String instanceId) {
517
+ try {
518
+ SpeakerIdApi api = instances.get(instanceId);
519
+ if (api == null) { emitDebug("debugOnAssetsWWD: instance gone: " + instanceId); return; }
520
+
521
+ // 1) Init a small FIFO cluster (K=3)
522
+ final int K = 3;
523
+ int clusterId = api.initCluster(K);
524
+ emitDebug("WWD DEBUG: initCluster -> " + clusterId + " (K=" + K + ")");
525
+
526
+ // 2) Onboarding from assets
527
+ String[] onboard = new String[] {
528
+ "onboard1.raw","onboard2.raw","onboard3.raw","onboard4.raw"
529
+ };
530
+ for (int i = 0; i < onboard.length; i++) {
531
+ short[] pcm = readPcm16LeRawAsset(onboard[i]);
532
+ api.createAndPushEmbeddingsToCluster(clusterId, pcm, pcm.length);
533
+ String line = String.format(Locale.US, "WWD DEBUG: push #%d '%s' samples=%d", i+1, onboard[i], pcm.length);
534
+ emitDebug(line);
535
+ }
536
+ emitDebug("WWD DEBUG: onboarding done; cluster+mean saved.");
537
+
538
+ // 3) Verification from assets
539
+ String[] tests = new String[] {
540
+ "test1.raw","test2.raw","test3.raw","test4.raw","test5.raw","test6.raw","test7.raw"
541
+ };
542
+ for (String tf : tests) {
543
+ short[] pcm = readPcm16LeRawAsset(tf);
544
+ float score = api.createAndVerifyEmbeddingsFromCluster(clusterId, pcm, pcm.length);
545
+ String line = String.format(Locale.US, "WWD DEBUG: verify '%s' samples=%d -> score=%.4f",
546
+ tf, pcm.length, score);
547
+ emitDebug(line);
548
+ }
549
+ emitDebug("WWD DEBUG: verification pass completed.");
550
+ } catch (Throwable t) {
551
+ emitDebug("WWD DEBUG ERROR: " + t.getMessage());
552
+ }
553
+ }
554
+
555
+ /** Read RAW little-endian PCM16 asset → short[] (exactly your requested method) */
556
+ private short[] readPcm16LeRawAsset(String assetName) throws IOException {
557
+ AssetManager am = reactContext.getAssets();
558
+ try (InputStream in = am.open(assetName);
559
+ BufferedInputStream bis = new BufferedInputStream(in);
560
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(32 * 1024)) {
561
+
562
+ byte[] buf = new byte[8192];
563
+ int n;
564
+ while ((n = bis.read(buf)) != -1) {
565
+ baos.write(buf, 0, n);
566
+ }
567
+ byte[] all = baos.toByteArray();
568
+ if ((all.length & 1) != 0) {
569
+ throw new IOException("Asset RAW has odd byte length: " + assetName + " (" + all.length + " bytes)");
570
+ }
571
+ int samples = all.length / 2;
572
+ short[] pcm = new short[samples];
573
+ for (int i = 0, s = 0; i < all.length; i += 2, s++) {
574
+ int lo = (all[i] & 0xFF);
575
+ int hi = (all[i + 1] << 8);
576
+ pcm[s] = (short) (hi | lo);
577
+ }
578
+ return pcm;
579
+ }
580
+ }
437
581
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-wakeword-sid",
3
- "version": "1.1.100",
3
+ "version": "1.1.101",
4
4
  "description": "Voice/Wake-word detection library for React Native",
5
5
  "main": "wakewords/index.js",
6
6
  "types": "wakewords/index.d.ts",