PorcupineManager.java
Go to the documentation of this file.
1 /*
2  Copyright 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.porcupine;
14 
15 
16 import android.content.Context;
17 import android.media.AudioFormat;
18 import android.media.AudioRecord;
19 import android.media.MediaRecorder;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Process;
23 import android.util.Log;
24 
25 import java.util.concurrent.Callable;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.atomic.AtomicBoolean;
28 
35 public class PorcupineManager {
37  private final Porcupine porcupine;
40 
51 
52 
53  this.porcupine = porcupine;
54  this.callback = callback;
55  this.errorCallback = errorCallback;
57  }
58 
62  public void delete() {
63  porcupine.delete();
64  }
65 
70  public void start() {
71  microphoneReader.start();
72  }
73 
80  public void stop() throws PorcupineException {
81  try {
83  } catch (InterruptedException e) {
84  throw new PorcupineException(e);
85  }
86  }
87 
91  public static class Builder {
92 
93  private String accessKey = null;
94  private String modelPath = null;
95  private String[] keywordPaths = null;
96  private Porcupine.BuiltInKeyword[] keywords = null;
97  private float[] sensitivities = null;
99 
101  this.accessKey = accessKey;
102  return this;
103  }
104 
106  this.modelPath = modelPath;
107  return this;
108  }
109 
111  this.keywordPaths = keywordPaths;
112  return this;
113  }
114 
116  this.keywordPaths = new String[]{keywordPaths};
117  return this;
118  }
119 
121  this.keywords = keywords;
122  return this;
123  }
124 
126  this.keywords = new Porcupine.BuiltInKeyword[]{keyword};
127  return this;
128  }
129 
131  this.sensitivities = sensitivities;
132  return this;
133  }
134 
135  public PorcupineManager.Builder setSensitivity(float sensitivity) {
136  this.sensitivities = new float[]{sensitivity};
137  return this;
138  }
139 
141  this.errorCallback = errorCallback;
142  return this;
143  }
144 
154 
161  .build(context);
163  }
164  }
165 
166  private class MicrophoneReader {
167  private final AtomicBoolean started = new AtomicBoolean(false);
168  private final AtomicBoolean stop = new AtomicBoolean(false);
169  private final AtomicBoolean stopped = new AtomicBoolean(false);
170 
171  private final Handler callbackHandler = new Handler(Looper.getMainLooper());
172 
173  void start() {
174 
175  if (started.get()) {
176  return;
177  }
178 
179  started.set(true);
180 
181  Executors.newSingleThreadExecutor().submit(new Callable<Void>() {
182  @Override
183  public Void call() throws PorcupineException {
184  android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
185  read();
186  return null;
187  }
188  });
189  }
190 
191  void stop() throws InterruptedException {
192  if (!started.get()) {
193  return;
194  }
195 
196  stop.set(true);
197 
198  while (!stopped.get()) {
199  Thread.sleep(10);
200  }
201 
202  started.set(false);
203  stop.set(false);
204  stopped.set(false);
205  }
206 
207  private void read() {
208  final int minBufferSize = AudioRecord.getMinBufferSize(
210  AudioFormat.CHANNEL_IN_MONO,
211  AudioFormat.ENCODING_PCM_16BIT);
212  final int bufferSize = Math.max(porcupine.getSampleRate() / 2, minBufferSize);
213 
214  AudioRecord audioRecord = null;
215 
216  short[] buffer = new short[porcupine.getFrameLength()];
217 
218  try {
219  audioRecord = new AudioRecord(
220  MediaRecorder.AudioSource.MIC,
222  AudioFormat.CHANNEL_IN_MONO,
223  AudioFormat.ENCODING_PCM_16BIT,
224  bufferSize);
225  audioRecord.startRecording();
226 
227  while (!stop.get()) {
228  if (audioRecord.read(buffer, 0, buffer.length) == buffer.length) {
229  try {
230  final int keywordIndex = porcupine.process(buffer);
231  if (keywordIndex >= 0) {
232  callbackHandler.post(new Runnable() {
233  @Override
234  public void run() {
235  callback.invoke(keywordIndex);
236  }
237  });
238  }
239  } catch (final PorcupineException e) {
240  if (errorCallback != null) {
241  callbackHandler.post(new Runnable() {
242  @Override
243  public void run() {
245  }
246  });
247  } else {
248  Log.e("PorcupineManager", e.toString());
249  }
250  }
251  }
252  }
253 
254  audioRecord.stop();
255  } catch (IllegalArgumentException | IllegalStateException e) {
256  if (errorCallback != null) {
257  callbackHandler.post(new Runnable() {
258  @Override
259  public void run() {
261  }
262  });
263  } else {
264  Log.e("PorcupineManager", e.toString());
265  }
266  } finally {
267  if (audioRecord != null) {
268  audioRecord.release();
269  }
270 
271  stopped.set(true);
272  }
273  }
274  }
275 }
ai.picovoice.porcupine.PorcupineManager.Builder.setKeywords
PorcupineManager.Builder setKeywords(Porcupine.BuiltInKeyword[] keywords)
Definition: PorcupineManager.java:120
call
bool call(const std::string &service_name, MReq &req, MRes &res)
run
void run(class_loader::ClassLoader *loader)
context
ma_context context
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_deviceio/ma_test_deviceio.c:56
ai.picovoice.porcupine.PorcupineManagerCallback.invoke
void invoke(int keywordIndex)
ai.picovoice.porcupine.PorcupineManager.start
void start()
Definition: PorcupineManager.java:70
ai.picovoice.porcupine.PorcupineManager.errorCallback
final PorcupineManagerErrorCallback errorCallback
Definition: PorcupineManager.java:39
ai.picovoice.porcupine.PorcupineManager.Builder.setKeyword
PorcupineManager.Builder setKeyword(Porcupine.BuiltInKeyword keyword)
Definition: PorcupineManager.java:125
ai.picovoice.porcupine.PorcupineManager.Builder.setAccessKey
PorcupineManager.Builder setAccessKey(String accessKey)
Definition: PorcupineManager.java:100
ai.picovoice.porcupine.PorcupineManager.MicrophoneReader.started
final AtomicBoolean started
Definition: PorcupineManager.java:167
ai.picovoice.porcupine.PorcupineManager.Builder
Definition: PorcupineManager.java:91
ai.picovoice.porcupine.Porcupine.Builder
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:165
ai.picovoice.porcupine.Porcupine.Builder.setKeywordPaths
Builder setKeywordPaths(String[] keywordPaths)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:183
ai.picovoice.porcupine.Porcupine.delete
void delete()
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:75
ai.picovoice.porcupine.PorcupineManager.MicrophoneReader.callbackHandler
final Handler callbackHandler
Definition: PorcupineManager.java:171
ai.picovoice.porcupine.PorcupineManager.Builder.errorCallback
PorcupineManagerErrorCallback errorCallback
Definition: PorcupineManager.java:98
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.porcupine.PorcupineManager.Builder.keywordPaths
String[] keywordPaths
Definition: PorcupineManager.java:95
ai.picovoice.porcupine.PorcupineManager.Builder.setSensitivities
PorcupineManager.Builder setSensitivities(float[] sensitivities)
Definition: PorcupineManager.java:130
ai.picovoice.porcupine.PorcupineManager.Builder.modelPath
String modelPath
Definition: PorcupineManager.java:94
ai.picovoice.porcupine.PorcupineManager.Builder.setKeywordPaths
PorcupineManager.Builder setKeywordPaths(String[] keywordPaths)
Definition: PorcupineManager.java:110
ai.picovoice.porcupine.PorcupineManager.MicrophoneReader.read
void read()
Definition: PorcupineManager.java:207
ai.picovoice.porcupine.PorcupineManager.microphoneReader
final MicrophoneReader microphoneReader
Definition: PorcupineManager.java:36
ai.picovoice.porcupine.PorcupineException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineException.java:13
ai.picovoice.porcupine.PorcupineManager.porcupine
final Porcupine porcupine
Definition: PorcupineManager.java:37
ai.picovoice.porcupine.PorcupineManager.Builder.setErrorCallback
PorcupineManager.Builder setErrorCallback(PorcupineManagerErrorCallback errorCallback)
Definition: PorcupineManager.java:140
ai.picovoice.porcupine.Porcupine.getFrameLength
native int getFrameLength()
ai.picovoice.porcupine.PorcupineManager.MicrophoneReader
Definition: PorcupineManager.java:166
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.porcupine.PorcupineManager.callback
final PorcupineManagerCallback callback
Definition: PorcupineManager.java:38
ai.picovoice.porcupine.PorcupineManagerErrorCallback.invoke
void invoke(PorcupineException error)
ai.picovoice.porcupine.PorcupineManager.Builder.keywords
Porcupine.BuiltInKeyword[] keywords
Definition: PorcupineManager.java:96
ai.picovoice.porcupine.Porcupine.Builder.setKeywords
Builder setKeywords(BuiltInKeyword[] keywords)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:193
ai.picovoice.porcupine.PorcupineManager.Builder.setSensitivity
PorcupineManager.Builder setSensitivity(float sensitivity)
Definition: PorcupineManager.java:135
ai.picovoice.porcupine.PorcupineManager.Builder.build
PorcupineManager build(Context context, PorcupineManagerCallback callback)
Definition: PorcupineManager.java:153
ai.picovoice.porcupine.PorcupineManager.MicrophoneReader.stopped
final AtomicBoolean stopped
Definition: PorcupineManager.java:169
ai.picovoice.porcupine.PorcupineManagerCallback
Definition: PorcupineManagerCallback.java:15
ai.picovoice.porcupine.PorcupineManager.Builder.setModelPath
PorcupineManager.Builder setModelPath(String modelPath)
Definition: PorcupineManager.java:105
ai.picovoice.porcupine.PorcupineManager.Builder.setKeywordPath
PorcupineManager.Builder setKeywordPath(String keywordPaths)
Definition: PorcupineManager.java:115
ai.picovoice.porcupine.PorcupineManagerErrorCallback
Definition: PorcupineManagerErrorCallback.java:15
ai.picovoice.porcupine.Porcupine.process
int process(short[] pcm)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:94
ai.picovoice.porcupine.PorcupineManager.Builder.sensitivities
float[] sensitivities
Definition: PorcupineManager.java:97
ai.picovoice.porcupine.PorcupineManager.Builder.accessKey
String accessKey
Definition: PorcupineManager.java:93
ai.picovoice.porcupine.Porcupine.getSampleRate
native int getSampleRate()
ai.picovoice.porcupine.PorcupineManager.MicrophoneReader.stop
final AtomicBoolean stop
Definition: PorcupineManager.java:168
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.PorcupineManager
Definition: PorcupineManager.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.PorcupineManager.stop
void stop()
Definition: PorcupineManager.java:80
ai.picovoice.porcupine.PorcupineManager.PorcupineManager
PorcupineManager(Porcupine porcupine, PorcupineManagerCallback callback, PorcupineManagerErrorCallback errorCallback)
Definition: PorcupineManager.java:48


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