@gmessier/nitro-speech 0.3.3 → 0.4.1
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/LICENSE +21 -0
- package/README.md +176 -148
- package/android/build.gradle +0 -1
- package/android/src/main/cpp/cpp-adapter.cpp +5 -1
- package/android/src/main/java/com/margelo/nitro/nitrospeech/HybridNitroSpeech.kt +2 -0
- package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/AutoStopper.kt +82 -18
- package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/HybridRecognizer.kt +118 -30
- package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/Logger.kt +16 -0
- package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/RecognitionListenerSession.kt +35 -24
- package/ios/{BufferUtil.swift → Audio/AudioBufferConverter.swift} +3 -34
- package/ios/Audio/AudioLevelTracker.swift +60 -0
- package/ios/Coordinator.swift +105 -0
- package/ios/Engines/AnalyzerEngine.swift +241 -0
- package/ios/Engines/DictationRuntime.swift +67 -0
- package/ios/Engines/RecognizerEngine.swift +315 -0
- package/ios/Engines/SFSpeechEngine.swift +119 -0
- package/ios/Engines/SpeechRuntime.swift +58 -0
- package/ios/Engines/TranscriberRuntimeProtocol.swift +21 -0
- package/ios/HybridNitroSpeech.swift +1 -10
- package/ios/HybridRecognizer.swift +142 -191
- package/ios/LocaleManager.swift +73 -0
- package/ios/{AppStateObserver.swift → Shared/AppStateObserver.swift} +1 -2
- package/ios/Shared/AutoStopper.swift +147 -0
- package/ios/Shared/HapticImpact.swift +24 -0
- package/ios/Shared/Log.swift +41 -0
- package/ios/Shared/Permissions.swift +59 -0
- package/ios/Shared/Utils.swift +58 -0
- package/lib/NitroSpeech.d.ts +2 -0
- package/lib/NitroSpeech.js +2 -0
- package/lib/Recognizer/RecognizerRef.d.ts +7 -0
- package/lib/Recognizer/RecognizerRef.js +16 -0
- package/lib/Recognizer/SpeechRecognizer.d.ts +8 -0
- package/lib/Recognizer/SpeechRecognizer.js +9 -0
- package/lib/Recognizer/methods.d.ts +9 -0
- package/lib/Recognizer/methods.js +33 -0
- package/lib/Recognizer/types.d.ts +6 -0
- package/lib/Recognizer/types.js +1 -0
- package/lib/Recognizer/useRecognizer.d.ts +16 -0
- package/lib/Recognizer/useRecognizer.js +71 -0
- package/lib/Recognizer/useRecognizerIsActive.d.ts +25 -0
- package/lib/Recognizer/useRecognizerIsActive.js +40 -0
- package/lib/Recognizer/useVoiceInputVolume.d.ts +25 -0
- package/lib/Recognizer/useVoiceInputVolume.js +52 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +7 -0
- package/lib/specs/NitroSpeech.nitro.d.ts +8 -0
- package/lib/specs/NitroSpeech.nitro.js +1 -0
- package/lib/specs/Recognizer.nitro.d.ts +97 -0
- package/lib/specs/Recognizer.nitro.js +1 -0
- package/lib/specs/SpeechRecognitionConfig.d.ts +162 -0
- package/lib/specs/SpeechRecognitionConfig.js +1 -0
- package/lib/specs/VolumeChangeEvent.d.ts +31 -0
- package/lib/specs/VolumeChangeEvent.js +1 -0
- package/nitro.json +0 -4
- package/nitrogen/generated/android/NitroSpeech+autolinking.cmake +2 -2
- package/nitrogen/generated/android/NitroSpeechOnLoad.cpp +4 -2
- package/nitrogen/generated/android/c++/JFunc_void_VolumeChangeEvent.hpp +78 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_std__string_.hpp +14 -14
- package/nitrogen/generated/android/c++/JHybridRecognizerSpec.cpp +73 -19
- package/nitrogen/generated/android/c++/JHybridRecognizerSpec.hpp +8 -4
- package/nitrogen/generated/android/c++/JIosPreset.hpp +58 -0
- package/nitrogen/generated/android/c++/JMutableSpeechRecognitionConfig.hpp +79 -0
- package/nitrogen/generated/android/c++/{JSpeechToTextParams.hpp → JSpeechRecognitionConfig.hpp} +48 -30
- package/nitrogen/generated/android/c++/JVolumeChangeEvent.hpp +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/Func_void_VolumeChangeEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/HybridRecognizerSpec.kt +22 -5
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/IosPreset.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/MutableSpeechRecognitionConfig.kt +76 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/SpeechRecognitionConfig.kt +121 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/VolumeChangeEvent.kt +61 -0
- package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Bridge.cpp +46 -30
- package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Bridge.hpp +211 -69
- package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Umbrella.hpp +13 -3
- package/nitrogen/generated/ios/c++/HybridRecognizerSpecSwift.hpp +49 -9
- package/nitrogen/generated/ios/swift/Func_void_VolumeChangeEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridRecognizerSpec.swift +7 -3
- package/nitrogen/generated/ios/swift/HybridRecognizerSpec_cxx.swift +78 -18
- package/nitrogen/generated/ios/swift/IosPreset.swift +40 -0
- package/nitrogen/generated/ios/swift/MutableSpeechRecognitionConfig.swift +118 -0
- package/nitrogen/generated/ios/swift/{SpeechToTextParams.swift → SpeechRecognitionConfig.swift} +108 -43
- package/nitrogen/generated/ios/swift/VolumeChangeEvent.swift +52 -0
- package/nitrogen/generated/shared/c++/HybridRecognizerSpec.cpp +5 -1
- package/nitrogen/generated/shared/c++/HybridRecognizerSpec.hpp +18 -7
- package/nitrogen/generated/shared/c++/IosPreset.hpp +76 -0
- package/nitrogen/generated/shared/c++/MutableSpeechRecognitionConfig.hpp +105 -0
- package/nitrogen/generated/shared/c++/{SpeechToTextParams.hpp → SpeechRecognitionConfig.hpp} +39 -20
- package/nitrogen/generated/shared/c++/VolumeChangeEvent.hpp +91 -0
- package/package.json +15 -16
- package/src/NitroSpeech.ts +5 -0
- package/src/Recognizer/RecognizerRef.ts +27 -0
- package/src/Recognizer/SpeechRecognizer.ts +10 -0
- package/src/Recognizer/methods.ts +45 -0
- package/src/Recognizer/types.ts +34 -0
- package/src/Recognizer/useRecognizer.ts +87 -0
- package/src/Recognizer/useRecognizerIsActive.ts +49 -0
- package/src/Recognizer/useVoiceInputVolume.ts +65 -0
- package/src/index.ts +13 -182
- package/src/specs/NitroSpeech.nitro.ts +2 -163
- package/src/specs/Recognizer.nitro.ts +113 -0
- package/src/specs/SpeechRecognitionConfig.ts +167 -0
- package/src/specs/VolumeChangeEvent.ts +31 -0
- package/android/proguard-rules.pro +0 -1
- package/ios/AnylyzerTranscriber.swift +0 -331
- package/ios/AutoStopper.swift +0 -69
- package/ios/HapticImpact.swift +0 -32
- package/ios/LegacySpeechRecognizer.swift +0 -161
- package/lib/commonjs/index.js +0 -145
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/package.json +0 -1
- package/lib/commonjs/specs/NitroSpeech.nitro.js +0 -6
- package/lib/commonjs/specs/NitroSpeech.nitro.js.map +0 -1
- package/lib/module/index.js +0 -138
- package/lib/module/index.js.map +0 -1
- package/lib/module/package.json +0 -1
- package/lib/module/specs/NitroSpeech.nitro.js +0 -4
- package/lib/module/specs/NitroSpeech.nitro.js.map +0 -1
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/lib/typescript/index.d.ts +0 -50
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/specs/NitroSpeech.nitro.d.ts +0 -162
- package/lib/typescript/specs/NitroSpeech.nitro.d.ts.map +0 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/SpeechToTextParams.kt +0 -68
package/src/index.ts
CHANGED
|
@@ -1,182 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
15
|
-
export const RecognizerSession = NitroSpeech.recognizer
|
|
16
|
-
|
|
17
|
-
type RecognizerCallbacks = Pick<
|
|
18
|
-
RecognizerSpec,
|
|
19
|
-
| 'onReadyForSpeech'
|
|
20
|
-
| 'onRecordingStopped'
|
|
21
|
-
| 'onResult'
|
|
22
|
-
| 'onAutoFinishProgress'
|
|
23
|
-
| 'onError'
|
|
24
|
-
| 'onPermissionDenied'
|
|
25
|
-
| 'onVolumeChange'
|
|
26
|
-
>
|
|
27
|
-
|
|
28
|
-
type RecognizerHandlers = Pick<
|
|
29
|
-
RecognizerSpec,
|
|
30
|
-
| 'startListening'
|
|
31
|
-
| 'stopListening'
|
|
32
|
-
| 'addAutoFinishTime'
|
|
33
|
-
| 'updateAutoFinishTime'
|
|
34
|
-
| 'getIsActive'
|
|
35
|
-
>
|
|
36
|
-
|
|
37
|
-
const recognizerStartListening = (params: SpeechToTextParams) => {
|
|
38
|
-
RecognizerSession.startListening(params)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const recognizerStopListening = () => {
|
|
42
|
-
RecognizerSession.stopListening()
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const recognizerAddAutoFinishTime = (additionalTimeMs?: number) => {
|
|
46
|
-
RecognizerSession.addAutoFinishTime(additionalTimeMs)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const recognizerUpdateAutoFinishTime = (
|
|
50
|
-
newTimeMs: number,
|
|
51
|
-
withRefresh?: boolean
|
|
52
|
-
) => {
|
|
53
|
-
RecognizerSession.updateAutoFinishTime(newTimeMs, withRefresh)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const recognizerGetIsActive = () => {
|
|
57
|
-
return RecognizerSession.getIsActive()
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const subscribers = new Set<RecognizerSpec['onVolumeChange']>()
|
|
61
|
-
let currentVolume = 0
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Subscription to the voice input volume changes
|
|
65
|
-
*
|
|
66
|
-
* Updates with arbitrary frequency (many times per second) while audio recording is active.
|
|
67
|
-
*
|
|
68
|
-
* @returns The current voice input volume normalized to a range of 0 to 1.
|
|
69
|
-
*/
|
|
70
|
-
export const useVoiceInputVolume = () => {
|
|
71
|
-
return React.useSyncExternalStore(
|
|
72
|
-
(subscriber) => {
|
|
73
|
-
subscribers.add(subscriber)
|
|
74
|
-
return () => subscribers.delete(subscriber)
|
|
75
|
-
},
|
|
76
|
-
() => currentVolume
|
|
77
|
-
)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const handleVolumeChange: RecognizerSpec['onVolumeChange'] = (normVolume) => {
|
|
81
|
-
if (normVolume === currentVolume) return
|
|
82
|
-
currentVolume = normVolume
|
|
83
|
-
subscribers.forEach((subscriber) => subscriber?.(normVolume))
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Unsafe access to default Recognizer Session's volume change handler.
|
|
88
|
-
*
|
|
89
|
-
* In case you use static Recognizer Session:
|
|
90
|
-
*
|
|
91
|
-
* ```typescript
|
|
92
|
-
* import { unsafe_onVolumeChange } from '@gmessier/nitro-speech'
|
|
93
|
-
*
|
|
94
|
-
* RecognizerSession.onVolumeChange = unsafe_onVolumeChange
|
|
95
|
-
* ... // do something
|
|
96
|
-
* RecognizerSession.startListening({ locale: 'en-US' })
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
export const unsafe_onVolumeChange = handleVolumeChange
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Safe, lifecycle-aware hook to use the recognizer.
|
|
103
|
-
*
|
|
104
|
-
* @param callbacks - The callbacks to use for the recognizer.
|
|
105
|
-
* @param destroyDeps - The additional dependencies to use for the cleanup effect.
|
|
106
|
-
*
|
|
107
|
-
* Example: To cleanup when the screen is unfocused.
|
|
108
|
-
*
|
|
109
|
-
* ```typescript
|
|
110
|
-
* const isFocused = useIsFocused()
|
|
111
|
-
* useRecognizer({ ... }, [isFocused])
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
export const useRecognizer = (
|
|
115
|
-
callbacks: RecognizerCallbacks,
|
|
116
|
-
destroyDeps: React.DependencyList = []
|
|
117
|
-
): RecognizerHandlers => {
|
|
118
|
-
React.useEffect(() => {
|
|
119
|
-
if (callbacks.onVolumeChange) {
|
|
120
|
-
RecognizerSession.onVolumeChange = (normVolume: number) => {
|
|
121
|
-
callbacks.onVolumeChange?.(normVolume)
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
RecognizerSession.onVolumeChange = handleVolumeChange
|
|
125
|
-
}
|
|
126
|
-
RecognizerSession.onReadyForSpeech = () => {
|
|
127
|
-
callbacks.onReadyForSpeech?.()
|
|
128
|
-
}
|
|
129
|
-
RecognizerSession.onRecordingStopped = () => {
|
|
130
|
-
callbacks.onRecordingStopped?.()
|
|
131
|
-
}
|
|
132
|
-
RecognizerSession.onResult = (resultBatches: string[]) => {
|
|
133
|
-
callbacks.onResult?.(resultBatches)
|
|
134
|
-
}
|
|
135
|
-
RecognizerSession.onAutoFinishProgress = (timeLeftMs: number) => {
|
|
136
|
-
callbacks.onAutoFinishProgress?.(timeLeftMs)
|
|
137
|
-
}
|
|
138
|
-
RecognizerSession.onError = (message: string) => {
|
|
139
|
-
callbacks.onError?.(message)
|
|
140
|
-
}
|
|
141
|
-
RecognizerSession.onPermissionDenied = () => {
|
|
142
|
-
callbacks.onPermissionDenied?.()
|
|
143
|
-
}
|
|
144
|
-
return () => {
|
|
145
|
-
RecognizerSession.onReadyForSpeech = undefined
|
|
146
|
-
RecognizerSession.onRecordingStopped = undefined
|
|
147
|
-
RecognizerSession.onResult = undefined
|
|
148
|
-
RecognizerSession.onAutoFinishProgress = undefined
|
|
149
|
-
RecognizerSession.onError = undefined
|
|
150
|
-
RecognizerSession.onPermissionDenied = undefined
|
|
151
|
-
RecognizerSession.onVolumeChange = undefined
|
|
152
|
-
}
|
|
153
|
-
}, [callbacks])
|
|
154
|
-
|
|
155
|
-
React.useEffect(() => {
|
|
156
|
-
return () => {
|
|
157
|
-
RecognizerSession.stopListening()
|
|
158
|
-
}
|
|
159
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
160
|
-
}, [...destroyDeps])
|
|
161
|
-
|
|
162
|
-
return {
|
|
163
|
-
startListening: recognizerStartListening,
|
|
164
|
-
stopListening: recognizerStopListening,
|
|
165
|
-
addAutoFinishTime: recognizerAddAutoFinishTime,
|
|
166
|
-
updateAutoFinishTime: recognizerUpdateAutoFinishTime,
|
|
167
|
-
getIsActive: recognizerGetIsActive,
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Safe reference to the Recognizer methods.
|
|
173
|
-
*/
|
|
174
|
-
export const RecognizerRef: RecognizerHandlers = {
|
|
175
|
-
startListening: recognizerStartListening,
|
|
176
|
-
stopListening: recognizerStopListening,
|
|
177
|
-
addAutoFinishTime: recognizerAddAutoFinishTime,
|
|
178
|
-
updateAutoFinishTime: recognizerUpdateAutoFinishTime,
|
|
179
|
-
getIsActive: recognizerGetIsActive,
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export type { RecognizerCallbacks, RecognizerHandlers, SpeechToTextParams }
|
|
1
|
+
export * from './Recognizer/types'
|
|
2
|
+
export { useRecognizer } from './Recognizer/useRecognizer'
|
|
3
|
+
export {
|
|
4
|
+
useVoiceInputVolume,
|
|
5
|
+
speechRecognizerVolumeChangeHandler,
|
|
6
|
+
} from './Recognizer/useVoiceInputVolume'
|
|
7
|
+
export {
|
|
8
|
+
useRecognizerIsActive,
|
|
9
|
+
speechRecognizerActiveStateHandler,
|
|
10
|
+
} from './Recognizer/useRecognizerIsActive'
|
|
11
|
+
export { SpeechRecognizer } from './Recognizer/SpeechRecognizer'
|
|
12
|
+
export { RecognizerRef } from './Recognizer/RecognizerRef'
|
|
13
|
+
export { NitroSpeech } from './NitroSpeech'
|
|
@@ -1,166 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
interface ParamsAndroid {
|
|
4
|
-
/**
|
|
5
|
-
* Default - false
|
|
6
|
-
*
|
|
7
|
-
* Prefer quality over latency (may break autofinish timing, depends on engine)
|
|
8
|
-
*
|
|
9
|
-
* Android 13+
|
|
10
|
-
*/
|
|
11
|
-
androidFormattingPreferQuality?: boolean
|
|
12
|
-
/**
|
|
13
|
-
* Default - false
|
|
14
|
-
*
|
|
15
|
-
* Language model based on web search terms. (may not work on some devices)
|
|
16
|
-
*
|
|
17
|
-
* Default - free form model
|
|
18
|
-
*/
|
|
19
|
-
androidUseWebSearchModel?: boolean
|
|
20
|
-
/**
|
|
21
|
-
* Default - false.
|
|
22
|
-
*
|
|
23
|
-
* If required to handle batches non-default way.
|
|
24
|
-
*
|
|
25
|
-
* Will add lots of batches with empty or similar content to the result.
|
|
26
|
-
*/
|
|
27
|
-
androidDisableBatchHandling?: boolean
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface ParamsIOS {
|
|
31
|
-
/**
|
|
32
|
-
* Default - true
|
|
33
|
-
*
|
|
34
|
-
* Adds punctuation to speech recognition results
|
|
35
|
-
*
|
|
36
|
-
* iOS 16+
|
|
37
|
-
*/
|
|
38
|
-
iosAddPunctuation?: boolean
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
type HapticFeedbackStyle = 'light' | 'medium' | 'heavy' | 'none'
|
|
42
|
-
|
|
43
|
-
export interface SpeechToTextParams extends ParamsAndroid, ParamsIOS {
|
|
44
|
-
/**
|
|
45
|
-
* Default - "en-US"
|
|
46
|
-
*/
|
|
47
|
-
locale?: string
|
|
48
|
-
/**
|
|
49
|
-
* Default - 8s
|
|
50
|
-
*/
|
|
51
|
-
autoFinishRecognitionMs?: number
|
|
52
|
-
/**
|
|
53
|
-
* Default - false
|
|
54
|
-
*
|
|
55
|
-
* Lots of repeating words in a row can be annoying
|
|
56
|
-
*/
|
|
57
|
-
disableRepeatingFilter?: boolean
|
|
58
|
-
/**
|
|
59
|
-
* Default - empty array
|
|
60
|
-
*
|
|
61
|
-
* An array of strings that should be recognized, even if they are not in the system vocabulary.
|
|
62
|
-
*/
|
|
63
|
-
contextualStrings?: string[]
|
|
64
|
-
/**
|
|
65
|
-
* Default - "medium"
|
|
66
|
-
*
|
|
67
|
-
* Haptic feedback style when microphone starts recording.
|
|
68
|
-
*/
|
|
69
|
-
startHapticFeedbackStyle?: HapticFeedbackStyle
|
|
70
|
-
/**
|
|
71
|
-
* Default - "medium"
|
|
72
|
-
*
|
|
73
|
-
* Haptic feedback style when microphone stops recording.
|
|
74
|
-
*/
|
|
75
|
-
stopHapticFeedbackStyle?: HapticFeedbackStyle
|
|
76
|
-
/**
|
|
77
|
-
* Default - false
|
|
78
|
-
*
|
|
79
|
-
* Android 13+
|
|
80
|
-
*
|
|
81
|
-
* iOS 26+ (iOS <26: always `false`)
|
|
82
|
-
*/
|
|
83
|
-
maskOffensiveWords?: boolean
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export interface Recognizer extends HybridObject<{
|
|
87
|
-
ios: 'swift'
|
|
88
|
-
android: 'kotlin'
|
|
89
|
-
}> {
|
|
90
|
-
/**
|
|
91
|
-
* Tries to start the speech recognition.
|
|
92
|
-
*
|
|
93
|
-
* Not guaranteed to start the speech recognition.
|
|
94
|
-
*/
|
|
95
|
-
startListening(params: SpeechToTextParams): void
|
|
96
|
-
/**
|
|
97
|
-
* Stops the speech recognition. if not started, does nothing.
|
|
98
|
-
*
|
|
99
|
-
* Not a sync operation for android, delay about 250ms to polish the result.
|
|
100
|
-
*
|
|
101
|
-
* Use onRecordingStopped to handle the stop event.
|
|
102
|
-
*/
|
|
103
|
-
stopListening(): void
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Manually adds time to the auto finish progress.
|
|
107
|
-
*
|
|
108
|
-
* If you want to give the user ability to manually increase time before timer calls stop.
|
|
109
|
-
*
|
|
110
|
-
* @param additionalTimeMs - time in ms to add to the current auto finish timer. If not set, will reset the timer to the original auto finish time.
|
|
111
|
-
*/
|
|
112
|
-
addAutoFinishTime(additionalTimeMs?: number): void
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Updates the auto finish time.
|
|
116
|
-
*
|
|
117
|
-
* Applies changes only within the current recognition session.
|
|
118
|
-
*
|
|
119
|
-
* @param newTimeMs - new time in ms for the auto finish timer.
|
|
120
|
-
* @param withRefresh - if true, will refresh the auto finish progress.
|
|
121
|
-
*/
|
|
122
|
-
updateAutoFinishTime(newTimeMs: number, withRefresh?: boolean): void
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Returns true if the speech recognition is active.
|
|
126
|
-
*/
|
|
127
|
-
getIsActive(): boolean
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* The speech recognition has started.
|
|
131
|
-
*/
|
|
132
|
-
onReadyForSpeech?: () => void
|
|
133
|
-
/**
|
|
134
|
-
* Audio recording has stopped.
|
|
135
|
-
*/
|
|
136
|
-
onRecordingStopped?: () => void
|
|
137
|
-
/**
|
|
138
|
-
* Called each time either a new batch has been added or the last batch has been updated.
|
|
139
|
-
*/
|
|
140
|
-
onResult?: (resultBatches: string[]) => void
|
|
141
|
-
/**
|
|
142
|
-
* Called each second while auto finish is in progress.
|
|
143
|
-
*
|
|
144
|
-
* Time left in milliseconds. Always more than 1000ms.
|
|
145
|
-
*
|
|
146
|
-
* TODO: Add for android
|
|
147
|
-
*/
|
|
148
|
-
onAutoFinishProgress?: (timeLeftMs: number) => void
|
|
149
|
-
/**
|
|
150
|
-
* Error of the speech recognition.
|
|
151
|
-
*/
|
|
152
|
-
onError?: (message: string) => void
|
|
153
|
-
/**
|
|
154
|
-
* Permission to record audio has been denied.
|
|
155
|
-
*/
|
|
156
|
-
onPermissionDenied?: () => void
|
|
157
|
-
/**
|
|
158
|
-
* Called with arbitrary frequency (many times per second) while audio recording is active.
|
|
159
|
-
*
|
|
160
|
-
* Voice input volume normalized to a range of 0 to 1.
|
|
161
|
-
*/
|
|
162
|
-
onVolumeChange?: (normVolume: number) => void
|
|
163
|
-
}
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules'
|
|
2
|
+
import type { Recognizer } from './Recognizer.nitro'
|
|
164
3
|
|
|
165
4
|
export interface NitroSpeech extends HybridObject<{
|
|
166
5
|
ios: 'swift'
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules'
|
|
2
|
+
import type {
|
|
3
|
+
MutableSpeechRecognitionConfig,
|
|
4
|
+
SpeechRecognitionConfig,
|
|
5
|
+
} from './SpeechRecognitionConfig'
|
|
6
|
+
import type { VolumeChangeEvent } from './VolumeChangeEvent'
|
|
7
|
+
|
|
8
|
+
export interface Recognizer extends HybridObject<{
|
|
9
|
+
ios: 'swift'
|
|
10
|
+
android: 'kotlin'
|
|
11
|
+
}> {
|
|
12
|
+
/**
|
|
13
|
+
* Prepare the speech recognition engine and the model for the given parameters.
|
|
14
|
+
*
|
|
15
|
+
* Omit the {@linkcode Promise} result if want to run synchronously.
|
|
16
|
+
* {@linkcode startListening} will start and resolve it automatically.
|
|
17
|
+
* Only `await` if run beforehand and want to react on the success
|
|
18
|
+
*/
|
|
19
|
+
prewarm(defaultParams?: SpeechRecognitionConfig): Promise<void>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Try to start the speech recognition.
|
|
23
|
+
*
|
|
24
|
+
* Not guaranteed to start the speech recognition.
|
|
25
|
+
*
|
|
26
|
+
* On success - {@linkcode onReadyForSpeech} is called
|
|
27
|
+
*
|
|
28
|
+
* On failure - {@linkcode onError} is called
|
|
29
|
+
*/
|
|
30
|
+
startListening(params?: SpeechRecognitionConfig): void
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Stops the speech recognition. if not started, does nothing.
|
|
34
|
+
*
|
|
35
|
+
* Not a sync operation for android, delay about 250ms to polish the result.
|
|
36
|
+
*
|
|
37
|
+
* Use {@linkcode onRecordingStopped} to handle the stop event.
|
|
38
|
+
*/
|
|
39
|
+
stopListening(): void
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Reset the auto finish timer to current {@linkcode SpeechRecognitionConfig.autoFinishRecognitionMs}.
|
|
43
|
+
*/
|
|
44
|
+
resetAutoFinishTime(): void
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Add time to the auto finish timer once without changing the timer threshold.
|
|
48
|
+
*
|
|
49
|
+
* @param additionalTimeMs - time in ms to add to the current auto finish timer. If not set, will reset the timer to the original {@linkcode SpeechRecognitionConfig.autoFinishRecognitionMs}.
|
|
50
|
+
*/
|
|
51
|
+
addAutoFinishTime(additionalTimeMs?: number): void
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Applies changes only within the active recognition session.
|
|
55
|
+
*
|
|
56
|
+
* @param newConfig - new dynamic params for the speech recognition.
|
|
57
|
+
* @param resetAutoFinishTime - if true, will reset auto finish time to actual {@linkcode SpeechRecognitionConfig.autoFinishRecognitionMs}.
|
|
58
|
+
*/
|
|
59
|
+
updateConfig(
|
|
60
|
+
newConfig?: MutableSpeechRecognitionConfig,
|
|
61
|
+
resetAutoFinishTime?: boolean
|
|
62
|
+
): void
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Returns true if the speech recognition is active.
|
|
66
|
+
*/
|
|
67
|
+
getIsActive(): boolean
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Returns the current voice input volume.
|
|
71
|
+
*/
|
|
72
|
+
getVoiceInputVolume(): VolumeChangeEvent
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Returns a list of supported locales.
|
|
76
|
+
*
|
|
77
|
+
* @platform iOS only
|
|
78
|
+
*/
|
|
79
|
+
getSupportedLocalesIOS(): string[]
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The speech recognition session has started.
|
|
83
|
+
*/
|
|
84
|
+
onReadyForSpeech?: () => void
|
|
85
|
+
/**
|
|
86
|
+
* The speech recognition session has stopped.
|
|
87
|
+
*/
|
|
88
|
+
onRecordingStopped?: () => void
|
|
89
|
+
/**
|
|
90
|
+
* Fires each time either a new batch has been added or the last batch has been updated.
|
|
91
|
+
*/
|
|
92
|
+
onResult?: (resultBatches: string[]) => void
|
|
93
|
+
/**
|
|
94
|
+
* Fires every {@linkcode SpeechRecognitionConfig.autoFinishProgressIntervalMs} or 1000ms
|
|
95
|
+
*
|
|
96
|
+
* Time left in milliseconds until the timer stops.
|
|
97
|
+
*
|
|
98
|
+
* @note not implemented for Android yet.
|
|
99
|
+
*/
|
|
100
|
+
onAutoFinishProgress?: (timeLeftMs: number) => void
|
|
101
|
+
/**
|
|
102
|
+
* Error of the speech recognition.
|
|
103
|
+
*/
|
|
104
|
+
onError?: (message: string) => void
|
|
105
|
+
/**
|
|
106
|
+
* The permission to use the microphone or recognize speech has been denied.
|
|
107
|
+
*/
|
|
108
|
+
onPermissionDenied?: () => void
|
|
109
|
+
/**
|
|
110
|
+
* Fires with high and arbitrary frequency (many times per second) while audio recording is active.
|
|
111
|
+
*/
|
|
112
|
+
onVolumeChange?: (event: VolumeChangeEvent) => void
|
|
113
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
interface ParamsAndroid {
|
|
2
|
+
/**
|
|
3
|
+
* Prefer quality over latency (may break autofinish, depends on device)
|
|
4
|
+
*
|
|
5
|
+
* @since Android 13+
|
|
6
|
+
*
|
|
7
|
+
* @default false
|
|
8
|
+
*/
|
|
9
|
+
androidFormattingPreferQuality?: boolean
|
|
10
|
+
/**
|
|
11
|
+
* Language model based on web search terms. (may not work on some devices)
|
|
12
|
+
*
|
|
13
|
+
* Free form model by default
|
|
14
|
+
*
|
|
15
|
+
* @default false
|
|
16
|
+
*/
|
|
17
|
+
androidUseWebSearchModel?: boolean
|
|
18
|
+
/**
|
|
19
|
+
* Without batch handling the result will contain empty or same content batches.
|
|
20
|
+
*
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
androidDisableBatchHandling?: boolean
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type IosPreset = 'shortform' | 'general'
|
|
27
|
+
|
|
28
|
+
interface ParamsIOS {
|
|
29
|
+
/**
|
|
30
|
+
* Add punctuation to speech recognition results
|
|
31
|
+
*
|
|
32
|
+
* @since iOS 16.0+
|
|
33
|
+
*
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
iosAddPunctuation?: boolean
|
|
37
|
+
/**
|
|
38
|
+
* `"shortForm"` - for a short phrase or sentence, also disables punctuation
|
|
39
|
+
*
|
|
40
|
+
* `"general"` - for longer speeches, more accurate but delayed response
|
|
41
|
+
*
|
|
42
|
+
* @since iOS 26.0+
|
|
43
|
+
*
|
|
44
|
+
* @default "general"
|
|
45
|
+
*/
|
|
46
|
+
iosPreset?: IosPreset
|
|
47
|
+
/**
|
|
48
|
+
* A processing hint indicating that the audio is from a speaker with a heavy accent, lisp, or other confounding factor.
|
|
49
|
+
*
|
|
50
|
+
* @since iOS 26.0+
|
|
51
|
+
*
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
iosAtypicalSpeech?: boolean
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type HapticFeedbackStyle = 'light' | 'medium' | 'heavy' | 'none'
|
|
58
|
+
|
|
59
|
+
export interface MutableSpeechRecognitionConfig {
|
|
60
|
+
/**
|
|
61
|
+
* Silence timer duration with no voice detected
|
|
62
|
+
*
|
|
63
|
+
* for Android - undefined behavior for > 300_000ms (5 minutes)
|
|
64
|
+
*
|
|
65
|
+
* @default 8000
|
|
66
|
+
*/
|
|
67
|
+
autoFinishRecognitionMs?: number
|
|
68
|
+
/**
|
|
69
|
+
* The interval at which `onAutoFinishProgress` will be triggered.
|
|
70
|
+
*
|
|
71
|
+
* min: 50ms,
|
|
72
|
+
*
|
|
73
|
+
* max: {@link autoFinishRecognitionMs}
|
|
74
|
+
*
|
|
75
|
+
* @default 1000
|
|
76
|
+
*/
|
|
77
|
+
autoFinishProgressIntervalMs?: number
|
|
78
|
+
/**
|
|
79
|
+
* The voice detector sensitivity to reset the auto finish timer.
|
|
80
|
+
*
|
|
81
|
+
* Adjust for your expected environment.
|
|
82
|
+
*
|
|
83
|
+
* `0.00` - `1.00`
|
|
84
|
+
*
|
|
85
|
+
* `0.10` - Quiet room level
|
|
86
|
+
*
|
|
87
|
+
* `0.40` - Default, street, medium restaurant background, office level
|
|
88
|
+
*
|
|
89
|
+
* `0.60` - Loud restaurants, noisy office, party, highly crowded places level
|
|
90
|
+
*
|
|
91
|
+
* `1` - Use to disable volume-based reset of the auto finish timer
|
|
92
|
+
*
|
|
93
|
+
* @default 0.40
|
|
94
|
+
*/
|
|
95
|
+
resetAutoFinishVoiceSensitivity?: number
|
|
96
|
+
/**
|
|
97
|
+
* Lots of repeating words in a row can be annoying
|
|
98
|
+
*
|
|
99
|
+
* @default false
|
|
100
|
+
*/
|
|
101
|
+
disableRepeatingFilter?: boolean
|
|
102
|
+
/**
|
|
103
|
+
* Haptic feedback level when microphone starts recording.
|
|
104
|
+
*
|
|
105
|
+
* Use `"none"` to disable.
|
|
106
|
+
*
|
|
107
|
+
* @default "medium"
|
|
108
|
+
*/
|
|
109
|
+
startHapticFeedbackStyle?: HapticFeedbackStyle
|
|
110
|
+
/**
|
|
111
|
+
* Haptic feedback level when microphone stops recording.
|
|
112
|
+
*
|
|
113
|
+
* Use `"none"` to disable.
|
|
114
|
+
*
|
|
115
|
+
* @default "medium"
|
|
116
|
+
*/
|
|
117
|
+
stopHapticFeedbackStyle?: HapticFeedbackStyle
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @note Configuration for android and ios may behave differently
|
|
122
|
+
* for the same properties, test both platforms
|
|
123
|
+
*
|
|
124
|
+
* Use `Plarform.select()` to match expectations
|
|
125
|
+
*/
|
|
126
|
+
export interface SpeechRecognitionConfig
|
|
127
|
+
extends MutableSpeechRecognitionConfig, ParamsAndroid, ParamsIOS {
|
|
128
|
+
/**
|
|
129
|
+
* @default "en-US"
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* "fr-FR"
|
|
133
|
+
* "pt-PT"
|
|
134
|
+
* "pt-BR"
|
|
135
|
+
*
|
|
136
|
+
*/
|
|
137
|
+
locale?: string
|
|
138
|
+
/**
|
|
139
|
+
* An array of strings that should be recognized, even if they are not in the system vocabulary.
|
|
140
|
+
*
|
|
141
|
+
* Use this property to specify short custom phrases that are unique to your app.
|
|
142
|
+
*
|
|
143
|
+
* You might include phrases with the names of characters, products, or places that are specific to your app.
|
|
144
|
+
*
|
|
145
|
+
* You might also include domain-specific terminology or unusual or made-up words.
|
|
146
|
+
*
|
|
147
|
+
* Assigning custom phrases to this property improves the likelihood of those phrases being recognized.
|
|
148
|
+
*
|
|
149
|
+
* Keep phrases relatively brief, limiting them to one or two words whenever possible.
|
|
150
|
+
* Lengthy phrases are less likely to be recognized.
|
|
151
|
+
* In addition, try to limit each phrase to something the user can say without pausing.
|
|
152
|
+
*
|
|
153
|
+
* Limit the total number of phrases to no more than 100.
|
|
154
|
+
*
|
|
155
|
+
* @default []
|
|
156
|
+
*/
|
|
157
|
+
contextualStrings?: string[]
|
|
158
|
+
/**
|
|
159
|
+
* Mask offensive words with asterisks.
|
|
160
|
+
*
|
|
161
|
+
* @since Android 13+
|
|
162
|
+
* @since iOS 26+ (iOS <26: always `false`)
|
|
163
|
+
*
|
|
164
|
+
* @default false
|
|
165
|
+
*/
|
|
166
|
+
maskOffensiveWords?: boolean
|
|
167
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contains data about each audio buffer volume.
|
|
3
|
+
*
|
|
4
|
+
* Emits with high frequency.
|
|
5
|
+
*/
|
|
6
|
+
export interface VolumeChangeEvent {
|
|
7
|
+
/**
|
|
8
|
+
* Smoothed voice input volume
|
|
9
|
+
*
|
|
10
|
+
* Normalized to a range of 0 to 1.
|
|
11
|
+
*
|
|
12
|
+
* Best choice for UI animations.
|
|
13
|
+
*/
|
|
14
|
+
smoothedVolume: number
|
|
15
|
+
/**
|
|
16
|
+
* Raw voice input volume
|
|
17
|
+
*
|
|
18
|
+
* Normalized to a range of 0 to 1.
|
|
19
|
+
*
|
|
20
|
+
* Appropriate for internal logic, quick reactions, not UI.
|
|
21
|
+
*/
|
|
22
|
+
rawVolume: number
|
|
23
|
+
/**
|
|
24
|
+
* Audio buffer volume in decibels.
|
|
25
|
+
*
|
|
26
|
+
* May vary on different devices and audio engines.
|
|
27
|
+
*
|
|
28
|
+
* db 0 is still a sound, undefined is no sound.
|
|
29
|
+
*/
|
|
30
|
+
db?: number
|
|
31
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
-keep class com.margelo.nitro.nitrospeech.** { *; }
|