@mentra/bluetooth-sdk 3.4.0-dev.999 → 3.5.0-dev.1238
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/README.md +33 -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 +76 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +27 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +98 -120
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +2 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +24 -9
- package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +170 -23
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +13 -316
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +0 -35
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +2 -10
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -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/sgcs/MentraLiveGlassesReadyBurstTest.kt +141 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveRgbLedResponseTest.kt +76 -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 +98 -1
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +42 -7
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js +1 -0
- package/build/_private/BluetoothSdkModule.js.map +1 -1
- package/build/generated/changelogCatalog.d.ts +4 -1
- package/build/generated/changelogCatalog.d.ts.map +1 -1
- package/build/generated/changelogCatalog.js +5 -1
- 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 +6 -0
- package/build/index.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +66 -14
- 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/DeviceManager.swift +43 -22
- package/ios/Source/GeneratedChangelogCatalog.swift +2 -1
- package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
- package/ios/Source/MentraBluetoothSDK.swift +27 -15
- 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/events/BluetoothEvents.swift +41 -0
- package/ios/Source/internal/BluetoothAvailability.swift +15 -5
- package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +5 -0
- package/ios/Source/sgcs/MentraLive.swift +173 -42
- package/ios/Source/sgcs/Nimo.swift +39 -272
- package/ios/Source/sgcs/NimoCanvasCoordinator.swift +9 -38
- package/ios/Source/sgcs/NimoCanvasSession.swift +0 -1
- package/ios/Source/sgcs/SGCManager.swift +6 -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/BesCrashReportTests.swift +142 -0
- package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
- package/ios/Tests/DeviceManagerReconnectTests.swift +0 -71
- package/ios/Tests/GlassesAudioSetupPolicyTests.swift +9 -3
- package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
- package/ios/Tests/MentraLiveAudioSetupTests.swift +6 -12
- package/ios/Tests/MentraLiveCommandQueueTests.swift +35 -0
- package/ios/Tests/MentraLiveSessionReadinessTests.swift +179 -10
- package/ios/Tests/NimoCanvasTransportTests.swift +0 -89
- 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 +9 -11
- package/plugin/build/withKotlinVersion.d.ts +2 -0
- package/plugin/build/withKotlinVersion.js +46 -0
- package/src/BluetoothSdk.types.ts +102 -1
- package/src/_private/BluetoothSdkModule.ts +45 -7
- package/src/generated/changelogCatalog.ts +5 -1
- package/src/generated/releaseMetadata.ts +6 -6
- package/src/index.ts +12 -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/debug/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -274
- 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/release/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -16
- 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 -851
- package/ios/Source/sgcs/NimoMacResourceTransport.swift +0 -217
- package/ios/Source/sgcs/NimoResourceFixture.swift +0 -384
- package/ios/Source/sgcs/NimoResourceSession.swift +0 -406
- package/ios/Tests/NimoResourceSessionTests.swift +0 -476
- /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,81 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.stt
|
|
2
|
+
|
|
3
|
+
import org.junit.Assert.assertEquals
|
|
4
|
+
import org.junit.Assert.assertNull
|
|
5
|
+
import org.junit.Test
|
|
6
|
+
|
|
7
|
+
class BoundedAudioQueueTest {
|
|
8
|
+
private sealed class Item {
|
|
9
|
+
data class Audio(val n: Int) : Item()
|
|
10
|
+
data class Control(val name: String) : Item()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
private fun queue(capacity: Int) = BoundedAudioQueue<Item>(capacity) { it is Item.Audio }
|
|
14
|
+
|
|
15
|
+
@Test
|
|
16
|
+
fun dropsOnlyTheOldestAudioWhenFull() {
|
|
17
|
+
val q = queue(2)
|
|
18
|
+
q.offer(Item.Audio(1))
|
|
19
|
+
q.offer(Item.Control("finish"))
|
|
20
|
+
q.offer(Item.Audio(2))
|
|
21
|
+
val dropped = q.offer(Item.Audio(3))
|
|
22
|
+
|
|
23
|
+
assertEquals(Item.Audio(1), dropped)
|
|
24
|
+
assertEquals(1L, q.droppedAudio)
|
|
25
|
+
assertEquals(Item.Control("finish"), q.poll(0))
|
|
26
|
+
assertEquals(Item.Audio(2), q.poll(0))
|
|
27
|
+
assertEquals(Item.Audio(3), q.poll(0))
|
|
28
|
+
assertNull(q.poll(0))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@Test
|
|
32
|
+
fun neverDropsControlItemsEvenWhenTheWorkerFallsBehind() {
|
|
33
|
+
val q = queue(1)
|
|
34
|
+
repeat(50) { i ->
|
|
35
|
+
q.offer(Item.Control("c$i"))
|
|
36
|
+
q.offer(Item.Audio(i))
|
|
37
|
+
}
|
|
38
|
+
val drained = q.drain()
|
|
39
|
+
assertEquals(50, drained.count { it is Item.Control })
|
|
40
|
+
// Only the newest audio frame fits.
|
|
41
|
+
assertEquals(listOf(Item.Audio(49)), drained.filterIsInstance<Item.Audio>())
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@Test
|
|
45
|
+
fun keepsControlItemsInOrderAfterEarlierAudio() {
|
|
46
|
+
val q = queue(10)
|
|
47
|
+
q.offer(Item.Audio(1))
|
|
48
|
+
q.offer(Item.Audio(2))
|
|
49
|
+
q.offer(Item.Control("finish"))
|
|
50
|
+
q.offer(Item.Audio(3))
|
|
51
|
+
|
|
52
|
+
assertEquals(
|
|
53
|
+
listOf(Item.Audio(1), Item.Audio(2), Item.Control("finish"), Item.Audio(3)),
|
|
54
|
+
List(4) { q.poll(0) },
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Test
|
|
59
|
+
fun clearAudioKeepsControlItems() {
|
|
60
|
+
val q = queue(10)
|
|
61
|
+
q.offer(Item.Audio(1))
|
|
62
|
+
q.offer(Item.Control("reset"))
|
|
63
|
+
q.offer(Item.Audio(2))
|
|
64
|
+
|
|
65
|
+
assertEquals(2, q.clearAudio())
|
|
66
|
+
assertEquals(Item.Control("reset"), q.poll(0))
|
|
67
|
+
assertNull(q.poll(0))
|
|
68
|
+
// Capacity is available again after clearing.
|
|
69
|
+
repeat(10) { q.offer(Item.Audio(it)) }
|
|
70
|
+
assertEquals(0L, q.droppedAudio)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@Test
|
|
74
|
+
fun pollTimesOutWhenEmpty() {
|
|
75
|
+
val q = queue(1)
|
|
76
|
+
val started = System.nanoTime()
|
|
77
|
+
assertNull(q.poll(30))
|
|
78
|
+
val elapsedMs = (System.nanoTime() - started) / 1_000_000
|
|
79
|
+
assert(elapsedMs >= 25) { "poll returned after ${elapsedMs}ms" }
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -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
|
+
}
|