java/src/ai/picovoice/rhino/Rhino.java
Go to the documentation of this file.
1 /*
2  Copyright 2018-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.rhino;
14 
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.LinkedHashMap;
18 import java.util.Map;
19 
20 
31 public class Rhino {
32 
33  private final long libraryHandle;
34 
35  public static final String LIBRARY_PATH;
36  public static final String MODEL_PATH;
37 
38  static {
39  LIBRARY_PATH = Utils.getPackagedLibraryPath();
40  MODEL_PATH = Utils.getPackagedModelPath();
41  }
42 
58  public Rhino(String accessKey, String libraryPath, String modelPath, String contextPath, float sensitivity, boolean requireEndpoint) throws RhinoException {
59 
60  try {
61  System.load(libraryPath);
62  } catch (Exception exception) {
63  throw new RhinoException(exception);
64  }
65  libraryHandle = init(accessKey, modelPath, contextPath, sensitivity, requireEndpoint);
66  }
67 
71  public void delete() {
72  delete(libraryHandle);
73  }
74 
87  public boolean process(short[] pcm) throws RhinoException {
88  return process(libraryHandle, pcm);
89  }
90 
101  final boolean isUnderstood = isUnderstood(libraryHandle);
102 
103  RhinoInference inference;
104 
105  if (isUnderstood) {
106  final String intentPacked = getIntent(libraryHandle);
107  String[] parts = intentPacked.split(",");
108  if (parts.length == 0) {
109  throw new RhinoException(String.format("Failed to retrieve intent from '%s'.", intentPacked));
110  }
111 
112  Map<String, String> slots = new LinkedHashMap<>();
113  for (int i = 1; i < parts.length; i++) {
114  String[] slotAndValue = parts[i].split(":");
115  if (slotAndValue.length != 2) {
116  throw new RhinoException(String.format("Failed to retrieve intent from '%s'.", intentPacked));
117  }
118  slots.put(slotAndValue[0], slotAndValue[1]);
119  }
120 
121  inference = new RhinoInference(true, parts[0], slots);
122  } else {
123  inference = new RhinoInference(false, null, null);
124  }
125 
127 
128  return inference;
129  }
130 
136  public String getContextInformation() {
138  }
139 
145  public native int getFrameLength();
146 
152  public native int getSampleRate();
153 
159  public native String getVersion();
160 
161  private native long init(String accessKey, String modelPath, String contextPath, float sensitivity, boolean requireEndpoint);
162 
163  private native void delete(long object);
164 
165  private native boolean process(long object, short[] pcm);
166 
167  private native boolean isUnderstood(long object);
168 
169  private native String getIntent(long object);
170 
171  private native boolean reset(long object);
172 
173  private native String getContextInfo(long object);
174 
178  public static class Builder {
179 
180  private String accessKey = null;
181  private String libraryPath = null;
182  private String modelPath = null;
183  private String contextPath = null;
184  private float sensitivity = 0.5f;
185  private boolean requireEndpoint = false;
186 
187  public Builder setAccessKey(String accessKey) {
188  this.accessKey = accessKey;
189  return this;
190  }
191 
193  this.libraryPath = libraryPath;
194  return this;
195  }
196 
197  public Builder setModelPath(String modelPath) {
198  this.modelPath = modelPath;
199  return this;
200  }
201 
203  this.contextPath = contextPath;
204  return this;
205  }
206 
208  this.sensitivity = sensitivity;
209  return this;
210  }
211 
213  this.requireEndpoint = requireEndpoint;
214  return this;
215  }
222  public Rhino build() throws RhinoException {
223 
224  if (!Utils.isEnvironmentSupported()) {
225  throw new RhinoRuntimeException("Could not initialize Rhino. " +
226  "Execution environment not currently supported by Rhino Java.");
227  }
228 
229  if (accessKey == null) {
230  throw new RhinoInvalidArgumentException("AccessKey is required for Rhino initialization.");
231  }
232 
233  if (libraryPath == null) {
234  if (Utils.isResourcesAvailable()) {
236  } else {
237  throw new RhinoInvalidArgumentException("Default library unavailable. Please " +
238  "provide a native Rhino library path (-l <library_path>).");
239  }
240  if (!new File(libraryPath).exists()) {
241  throw new RhinoIOException(String.format("Couldn't find library file at " +
242  "'%s'", libraryPath));
243  }
244  }
245 
246  if (modelPath == null) {
247  if (Utils.isResourcesAvailable()) {
249  } else {
250  throw new RhinoInvalidArgumentException("Default model unavailable. Please provide a " +
251  "valid Rhino model path (-m <model_path>).");
252  }
253  if (!new File(modelPath).exists()) {
254  throw new RhinoIOException(String.format("Couldn't find model file at " +
255  "'%s'", modelPath));
256  }
257  }
258 
259  if (contextPath == null) {
260  throw new RhinoInvalidArgumentException("No context file provided");
261  }
262 
263  if (!new File(contextPath).exists()) {
264  throw new RhinoIOException(String.format("Couldn't find context file at " +
265  "'%s'", contextPath));
266  }
267 
268  if (sensitivity < 0 || sensitivity > 1) {
269  throw new RhinoInvalidArgumentException("Sensitivity value should be within [0, 1].");
270  }
271 
273  }
274  }
275 }
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: java/src/ai/picovoice/rhino/Rhino.java:87
ai.picovoice.rhino.Rhino.Builder.setSensitivity
Builder setSensitivity(float sensitivity)
Definition: java/src/ai/picovoice/rhino/Rhino.java:207
ai.picovoice.rhino.Rhino.MODEL_PATH
static final String MODEL_PATH
Definition: java/src/ai/picovoice/rhino/Rhino.java:36
exists
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
ai.picovoice.rhino.Rhino.Builder.accessKey
String accessKey
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:200
ai.picovoice.rhino.Rhino.Builder.setContextPath
Builder setContextPath(String contextPath)
Definition: java/src/ai/picovoice/rhino/Rhino.java:202
ai.picovoice.rhino.Rhino.Builder.setAccessKey
Builder setAccessKey(String accessKey)
Definition: java/src/ai/picovoice/rhino/Rhino.java:187
ai.picovoice.rhino.RhinoInvalidArgumentException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoInvalidArgumentException.java:13
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.libraryHandle
final long libraryHandle
Definition: java/src/ai/picovoice/rhino/Rhino.java:33
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.LIBRARY_PATH
static final String LIBRARY_PATH
Definition: java/src/ai/picovoice/rhino/Rhino.java:35
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.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.Builder.setLibraryPath
Builder setLibraryPath(String libraryPath)
Definition: java/src/ai/picovoice/rhino/Rhino.java:192
ai.picovoice.rhino.Rhino.Builder.build
Rhino build()
Definition: java/src/ai/picovoice/rhino/Rhino.java:222
ai.picovoice.rhino.Rhino.Rhino
Rhino(String accessKey, String libraryPath, String modelPath, String contextPath, float sensitivity, boolean requireEndpoint)
Definition: java/src/ai/picovoice/rhino/Rhino.java:58
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.Builder.libraryPath
String libraryPath
Definition: java/src/ai/picovoice/rhino/Rhino.java:181
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.Builder.setRequireEndpoint
Builder setRequireEndpoint(boolean requireEndpoint)
Definition: java/src/ai/picovoice/rhino/Rhino.java:212
ai.picovoice.rhino.Rhino.Builder.setModelPath
Builder setModelPath(String modelPath)
Definition: java/src/ai/picovoice/rhino/Rhino.java:197
ai.picovoice.rhino.Rhino.Builder
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:199
ai.picovoice.rhino.Rhino.getInference
RhinoInference getInference()
Definition: java/src/ai/picovoice/rhino/Rhino.java:100
ai.picovoice.rhino.RhinoRuntimeException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoRuntimeException.java:13
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.getFrameLength
native int getFrameLength()
ai.picovoice.rhino.Rhino.getContextInformation
String getContextInformation()
Definition: java/src/ai/picovoice/rhino/Rhino.java:136


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