PorcupineModule.java
Go to the documentation of this file.
1 /*
2  Copyright 2020-2021 Picovoice Inc.
3 
4  You may not use this file except in compliance with the license. A copy of the license is
5  located in the "LICENSE" file accompanying this source.
6 
7  Unless required by applicable law or agreed to in writing, software distributed under the
8  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
9  express or implied. See the License for the specific language governing permissions and
10  limitations under the License.
11 */
12 
13 package ai.picovoice.reactnative.porcupine;
14 
15 import ai.picovoice.porcupine.*;
16 
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;
24 
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Map;
29 
30 
31 public class PorcupineModule extends ReactContextBaseJavaModule {
32 
33  private static final String LOG_TAG = "PvPorcupine";
34  private final ReactApplicationContext reactContext;
35  private final Map<String, Porcupine> porcupinePool = new HashMap<>();
36 
37  public PorcupineModule(ReactApplicationContext reactContext) {
38  super(reactContext);
39  this.reactContext = reactContext;
40  }
41 
42  @Override
43  public String getName() {
44  return "PvPorcupine";
45  }
46 
47  @ReactMethod
48  public void fromBuiltInKeywords(String accessKey, String modelPath, ReadableArray keywords, ReadableArray sensitivities, Promise promise) {
49  // convert from ReadableArrays to Java types
50  Porcupine.BuiltInKeyword[] keywordsJava = new Porcupine.BuiltInKeyword[keywords.size()];
51  for (int i = 0; i < keywords.size(); i++) {
52  try {
53  String keyword = keywords.getString(i);
54  if (keyword == null) {
55  promise.reject(
56  PorcupineInvalidArgumentException.class.getSimpleName(),
57  "keyword must be a valid string");
58  return;
59  }
60 
61  keyword = keyword.replace(' ', '_');
62  keywordsJava[i] = Porcupine.BuiltInKeyword.valueOf(keyword.toUpperCase());
63  } catch (IllegalArgumentException e) {
64  promise.reject(
65  PorcupineInvalidArgumentException.class.getSimpleName(),
66  String.format("'%s' is not a built-in keyword", keywords.getString(i)));
67  return;
68  }
69  }
70 
71  float[] sensitivitiesJava = new float[sensitivities.size()];
72  for (int i = 0; i < sensitivities.size(); i++) {
73  sensitivitiesJava[i] = (float) sensitivities.getDouble(i);
74  }
75 
76  try {
77  Porcupine porcupine = new Porcupine.Builder()
78  .setAccessKey(accessKey)
79  .setModelPath(modelPath.isEmpty() ? null : modelPath)
80  .setKeywords(keywordsJava.length == 0 ? null : keywordsJava)
81  .setSensitivities(sensitivitiesJava.length == 0 ? null : sensitivitiesJava)
83  porcupinePool.put(String.valueOf(System.identityHashCode(porcupine)), porcupine);
84 
85  WritableMap paramMap = Arguments.createMap();
86  paramMap.putString("handle", String.valueOf(System.identityHashCode(porcupine)));
87  paramMap.putInt("frameLength", porcupine.getFrameLength());
88  paramMap.putInt("sampleRate", porcupine.getSampleRate());
89  paramMap.putString("version", porcupine.getVersion());
90  promise.resolve(paramMap);
91  } catch (PorcupineException e) {
92  promise.reject(e.getClass().getSimpleName(), e.getMessage());
93  }
94  }
95 
96  @ReactMethod
97  public void fromKeywordPaths(String accessKey, String modelPath, ReadableArray keywordPaths, ReadableArray sensitivities, Promise promise) {
98  // convert from ReadableArrays to Java types
99  String[] keywordPathsJava = new String[keywordPaths.size()];
100  for (int i = 0; i < keywordPaths.size(); i++) {
101  keywordPathsJava[i] = keywordPaths.getString(i);
102  }
103 
104  float[] sensitivitiesJava = new float[sensitivities.size()];
105  for (int i = 0; i < sensitivities.size(); i++) {
106  sensitivitiesJava[i] = (float) sensitivities.getDouble(i);
107  }
108 
109  try {
110  Porcupine porcupine = new Porcupine.Builder()
111  .setAccessKey(accessKey)
112  .setModelPath(modelPath.isEmpty() ? null : modelPath)
113  .setKeywordPaths(keywordPathsJava.length == 0 ? null : keywordPathsJava)
114  .setSensitivities(sensitivitiesJava.length == 0 ? null : sensitivitiesJava)
116  porcupinePool.put(String.valueOf(System.identityHashCode(porcupine)), porcupine);
117 
118  WritableMap paramMap = Arguments.createMap();
119  paramMap.putString("handle", String.valueOf(System.identityHashCode(porcupine)));
120  paramMap.putInt("frameLength", porcupine.getFrameLength());
121  paramMap.putInt("sampleRate", porcupine.getSampleRate());
122  paramMap.putString("version", porcupine.getVersion());
123  promise.resolve(paramMap);
124  } catch (PorcupineException e) {
125  promise.reject(e.getClass().getSimpleName(), e.getMessage());
126  }
127  }
128 
129  @ReactMethod
130  public void delete(String handle) {
131  if (porcupinePool.containsKey(handle)) {
132  porcupinePool.get(handle).delete();
133  porcupinePool.remove(handle);
134  }
135  }
136 
137  @ReactMethod
138  public void process(String handle, ReadableArray pcmArray, Promise promise) {
139  try {
140 
141  if (!porcupinePool.containsKey(handle)) {
142  promise.reject(PorcupineInvalidStateException.class.getSimpleName(), "Invalid Porcupine handle provided to native module.");
143  return;
144  }
145 
146  Porcupine porcupine = porcupinePool.get(handle);
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();
151  }
152  int result = porcupine.process(buffer);
153  promise.resolve(result);
154  } catch (PorcupineException e) {
155  promise.reject(e.getClass().getSimpleName(), e.getMessage());
156  }
157  }
158 }
ai.picovoice.reactnative.porcupine.PorcupineModule.fromBuiltInKeywords
void fromBuiltInKeywords(String accessKey, String modelPath, ReadableArray keywords, ReadableArray sensitivities, Promise promise)
Definition: PorcupineModule.java:48
ai.picovoice.porcupine
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineActivationException.java:11
ai.picovoice.reactnative.porcupine.PorcupineModule.porcupinePool
final Map< String, Porcupine > porcupinePool
Definition: PorcupineModule.java:35
ai.picovoice.reactnative.porcupine.PorcupineModule.getName
String getName()
Definition: PorcupineModule.java:43
ai.picovoice.porcupine.Porcupine.Builder
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:165
ai.picovoice.reactnative.porcupine.PorcupineModule.fromKeywordPaths
void fromKeywordPaths(String accessKey, String modelPath, ReadableArray keywordPaths, ReadableArray sensitivities, Promise promise)
Definition: PorcupineModule.java:97
ai.picovoice.porcupine.Porcupine.getVersion
native String getVersion()
ai.picovoice.porcupine.Porcupine.Builder.build
Porcupine build(Context context)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:256
ai.picovoice.reactnative.porcupine.PorcupineModule.process
void process(String handle, ReadableArray pcmArray, Promise promise)
Definition: PorcupineModule.java:138
File
ai.picovoice.porcupine.PorcupineException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineException.java:13
ai.picovoice.reactnative.porcupine.PorcupineModule.reactContext
final ReactApplicationContext reactContext
Definition: PorcupineModule.java:34
ai.picovoice.porcupine.Porcupine.getFrameLength
native int getFrameLength()
ai.picovoice.porcupine.Porcupine.Builder.setSensitivities
Builder setSensitivities(float[] sensitivities)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:203
ai.picovoice.reactnative.porcupine.PorcupineModule.PorcupineModule
PorcupineModule(ReactApplicationContext reactContext)
Definition: PorcupineModule.java:37
ai.picovoice.reactnative.porcupine.PorcupineModule.LOG_TAG
static final String LOG_TAG
Definition: PorcupineModule.java:33
ai
ai.picovoice
python.setup.keywords
keywords
Definition: porcupine/binding/python/setup.py:88
ai.picovoice.porcupine.Porcupine.process
int process(short[] pcm)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:94
ai.picovoice.reactnative.porcupine.PorcupineModule
Definition: PorcupineModule.java:31
ai.picovoice.porcupine.Porcupine.getSampleRate
native int getSampleRate()
ai.picovoice.porcupine.Porcupine.Builder.setAccessKey
Builder setAccessKey(String accessKey)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:173
ai.picovoice.porcupine.Porcupine
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:35
ai.picovoice.porcupine.Porcupine.Builder.setModelPath
Builder setModelPath(String modelPath)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:178
ai.picovoice.porcupine.Porcupine.BuiltInKeyword
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:145
ai.picovoice.porcupine.PorcupineInvalidArgumentException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineInvalidArgumentException.java:13
ai.picovoice.porcupine.PorcupineInvalidStateException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineInvalidStateException.java:13


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50