RhinoManager.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 package ai.picovoice.rhino;
12 
13 import android.content.Context;
14 import android.media.AudioFormat;
15 import android.media.AudioRecord;
16 import android.media.MediaRecorder;
17 import android.os.Handler;
18 import android.os.Looper;
19 import android.os.Process;
20 import android.util.Log;
21 
22 import java.util.concurrent.Callable;
23 import java.util.concurrent.Executors;
24 
30 public class RhinoManager {
31  private final Rhino rhino;
34  private final Handler callbackHandler = new Handler(Looper.getMainLooper());
35 
43  private RhinoManager(
44  Rhino rhino,
47 
48  this.rhino = rhino;
49  this.callback = callback;
50  this.errorCallback = errorCallback;
51  }
52 
58  public void process() {
59  Executors.newSingleThreadExecutor().submit(new Callable<Void>() {
60  @Override
61  public Void call() {
62  android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
63  final int minBufferSize = AudioRecord.getMinBufferSize(
65  AudioFormat.CHANNEL_IN_MONO,
66  AudioFormat.ENCODING_PCM_16BIT);
67  final int bufferSize = Math.max(rhino.getSampleRate() / 2, minBufferSize);
68 
69  AudioRecord audioRecord = null;
70 
71  short[] buffer = new short[rhino.getFrameLength()];
72 
73  boolean isFinalized = false;
74  try {
75  audioRecord = new AudioRecord(
76  MediaRecorder.AudioSource.MIC,
78  AudioFormat.CHANNEL_IN_MONO,
79  AudioFormat.ENCODING_PCM_16BIT,
80  bufferSize);
81  audioRecord.startRecording();
82 
83  while (!isFinalized) {
84  if (audioRecord.read(buffer, 0, buffer.length) == buffer.length) {
85  isFinalized = rhino.process(buffer);
86  }
87  }
88  audioRecord.stop();
89  } catch (final Exception e) {
90  if (errorCallback != null) {
91  callbackHandler.post(new Runnable() {
92  @Override
93  public void run() {
95  }
96  });
97  } else {
98  Log.e("RhinoManager", e.toString());
99  }
100  } finally {
101  if (audioRecord != null) {
102  audioRecord.release();
103  }
104 
105  if (isFinalized) {
106  try {
107  final RhinoInference inference = rhino.getInference();
108  callbackHandler.post(new Runnable() {
109  @Override
110  public void run() {
111  callback.invoke(inference);
112  }
113  });
114  } catch (final RhinoException e) {
115  if (errorCallback != null) {
116  callbackHandler.post(new Runnable() {
117  @Override
118  public void run() {
120  }
121  });
122  } else {
123  Log.e("RhinoManager", e.toString());
124  }
125  }
126  }
127  }
128  return null;
129  }
130  });
131  }
132 
136  public void delete() {
137  rhino.delete();
138  }
139 
145  public String getVersion() {
146  return rhino.getVersion();
147  }
148 
154  public String getContextInformation() {
155  return rhino.getContextInformation();
156  }
157 
161  public static class Builder {
162 
163  private String accessKey = null;
164  private String modelPath = null;
165  private String contextPath = null;
166  private float sensitivity = 0.5f;
167  private boolean requireEndpoint = true;
169 
171  this.accessKey = accessKey;
172  return this;
173  }
174 
176  this.modelPath = modelPath;
177  return this;
178  }
179 
181  this.contextPath = contextPath;
182  return this;
183  }
184 
186  this.sensitivity = sensitivity;
187  return this;
188  }
189 
191  this.requireEndpoint = requireEndpoint;
192  return this;
193  }
194 
196  this.errorCallback = errorCallback;
197  return this;
198  }
199 
209  Rhino rhino = new Rhino.Builder()
215  .build(context);
217  }
218  }
219 }
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.RhinoManagerCallback
Definition: RhinoManagerCallback.java:13
ai.picovoice.rhino.Rhino.Builder.setSensitivity
Builder setSensitivity(float sensitivity)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:221
call
bool call(const std::string &service_name, MReq &req, MRes &res)
ai.picovoice.rhino.RhinoManager.Builder.setRequireEndpoint
RhinoManager.Builder setRequireEndpoint(boolean requireEndpoint)
Definition: RhinoManager.java:190
run
void run(class_loader::ClassLoader *loader)
ai.picovoice.rhino.RhinoManager.Builder.setAccessKey
RhinoManager.Builder setAccessKey(String accessKey)
Definition: RhinoManager.java:170
context
ma_context context
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_deviceio/ma_test_deviceio.c:56
ai.picovoice.rhino.RhinoManager.Builder.errorCallback
RhinoManagerErrorCallback errorCallback
Definition: RhinoManager.java:168
ai.picovoice.rhino.RhinoManager.Builder.setContextPath
RhinoManager.Builder setContextPath(String contextPath)
Definition: RhinoManager.java:180
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.RhinoManager.callbackHandler
final Handler callbackHandler
Definition: RhinoManager.java:34
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.RhinoManager.process
void process()
Definition: RhinoManager.java:58
ai.picovoice.rhino.Rhino.getSampleRate
native int getSampleRate()
ai.picovoice.rhino.RhinoManager.errorCallback
final RhinoManagerErrorCallback errorCallback
Definition: RhinoManager.java:33
ai.picovoice.rhino.RhinoManagerErrorCallback.invoke
void invoke(RhinoException error)
ai.picovoice.rhino.RhinoManager.getContextInformation
String getContextInformation()
Definition: RhinoManager.java:154
ai.picovoice.rhino.RhinoManager.getVersion
String getVersion()
Definition: RhinoManager.java:145
ai.picovoice.rhino.RhinoManager.callback
final RhinoManagerCallback callback
Definition: RhinoManager.java:32
ai.picovoice.rhino.RhinoManager
Definition: RhinoManager.java:30
ai.picovoice.rhino.RhinoManager.rhino
final Rhino rhino
Definition: RhinoManager.java:31
ai.picovoice.rhino.RhinoManager.RhinoManager
RhinoManager(Rhino rhino, RhinoManagerCallback callback, RhinoManagerErrorCallback errorCallback)
Definition: RhinoManager.java:43
ai.picovoice.rhino.RhinoManager.Builder
Definition: RhinoManager.java:161
ai.picovoice.rhino.RhinoManager.Builder.accessKey
String accessKey
Definition: RhinoManager.java:163
ai.picovoice.rhino.RhinoManager.Builder.setSensitivity
RhinoManager.Builder setSensitivity(float sensitivity)
Definition: RhinoManager.java:185
ai.picovoice.rhino.RhinoManager.Builder.contextPath
String contextPath
Definition: RhinoManager.java:165
ai.picovoice.rhino.RhinoInference
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:15
ai.picovoice.rhino.RhinoManagerCallback.invoke
void invoke(RhinoInference inference)
ai.picovoice.rhino.Rhino.getVersion
native String getVersion()
ai.picovoice.rhino.RhinoManager.Builder.setErrorCallback
RhinoManager.Builder setErrorCallback(RhinoManagerErrorCallback errorCallback)
Definition: RhinoManager.java:195
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: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:226
ai.picovoice.rhino.RhinoManagerErrorCallback
Definition: RhinoManagerErrorCallback.java:15
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.RhinoManager.Builder.sensitivity
float sensitivity
Definition: RhinoManager.java:166
ai.picovoice.rhino.Rhino.getInference
RhinoInference getInference()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:120
ai.picovoice.rhino.RhinoManager.Builder.modelPath
String modelPath
Definition: RhinoManager.java:164
ai.picovoice.rhino.RhinoManager.Builder.setModelPath
RhinoManager.Builder setModelPath(String modelPath)
Definition: RhinoManager.java:175
ai.picovoice.rhino.RhinoManager.Builder.build
RhinoManager build(Context context, RhinoManagerCallback callback)
Definition: RhinoManager.java:208
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.RhinoManager.Builder.requireEndpoint
boolean requireEndpoint
Definition: RhinoManager.java:167
ai.picovoice.rhino.Rhino.getFrameLength
native int getFrameLength()
ai.picovoice.rhino.Rhino.delete
void delete()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:71
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