@mentra/bluetooth-sdk 3.4.0-dev.1166 → 3.4.0-dev.1170
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/android/build.gradle +6 -36
- package/android/settings.gradle +1 -4
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +61 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +27 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +89 -104
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueue.kt +92 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHost.kt +169 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +74 -115
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +348 -357
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetector.kt +211 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngine.kt +137 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SttTextFormatter.kt +52 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/WakeWordDetector.kt +235 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/AudioRouteWatcher.kt +120 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmStreamPlayer.kt +181 -16
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlayback.kt +177 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueueTest.kt +81 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHostTest.kt +216 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/STTToolsExtractionTest.kt +108 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetectorTest.kt +126 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngineTest.kt +245 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SttTextFormatterTest.kt +67 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlaybackTest.kt +76 -0
- package/build/BluetoothSdk.types.d.ts +49 -0
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +31 -4
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js.map +1 -1
- package/build/generated/releaseMetadata.js +5 -5
- package/build/generated/releaseMetadata.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +5 -0
- package/build/index.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +58 -8
- package/ios/MentraBluetoothSDK.podspec +4 -0
- package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
- package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
- package/ios/Source/BluetoothSdkDefaults.swift +1 -1
- package/ios/Source/DeviceManager.swift +30 -12
- package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
- package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
- package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
- package/ios/Source/audio/SpeechActivity.swift +277 -0
- package/ios/Source/stt/STTTools.swift +13 -31
- package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
- package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
- package/ios/Source/stt/WakeWordDetector.swift +145 -0
- package/ios/Source/utils/BoundedWait.swift +44 -0
- package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
- package/ios/Source/utils/SttModelTraits.swift +62 -0
- package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
- package/ios/Tests/PcmStreamTests.swift +299 -0
- package/ios/Tests/SpeechActivityTests.swift +238 -0
- package/ios/Tests/SttModelTraitsTests.swift +62 -0
- package/package.json +2 -2
- package/plugin/build/withAndroid.js +7 -11
- package/src/BluetoothSdk.types.ts +49 -0
- package/src/_private/BluetoothSdkModule.ts +33 -4
- package/src/generated/releaseMetadata.ts +5 -5
- package/src/index.ts +7 -0
- package/android/silero/LICENSE.md +0 -17
- package/android/silero/build.gradle +0 -50
- package/android/silero/proguard-rules.pro +0 -24
- package/android/silero/src/androidTest/assets/hello.wav +0 -0
- package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
- package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
- package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
- package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
- package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
- package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
- /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
- /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
package com.mentra.bluetoothsdk.stt
|
|
2
|
-
|
|
3
|
-
import android.content.Context
|
|
4
|
-
import com.mentra.bluetoothsdk.utils.NativeLog as Log
|
|
5
|
-
import com.konovalov.vad.silero.Vad
|
|
6
|
-
import com.konovalov.vad.silero.VadSilero
|
|
7
|
-
import com.konovalov.vad.silero.config.FrameSize
|
|
8
|
-
import com.konovalov.vad.silero.config.Mode
|
|
9
|
-
import com.konovalov.vad.silero.config.SampleRate
|
|
10
|
-
import java.nio.ByteBuffer
|
|
11
|
-
import java.nio.ByteOrder
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* VadGateSpeechPolicy handles Voice Activity Detection using Silero VAD.
|
|
15
|
-
*
|
|
16
|
-
* Features:
|
|
17
|
-
* - Dynamic VAD check throttling (adjusts frequency based on silence duration)
|
|
18
|
-
* - 8-second silence timeout before declaring "not speaking"
|
|
19
|
-
* - Bypass modes for debugging and PCM streaming
|
|
20
|
-
* - 512-sample frame size (matches Sherpa-ONNX requirements)
|
|
21
|
-
*
|
|
22
|
-
* Ported from android_core VadGateSpeechPolicy.java
|
|
23
|
-
*/
|
|
24
|
-
class VadGateSpeechPolicy(private val context: Context) {
|
|
25
|
-
companion object {
|
|
26
|
-
private const val TAG = "VadGateSpeechPolicy"
|
|
27
|
-
|
|
28
|
-
// Total required silence duration
|
|
29
|
-
private const val REQUIRED_SILENCE_DURATION_MS = 8000L
|
|
30
|
-
|
|
31
|
-
// Dynamic VAD check intervals
|
|
32
|
-
private const val INITIAL_SILENCE_VAD_INTERVAL_MS = 50L // Check frequently at first
|
|
33
|
-
private const val MEDIUM_SILENCE_VAD_INTERVAL_MS = 100L // Medium frequency after some time
|
|
34
|
-
private const val LONG_SILENCE_VAD_INTERVAL_MS =
|
|
35
|
-
200L // Less frequent after extended silence
|
|
36
|
-
|
|
37
|
-
// Thresholds for switching intervals
|
|
38
|
-
private const val MEDIUM_SILENCE_THRESHOLD_MS =
|
|
39
|
-
20000L // Switch to medium interval after 20s
|
|
40
|
-
private const val LONG_SILENCE_THRESHOLD_MS = 60000L // Switch to long interval after 60s
|
|
41
|
-
|
|
42
|
-
private const val FRAME_SIZE = 512
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private var vad: VadSilero? = null
|
|
46
|
-
private var isCurrentlySpeech = false
|
|
47
|
-
private var bypassVadForDebugging = false
|
|
48
|
-
private var bypassVadForPCM = false
|
|
49
|
-
|
|
50
|
-
/** Notified whenever the speech/silence state actually flips. */
|
|
51
|
-
var onSpeechStateChanged: ((Boolean) -> Unit)? = null
|
|
52
|
-
|
|
53
|
-
// Timestamp of the last detected speech
|
|
54
|
-
private var lastSpeechDetectedTime = 0L
|
|
55
|
-
// Throttle timer for silence VAD checks
|
|
56
|
-
private var lastVadCheckTime = 0L
|
|
57
|
-
|
|
58
|
-
/** Initialize the Silero VAD model */
|
|
59
|
-
fun init(blockSizeSamples: Int) {
|
|
60
|
-
startVad()
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private fun startVad() {
|
|
64
|
-
try {
|
|
65
|
-
// Set internal silence duration very low; external logic manages the full required
|
|
66
|
-
// silence duration
|
|
67
|
-
vad =
|
|
68
|
-
Vad.builder()
|
|
69
|
-
.setContext(context)
|
|
70
|
-
.setSampleRate(SampleRate.SAMPLE_RATE_16K)
|
|
71
|
-
.setFrameSize(FrameSize.FRAME_SIZE_512)
|
|
72
|
-
.setMode(Mode.NORMAL)
|
|
73
|
-
.setSilenceDurationMs(50)
|
|
74
|
-
.setSpeechDurationMs(50)
|
|
75
|
-
.build()
|
|
76
|
-
|
|
77
|
-
Log.d(TAG, "VAD initialized successfully")
|
|
78
|
-
} catch (e: Exception) {
|
|
79
|
-
Log.e(TAG, "Failed to initialize VAD", e)
|
|
80
|
-
vad = null
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** Check if audio should be passed to the recognizer based on VAD state */
|
|
85
|
-
fun shouldPassAudioToRecognizer(): Boolean {
|
|
86
|
-
// CRITICAL: Handle VAD null case
|
|
87
|
-
if (vad == null) {
|
|
88
|
-
return bypassVadForDebugging || bypassVadForPCM || true
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return bypassVadForDebugging || bypassVadForPCM || isCurrentlySpeech
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Process audio bytes through VAD to detect speech */
|
|
95
|
-
fun processAudioBytes(bytes: ByteArray, offset: Int, length: Int) {
|
|
96
|
-
val now = System.currentTimeMillis()
|
|
97
|
-
|
|
98
|
-
// If in speech state and it hasn't been 8 seconds since the last speech was detected, skip
|
|
99
|
-
// processing
|
|
100
|
-
if (isCurrentlySpeech && (now - lastSpeechDetectedTime < REQUIRED_SILENCE_DURATION_MS)) {
|
|
101
|
-
return
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Calculate silence duration
|
|
105
|
-
val silenceDuration = now - lastSpeechDetectedTime
|
|
106
|
-
|
|
107
|
-
// Determine which VAD interval to use based on silence duration
|
|
108
|
-
val currentVadInterval =
|
|
109
|
-
when {
|
|
110
|
-
silenceDuration < MEDIUM_SILENCE_THRESHOLD_MS -> INITIAL_SILENCE_VAD_INTERVAL_MS
|
|
111
|
-
silenceDuration < LONG_SILENCE_THRESHOLD_MS -> MEDIUM_SILENCE_VAD_INTERVAL_MS
|
|
112
|
-
else -> LONG_SILENCE_VAD_INTERVAL_MS
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// During silence, throttle VAD checks based on the dynamic interval
|
|
116
|
-
if (!isCurrentlySpeech && (now - lastVadCheckTime < currentVadInterval)) {
|
|
117
|
-
return
|
|
118
|
-
}
|
|
119
|
-
lastVadCheckTime = now
|
|
120
|
-
|
|
121
|
-
val audioBytesFull = bytesToShort(bytes)
|
|
122
|
-
val totalSamples = audioBytesFull.size
|
|
123
|
-
|
|
124
|
-
if (totalSamples % FRAME_SIZE != 0) {
|
|
125
|
-
Log.e(
|
|
126
|
-
TAG,
|
|
127
|
-
"Invalid audio frame size: $totalSamples samples. Needs to be multiple of $FRAME_SIZE."
|
|
128
|
-
)
|
|
129
|
-
return
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
val currentVad =
|
|
133
|
-
vad
|
|
134
|
-
?: run {
|
|
135
|
-
Log.w(TAG, "VAD not initialized, skipping audio processing")
|
|
136
|
-
return
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
var previousSpeechState = isCurrentlySpeech
|
|
140
|
-
|
|
141
|
-
// Process each 512-sample frame
|
|
142
|
-
val numFrames = totalSamples / FRAME_SIZE
|
|
143
|
-
for (i in 0 until numFrames) {
|
|
144
|
-
val currentTime = System.currentTimeMillis()
|
|
145
|
-
val startIdx = i * FRAME_SIZE
|
|
146
|
-
val audioFrame = audioBytesFull.copyOfRange(startIdx, startIdx + FRAME_SIZE)
|
|
147
|
-
val detectedSpeech = currentVad.isSpeech(audioFrame)
|
|
148
|
-
|
|
149
|
-
if (detectedSpeech) {
|
|
150
|
-
isCurrentlySpeech = true
|
|
151
|
-
// Update the last speech detection timestamp
|
|
152
|
-
lastSpeechDetectedTime = currentTime
|
|
153
|
-
} else {
|
|
154
|
-
// If no speech detected, and 8 seconds have elapsed since the last speech, mark as
|
|
155
|
-
// silence
|
|
156
|
-
if (currentTime - lastSpeechDetectedTime >= REQUIRED_SILENCE_DURATION_MS) {
|
|
157
|
-
isCurrentlySpeech = false
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (isCurrentlySpeech != previousSpeechState) {
|
|
162
|
-
Log.d(
|
|
163
|
-
TAG,
|
|
164
|
-
"Speech detection changed to: ${if (isCurrentlySpeech) "SPEECH" else "SILENCE"}"
|
|
165
|
-
)
|
|
166
|
-
previousSpeechState = isCurrentlySpeech
|
|
167
|
-
onSpeechStateChanged?.invoke(isCurrentlySpeech)
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** Convert byte array (PCM16LE) to short array */
|
|
173
|
-
private fun bytesToShort(bytes: ByteArray): ShortArray {
|
|
174
|
-
val shorts = ShortArray(bytes.size / 2)
|
|
175
|
-
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts)
|
|
176
|
-
return shorts
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/** Stop VAD and release resources */
|
|
180
|
-
fun stop() {
|
|
181
|
-
try {
|
|
182
|
-
vad?.close()
|
|
183
|
-
vad = null
|
|
184
|
-
Log.d(TAG, "VAD stopped and resources released")
|
|
185
|
-
} catch (e: Exception) {
|
|
186
|
-
Log.e(TAG, "Error stopping VAD", e)
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** Reset VAD state */
|
|
191
|
-
fun reset() {
|
|
192
|
-
val wasSpeaking = isCurrentlySpeech
|
|
193
|
-
isCurrentlySpeech = false
|
|
194
|
-
lastSpeechDetectedTime = 0
|
|
195
|
-
lastVadCheckTime = 0
|
|
196
|
-
if (wasSpeaking) onSpeechStateChanged?.invoke(false)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/** Change bypass VAD for debugging state */
|
|
200
|
-
fun changeBypassVadForDebugging(bypass: Boolean) {
|
|
201
|
-
bypassVadForDebugging = bypass
|
|
202
|
-
Log.d(TAG, "Bypass VAD for debugging: $bypass")
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/** Change bypass VAD for PCM streaming state */
|
|
206
|
-
fun changeBypassVadForPCM(bypass: Boolean) {
|
|
207
|
-
Log.d(TAG, "VAD PCM Bypass State Change: $bypassVadForPCM -> $bypass")
|
|
208
|
-
bypassVadForPCM = bypass
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/** Handle microphone state changes */
|
|
212
|
-
fun microphoneStateChanged(state: Boolean) {
|
|
213
|
-
if (!state) {
|
|
214
|
-
// Microphone turned off: force immediate silence
|
|
215
|
-
val wasSpeaking = isCurrentlySpeech
|
|
216
|
-
isCurrentlySpeech = false
|
|
217
|
-
lastSpeechDetectedTime = 0
|
|
218
|
-
lastVadCheckTime = 0
|
|
219
|
-
|
|
220
|
-
// Optionally flush the VAD's internal state by processing a silent frame
|
|
221
|
-
vad?.let { vadInstance ->
|
|
222
|
-
val silentFrame = ShortArray(FRAME_SIZE)
|
|
223
|
-
vadInstance.isSpeech(silentFrame)
|
|
224
|
-
}
|
|
225
|
-
Log.d(TAG, "Microphone turned off; forced state to SILENCE.")
|
|
226
|
-
if (wasSpeaking) onSpeechStateChanged?.invoke(false)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
File without changes
|
|
File without changes
|