13 package ai.picovoice.reactnative.porcupine;
17 import com.facebook.react.bridge.Arguments;
18 import com.facebook.react.bridge.Promise;
19 import com.facebook.react.bridge.ReactApplicationContext;
20 import com.facebook.react.bridge.ReactContextBaseJavaModule;
21 import com.facebook.react.bridge.ReactMethod;
22 import com.facebook.react.bridge.ReadableArray;
23 import com.facebook.react.bridge.WritableMap;
26 import java.util.ArrayList;
27 import java.util.HashMap;
33 private static final String
LOG_TAG =
"PvPorcupine";
51 for (
int i = 0; i <
keywords.size(); i++) {
53 String keyword =
keywords.getString(i);
54 if (keyword ==
null) {
57 "keyword must be a valid string");
61 keyword = keyword.replace(
' ',
'_');
63 }
catch (IllegalArgumentException e) {
66 String.format(
"'%s' is not a built-in keyword",
keywords.getString(i)));
71 float[] sensitivitiesJava =
new float[sensitivities.size()];
72 for (
int i = 0; i < sensitivities.size(); i++) {
73 sensitivitiesJava[i] = (float) sensitivities.getDouble(i);
80 .setKeywords(keywordsJava.length == 0 ?
null : keywordsJava)
83 porcupinePool.put(String.valueOf(System.identityHashCode(porcupine)), porcupine);
85 WritableMap paramMap = Arguments.createMap();
86 paramMap.putString(
"handle", String.valueOf(System.identityHashCode(porcupine)));
89 paramMap.putString(
"version", porcupine.
getVersion());
90 promise.resolve(paramMap);
92 promise.reject(e.getClass().getSimpleName(), e.getMessage());
97 public void fromKeywordPaths(String accessKey, String modelPath, ReadableArray keywordPaths, ReadableArray sensitivities, Promise promise) {
99 String[] keywordPathsJava =
new String[keywordPaths.size()];
100 for (
int i = 0; i < keywordPaths.size(); i++) {
101 keywordPathsJava[i] = keywordPaths.getString(i);
104 float[] sensitivitiesJava =
new float[sensitivities.size()];
105 for (
int i = 0; i < sensitivities.size(); i++) {
106 sensitivitiesJava[i] = (float) sensitivities.getDouble(i);
113 .setKeywordPaths(keywordPathsJava.length == 0 ?
null : keywordPathsJava)
116 porcupinePool.put(String.valueOf(System.identityHashCode(porcupine)), porcupine);
118 WritableMap paramMap = Arguments.createMap();
119 paramMap.putString(
"handle", String.valueOf(System.identityHashCode(porcupine)));
122 paramMap.putString(
"version", porcupine.
getVersion());
123 promise.resolve(paramMap);
125 promise.reject(e.getClass().getSimpleName(), e.getMessage());
130 public void delete(String handle) {
138 public void process(String handle, ReadableArray pcmArray, Promise promise) {
147 ArrayList<Object> pcmArrayList = pcmArray.toArrayList();
148 short[] buffer =
new short[pcmArray.size()];
149 for (
int i = 0; i < pcmArray.size(); i++) {
150 buffer[i] = ((Number) pcmArrayList.get(i)).shortValue();
152 int result = porcupine.
process(buffer);
153 promise.resolve(result);
155 promise.reject(e.getClass().getSimpleName(), e.getMessage());