expo-speech-recognition 0.2.24 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +94 -69
- package/android/src/main/java/expo/modules/speechrecognition/ExpoSpeechRecognitionModule.kt +44 -0
- package/android/src/main/java/expo/modules/speechrecognition/ExpoSpeechService.kt +37 -3
- package/build/ExpoSpeechRecognitionModule.types.d.ts +59 -3
- package/build/ExpoSpeechRecognitionModule.types.d.ts.map +1 -1
- package/build/ExpoSpeechRecognitionModule.types.js.map +1 -1
- package/build/ExpoSpeechRecognitionModule.web.d.ts.map +1 -1
- package/build/ExpoSpeechRecognitionModule.web.js +85 -14
- package/build/ExpoSpeechRecognitionModule.web.js.map +1 -1
- package/build/ExpoWebSpeechRecognition.d.ts.map +1 -1
- package/build/ExpoWebSpeechRecognition.js +0 -2
- package/build/ExpoWebSpeechRecognition.js.map +1 -1
- package/build/index.d.ts +2 -3
- package/build/index.d.ts.map +1 -1
- package/build/index.js +3 -3
- package/build/index.js.map +1 -1
- package/ios/ExpoSpeechRecognitionModule.swift +57 -3
- package/ios/ExpoSpeechRecognizer.swift +182 -112
- package/ios/MicrophoneRequester.swift +33 -0
- package/ios/SpeechRecognizerRequester.swift +34 -0
- package/package.json +6 -1
- package/src/ExpoSpeechRecognitionModule.types.ts +60 -3
- package/src/ExpoSpeechRecognitionModule.web.ts +102 -16
- package/src/ExpoWebSpeechRecognition.ts +1 -2
- package/src/index.ts +4 -14
|
@@ -2,6 +2,7 @@ import type { PermissionResponse } from "expo-modules-core";
|
|
|
2
2
|
import type {
|
|
3
3
|
ExpoSpeechRecognitionModuleType,
|
|
4
4
|
ExpoSpeechRecognitionNativeEventMap,
|
|
5
|
+
ExpoSpeechRecognitionResultSegment,
|
|
5
6
|
} from "./ExpoSpeechRecognitionModule.types";
|
|
6
7
|
|
|
7
8
|
let _speechRecognitionRef: SpeechRecognition | null = null;
|
|
@@ -51,15 +52,15 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
|
|
|
51
52
|
status: "granted",
|
|
52
53
|
} as PermissionResponse);
|
|
53
54
|
},
|
|
54
|
-
getPermissionsAsync
|
|
55
|
+
getPermissionsAsync() {
|
|
55
56
|
console.warn(
|
|
56
|
-
"getPermissionsAsync is not supported on web. Returning a
|
|
57
|
+
"getPermissionsAsync is not supported on web. Returning a granted permission response.",
|
|
57
58
|
);
|
|
58
59
|
return Promise.resolve({
|
|
59
|
-
granted:
|
|
60
|
+
granted: true,
|
|
60
61
|
canAskAgain: false,
|
|
61
62
|
expires: "never",
|
|
62
|
-
status: "
|
|
63
|
+
status: "granted",
|
|
63
64
|
} as PermissionResponse);
|
|
64
65
|
},
|
|
65
66
|
getSupportedLocales: async () => {
|
|
@@ -132,6 +133,50 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
|
|
|
132
133
|
|
|
133
134
|
return hasSpeechRecognitionAPI;
|
|
134
135
|
},
|
|
136
|
+
getMicrophonePermissionsAsync() {
|
|
137
|
+
console.warn(
|
|
138
|
+
"getMicrophonePermissionsAsync is not supported on web. Returning a granted permission response.",
|
|
139
|
+
);
|
|
140
|
+
return Promise.resolve({
|
|
141
|
+
granted: true,
|
|
142
|
+
canAskAgain: false,
|
|
143
|
+
expires: "never",
|
|
144
|
+
status: "granted",
|
|
145
|
+
} as PermissionResponse);
|
|
146
|
+
},
|
|
147
|
+
requestMicrophonePermissionsAsync() {
|
|
148
|
+
console.warn(
|
|
149
|
+
"requestMicrophonePermissionsAsync is not supported on web. Returning a granted permission response.",
|
|
150
|
+
);
|
|
151
|
+
return Promise.resolve({
|
|
152
|
+
granted: true,
|
|
153
|
+
canAskAgain: false,
|
|
154
|
+
expires: "never",
|
|
155
|
+
status: "granted",
|
|
156
|
+
} as PermissionResponse);
|
|
157
|
+
},
|
|
158
|
+
getSpeechRecognizerPermissionsAsync() {
|
|
159
|
+
console.warn(
|
|
160
|
+
"getSpeechRecognizerPermissionsAsync is not supported on web. Returning a granted permission response.",
|
|
161
|
+
);
|
|
162
|
+
return Promise.resolve({
|
|
163
|
+
granted: true,
|
|
164
|
+
canAskAgain: false,
|
|
165
|
+
expires: "never",
|
|
166
|
+
status: "granted",
|
|
167
|
+
} as PermissionResponse);
|
|
168
|
+
},
|
|
169
|
+
requestSpeechRecognizerPermissionsAsync() {
|
|
170
|
+
console.warn(
|
|
171
|
+
"requestSpeechRecognizerPermissionsAsync is not supported on web. Returning a granted permission response.",
|
|
172
|
+
);
|
|
173
|
+
return Promise.resolve({
|
|
174
|
+
granted: true,
|
|
175
|
+
canAskAgain: false,
|
|
176
|
+
expires: "never",
|
|
177
|
+
status: "granted",
|
|
178
|
+
} as PermissionResponse);
|
|
179
|
+
},
|
|
135
180
|
};
|
|
136
181
|
|
|
137
182
|
/**
|
|
@@ -147,22 +192,63 @@ const webToNativeEventMap: {
|
|
|
147
192
|
end: (ev) => null,
|
|
148
193
|
error: (ev) => ({ error: ev.error, message: ev.message }),
|
|
149
194
|
nomatch: (ev) => null,
|
|
150
|
-
result: (ev) => {
|
|
151
|
-
const
|
|
152
|
-
|
|
195
|
+
result: (ev): ExpoSpeechRecognitionNativeEventMap["result"] => {
|
|
196
|
+
const isFinal = Boolean(ev.results[ev.resultIndex]?.isFinal);
|
|
197
|
+
|
|
198
|
+
if (isFinal) {
|
|
199
|
+
const results: ExpoSpeechRecognitionNativeEventMap["result"]["results"] =
|
|
200
|
+
[];
|
|
201
|
+
|
|
202
|
+
for (let i = 0; i < ev.results[ev.resultIndex].length; i++) {
|
|
203
|
+
const result = ev.results[ev.resultIndex][i];
|
|
204
|
+
results.push({
|
|
205
|
+
transcript: result.transcript,
|
|
206
|
+
confidence: result.confidence,
|
|
207
|
+
segments: [],
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
isFinal: true,
|
|
212
|
+
results,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
153
215
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
216
|
+
// Interim results: Append to the transcript
|
|
217
|
+
let transcript = "";
|
|
218
|
+
const segments: ExpoSpeechRecognitionResultSegment[] = [];
|
|
219
|
+
|
|
220
|
+
for (let i = ev.resultIndex; i < ev.results.length; i++) {
|
|
221
|
+
const resultList = ev.results[i];
|
|
222
|
+
|
|
223
|
+
for (let j = 0; j < resultList.length; j++) {
|
|
224
|
+
const result = resultList[j];
|
|
225
|
+
if (!result) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
segments.push({
|
|
229
|
+
confidence: result.confidence,
|
|
230
|
+
segment: result.transcript,
|
|
231
|
+
startTimeMillis: 0,
|
|
232
|
+
endTimeMillis: 0,
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
if (!isFinal) {
|
|
236
|
+
transcript += result.transcript;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
161
239
|
}
|
|
162
240
|
|
|
163
241
|
return {
|
|
164
|
-
isFinal:
|
|
165
|
-
results:
|
|
242
|
+
isFinal: false,
|
|
243
|
+
results: [
|
|
244
|
+
{
|
|
245
|
+
transcript,
|
|
246
|
+
confidence:
|
|
247
|
+
segments.reduce((acc, curr) => acc + curr.confidence, 0) /
|
|
248
|
+
segments.length,
|
|
249
|
+
segments,
|
|
250
|
+
},
|
|
251
|
+
],
|
|
166
252
|
};
|
|
167
253
|
},
|
|
168
254
|
soundstart: (ev) => null,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { Subscription } from "expo-modules-core";
|
|
2
|
-
|
|
3
1
|
// Import the native module. On web, it will be resolved to ExpoSpeechRecognition.web.ts
|
|
4
2
|
// and on native platforms to ExpoSpeechRecognition.ts
|
|
3
|
+
import type { Subscription } from "expo-modules-core";
|
|
5
4
|
import {
|
|
6
5
|
ExpoSpeechRecognitionModule,
|
|
7
6
|
ExpoSpeechRecognitionModuleEmitter,
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ExpoSpeechRecognitionModule,
|
|
3
|
-
ExpoSpeechRecognitionModuleEmitter,
|
|
4
|
-
} from "./ExpoSpeechRecognitionModule";
|
|
1
|
+
import { ExpoSpeechRecognitionModule } from "./ExpoSpeechRecognitionModule";
|
|
5
2
|
import type { ExpoSpeechRecognitionNativeEventMap } from "./ExpoSpeechRecognitionModule.types";
|
|
6
3
|
|
|
7
4
|
// Export the SpeechRecognition APIs
|
|
@@ -12,10 +9,7 @@ export {
|
|
|
12
9
|
} from "./ExpoWebSpeechRecognition";
|
|
13
10
|
|
|
14
11
|
// Native module
|
|
15
|
-
export {
|
|
16
|
-
ExpoSpeechRecognitionModule,
|
|
17
|
-
ExpoSpeechRecognitionModuleEmitter,
|
|
18
|
-
} from "./ExpoSpeechRecognitionModule";
|
|
12
|
+
export { ExpoSpeechRecognitionModule } from "./ExpoSpeechRecognitionModule";
|
|
19
13
|
|
|
20
14
|
// Hooks
|
|
21
15
|
export { useSpeechRecognitionEvent } from "./useSpeechRecognitionEvent";
|
|
@@ -63,12 +57,8 @@ export const getDefaultRecognitionService =
|
|
|
63
57
|
export const getAssistantService =
|
|
64
58
|
ExpoSpeechRecognitionModule.getAssistantService;
|
|
65
59
|
|
|
66
|
-
export const addSpeechRecognitionListener =
|
|
67
|
-
|
|
68
|
-
>(
|
|
69
|
-
eventName: T,
|
|
70
|
-
listener: (ev: ExpoSpeechRecognitionNativeEventMap[T]) => void,
|
|
71
|
-
) => ExpoSpeechRecognitionModuleEmitter.addListener(eventName, listener);
|
|
60
|
+
export const addSpeechRecognitionListener =
|
|
61
|
+
ExpoSpeechRecognitionModule.addListener;
|
|
72
62
|
|
|
73
63
|
export {
|
|
74
64
|
type ExpoSpeechRecognitionOptions,
|