12 package ai.picovoice.flutter.porcupine;
14 import android.content.Context;
16 import androidx.annotation.NonNull;
18 import java.util.ArrayList;
19 import java.util.HashMap;
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;
46 channel =
new MethodChannel(flutterPluginBinding.getBinaryMessenger(),
"porcupine");
47 channel.setMethodCallHandler(
this);
51 public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
54 method =
Method.valueOf(
call.method.toUpperCase());
55 }
catch (IllegalArgumentException e) {
58 String.format(
"Porcupine method '%s' is not a valid function",
call.method),
64 case FROM_BUILTIN_KEYWORDS:
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");
72 if (keywordsList !=
null) {
74 for (
int i = 0; i < keywordsList.size(); i++) {
76 String keyword = keywordsList.get(i).replace(
' ',
'_').toUpperCase();
78 }
catch (IllegalArgumentException e) {
81 String.format(
"'%s' is not a built-in keyword", keywordsList.get(i)),
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();
103 porcupinePool.put(String.valueOf(System.identityHashCode(porcupine)), porcupine);
105 Map<String, Object>
param =
new HashMap<>();
106 param.put(
"handle", String.valueOf(System.identityHashCode(porcupine)));
111 result.success(
param);
113 result.error(e.getClass().getSimpleName(), e.getMessage(),
null);
114 }
catch (Exception e) {
118 case FROM_KEYWORD_PATHS:
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");
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);
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();
148 porcupinePool.put(String.valueOf(System.identityHashCode(porcupine)), porcupine);
150 Map<String, Object>
param =
new HashMap<>();
151 param.put(
"handle", String.valueOf(System.identityHashCode(porcupine)));
156 result.success(
param);
158 result.error(e.getClass().getSimpleName(), e.getMessage(),
null);
159 }
catch (Exception e) {
165 String handle =
call.argument(
"handle");
166 ArrayList<Integer> pcmList =
call.argument(
"frame");
171 "Invalid Porcupine handle provided to native module",
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();
185 int keywordIndex = porcupine.
process(pcm);
186 result.success(keywordIndex);
189 e.getClass().getSimpleName(),
195 String handle =
call.argument(
"handle");
200 "Invalid Porcupine handle provided to native module.",
209 result.success(
null);
216 channel.setMethodCallHandler(
null);