PorcupinePlugin.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 located in the "LICENSE"
5 // file accompanying this source.
6 //
7 // Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8 // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 // specific language governing permissions and limitations under the License.
10 //
11 
12 package ai.picovoice.flutter.porcupine;
13 
14 import android.content.Context;
15 
16 import androidx.annotation.NonNull;
17 
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Map;
21 
22 import io.flutter.embedding.engine.plugins.FlutterPlugin;
23 import io.flutter.plugin.common.MethodCall;
24 import io.flutter.plugin.common.MethodChannel;
25 import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
26 import io.flutter.plugin.common.MethodChannel.Result;
27 
28 import ai.picovoice.porcupine.*;
29 
30 public class PorcupinePlugin implements FlutterPlugin, MethodCallHandler {
31 
32  private enum Method {
36  DELETE
37  }
38 
39  private Context flutterContext;
40  private MethodChannel channel;
41  private final Map<String, Porcupine> porcupinePool = new HashMap<>();
42 
43  @Override
44  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
45  flutterContext = flutterPluginBinding.getApplicationContext();
46  channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "porcupine");
47  channel.setMethodCallHandler(this);
48  }
49 
50  @Override
51  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
52  Method method;
53  try {
54  method = Method.valueOf(call.method.toUpperCase());
55  } catch (IllegalArgumentException e) {
56  result.error(
57  PorcupineRuntimeException.class.getSimpleName(),
58  String.format("Porcupine method '%s' is not a valid function", call.method),
59  null);
60  return;
61  }
62 
63  switch (method) {
64  case FROM_BUILTIN_KEYWORDS:
65  try {
66  String accessKey = call.argument("accessKey");
67  String modelPath = call.argument("modelPath");
68  ArrayList<String> keywordsList = call.argument("keywords");
69  ArrayList<Double> sensitivitiesList = call.argument("sensitivities");
70 
72  if (keywordsList != null) {
73  keywords = new Porcupine.BuiltInKeyword[keywordsList.size()];
74  for (int i = 0; i < keywordsList.size(); i++) {
75  try {
76  String keyword = keywordsList.get(i).replace(' ', '_').toUpperCase();
77  keywords[i] = Porcupine.BuiltInKeyword.valueOf(keyword);
78  } catch (IllegalArgumentException e) {
79  result.error(
80  PorcupineInvalidArgumentException.class.getSimpleName(),
81  String.format("'%s' is not a built-in keyword", keywordsList.get(i)),
82  null);
83  return;
84  }
85  }
86  }
87 
88  float [] sensitivities = null;
89  if (sensitivitiesList != null) {
90  sensitivities = new float[sensitivitiesList.size()];
91  for (int i = 0; i < sensitivitiesList.size(); i++) {
92  sensitivities[i] = sensitivitiesList.get(i).floatValue();
93  }
94  }
95 
96  Porcupine porcupine = new Porcupine.Builder()
97  .setAccessKey(accessKey)
98  .setModelPath(modelPath)
100  .setSensitivities(sensitivities)
102 
103  porcupinePool.put(String.valueOf(System.identityHashCode(porcupine)), porcupine);
104 
105  Map<String, Object> param = new HashMap<>();
106  param.put("handle", String.valueOf(System.identityHashCode(porcupine)));
107  param.put("frameLength", porcupine.getFrameLength());
108  param.put("sampleRate", porcupine.getSampleRate());
109  param.put("version", porcupine.getVersion());
110 
111  result.success(param);
112  } catch (PorcupineException e) {
113  result.error(e.getClass().getSimpleName(), e.getMessage(), null);
114  } catch (Exception e) {
115  result.error(PorcupineException.class.getSimpleName(), e.getMessage(), null);
116  }
117  break;
118  case FROM_KEYWORD_PATHS:
119  try {
120  String accessKey = call.argument("accessKey");
121  String modelPath = call.argument("modelPath");
122  ArrayList<String> keywordPathsList = call.argument("keywordPaths");
123  ArrayList<Double> sensitivitiesList = call.argument("sensitivities");
124 
125  String[] keywordPaths = null;
126  if (keywordPathsList != null) {
127  keywordPaths = new String[keywordPathsList.size()];
128  for (int i = 0; i < keywordPathsList.size(); i++) {
129  keywordPaths[i] = keywordPathsList.get(i);
130  }
131  }
132 
133  float [] sensitivities = null;
134  if (sensitivitiesList != null) {
135  sensitivities = new float[sensitivitiesList.size()];
136  for (int i = 0; i < sensitivitiesList.size(); i++) {
137  sensitivities[i] = sensitivitiesList.get(i).floatValue();
138  }
139  }
140 
141  Porcupine porcupine = new Porcupine.Builder()
142  .setAccessKey(accessKey)
143  .setModelPath(modelPath)
144  .setKeywordPaths(keywordPaths)
145  .setSensitivities(sensitivities)
147 
148  porcupinePool.put(String.valueOf(System.identityHashCode(porcupine)), porcupine);
149 
150  Map<String, Object> param = new HashMap<>();
151  param.put("handle", String.valueOf(System.identityHashCode(porcupine)));
152  param.put("frameLength", porcupine.getFrameLength());
153  param.put("sampleRate", porcupine.getSampleRate());
154  param.put("version", porcupine.getVersion());
155 
156  result.success(param);
157  } catch (PorcupineException e) {
158  result.error(e.getClass().getSimpleName(), e.getMessage(), null);
159  } catch (Exception e) {
160  result.error(PorcupineException.class.getSimpleName(), e.getMessage(), null);
161  }
162  break;
163  case PROCESS:
164  try {
165  String handle = call.argument("handle");
166  ArrayList<Integer> pcmList = call.argument("frame");
167 
168  if (!porcupinePool.containsKey(handle)) {
169  result.error(
170  PorcupineInvalidArgumentException.class.getSimpleName(),
171  "Invalid Porcupine handle provided to native module",
172  null);
173  return;
174  }
175 
176  short[] pcm = null;
177  if (pcmList != null) {
178  pcm = new short[pcmList.size()];
179  for (int i = 0; i < pcmList.size(); i++) {
180  pcm[i] = pcmList.get(i).shortValue();
181  }
182  }
183 
184  Porcupine porcupine = porcupinePool.get(handle);
185  int keywordIndex = porcupine.process(pcm);
186  result.success(keywordIndex);
187  } catch (PorcupineException e) {
188  result.error(
189  e.getClass().getSimpleName(),
190  e.getMessage(),
191  null);
192  }
193  break;
194  case DELETE:
195  String handle = call.argument("handle");
196 
197  if (!porcupinePool.containsKey(handle)) {
198  result.error(
199  PorcupineInvalidArgumentException.class.getSimpleName(),
200  "Invalid Porcupine handle provided to native module.",
201  null);
202  return;
203  }
204 
205  Porcupine porcupine = porcupinePool.get(handle);
206  porcupine.delete();
207  porcupinePool.remove(handle);
208 
209  result.success(null);
210  break;
211  }
212  }
213 
214  @Override
215  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
216  channel.setMethodCallHandler(null);
217  }
218 }
ai.picovoice.porcupine.PorcupineRuntimeException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineRuntimeException.java:13
ai.picovoice.porcupine
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineActivationException.java:11
call
bool call(const std::string &service_name, MReq &req, MRes &res)
ai.picovoice.flutter.porcupine.PorcupinePlugin.Method.FROM_KEYWORD_PATHS
FROM_KEYWORD_PATHS
Definition: PorcupinePlugin.java:34
ai.picovoice.flutter.porcupine.PorcupinePlugin.flutterContext
Context flutterContext
Definition: PorcupinePlugin.java:39
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.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.flutter.porcupine.PorcupinePlugin.onMethodCall
void onMethodCall(@NonNull MethodCall call, @NonNull Result result)
Definition: PorcupinePlugin.java:51
ai.picovoice.flutter.porcupine.PorcupinePlugin.onAttachedToEngine
void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding)
Definition: PorcupinePlugin.java:44
ai.picovoice.flutter.porcupine.PorcupinePlugin
Definition: PorcupinePlugin.java:30
ai.picovoice.porcupine.PorcupineException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineException.java:13
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.porcupine.Porcupine.Builder.setKeywords
Builder setKeywords(BuiltInKeyword[] keywords)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:193
ai.picovoice.flutter.porcupine.PorcupinePlugin.channel
MethodChannel channel
Definition: PorcupinePlugin.java:40
ai.picovoice.flutter.porcupine.PorcupinePlugin.onDetachedFromEngine
void onDetachedFromEngine(@NonNull FlutterPluginBinding binding)
Definition: PorcupinePlugin.java:215
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.flutter.porcupine.PorcupinePlugin.porcupinePool
final Map< String, Porcupine > porcupinePool
Definition: PorcupinePlugin.java:41
ai.picovoice.porcupine.Porcupine.getSampleRate
native int getSampleRate()
ai.picovoice.flutter.porcupine.PorcupinePlugin.Method
Definition: PorcupinePlugin.java:32
ai.picovoice.porcupine.Porcupine.Builder.setAccessKey
Builder setAccessKey(String accessKey)
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:173
param
T param(const std::string &param_name, const T &default_val)
ai.picovoice.porcupine.Porcupine
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:35
ai.picovoice.flutter.porcupine.PorcupinePlugin.Method.PROCESS
PROCESS
Definition: PorcupinePlugin.java:35
ai.picovoice.flutter.porcupine.PorcupinePlugin.Method.FROM_BUILTIN_KEYWORDS
FROM_BUILTIN_KEYWORDS
Definition: PorcupinePlugin.java:33
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


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