@mentra/bluetooth-sdk 3.3.0 → 3.4.0-beta.1241
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/MIGRATION.md +61 -0
- package/README.md +80 -11
- package/android/build.gradle +60 -51
- package/android/gradle.properties +3 -0
- package/android/settings.gradle +47 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +85 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +35 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +154 -110
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceStore.kt +21 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +1 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +27 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/connection/ConnectionModels.kt +5 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicy.kt +64 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +925 -155
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +227 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisement.kt +19 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicy.kt +94 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachine.kt +411 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateStore.kt +124 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +54 -59
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +5 -32
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasEncoder.kt +8 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +3 -10
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/status/DeviceStatus.kt +50 -0
- 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/AvifExifStripper.java +2 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +3 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNaming.java +11 -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/test/java/com/mentra/bluetoothsdk/BesCrashReportWaitersTest.kt +112 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/DeviceManagerOwnerLossTest.kt +146 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusAudioSetupTest.kt +51 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusClassicAudioTest.kt +2 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/WifiAdbPeerIntentTest.kt +19 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicyTest.kt +65 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveAudioSetupReadinessTest.kt +231 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveGlassesReadyBurstTest.kt +5 -1
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +368 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisementParserTest.kt +20 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicyTest.kt +136 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachineTest.kt +346 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
- package/android/src/test/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNamingTest.kt +23 -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 +201 -1
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +43 -5
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js +2 -0
- package/build/_private/BluetoothSdkModule.js.map +1 -1
- package/build/generated/changelogCatalog.d.ts +3 -0
- package/build/generated/changelogCatalog.d.ts.map +1 -1
- package/build/generated/changelogCatalog.js +4 -0
- package/build/generated/changelogCatalog.js.map +1 -1
- package/build/generated/releaseMetadata.js +6 -6
- 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 +9 -0
- package/build/index.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +76 -12
- 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/BesCrashReportWaiters.swift +64 -0
- package/ios/Source/BluetoothSdkDefaults.swift +2 -2
- package/ios/Source/Bridge.swift +11 -3
- package/ios/Source/DeviceManager.swift +131 -18
- package/ios/Source/DeviceStore.swift +27 -0
- package/ios/Source/GeneratedChangelogCatalog.swift +1 -0
- package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
- package/ios/Source/MentraBluetoothSDK.swift +40 -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/errors/BluetoothSdkError.swift +5 -1
- package/ios/Source/events/BluetoothEvents.swift +41 -0
- package/ios/Source/internal/BleTraceLogger.swift +9 -6
- package/ios/Source/internal/BluetoothAvailability.swift +15 -5
- package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +186 -0
- package/ios/Source/sgcs/MentraLive.swift +1270 -189
- package/ios/Source/sgcs/MentraLivePairingAdvertisement.swift +260 -1
- package/ios/Source/sgcs/MentraLivePairingStateMachine.swift +385 -0
- package/ios/Source/sgcs/MentraLivePairingStateStore.swift +121 -0
- package/ios/Source/sgcs/Nimo.swift +10 -3
- package/ios/Source/sgcs/NimoCanvasCoordinator.swift +39 -9
- package/ios/Source/sgcs/SGCManager.swift +13 -0
- package/ios/Source/sgcs/Simulated.swift +2 -0
- package/ios/Source/status/DeviceStatus.swift +35 -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/types/DeviceModels.swift +8 -1
- package/ios/Source/utils/AudioSessionMonitor.swift +158 -5
- 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/BesCrashReportTests.swift +142 -0
- package/ios/Tests/BluetoothAudioRouteTests.swift +82 -0
- package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
- package/ios/Tests/DeviceManagerReconnectTests.swift +4 -2
- package/ios/Tests/GlassesAudioSetupPolicyTests.swift +197 -0
- package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
- package/ios/Tests/MentraLiveAudioSetupTests.swift +209 -0
- package/ios/Tests/MentraLiveConnectionOptionsTests.swift +55 -0
- package/ios/Tests/MentraLivePairingAdvertisementTests.swift +327 -0
- package/ios/Tests/MentraLivePairingStateMachineTests.swift +402 -0
- package/ios/Tests/MentraLiveSessionReadinessTests.swift +368 -26
- package/ios/Tests/NimoCanvasTransportTests.swift +58 -0
- package/ios/Tests/PcmStreamTests.swift +299 -0
- package/ios/Tests/SpeechActivityTests.swift +238 -0
- package/ios/Tests/SttModelTraitsTests.swift +62 -0
- package/ios/Tests/WifiAdbDispatchTests.swift +118 -0
- package/package.json +3 -2
- package/plugin/build/withAndroid.js +9 -11
- package/plugin/build/withKotlinVersion.d.ts +2 -0
- package/plugin/build/withKotlinVersion.js +46 -0
- package/src/BluetoothSdk.types.ts +201 -1
- package/src/_private/BluetoothSdkModule.ts +47 -5
- package/src/generated/changelogCatalog.ts +4 -0
- package/src/generated/releaseMetadata.ts +6 -6
- package/src/index.ts +22 -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/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
- package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -288
- /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
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.stt
|
|
2
|
+
|
|
3
|
+
import java.util.ArrayDeque
|
|
4
|
+
import java.util.concurrent.CountDownLatch
|
|
5
|
+
import java.util.concurrent.ExecutionException
|
|
6
|
+
import java.util.concurrent.Executor
|
|
7
|
+
import java.util.concurrent.TimeUnit
|
|
8
|
+
import org.junit.Assert.assertEquals
|
|
9
|
+
import org.junit.Assert.assertFalse
|
|
10
|
+
import org.junit.Assert.assertNull
|
|
11
|
+
import org.junit.Assert.assertSame
|
|
12
|
+
import org.junit.Assert.assertTrue
|
|
13
|
+
import org.junit.Assert.fail
|
|
14
|
+
import org.junit.Test
|
|
15
|
+
|
|
16
|
+
class OnDeviceRecognizerHostTest {
|
|
17
|
+
private class Recognizer(val path: String) {
|
|
18
|
+
var released = false
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Runs load jobs only when the test says so, to control interleavings. */
|
|
22
|
+
private class ManualExecutor : Executor {
|
|
23
|
+
val jobs = ArrayDeque<Runnable>()
|
|
24
|
+
|
|
25
|
+
override fun execute(command: Runnable) {
|
|
26
|
+
jobs.add(command)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fun runNext() = jobs.removeFirst().run()
|
|
30
|
+
|
|
31
|
+
fun runAll() {
|
|
32
|
+
while (jobs.isNotEmpty()) runNext()
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private var selected: OnDeviceRecognizerHost.Selection? = OnDeviceRecognizerHost.Selection("/models/en-kroko", "en-US")
|
|
37
|
+
private val failing = mutableSetOf<String>()
|
|
38
|
+
private val loads = mutableListOf<String>()
|
|
39
|
+
private val events = mutableListOf<String>()
|
|
40
|
+
private val loader = ManualExecutor()
|
|
41
|
+
@Volatile private var loadStarted: CountDownLatch? = null
|
|
42
|
+
@Volatile private var loadGate: CountDownLatch? = null
|
|
43
|
+
|
|
44
|
+
private val host: OnDeviceRecognizerHost<Recognizer> =
|
|
45
|
+
OnDeviceRecognizerHost(
|
|
46
|
+
selection = { selected },
|
|
47
|
+
loadModel = { selection ->
|
|
48
|
+
loads.add(selection.path)
|
|
49
|
+
loadStarted?.countDown()
|
|
50
|
+
loadGate?.await(5, TimeUnit.SECONDS)
|
|
51
|
+
if (selection.path in failing) throw IllegalStateException("corrupt model")
|
|
52
|
+
Recognizer(selection.path)
|
|
53
|
+
},
|
|
54
|
+
pathMatches = { recognizer, path -> recognizer.path == path },
|
|
55
|
+
release = { recognizer ->
|
|
56
|
+
// The audio thread must no longer be able to reach a recognizer being released.
|
|
57
|
+
assertFalse("released while still current", host().current === recognizer)
|
|
58
|
+
recognizer.released = true
|
|
59
|
+
events.add("release ${recognizer.path}")
|
|
60
|
+
},
|
|
61
|
+
loader = loader,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
private fun host(): OnDeviceRecognizerHost<Recognizer> = host
|
|
65
|
+
|
|
66
|
+
@Test
|
|
67
|
+
fun gainingDemandLoadsOffTheCallingThreadAndLosingItReleases() {
|
|
68
|
+
host.setDemand(true)
|
|
69
|
+
assertNull("loading must not happen on the caller", host.current)
|
|
70
|
+
assertTrue(loader.jobs.isNotEmpty())
|
|
71
|
+
loader.runAll()
|
|
72
|
+
|
|
73
|
+
val loaded = host.current!!
|
|
74
|
+
assertEquals("/models/en-kroko", loaded.path)
|
|
75
|
+
assertTrue(host.isLoaded("/models/en-kroko"))
|
|
76
|
+
|
|
77
|
+
host.setDemand(false)
|
|
78
|
+
assertNull(host.current)
|
|
79
|
+
assertTrue(loaded.released)
|
|
80
|
+
assertFalse(host.isLoaded("/models/en-kroko"))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@Test
|
|
84
|
+
fun restartWithoutDemandResolvesImmediatelyWithoutLoading() {
|
|
85
|
+
val result = host.restart()
|
|
86
|
+
assertTrue(result.isDone)
|
|
87
|
+
result.get()
|
|
88
|
+
assertTrue(loader.jobs.isEmpty())
|
|
89
|
+
assertTrue(loads.isEmpty())
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@Test
|
|
93
|
+
fun restartJoinsTheLoadAlreadyInFlight() {
|
|
94
|
+
host.setDemand(true)
|
|
95
|
+
val result = host.restart()
|
|
96
|
+
assertFalse(result.isDone)
|
|
97
|
+
loader.runAll()
|
|
98
|
+
result.get()
|
|
99
|
+
assertEquals(listOf("/models/en-kroko"), loads)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@Test
|
|
103
|
+
fun restartWithTheLoadedModelDoesNotReload() {
|
|
104
|
+
host.setDemand(true)
|
|
105
|
+
loader.runAll()
|
|
106
|
+
val result = host.restart()
|
|
107
|
+
assertTrue(result.isDone)
|
|
108
|
+
assertEquals(1, loads.size)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
@Test
|
|
112
|
+
fun restartSwapsInTheNewModelThenReleasesTheOldOne() {
|
|
113
|
+
host.setDemand(true)
|
|
114
|
+
loader.runAll()
|
|
115
|
+
val old = host.current!!
|
|
116
|
+
|
|
117
|
+
selected = OnDeviceRecognizerHost.Selection("/models/fr", "fr-FR")
|
|
118
|
+
val result = host.restart()
|
|
119
|
+
assertSame("the old model keeps serving audio while the new one loads", old, host.current)
|
|
120
|
+
loader.runAll()
|
|
121
|
+
result.get()
|
|
122
|
+
|
|
123
|
+
assertEquals("/models/fr", host.current!!.path)
|
|
124
|
+
assertTrue(old.released)
|
|
125
|
+
assertEquals(listOf("release /models/en-kroko"), events)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
@Test
|
|
129
|
+
fun failedRestartRejectsAndDoesNotKeepAModelThatIsNoLongerSelected() {
|
|
130
|
+
host.setDemand(true)
|
|
131
|
+
loader.runAll()
|
|
132
|
+
val old = host.current!!
|
|
133
|
+
|
|
134
|
+
selected = OnDeviceRecognizerHost.Selection("/models/de", "de-DE")
|
|
135
|
+
failing.add("/models/de")
|
|
136
|
+
val result = host.restart()
|
|
137
|
+
loader.runAll()
|
|
138
|
+
try {
|
|
139
|
+
result.get()
|
|
140
|
+
fail("restart should fail")
|
|
141
|
+
} catch (error: ExecutionException) {
|
|
142
|
+
assertEquals("corrupt model", error.cause!!.message)
|
|
143
|
+
}
|
|
144
|
+
assertNull(host.current)
|
|
145
|
+
assertTrue(old.released)
|
|
146
|
+
|
|
147
|
+
// The next attempt retries the load.
|
|
148
|
+
failing.clear()
|
|
149
|
+
val retry = host.restart()
|
|
150
|
+
loader.runAll()
|
|
151
|
+
retry.get()
|
|
152
|
+
assertEquals("/models/de", host.current!!.path)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
@Test
|
|
156
|
+
fun demandEndingDuringALoadDiscardsTheResult() {
|
|
157
|
+
host.setDemand(true)
|
|
158
|
+
val result = host.restart()
|
|
159
|
+
host.setDemand(false)
|
|
160
|
+
assertTrue("a waiting restart resolves once there is no demand", result.isDone)
|
|
161
|
+
result.get()
|
|
162
|
+
|
|
163
|
+
loader.runAll()
|
|
164
|
+
assertNull(host.current)
|
|
165
|
+
assertTrue(loads.isEmpty() || events.contains("release /models/en-kroko"))
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@Test
|
|
169
|
+
fun aLoadThatFinishesAfterDemandEndedIsReleasedNotSwappedIn() {
|
|
170
|
+
loadStarted = CountDownLatch(1)
|
|
171
|
+
loadGate = CountDownLatch(1)
|
|
172
|
+
host.setDemand(true)
|
|
173
|
+
val job = loader.jobs.removeFirst()
|
|
174
|
+
val running = Thread { job.run() }
|
|
175
|
+
running.start()
|
|
176
|
+
assertTrue(loadStarted!!.await(5, TimeUnit.SECONDS))
|
|
177
|
+
|
|
178
|
+
// Demand ends while the model is still loading.
|
|
179
|
+
host.setDemand(false)
|
|
180
|
+
loadGate!!.countDown()
|
|
181
|
+
running.join(5_000)
|
|
182
|
+
|
|
183
|
+
assertNull(host.current)
|
|
184
|
+
assertEquals(listOf("/models/en-kroko"), loads)
|
|
185
|
+
assertEquals(listOf("release /models/en-kroko"), events)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
@Test
|
|
189
|
+
fun aSupersededRestartFollowsTheNewerLoad() {
|
|
190
|
+
host.setDemand(true)
|
|
191
|
+
loader.runAll()
|
|
192
|
+
selected = OnDeviceRecognizerHost.Selection("/models/fr", "fr-FR")
|
|
193
|
+
val first = host.restart()
|
|
194
|
+
selected = OnDeviceRecognizerHost.Selection("/models/es", "es-ES")
|
|
195
|
+
val second = host.restart()
|
|
196
|
+
loader.runAll()
|
|
197
|
+
second.get()
|
|
198
|
+
first.get()
|
|
199
|
+
assertEquals("/models/es", host.current!!.path)
|
|
200
|
+
// The superseded French load never ran or was released.
|
|
201
|
+
assertFalse(host.isLoaded("/models/fr"))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
@Test
|
|
205
|
+
fun noSelectedModelRejectsRestart() {
|
|
206
|
+
selected = null
|
|
207
|
+
host.setDemand(true)
|
|
208
|
+
assertTrue(loader.jobs.isEmpty())
|
|
209
|
+
try {
|
|
210
|
+
host.restart().get()
|
|
211
|
+
fail("restart should fail without a model")
|
|
212
|
+
} catch (error: ExecutionException) {
|
|
213
|
+
assertTrue(error.cause is IllegalStateException)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.stt
|
|
2
|
+
|
|
3
|
+
import java.io.File
|
|
4
|
+
import java.nio.file.Files
|
|
5
|
+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
|
|
6
|
+
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
|
|
7
|
+
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream
|
|
8
|
+
import org.junit.After
|
|
9
|
+
import org.junit.Assert.assertEquals
|
|
10
|
+
import org.junit.Assert.assertFalse
|
|
11
|
+
import org.junit.Assert.assertTrue
|
|
12
|
+
import org.junit.Test
|
|
13
|
+
import org.junit.runner.RunWith
|
|
14
|
+
import org.robolectric.RobolectricTestRunner
|
|
15
|
+
import org.robolectric.annotation.Config
|
|
16
|
+
|
|
17
|
+
@RunWith(RobolectricTestRunner::class)
|
|
18
|
+
@Config(sdk = [33])
|
|
19
|
+
class STTToolsExtractionTest {
|
|
20
|
+
private val root = Files.createTempDirectory("stt-extract").toFile()
|
|
21
|
+
|
|
22
|
+
@After
|
|
23
|
+
fun tearDown() {
|
|
24
|
+
root.deleteRecursively()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private fun archive(name: String, entries: Map<String, ByteArray>, truncateTo: Int? = null): File {
|
|
28
|
+
val file = File(root, name)
|
|
29
|
+
TarArchiveOutputStream(BZip2CompressorOutputStream(file.outputStream())).use { tar ->
|
|
30
|
+
for ((path, data) in entries) {
|
|
31
|
+
val entry = TarArchiveEntry(path)
|
|
32
|
+
entry.size = data.size.toLong()
|
|
33
|
+
tar.putArchiveEntry(entry)
|
|
34
|
+
tar.write(data)
|
|
35
|
+
tar.closeArchiveEntry()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (truncateTo != null) {
|
|
39
|
+
val bytes = file.readBytes()
|
|
40
|
+
file.writeBytes(bytes.copyOf(truncateTo))
|
|
41
|
+
}
|
|
42
|
+
return file
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private fun leftovers() = root.listFiles()!!.filter { it.name.contains(".extracting-") }
|
|
46
|
+
|
|
47
|
+
@Test
|
|
48
|
+
fun extractsIntoPlaceAndFlattensTheTopLevelDirectory() {
|
|
49
|
+
val source =
|
|
50
|
+
archive(
|
|
51
|
+
"model.tar.bz2",
|
|
52
|
+
mapOf(
|
|
53
|
+
"sherpa-model/tokens.txt" to "a 0\n".toByteArray(),
|
|
54
|
+
"sherpa-model/encoder-epoch-99-avg-1.onnx" to ByteArray(1000) { 1 },
|
|
55
|
+
),
|
|
56
|
+
)
|
|
57
|
+
val dest = File(root, "models/en")
|
|
58
|
+
|
|
59
|
+
assertTrue(STTTools.extractTarBz2(source.absolutePath, dest.absolutePath))
|
|
60
|
+
assertEquals("a 0\n", File(dest, "tokens.txt").readText())
|
|
61
|
+
assertEquals(1000L, File(dest, "encoder.onnx").length())
|
|
62
|
+
assertFalse(File(dest, "sherpa-model").exists())
|
|
63
|
+
assertTrue(leftovers().isEmpty() && File(root, "models").listFiles()!!.size == 1)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@Test
|
|
67
|
+
fun aTruncatedArchiveLeavesNoPartialModelBehind() {
|
|
68
|
+
val big = ByteArray(400_000) { (it % 251).toByte() }
|
|
69
|
+
val source = archive("broken.tar.bz2", mapOf("m/tokens.txt" to "a 0\n".toByteArray(), "m/model.onnx" to big))
|
|
70
|
+
source.writeBytes(source.readBytes().copyOf(source.length().toInt() / 2))
|
|
71
|
+
val dest = File(root, "models/fr")
|
|
72
|
+
|
|
73
|
+
assertFalse(STTTools.extractTarBz2(source.absolutePath, dest.absolutePath))
|
|
74
|
+
assertFalse("a partial extract must not be visible", dest.exists())
|
|
75
|
+
assertTrue(File(root, "models").listFiles().orEmpty().isEmpty())
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@Test
|
|
79
|
+
fun aFailedExtractKeepsTheExistingModel() {
|
|
80
|
+
val dest = File(root, "models/de").apply { mkdirs() }
|
|
81
|
+
File(dest, "tokens.txt").writeText("old")
|
|
82
|
+
val missing = File(root, "missing.tar.bz2")
|
|
83
|
+
|
|
84
|
+
assertFalse(STTTools.extractTarBz2(missing.absolutePath, dest.absolutePath))
|
|
85
|
+
assertEquals("old", File(dest, "tokens.txt").readText())
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@Test
|
|
89
|
+
fun aSuccessfulExtractReplacesTheExistingModel() {
|
|
90
|
+
val dest = File(root, "models/es").apply { mkdirs() }
|
|
91
|
+
File(dest, "stale.onnx").writeText("stale")
|
|
92
|
+
val source = archive("es.tar.bz2", mapOf("es/tokens.txt" to "b 0\n".toByteArray()))
|
|
93
|
+
|
|
94
|
+
assertTrue(STTTools.extractTarBz2(source.absolutePath, dest.absolutePath))
|
|
95
|
+
assertEquals("b 0\n", File(dest, "tokens.txt").readText())
|
|
96
|
+
assertFalse(File(dest, "stale.onnx").exists())
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@Test
|
|
100
|
+
fun rejectsEntriesThatEscapeTheDestination() {
|
|
101
|
+
val source = archive("evil.tar.bz2", mapOf("../evil.txt" to "x".toByteArray()))
|
|
102
|
+
val dest = File(root, "models/x")
|
|
103
|
+
|
|
104
|
+
assertFalse(STTTools.extractTarBz2(source.absolutePath, dest.absolutePath))
|
|
105
|
+
assertFalse(File(root, "models/evil.txt").exists())
|
|
106
|
+
assertFalse(dest.exists())
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.stt
|
|
2
|
+
|
|
3
|
+
import java.util.concurrent.CopyOnWriteArrayList
|
|
4
|
+
import java.util.concurrent.CountDownLatch
|
|
5
|
+
import java.util.concurrent.TimeUnit
|
|
6
|
+
import org.junit.After
|
|
7
|
+
import org.junit.Assert.assertEquals
|
|
8
|
+
import org.junit.Assert.assertFalse
|
|
9
|
+
import org.junit.Assert.assertTrue
|
|
10
|
+
import org.junit.Before
|
|
11
|
+
import org.junit.Test
|
|
12
|
+
import org.junit.runner.RunWith
|
|
13
|
+
import org.robolectric.RobolectricTestRunner
|
|
14
|
+
import org.robolectric.annotation.Config
|
|
15
|
+
|
|
16
|
+
/** Lifecycle of the threaded detector with a stand-in classifier (no native model). */
|
|
17
|
+
@RunWith(RobolectricTestRunner::class)
|
|
18
|
+
@Config(sdk = [33])
|
|
19
|
+
class SpeechActivityDetectorTest {
|
|
20
|
+
private class Classifier(private val quietFramesToEnd: Int) : SpeechFrameClassifier {
|
|
21
|
+
@Volatile var released = false
|
|
22
|
+
private var speaking = false
|
|
23
|
+
private var quiet = 0
|
|
24
|
+
|
|
25
|
+
override fun accept(frame: FloatArray): Boolean {
|
|
26
|
+
if (frame.any { it > 0.1f }) {
|
|
27
|
+
speaking = true
|
|
28
|
+
quiet = 0
|
|
29
|
+
} else if (speaking && ++quiet >= quietFramesToEnd) {
|
|
30
|
+
speaking = false
|
|
31
|
+
}
|
|
32
|
+
return speaking
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
override fun release() {
|
|
36
|
+
released = true
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private val created = CopyOnWriteArrayList<Classifier>()
|
|
41
|
+
private val events = CopyOnWriteArrayList<Pair<Boolean, Long>>()
|
|
42
|
+
@Volatile private var onEvent: () -> Unit = {}
|
|
43
|
+
|
|
44
|
+
private val defaultFactory = SpeechActivityDetector.classifierFactory
|
|
45
|
+
private val defaultEmitter = SpeechActivityDetector.emitter
|
|
46
|
+
private val defaultClock = SpeechActivityDetector.clock
|
|
47
|
+
|
|
48
|
+
@Before
|
|
49
|
+
fun setUp() {
|
|
50
|
+
SpeechActivityDetector.classifierFactory = { minSilenceMs ->
|
|
51
|
+
Classifier(((minSilenceMs + 31) / 32)).also { created.add(it) }
|
|
52
|
+
}
|
|
53
|
+
SpeechActivityDetector.emitter = { speaking, silentForMs ->
|
|
54
|
+
events.add(speaking to silentForMs)
|
|
55
|
+
onEvent()
|
|
56
|
+
}
|
|
57
|
+
SpeechActivityDetector.clock = { System.nanoTime() / 1_000_000 }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@After
|
|
61
|
+
fun tearDown() {
|
|
62
|
+
SpeechActivityDetector.configure(false)
|
|
63
|
+
SpeechActivityDetector.classifierFactory = defaultFactory
|
|
64
|
+
SpeechActivityDetector.emitter = defaultEmitter
|
|
65
|
+
SpeechActivityDetector.clock = defaultClock
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private fun loudPcm(samples: Int): ByteArray =
|
|
69
|
+
ByteArray(samples * 2).also { bytes ->
|
|
70
|
+
for (i in 0 until samples) {
|
|
71
|
+
bytes[i * 2] = 0x00
|
|
72
|
+
bytes[i * 2 + 1] = 0x40 // 0x4000 = 0.5 of full scale
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Test
|
|
77
|
+
fun missingAudioAfterSpeechIsReportedAsSilence() {
|
|
78
|
+
val speaking = CountDownLatch(1)
|
|
79
|
+
val silent = CountDownLatch(1)
|
|
80
|
+
onEvent = {
|
|
81
|
+
if (events.last().first) speaking.countDown() else silent.countDown()
|
|
82
|
+
}
|
|
83
|
+
SpeechActivityDetector.configure(true, 300)
|
|
84
|
+
|
|
85
|
+
// 200 ms of speech in odd-sized chunks, then the microphone stops sending.
|
|
86
|
+
val pcm = loudPcm(3_200)
|
|
87
|
+
var off = 0
|
|
88
|
+
while (off < pcm.size) {
|
|
89
|
+
val end = minOf(pcm.size, off + 333)
|
|
90
|
+
SpeechActivityDetector.accept(pcm.copyOfRange(off, end))
|
|
91
|
+
off = end
|
|
92
|
+
}
|
|
93
|
+
assertTrue("no speaking event", speaking.await(2, TimeUnit.SECONDS))
|
|
94
|
+
val stoppedAt = System.nanoTime()
|
|
95
|
+
assertTrue("no silence after audio stopped", silent.await(3, TimeUnit.SECONDS))
|
|
96
|
+
val elapsedMs = (System.nanoTime() - stoppedAt) / 1_000_000
|
|
97
|
+
assertTrue("silence took ${elapsedMs}ms", elapsedMs in 250..1_500)
|
|
98
|
+
assertEquals(listOf(true to 0L, false to 300L), events.toList())
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@Test
|
|
102
|
+
fun disablingReleasesTheModelAndIgnoresAudio() {
|
|
103
|
+
SpeechActivityDetector.configure(true, 300)
|
|
104
|
+
assertEquals(1, created.size)
|
|
105
|
+
assertTrue(SpeechActivityDetector.isModelLoadedForTest)
|
|
106
|
+
|
|
107
|
+
SpeechActivityDetector.configure(false)
|
|
108
|
+
assertTrue(created.single().released)
|
|
109
|
+
assertFalse(SpeechActivityDetector.isModelLoadedForTest)
|
|
110
|
+
|
|
111
|
+
SpeechActivityDetector.accept(loudPcm(1_600))
|
|
112
|
+
Thread.sleep(200)
|
|
113
|
+
assertTrue(events.isEmpty())
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@Test
|
|
117
|
+
fun changingTheSilenceGapReloadsTheModel() {
|
|
118
|
+
SpeechActivityDetector.configure(true, 300)
|
|
119
|
+
SpeechActivityDetector.configure(true, 300)
|
|
120
|
+
assertEquals(1, created.size)
|
|
121
|
+
SpeechActivityDetector.configure(true, 1_000)
|
|
122
|
+
assertEquals(2, created.size)
|
|
123
|
+
assertTrue(created[0].released)
|
|
124
|
+
assertFalse(created[1].released)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.stt
|
|
2
|
+
|
|
3
|
+
import org.junit.Assert.assertEquals
|
|
4
|
+
import org.junit.Assert.assertFalse
|
|
5
|
+
import org.junit.Assert.assertTrue
|
|
6
|
+
import org.junit.Test
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Pure framing, transition and gap-fill logic. The classifier is a stand-in with Silero-like
|
|
10
|
+
* hysteresis: a frame is speech when its peak is above 0.1, and speaking only ends after
|
|
11
|
+
* minSilenceMs of quiet frames (sherpa-onnx's min_silence_duration).
|
|
12
|
+
*/
|
|
13
|
+
class SpeechActivityEngineTest {
|
|
14
|
+
private class FakeClassifier(minSilenceMs: Int) : SpeechFrameClassifier {
|
|
15
|
+
private val quietFramesToEnd =
|
|
16
|
+
((minSilenceMs + SpeechActivityEngine.FRAME_MS - 1) / SpeechActivityEngine.FRAME_MS).toInt()
|
|
17
|
+
val frames = mutableListOf<FloatArray>()
|
|
18
|
+
var released = false
|
|
19
|
+
private var speaking = false
|
|
20
|
+
private var quiet = 0
|
|
21
|
+
|
|
22
|
+
override fun accept(frame: FloatArray): Boolean {
|
|
23
|
+
frames.add(frame)
|
|
24
|
+
val loud = frame.any { kotlin.math.abs(it) > 0.1f }
|
|
25
|
+
if (loud) {
|
|
26
|
+
speaking = true
|
|
27
|
+
quiet = 0
|
|
28
|
+
} else if (speaking && ++quiet >= quietFramesToEnd) {
|
|
29
|
+
speaking = false
|
|
30
|
+
quiet = 0
|
|
31
|
+
}
|
|
32
|
+
return speaking
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
override fun release() {
|
|
36
|
+
released = true
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private data class Event(val speaking: Boolean, val silentForMs: Long, val atMs: Long)
|
|
41
|
+
|
|
42
|
+
private var now = 0L
|
|
43
|
+
private val events = mutableListOf<Event>()
|
|
44
|
+
|
|
45
|
+
private fun engine(classifier: SpeechFrameClassifier, minSilenceMs: Int = 300) =
|
|
46
|
+
SpeechActivityEngine(
|
|
47
|
+
classifier = classifier,
|
|
48
|
+
minSilenceMs = minSilenceMs,
|
|
49
|
+
emit = { speaking, silentForMs -> events.add(Event(speaking, silentForMs, now)) },
|
|
50
|
+
startedAtMs = now,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
/** 16-bit LE PCM for [samples] samples of [value] (as a fraction of full scale). */
|
|
54
|
+
private fun pcm(samples: Int, value: Float): ByteArray {
|
|
55
|
+
val out = ByteArray(samples * 2)
|
|
56
|
+
val s = (value * 32767).toInt()
|
|
57
|
+
for (i in 0 until samples) {
|
|
58
|
+
out[i * 2] = (s and 0xff).toByte()
|
|
59
|
+
out[i * 2 + 1] = (s shr 8).toByte()
|
|
60
|
+
}
|
|
61
|
+
return out
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Deliver [bytes] in chunks of [chunkBytes], advancing the clock in real time. */
|
|
65
|
+
private fun deliver(engine: SpeechActivityEngine, bytes: ByteArray, chunkBytes: Int) {
|
|
66
|
+
var off = 0
|
|
67
|
+
while (off < bytes.size) {
|
|
68
|
+
val end = minOf(bytes.size, off + chunkBytes)
|
|
69
|
+
now += ((end - off) / 2) * 1000L / SpeechActivityEngine.SAMPLE_RATE
|
|
70
|
+
engine.onAudio(bytes.copyOfRange(off, end), now)
|
|
71
|
+
engine.onIdle(now)
|
|
72
|
+
off = end
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Let wall time pass with no audio, waking the engine the way the worker does. */
|
|
77
|
+
private fun idle(engine: SpeechActivityEngine, forMs: Long) {
|
|
78
|
+
val until = now + forMs
|
|
79
|
+
while (now < until) {
|
|
80
|
+
val wait = engine.nextWakeDelayMs(now) ?: (until - now)
|
|
81
|
+
now = minOf(until, now + wait)
|
|
82
|
+
engine.onIdle(now)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Test
|
|
87
|
+
fun assemblerAnalyzesEverySampleWhateverTheChunkSize() {
|
|
88
|
+
val assembler = PcmFrameAssembler(512)
|
|
89
|
+
val frames = mutableListOf<FloatArray>()
|
|
90
|
+
// A ramp so order and alignment are checkable; odd chunk sizes split samples.
|
|
91
|
+
val total = 512 * 7 + 100
|
|
92
|
+
val bytes = ByteArray(total * 2)
|
|
93
|
+
for (i in 0 until total) {
|
|
94
|
+
bytes[i * 2] = (i and 0xff).toByte()
|
|
95
|
+
bytes[i * 2 + 1] = (i shr 8).toByte()
|
|
96
|
+
}
|
|
97
|
+
var off = 0
|
|
98
|
+
val sizes = intArrayOf(333, 1, 2, 1001, 77, 4096, 5)
|
|
99
|
+
var k = 0
|
|
100
|
+
while (off < bytes.size) {
|
|
101
|
+
val end = minOf(bytes.size, off + sizes[k++ % sizes.size])
|
|
102
|
+
assembler.push(bytes.copyOfRange(off, end)) { frames.add(it) }
|
|
103
|
+
off = end
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
assertEquals(7, frames.size)
|
|
107
|
+
assertEquals(100, assembler.pendingSamples)
|
|
108
|
+
frames.forEachIndexed { f, frame ->
|
|
109
|
+
assertEquals(512, frame.size)
|
|
110
|
+
frame.forEachIndexed { j, value -> assertEquals((f * 512 + j) / 32768f, value, 0f) }
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@Test
|
|
115
|
+
fun assemblerDecodesNegativeSamplesAcrossChunkBoundaries() {
|
|
116
|
+
val assembler = PcmFrameAssembler(2)
|
|
117
|
+
val frames = mutableListOf<FloatArray>()
|
|
118
|
+
// -2 = 0xFFFE, -32768 = 0x8000, split mid-sample.
|
|
119
|
+
assembler.push(byteArrayOf(0xFE.toByte())) { frames.add(it) }
|
|
120
|
+
assembler.push(byteArrayOf(0xFF.toByte(), 0x00)) { frames.add(it) }
|
|
121
|
+
assembler.push(byteArrayOf(0x80.toByte())) { frames.add(it) }
|
|
122
|
+
assertEquals(1, frames.size)
|
|
123
|
+
assertEquals(-2 / 32768f, frames[0][0], 0f)
|
|
124
|
+
assertEquals(-1f, frames[0][1], 0f)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
@Test
|
|
128
|
+
fun nonAlignedChunksAreFullyAnalyzed() {
|
|
129
|
+
val classifier = FakeClassifier(300)
|
|
130
|
+
val engine = engine(classifier)
|
|
131
|
+
// 40 frames of audio delivered in 333-byte (odd) chunks.
|
|
132
|
+
deliver(engine, pcm(512 * 40, 0.5f), 333)
|
|
133
|
+
assertEquals(40, classifier.frames.size)
|
|
134
|
+
assertEquals(40L, engine.framesClassified)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@Test
|
|
138
|
+
fun emitsOnlyOnTransitions() {
|
|
139
|
+
val engine = engine(FakeClassifier(300))
|
|
140
|
+
deliver(engine, pcm(16_000, 0.5f), 640) // 1 s of speech
|
|
141
|
+
deliver(engine, pcm(16_000, 0f), 640) // 1 s of quiet audio
|
|
142
|
+
deliver(engine, pcm(8_000, 0.5f), 640) // speech again
|
|
143
|
+
|
|
144
|
+
assertEquals(listOf(true, false, true), events.map { it.speaking })
|
|
145
|
+
assertEquals(listOf(0L, 300L, 0L), events.map { it.silentForMs })
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
@Test
|
|
149
|
+
fun speechToSilenceReportsWithinTheConfiguredGap() {
|
|
150
|
+
for (minSilenceMs in listOf(300, 800)) {
|
|
151
|
+
events.clear()
|
|
152
|
+
now = 0
|
|
153
|
+
val engine = engine(FakeClassifier(minSilenceMs), minSilenceMs)
|
|
154
|
+
deliver(engine, pcm(16_000, 0.5f), 640)
|
|
155
|
+
val speechEndedAt = now
|
|
156
|
+
deliver(engine, pcm(32_000, 0f), 640)
|
|
157
|
+
|
|
158
|
+
val silent = events.single { !it.speaking }
|
|
159
|
+
val latency = silent.atMs - speechEndedAt
|
|
160
|
+
assertTrue("silence after ${latency}ms for $minSilenceMs", latency in minSilenceMs.toLong()..(minSilenceMs + 80L))
|
|
161
|
+
assertEquals(minSilenceMs.toLong(), silent.silentForMs)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
@Test
|
|
166
|
+
fun audioThatStopsMidSessionProducesSilence() {
|
|
167
|
+
val classifier = FakeClassifier(300)
|
|
168
|
+
val engine = engine(classifier)
|
|
169
|
+
deliver(engine, pcm(16_000, 0.5f), 640) // speaking, then the loudness gate cuts audio
|
|
170
|
+
val audioStoppedAt = now
|
|
171
|
+
assertTrue(engine.speaking)
|
|
172
|
+
|
|
173
|
+
idle(engine, 2_000)
|
|
174
|
+
|
|
175
|
+
val silent = events.last()
|
|
176
|
+
assertFalse(silent.speaking)
|
|
177
|
+
// ~100 ms gap before filling starts from the end of the audio, then ~300 ms of silence.
|
|
178
|
+
val latency = silent.atMs - audioStoppedAt
|
|
179
|
+
assertTrue("silence reported after ${latency}ms", latency in 300L..500L)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
@Test
|
|
183
|
+
fun gapFillingIsRealTimeAndStopsOnceSettled() {
|
|
184
|
+
val classifier = FakeClassifier(300)
|
|
185
|
+
val engine = engine(classifier)
|
|
186
|
+
deliver(engine, pcm(16_000, 0.5f), 640)
|
|
187
|
+
val framesBefore = classifier.frames.size
|
|
188
|
+
|
|
189
|
+
// Shorter than the gap: nothing synthetic yet.
|
|
190
|
+
idle(engine, 90)
|
|
191
|
+
assertEquals(framesBefore, classifier.frames.size)
|
|
192
|
+
|
|
193
|
+
// Past the gap: silent frames for the elapsed time, never ahead of the clock.
|
|
194
|
+
idle(engine, 110)
|
|
195
|
+
val synthetic = classifier.frames.size - framesBefore
|
|
196
|
+
assertTrue("fed $synthetic frames for 200 ms", synthetic in 5..7)
|
|
197
|
+
// The first one completes the partial frame of real audio (16000 % 512 = 128 samples).
|
|
198
|
+
val firstFill = classifier.frames[framesBefore]
|
|
199
|
+
assertTrue(firstFill.take(128).all { it > 0.4f } && firstFill.drop(128).all { it == 0f })
|
|
200
|
+
assertTrue(classifier.frames.drop(framesBefore + 1).all { frame -> frame.all { it == 0f } })
|
|
201
|
+
|
|
202
|
+
// After silence is reported and settled, no more inference while audio stays off.
|
|
203
|
+
idle(engine, 5_000)
|
|
204
|
+
val settled = classifier.frames.size
|
|
205
|
+
idle(engine, 5_000)
|
|
206
|
+
assertEquals(settled, classifier.frames.size)
|
|
207
|
+
assertEquals(null, engine.nextWakeDelayMs(now))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
@Test
|
|
211
|
+
fun missingAudioWhileSilentNeedsNoFill() {
|
|
212
|
+
val classifier = FakeClassifier(300)
|
|
213
|
+
val engine = engine(classifier)
|
|
214
|
+
deliver(engine, pcm(16_000, 0f), 640)
|
|
215
|
+
val frames = classifier.frames.size
|
|
216
|
+
assertEquals(null, engine.nextWakeDelayMs(now))
|
|
217
|
+
idle(engine, 2_000)
|
|
218
|
+
assertEquals(frames, classifier.frames.size)
|
|
219
|
+
assertTrue(events.isEmpty())
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
@Test
|
|
223
|
+
fun fillStopsAsSoonAsSilenceIsReported() {
|
|
224
|
+
val classifier = FakeClassifier(300)
|
|
225
|
+
val engine = engine(classifier)
|
|
226
|
+
deliver(engine, pcm(16_000, 0.5f), 640)
|
|
227
|
+
val framesBefore = classifier.frames.size
|
|
228
|
+
idle(engine, 5_000)
|
|
229
|
+
// 1 frame completing the partial real frame + 10 quiet frames (320 ms) to report silence.
|
|
230
|
+
assertEquals(11, classifier.frames.size - framesBefore)
|
|
231
|
+
assertFalse(events.last().speaking)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
@Test
|
|
235
|
+
fun realAudioResumingStopsTheFill() {
|
|
236
|
+
val classifier = FakeClassifier(300)
|
|
237
|
+
val engine = engine(classifier)
|
|
238
|
+
deliver(engine, pcm(16_000, 0.5f), 640)
|
|
239
|
+
idle(engine, 150) // gap fill has started; still speaking
|
|
240
|
+
assertTrue(engine.speaking)
|
|
241
|
+
deliver(engine, pcm(16_000, 0.5f), 640) // speech resumes
|
|
242
|
+
assertEquals(listOf(true), events.map { it.speaking })
|
|
243
|
+
assertEquals(SpeechActivityEngine.GAP_MS, engine.nextWakeDelayMs(now))
|
|
244
|
+
}
|
|
245
|
+
}
|