android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java
Go to the documentation of this file.
1 /*
2  Copyright 2018-2021 Picovoice Inc.
3  You may not use this file except in compliance with the license. A copy of the license is
4  located in the "LICENSE" file accompanying this source.
5  Unless required by applicable law or agreed to in writing, software distributed under the
6  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
7  express or implied. See the License for the specific language governing permissions and
8  limitations under the License.
9 */
10 
11 
12 package ai.picovoice.rhino;
13 
14 import android.content.Context;
15 import android.content.res.Resources;
16 
17 import java.io.BufferedInputStream;
18 import java.io.BufferedOutputStream;
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 
35 public class Rhino {
36 
37  private static String DEFAULT_MODEL_PATH;
38  private static boolean isExtracted;
39 
40  static {
41  System.loadLibrary("pv_rhino");
42  }
43 
44  private long handle;
45 
60  private Rhino(String accessKey,
61  String modelPath,
62  String contextPath,
63  float sensitivity,
64  boolean requireEndpoint) {
65  handle = init(accessKey, modelPath, contextPath, sensitivity, requireEndpoint);
66  }
67 
71  public void delete() {
72  if (handle != 0) {
73  delete(handle);
74  handle = 0;
75  }
76  }
77 
90  public boolean process(short[] pcm) throws RhinoException {
91  if (handle == 0) {
92  new RhinoInvalidArgumentException("Attempted to call Rhino process after delete.");
93  }
94  if (pcm == null) {
95  new RhinoInvalidArgumentException("Passed null frame to Rhino process.");
96  }
97 
98  if (pcm.length != getFrameLength()) {
100  String.format("Rhino process requires frames of length %d. " +
101  "Received frame of size %d.", getFrameLength(), pcm.length));
102  }
103 
104  try {
105  return process(handle, pcm);
106  } catch (Exception e) {
107  throw new RhinoException(e);
108  }
109  }
110 
121 
122  final boolean isUnderstood = isUnderstood(handle);
123 
124  RhinoInference inference;
125 
126  if (isUnderstood) {
127  final String intentPacked = getIntent(handle);
128  String[] parts = intentPacked.split(",");
129  if (parts.length == 0) {
130  throw new RhinoException(String.format("Failed to retrieve intent from '%s'.", intentPacked));
131  }
132 
133  Map<String, String> slots = new LinkedHashMap<>();
134  for (int i = 1; i < parts.length; i++) {
135  String[] slotAndValue = parts[i].split(":");
136  if (slotAndValue.length != 2) {
137  throw new RhinoException(String.format("Failed to retrieve intent from '%s'.", intentPacked));
138  }
139  slots.put(slotAndValue[0], slotAndValue[1]);
140  }
141 
142  inference = new RhinoInference(true, parts[0], slots);
143  } else {
144  inference = new RhinoInference(false, null, null);
145  }
146 
147  reset(handle);
148 
149  return inference;
150  }
151 
157  public String getContextInformation() {
158  return getContextInfo(handle);
159  }
160 
166  public native int getFrameLength();
167 
173  public native int getSampleRate();
174 
180  public native String getVersion();
181 
182  private native long init(String accessKey, String modelPath, String contextPath, float sensitivity, boolean requireEndpoint);
183 
184  private native void delete(long object);
185 
186  private native boolean process(long object, short[] pcm);
187 
188  private native boolean isUnderstood(long object);
189 
190  private native String getIntent(long object);
191 
192  private native boolean reset(long object);
193 
194  private native String getContextInfo(long object);
195 
199  public static class Builder {
200  private String accessKey = null;
201  private String modelPath = null;
202  private String contextPath = null;
203  private float sensitivity = 0.5f;
204  private boolean requireEndpoint = true;
205 
206  public Builder setAccessKey(String accessKey) {
207  this.accessKey = accessKey;
208  return this;
209  }
210 
211  public Builder setModelPath(String modelPath) {
212  this.modelPath = modelPath;
213  return this;
214  }
215 
217  this.contextPath = contextPath;
218  return this;
219  }
220 
222  this.sensitivity = sensitivity;
223  return this;
224  }
225 
227  this.requireEndpoint = requireEndpoint;
228  return this;
229  }
230 
231  private void extractPackageResources(Context context) throws RhinoIOException {
232  final Resources resources = context.getResources();
233 
234  try {
236  resources.openRawResource(R.raw.rhino_params),
237  resources.getResourceEntryName(R.raw.rhino_params) + ".pv");
238 
239  isExtracted = true;
240  } catch (IOException ex) {
241  throw new RhinoIOException(ex);
242  }
243  }
244 
245  private String extractResource(Context context, InputStream srcFileStream, String dstFilename) throws IOException {
246  InputStream is = new BufferedInputStream(srcFileStream, 256);
247  OutputStream os = new BufferedOutputStream(context.openFileOutput(dstFilename, Context.MODE_PRIVATE), 256);
248  int r;
249  while ((r = is.read()) != -1) {
250  os.write(r);
251  }
252  os.flush();
253 
254  is.close();
255  os.close();
256  return new File(context.getFilesDir(), dstFilename).getAbsolutePath();
257  }
258 
266  public Rhino build(Context context) throws RhinoException {
267 
268  if (!isExtracted) {
270  }
271 
272  if (accessKey == null || accessKey.equals("")) {
273  throw new RhinoInvalidArgumentException("No AccessKey provided to Rhino.");
274  }
275 
276  if (modelPath == null) {
278  } else {
279  File modelFile = new File(modelPath);
280  String modelFilename = modelFile.getName();
281  if (!modelFile.exists() && !modelFilename.equals("")) {
282  try {
284  context.getAssets().open(modelPath),
285  modelFilename);
286  } catch (IOException ex) {
287  throw new RhinoIOException(ex);
288  }
289  }
290  }
291 
292  if (this.contextPath == null) {
293  throw new RhinoInvalidArgumentException("No context file (.rhn) was provided.");
294  }
295 
296  File contextFile = new File(contextPath);
297  String contextFilename = contextFile.getName();
298  if (!contextFile.exists() && !contextFilename.equals("")) {
299  try {
301  context.getAssets().open(contextPath),
302  contextFilename);
303  } catch (IOException ex) {
304  throw new RhinoIOException(ex);
305  }
306  }
307 
308  if (sensitivity < 0 || sensitivity > 1) {
309  throw new RhinoInvalidArgumentException("Sensitivity value should be within [0, 1].");
310  }
311 
313  }
314  }
315 }
R
#define R
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5104
ai.picovoice.rhino.RhinoException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoException.java:14
ai.picovoice.rhino.Rhino.process
boolean process(short[] pcm)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:90
ai.picovoice.rhino.Rhino.Builder.setSensitivity
Builder setSensitivity(float sensitivity)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:221
ai.picovoice.rhino.Rhino.Builder.accessKey
String accessKey
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:200
context
ma_context context
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_deviceio/ma_test_deviceio.c:56
ai.picovoice.rhino.Rhino.Builder.extractResource
String extractResource(Context context, InputStream srcFileStream, String dstFilename)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:245
ai.picovoice.rhino.Rhino.Builder.setContextPath
Builder setContextPath(String contextPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:216
ai.picovoice.rhino.Rhino.Builder.setAccessKey
Builder setAccessKey(String accessKey)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:206
ai.picovoice.rhino.RhinoInvalidArgumentException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoInvalidArgumentException.java:13
ai.picovoice.rhino.Rhino.handle
long handle
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:44
ai.picovoice.rhino.Rhino.init
native long init(String accessKey, String modelPath, String contextPath, float sensitivity, boolean requireEndpoint)
ai.picovoice.rhino.Rhino.getSampleRate
native int getSampleRate()
ai.picovoice.rhino.Rhino.isUnderstood
native boolean isUnderstood(long object)
ai.picovoice.rhino.Rhino.Builder.modelPath
String modelPath
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:201
ai.picovoice.rhino.Rhino.Builder.requireEndpoint
boolean requireEndpoint
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:204
ai.picovoice.rhino.Rhino.DEFAULT_MODEL_PATH
static String DEFAULT_MODEL_PATH
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:37
ai.picovoice.rhino.Rhino.Builder.contextPath
String contextPath
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:202
ai.picovoice.rhino.Rhino.reset
native boolean reset(long object)
ai.picovoice.rhino.RhinoIOException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoIOException.java:13
ai.picovoice.rhino.Rhino.getIntent
native String getIntent(long object)
ai.picovoice.rhino.RhinoInference
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:15
ai.picovoice.rhino.Rhino.getVersion
native String getVersion()
ai.picovoice.rhino.Rhino.Rhino
Rhino(String accessKey, String modelPath, String contextPath, float sensitivity, boolean requireEndpoint)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:60
ai.picovoice.rhino.Rhino
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:35
ai.picovoice.rhino.Rhino.isExtracted
static boolean isExtracted
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:38
ai.picovoice.rhino.Rhino.Builder.setRequireEndpoint
Builder setRequireEndpoint(boolean requireEndpoint)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:226
ai.picovoice.rhino.Rhino.Builder.setModelPath
Builder setModelPath(String modelPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:211
ai.picovoice.rhino.Rhino.Builder
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:199
ai.picovoice.rhino.Rhino.Builder.extractPackageResources
void extractPackageResources(Context context)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:231
ai.picovoice.rhino.Rhino.getInference
RhinoInference getInference()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:120
ai.picovoice.rhino.Rhino.Builder.sensitivity
float sensitivity
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:203
ai.picovoice.rhino.Rhino.getContextInfo
native String getContextInfo(long object)
ai.picovoice.rhino.Rhino.Builder.build
Rhino build(Context context)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:266
ai.picovoice.rhino.Rhino.getFrameLength
native int getFrameLength()
ai.picovoice.rhino.Rhino.getContextInformation
String getContextInformation()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:157


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