forked from Blendman974/faceclaw
[verified] merge upstream 25cb255b into all-features
This commit is contained in:
commit
ffcdae660d
169 files changed
+16201
-597
No files matched your search
@@ -0,0 +1,63 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import java.io.File
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.sin
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class MicRecordingFileTest {
|
||||
/**
|
||||
* A capture recorded through the real file adapters and read back. Set FACECLAW_MKA_SAMPLE
|
||||
* to a path to keep the file, for checking it against other Matroska tools (ffprobe, mpv).
|
||||
*/
|
||||
@Test
|
||||
fun recordsACaptureToAFileThatReadsBack() {
|
||||
val kept = System.getenv("FACECLAW_MKA_SAMPLE")
|
||||
val file = if (kept.isNullOrEmpty()) File.createTempFile("capture", ".mka") else File(kept)
|
||||
try {
|
||||
val writer = MicRecordingWriter(openFileOutput(file.path), "{\"mics\":4,\"app\":\"test\"}", 1_700_000_000_000)
|
||||
val mixer = SingleTempleMixer()
|
||||
// Five seconds: each temple streams a stereo pair of tones on its own tick.
|
||||
for (index in 0 until 100) {
|
||||
for (side in 0..1) {
|
||||
val pcm = ShortArray(1600) {
|
||||
val frame = index * 800 + it / 2
|
||||
val hz = if (side == 0) 440.0 else 660.0
|
||||
(sin(2 * PI * hz * frame / 16000) * (if (it % 2 == 0) 8000 else 4000)).toInt().toShort()
|
||||
}
|
||||
val chunk = MicChunk(if (side == 0) MicStream.LEFT else MicStream.RIGHT, 2, 16000, pcm, 800, 10_000 + index * 50L + side * 7)
|
||||
chunk.sequence = index
|
||||
chunk.deviceTickMs = (if (side == 0) 500_000L else 123_456L) + index * 50L
|
||||
chunk.hasDirection = true
|
||||
chunk.angleDegrees = if (side == 0) -30 else 25
|
||||
chunk.ssr = 12
|
||||
writer.write(chunk)
|
||||
mixer.mix(chunk)?.let { writer.write(it) }
|
||||
}
|
||||
}
|
||||
// The right temple's chunks arrive 7 ms behind the left's.
|
||||
assertEquals(5007L, writer.finish())
|
||||
|
||||
val reader = MicRecordingReader(openFileInput(file.path))
|
||||
assertEquals(5007L, reader.durationMs)
|
||||
assertEquals("left temple 2ch + right temple 2ch", reader.description)
|
||||
assertEquals("{\"mics\":4,\"app\":\"test\"}", reader.configJson)
|
||||
var left = 0
|
||||
var right = 0
|
||||
while (true) {
|
||||
val chunk = reader.next() ?: break
|
||||
if (chunk.stream == MicStream.LEFT) left++ else right++
|
||||
assertEquals(12, chunk.ssr)
|
||||
}
|
||||
assertEquals(100, left)
|
||||
assertEquals(100, right)
|
||||
reader.close()
|
||||
// Audio (three tracks) plus the frame metadata.
|
||||
assertTrue(file.length() > 100 * 2 * 3200 + 100 * 1600)
|
||||
} finally {
|
||||
if (kept.isNullOrEmpty()) file.delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -347,9 +347,9 @@ class DisplayListTest {
|
||||
val gray = ByteArray(640 * 480) { 48 }; val app = BmpUtil.pack4bppFromGray8(gray, 640, 480)
|
||||
fun layer(key: Int, y: Int, h: Int, nibble: Int, depth: Int = 0) =
|
||||
ShellScene.Layer(key, 14, y, 612, h, 256, ByteArray(306 * h) { (nibble * 17).toByte() }, depth = depth)
|
||||
// Each 612x214 layer is just under the 64 KiB resource cap; three exceed the arena on their own.
|
||||
// Each 612x200 layer takes 60 KiB of the 320 KiB arena: five fit and six exceed it on their own.
|
||||
fun chrome(depth: Int) = layer(1, 444, 36, 7, depth)
|
||||
val modals = listOf(layer(2, 10, 214, 9), layer(3, 120, 214, 11), layer(4, 230, 214, 13, depth = 4))
|
||||
val modals = (0..5).map { layer(2 + it, 10 + 50 * it, 200, 8 + it, depth = if (it == 5) 4 else 0) }
|
||||
val overflow = ShellScene(listOf(chrome(6)) + modals)
|
||||
val plan = planner.plan(app, 640, 480, null, overflow, 1)
|
||||
assertEquals(2, plan.bakedLayers); assertFalse(plan.bakedSelections)
|
||||
@@ -363,7 +363,7 @@ class DisplayListTest {
|
||||
assertContentEquals(BmpUtil.pack4bppFromGray8(expected.preview(gray, 640, 480, glasses.right), 640, 480), glasses.composition)
|
||||
}
|
||||
// Once the shell fits again, the next frame is fully live and the screen is the app's again.
|
||||
val fits = ShellScene(listOf(chrome(0), modals[2]))
|
||||
val fits = ShellScene(listOf(chrome(0), modals.last()))
|
||||
val next = planner.plan(app, 640, 480, null, fits, plan.nextFid)
|
||||
assertEquals(0, next.bakedLayers)
|
||||
for (glasses in lenses) {
|
||||
@@ -372,13 +372,13 @@ class DisplayListTest {
|
||||
assertContentEquals(BmpUtil.pack4bppFromGray8(fits.preview(gray, 640, 480, glasses.right), 640, 480), glasses.composition)
|
||||
}
|
||||
}
|
||||
@Test fun noisyFramesRemainBoundedAndResourceBudgetIs192KiB() {
|
||||
@Test fun noisyFramesRemainBoundedAndResourceBudgetIs322KiB() {
|
||||
val cache=ResourceCacheState();val planner=ScenePlanner(cache);val glasses=Glasses()
|
||||
val app=ByteArray(640*480/2){(it*37).toByte()}
|
||||
val plan=planner.plan(app,640,480,null,ShellScene.EMPTY,1)
|
||||
assertTrue(plan.commands.all { it.size<=65535 });glasses.apply(plan.commands)
|
||||
assertContentEquals(app,glasses.screen);assertContentEquals(app,glasses.composition)
|
||||
assertEquals(196608,ResourceCacheState.CACHE_SIZE)
|
||||
assertEquals(2048+320*1024,ResourceCacheState.CACHE_SIZE)
|
||||
}
|
||||
/** Revision 35. Same calls and expectations as ClipTest in g2flash/tests/test_display_list.py. */
|
||||
@Test fun clipRectsNarrowEveryOpNestThroughListsAndMoveWithDepth() {
|
||||
|
||||
@@ -1130,7 +1130,8 @@ class G1FeaturesTest {
|
||||
}
|
||||
s.core.setImuReportEnabled(true, 200)
|
||||
assertTrue(awaitTrue(3_000) { lock.withLock { readings.size >= 5 } }, "samples: " + lock.withLock { readings.size } + "\n" + s.tail())
|
||||
assertEquals(listOf(0.1, -1.0, 0.25, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() })
|
||||
// The G1's (x, y, z) arrive in the G2's frame as (-z, -x, y) (ImuOrientation).
|
||||
assertEquals(listOf(-0.25, -0.1, -1.0, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() })
|
||||
assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_STREAM } })
|
||||
s.core.setImuReportEnabled(false, 0)
|
||||
assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_IDLE } })
|
||||
|
||||
@@ -316,6 +316,197 @@ private class Session {
|
||||
}
|
||||
|
||||
class GlassesSessionCoreTest {
|
||||
@Test fun headUpUnsetPreferencesKeepUpstreamRightArmPolicyAndRestore() {
|
||||
for (preferences in listOf<Pair<Int, Int>?>(null, -1 to -1)) {
|
||||
val s = Session()
|
||||
try {
|
||||
s.core.monitor.withLock {
|
||||
s.core.running = true
|
||||
s.core.sessionReady = true
|
||||
s.core.headUpPreferences = preferences
|
||||
s.core.headUpSwitch = 0
|
||||
s.core.headUpAngle = 10
|
||||
s.core.headUpCalibration = 25
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
assertTrue(s.core.pendingMessages.isEmpty(), "unset preferences leave firmware alone")
|
||||
s.core.setHeadUpTrigger(20, -1, 0)
|
||||
val writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
|
||||
assertEquals(2, writes.size)
|
||||
assertTrue(writes.none { it.isLeftArmMessage })
|
||||
assertContentEquals(BleProtocol.buildSetHeadUpWake(writes[0].magic, true), writes[0].message,
|
||||
"unset preferences preserve upstream switch-before-angle ordering")
|
||||
assertContentEquals(BleProtocol.buildSetHeadUp(writes[1].magic, BleProtocol.HEAD_UP_FIELD_ANGLE, -5), writes[1].message)
|
||||
s.core.setHeadUpTrigger(0, -1, 0)
|
||||
assertTrue(s.core.pendingMessages.isEmpty())
|
||||
assertFalse(s.core.headUpRestorePending)
|
||||
}
|
||||
} finally { s.core.close() }
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun headUpPartialPreferencesRestoreOnlyUnspecifiedFieldsFromOriginal() {
|
||||
val s = Session()
|
||||
try {
|
||||
s.core.monitor.withLock {
|
||||
s.core.running = true
|
||||
s.core.sessionReady = true
|
||||
s.core.headUpSwitch = 0
|
||||
s.core.headUpAngle = 10
|
||||
s.core.headUpCalibration = 5
|
||||
s.core.configureHeadUpWake(-1, 30)
|
||||
s.core.setHeadUpTrigger(20, 0, 10)
|
||||
assertEquals(4, s.core.pendingMessages.count { it.kind == "head-up-control" })
|
||||
s.core.headUpSwitch = 1
|
||||
s.core.headUpAngle = 15
|
||||
s.core.setHeadUpTrigger(0, 0, 10)
|
||||
val writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
|
||||
assertEquals(4, writes.size)
|
||||
assertTrue(writes.filter { it.label?.contains("angle=") == true }.all { it.label == "head-up angle=30" })
|
||||
assertTrue(writes.filter { it.label?.contains("wake=") == true }.all { it.label == "head-up wake=false" })
|
||||
}
|
||||
} finally { s.core.close() }
|
||||
}
|
||||
|
||||
@Test fun headUpQueueCancellationAndTimeoutReapplyCompletePolicy() {
|
||||
val s = Session()
|
||||
try {
|
||||
s.core.monitor.withLock {
|
||||
s.core.sessionReady = true
|
||||
// Matching right-arm settings cannot prove the left arm is set.
|
||||
s.core.headUpSwitch = 0
|
||||
s.core.headUpAngle = 30
|
||||
s.core.configureHeadUpWake(0, 30)
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
assertEquals(4, s.core.pendingMessages.size)
|
||||
s.core.clearPendingMessagesLocked("test cancellation")
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
assertEquals(4, s.core.pendingMessages.size)
|
||||
val timeout = s.core.pendingMessages.first().onTimeout!!
|
||||
while (s.core.pendingMessages.isNotEmpty()) {
|
||||
val m = s.core.pendingMessages.removeFirst()
|
||||
s.core.magicPool.release(m.sid, m.magic, m.label, "test ack")
|
||||
}
|
||||
timeout.run()
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
assertEquals(4, s.core.pendingMessages.size)
|
||||
}
|
||||
} finally { s.core.close() }
|
||||
}
|
||||
|
||||
@Test fun headUpQueuedOffSurvivesGlanceboardQueueReplacement() {
|
||||
val s = Session()
|
||||
try {
|
||||
s.core.monitor.withLock {
|
||||
s.core.running = true
|
||||
s.core.sessionReady = true
|
||||
s.core.headUpSwitch = 1
|
||||
s.core.configureHeadUpWake(0, 30)
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
s.core.setHeadUpTrigger(20, -1, 0)
|
||||
assertEquals(4, s.core.pendingMessages.count { it.kind == "head-up-control" },
|
||||
"clearing the old batch must invalidate its optimistic sent cache")
|
||||
assertTrue(s.core.pendingMessages.none { it.label == "head-up wake=true" })
|
||||
}
|
||||
} finally { s.core.close() }
|
||||
}
|
||||
|
||||
@Test fun headUpPreferenceChangeWaitsForInflightThenReplacesBothArms() {
|
||||
val s = Session()
|
||||
try {
|
||||
s.core.monitor.withLock {
|
||||
s.core.sessionReady = true
|
||||
s.core.headUpSwitch = 1
|
||||
s.core.configureHeadUpWake(1, 30)
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
val inflight = s.core.pendingMessages.removeFirst()
|
||||
s.core.inFlightMessages.addLast(inflight)
|
||||
s.core.configureHeadUpWake(0, 40)
|
||||
assertTrue(s.core.pendingMessages.none { it.kind == "head-up-control" },
|
||||
"unsent old-policy writes must be cancelled")
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
assertTrue(s.core.pendingMessages.isEmpty(), "do not race an old in-flight write")
|
||||
s.core.inFlightMessages.remove(inflight)
|
||||
s.core.magicPool.release(inflight.sid, inflight.magic, inflight.label, "test ack")
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
assertEquals(4, s.core.pendingMessages.count { it.kind == "head-up-control" })
|
||||
assertTrue(s.core.pendingMessages.filter { it.label?.contains("wake=") == true }
|
||||
.all { it.label == "head-up wake=false" })
|
||||
}
|
||||
} finally { s.core.close() }
|
||||
}
|
||||
|
||||
@Test fun headUpGlanceboardOverrideRestoresForkBaselineNotStaleOriginal() {
|
||||
val s = Session()
|
||||
try {
|
||||
s.core.monitor.withLock {
|
||||
s.core.running = true
|
||||
s.core.sessionReady = true
|
||||
s.core.headUpSwitch = 1
|
||||
s.core.headUpAngle = 30
|
||||
s.core.headUpCalibration = 25
|
||||
s.core.configureHeadUpWake(1, 30)
|
||||
s.core.setHeadUpTrigger(20, 0, 10)
|
||||
var writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
|
||||
assertEquals(4, writes.size)
|
||||
assertEquals(setOf(true, false), writes.map { it.isLeftArmMessage }.toSet())
|
||||
assertTrue(writes.filter { it.label?.contains("angle=") == true }.all {
|
||||
it.message.contentEquals(BleProtocol.buildSetHeadUp(it.magic, BleProtocol.HEAD_UP_FIELD_ANGLE, -5))
|
||||
}, "Glanceboard offsets stay signed, not fork-clamped")
|
||||
s.core.setHeadUpTrigger(0, 0, 10)
|
||||
writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
|
||||
assertEquals(4, writes.size)
|
||||
assertTrue(writes.filter { it.label?.contains("angle=") == true }.all { it.label == "head-up angle=30" })
|
||||
assertTrue(writes.filter { it.label?.contains("wake=") == true }.all { it.label == "head-up wake=true" })
|
||||
}
|
||||
} finally { s.core.close() }
|
||||
}
|
||||
|
||||
@Test fun headUpRefusedSettingRetriesOnRegularPollNotSenderLoop() {
|
||||
val s = Session()
|
||||
try {
|
||||
s.core.monitor.withLock {
|
||||
s.core.sessionReady = true
|
||||
s.core.headUpSwitch = 1
|
||||
s.core.configureHeadUpWake(0, 30)
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
while (s.core.pendingMessages.isNotEmpty()) {
|
||||
val m = s.core.pendingMessages.removeFirst()
|
||||
s.core.magicPool.release(m.sid, m.magic, m.label, "test ack")
|
||||
}
|
||||
val read = BleProtocol.encodeBytesField(4,
|
||||
BleProtocol.encodeVarintField(7, 1) + BleProtocol.encodeVarintField(8, 10)) + byteArrayOf(0, 0)
|
||||
s.core.reconcileHeadUpLocked(read)
|
||||
s.core.enqueueHeadUpPreferencesLocked()
|
||||
assertTrue(s.core.pendingMessages.isEmpty(), "verifying disagreement must not spin")
|
||||
s.core.reconcileHeadUpLocked(read)
|
||||
assertEquals(4, s.core.pendingMessages.count { it.kind == "head-up-control" },
|
||||
"next regular poll retries a refused batch")
|
||||
}
|
||||
} finally { s.core.close() }
|
||||
}
|
||||
|
||||
@Test fun explicitHeadUpOffWinsOverGlanceboardOnBothArms() {
|
||||
val s = Session()
|
||||
try {
|
||||
s.core.monitor.withLock {
|
||||
s.core.sessionReady = true
|
||||
s.core.headUpSwitch = 1
|
||||
s.core.headUpAngle = 10
|
||||
s.core.headUpCalibration = 5
|
||||
s.core.configureHeadUpWake(0, 30)
|
||||
s.core.desiredHeadUpTrigger = 20
|
||||
s.core.applyHeadUpLocked(false)
|
||||
val writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
|
||||
assertEquals(4, writes.size, "one effective policy must reach both arms")
|
||||
for (left in listOf(true, false)) {
|
||||
val arm = writes.filter { it.isLeftArmMessage == left }
|
||||
assertContentEquals(BleProtocol.buildSetHeadUpAngle(arm[0].magic, 30), arm[0].message)
|
||||
assertContentEquals(BleProtocol.buildSetHeadUpWake(arm[1].magic, false), arm[1].message)
|
||||
}
|
||||
}
|
||||
} finally { s.core.close() }
|
||||
}
|
||||
|
||||
@Test fun headUpEnqueueIsG2OnlyAndPreservesPreferencesForOtherModels() {
|
||||
for (model in listOf(GlassesType.G2, GlassesType.G1, GlassesType.RAYNEO_IO)) {
|
||||
val s = Session()
|
||||
@@ -900,4 +1091,62 @@ class GlassesSessionCoreTest {
|
||||
s.core.close()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keyboardControlsGoToTheRightArmAndKeyboardRecordsReachTheirListenerOnly() {
|
||||
val s = Session()
|
||||
val records = ArrayList<ByteArray>()
|
||||
val lock = s.platform.createLock()
|
||||
val listener = object : FaceclawKeyboardEventListener {
|
||||
override fun onKeyboardEvent(body: ByteArray?) { lock.withLock { records.add(body!!) } }
|
||||
}
|
||||
s.core.addKeyboardEventListener(listener)
|
||||
// ['K','B',1,QUERY, request 7, epoch 0]
|
||||
val control = byteArrayOf(75, 66, 1, 0, 7, 0, 0, 0)
|
||||
try {
|
||||
assertFalse(s.core.sendFaceclawKeyboardControl(control, "query"), "refused before the session is up")
|
||||
s.startAndAwaitLayout()
|
||||
assertFalse(s.core.sendFaceclawKeyboardControl(null, "none"))
|
||||
assertFalse(s.core.sendFaceclawKeyboardControl(byteArrayOf(75, 66, 1), "short"))
|
||||
assertTrue(s.core.sendFaceclawKeyboardControl(control, "query"))
|
||||
fun controls() = s.link.writes.filter { it.sid == BleProtocol.SID_UI_SETTING && it.pb.containsSequence(control) }
|
||||
assertTrue(waitUntil(5_000) { controls().isNotEmpty() })
|
||||
// One copy, down the right arm: a second one would run the control twice.
|
||||
assertEquals(listOf(RIGHT), controls().map { it.address })
|
||||
assertContentEquals(
|
||||
BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, 0) +
|
||||
BleProtocol.encodeBytesField(BleProtocol.FACECLAW_KEYBOARD_CONTROL_FIELD, control),
|
||||
controls().single().pb,
|
||||
)
|
||||
|
||||
// A field-131 record: ['K','B',1,STATUS, seq 1, epoch 0, request 7, ...] with a 10-byte body.
|
||||
val record = byteArrayOf(75, 66, 1, 1, 1, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 10, 0, 0, 0, 1, 2, 3, 4) + ByteArray(10)
|
||||
fun push(address: String, body: ByteArray, flag: Int) = s.core.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID,
|
||||
BleProtocol.framePb(
|
||||
BleProtocol.encodeVarintField(1, 3) + BleProtocol.encodeVarintField(2, 0) +
|
||||
BleProtocol.encodeBytesField(BleProtocol.FACECLAW_KEYBOARD_EVENT_FIELD, body),
|
||||
BleProtocol.SID_UI_SETTING, flag, 33)[0])
|
||||
val eventsBefore = s.listener.events.size
|
||||
val activityBefore = s.core.monitor.withLock { s.core.lastConnectionOrInputAtMs }
|
||||
s.platform.offsetMs += 5_000
|
||||
push(RIGHT, record, BleProtocol.FLAG_NOTIFY)
|
||||
// Whichever temple hosts the keyboard reports it, and the flag doesn't matter.
|
||||
push(LEFT, record, BleProtocol.FLAG_REQUEST)
|
||||
assertEquals(2, lock.withLock { records.size })
|
||||
assertContentEquals(record, lock.withLock { records[0] })
|
||||
// Not glasses input, not user activity, and not an ACK for anything.
|
||||
assertEquals(eventsBefore, s.listener.events.size)
|
||||
assertEquals(activityBefore, s.core.monitor.withLock { s.core.lastConnectionOrInputAtMs })
|
||||
assertFalse(s.host.hasLog("unexpected ack"), s.host.logs().takeLast(5).toString())
|
||||
// Too short to hold the fixed header, or not a keyboard record: left to the normal path.
|
||||
push(RIGHT, record.copyOf(23), BleProtocol.FLAG_NOTIFY)
|
||||
push(RIGHT, byteArrayOf(77, 67) + ByteArray(30), BleProtocol.FLAG_NOTIFY)
|
||||
assertEquals(2, lock.withLock { records.size })
|
||||
s.core.removeKeyboardEventListener(listener)
|
||||
push(RIGHT, record, BleProtocol.FLAG_NOTIFY)
|
||||
assertEquals(2, lock.withLock { records.size })
|
||||
} finally {
|
||||
s.core.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class HeadUpProtocolTest {
|
||||
private fun bytes(hex: String) = hex.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
|
||||
@@ -14,4 +17,46 @@ class HeadUpProtocolTest {
|
||||
assertContentEquals(bytes("080110071a0422021000"), BleProtocol.buildSetHeadUpAngle(7, -10))
|
||||
assertContentEquals(bytes("080110071a042202103c"), BleProtocol.buildSetHeadUpAngle(7, 100))
|
||||
}
|
||||
|
||||
@Test fun generalizedAngleKeepsSignedOffsetsAndDoesNotUseForkClamp() {
|
||||
assertContentEquals(bytes("080110071a08220610f6ffffff0f"),
|
||||
BleProtocol.buildSetHeadUp(7, BleProtocol.HEAD_UP_FIELD_ANGLE, -10))
|
||||
assertContentEquals(bytes("080110071a042202105a"),
|
||||
BleProtocol.buildSetHeadUp(7, BleProtocol.HEAD_UP_FIELD_ANGLE, 90))
|
||||
// The generic API must also retain fields other than switch/angle.
|
||||
assertContentEquals(bytes("080110071a0422021801"), BleProtocol.buildSetHeadUp(7, 3, 1))
|
||||
}
|
||||
|
||||
@Test fun buildersPreserveGeneralizedValuesAndForkArmTargeting() {
|
||||
val builder = MessageBuilder(BleMagicPool(testPlatform()))
|
||||
val generic = builder.setHeadUp(BleProtocol.HEAD_UP_FIELD_ANGLE, -10)
|
||||
assertContentEquals(BleProtocol.buildSetHeadUp(generic.magic, BleProtocol.HEAD_UP_FIELD_ANGLE, -10), generic.message)
|
||||
assertFalse(generic.isLeftArmMessage)
|
||||
assertEquals("head-up field 2=-10", generic.label)
|
||||
for (leftArm in listOf(true, false)) {
|
||||
val angle = builder.headUpControl(leftArm, null, 100)
|
||||
assertEquals(leftArm, angle.isLeftArmMessage)
|
||||
assertEquals("head-up angle=100", angle.label)
|
||||
assertContentEquals(BleProtocol.buildSetHeadUpAngle(angle.magic, 100), angle.message)
|
||||
val wake = builder.headUpControl(leftArm, false, -1)
|
||||
assertEquals(leftArm, wake.isLeftArmMessage)
|
||||
assertEquals("head-up wake=false", wake.label)
|
||||
assertContentEquals(BleProtocol.buildSetHeadUpWake(wake.magic, false), wake.message)
|
||||
for (message in listOf(generic, angle, wake)) {
|
||||
assertEquals("head-up-control", message.kind)
|
||||
assertEquals(BleProtocol.SID_UI_SETTING, message.sid)
|
||||
assertEquals(BleProtocol.FLAG_REQUEST, message.flag)
|
||||
assertEquals(3_500, message.ackTimeoutMs)
|
||||
assertEquals(-1, message.tileIndex)
|
||||
}
|
||||
assertTrue(angle.magic != wake.magic)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun settingsReadKeepsSignedAngleAndCalibration() {
|
||||
// parseSettingsHeadUp accepts a payload with its trailing CRC still attached.
|
||||
val reply = bytes("08021007220e380140f6ffffff0f4882ffffff0f0000")
|
||||
assertContentEquals(intArrayOf(1, -10, -126), BleProtocol.parseSettingsHeadUp(reply))
|
||||
assertContentEquals(intArrayOf(0, 0, 0), BleProtocol.parseSettingsHeadUp(bytes("22000000")))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.sqrt
|
||||
import kotlin.test.*
|
||||
|
||||
class ImuOrientationTest {
|
||||
private val s = sqrt(0.5)
|
||||
|
||||
private fun assertVec(expected: DoubleArray, actual: DoubleArray, what: String) {
|
||||
for (i in 0..2) assertTrue(abs(expected[i] - actual[i]) < 1e-9, "$what: expected ${expected.toList()}, got ${actual.toList()}")
|
||||
}
|
||||
|
||||
/** The poses measured on hardware (level, 45° up, 45° to the right) read the G2's values on every model. */
|
||||
@Test fun measuredPosesMatchTheG2Frame() {
|
||||
val level = doubleArrayOf(0.0, 0.0, 1.0)
|
||||
val up = doubleArrayOf(s, 0.0, s)
|
||||
val right = doubleArrayOf(0.0, s, s)
|
||||
val g1 = listOf(doubleArrayOf(0.0, 1.0, 0.0), doubleArrayOf(0.0, s, -s), doubleArrayOf(-s, s, 0.0))
|
||||
for ((name, canonical, raw) in listOf(Triple("level", level, g1[0]), Triple("up", up, g1[1]), Triple("right", right, g1[2]))) {
|
||||
assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G1, raw[0], raw[1], raw[2]), "G1 $name")
|
||||
assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G2, canonical[0], canonical[1], canonical[2]), "G2 $name")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,572 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/** A file held in memory: written through [SeekableOutput], read back through [input]. */
|
||||
internal class MemoryFile : SeekableOutput {
|
||||
var bytes = ByteArray(0)
|
||||
var closed = false
|
||||
override val size: Long get() = bytes.size.toLong()
|
||||
|
||||
override fun write(bytes: ByteArray, offset: Int, length: Int) {
|
||||
this.bytes += bytes.copyOfRange(offset, offset + length)
|
||||
}
|
||||
|
||||
override fun writeAt(position: Long, bytes: ByteArray) {
|
||||
bytes.copyInto(this.bytes, position.toInt())
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
closed = true
|
||||
}
|
||||
|
||||
/** The file as it stands, or its first [length] bytes. */
|
||||
fun input(length: Int = bytes.size): SeekableInput {
|
||||
val snapshot = bytes.copyOf(length)
|
||||
return object : SeekableInput {
|
||||
override val length = snapshot.size.toLong()
|
||||
|
||||
override fun readAt(position: Long, buffer: ByteArray, offset: Int, length: Int): Int {
|
||||
val count = minOf(length.toLong(), maxOf(0L, snapshot.size - position)).toInt()
|
||||
snapshot.copyInto(buffer, offset, position.toInt(), position.toInt() + count)
|
||||
return count
|
||||
}
|
||||
|
||||
override fun close() {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun pcmBytes(frames: Int, channels: Int, value: Int): ByteArray =
|
||||
AudioSegmentation.pcm16ToLittleEndian(ShortArray(frames * channels) { value.toShort() }, 0, frames * channels)
|
||||
|
||||
private fun chunk(stream: Int, timeMs: Long, value: Int, channels: Int = 1, frames: Int = 800): MicChunk =
|
||||
MicChunk(stream, channels, 16000, ShortArray(frames * channels) { (value + it % channels).toShort() }, frames, timeMs)
|
||||
|
||||
private fun drain(reader: MicRecordingReader): List<MicChunk> {
|
||||
val out = ArrayList<MicChunk>()
|
||||
while (true) out.add(reader.next() ?: break)
|
||||
return out
|
||||
}
|
||||
|
||||
class MatroskaTest {
|
||||
@Test
|
||||
fun roundTripsTracksBlocksAndAttachments() {
|
||||
val file = MemoryFile()
|
||||
val writer = MkaWriter(file, "session", 1_700_000_000_000, listOf(MkaAttachment("capture.json", "application/json", "{\"a\":1}".encodeToByteArray())))
|
||||
val temple = writer.addAudioTrack("left temple", 16000, 2, false)
|
||||
val text = writer.addTextTrack("frames")
|
||||
for (index in 0 until 30) {
|
||||
writer.writeText(text, index * 50L, 50, "{\"n\":$index}")
|
||||
writer.writeAudio(temple, index * 50L, pcmBytes(800, 2, index), 0, 3200)
|
||||
}
|
||||
// A track that first appears mid-file, and is the one players should pick.
|
||||
val mix = writer.addAudioTrack("mix", 16000, 1, true)
|
||||
for (index in 30 until 60) {
|
||||
writer.writeAudio(temple, index * 50L, pcmBytes(800, 2, index), 0, 3200)
|
||||
writer.writeAudio(mix, index * 50L, pcmBytes(800, 1, -index), 0, 1600)
|
||||
}
|
||||
assertEquals(3000L, writer.durationMs)
|
||||
writer.finish()
|
||||
assertTrue(file.closed)
|
||||
|
||||
val reader = MkaReader(file.input())
|
||||
// Listed with the preferred audio track first, then the others, then text; numbers are as added.
|
||||
assertEquals(listOf("mix", "left temple", "frames"), reader.tracks.map { it.name })
|
||||
assertEquals(listOf(mix, temple, text), reader.tracks.map { it.number })
|
||||
val templeInfo = reader.track(temple)!!
|
||||
assertTrue(templeInfo.isPcm16)
|
||||
assertEquals(2, templeInfo.channels)
|
||||
assertEquals(16000, templeInfo.sampleRate)
|
||||
assertFalse(reader.track(text)!!.isPcm16)
|
||||
assertEquals("{\"a\":1}", reader.attachments.single().data.decodeToString())
|
||||
assertEquals("application/json", reader.attachments.single().mimeType)
|
||||
assertEquals(3000L, reader.durationMs)
|
||||
|
||||
val blocks = ArrayList<MkaBlock>()
|
||||
while (true) blocks.add(reader.next() ?: break)
|
||||
assertEquals(30 + 60 + 30, blocks.size)
|
||||
assertEquals(text, blocks[0].track)
|
||||
assertEquals("{\"n\":0}", blocks[0].data.decodeToString())
|
||||
assertEquals(50L, blocks[0].durationMs)
|
||||
val templeBlocks = blocks.filter { it.track == temple }
|
||||
assertEquals((0 until 60).map { it * 50L }, templeBlocks.map { it.timeMs })
|
||||
assertEquals(pcmBytes(800, 2, 59).toList(), templeBlocks.last().data.toList())
|
||||
assertEquals(1500L, blocks.first { it.track == mix }.timeMs)
|
||||
|
||||
reader.seekTo(2000)
|
||||
val afterSeek = ArrayList<MkaBlock>()
|
||||
while (true) afterSeek.add(reader.next() ?: break)
|
||||
assertEquals(2000L, afterSeek.first().timeMs)
|
||||
assertEquals(40, afterSeek.size)
|
||||
reader.seekTo(99_000)
|
||||
assertNull(reader.next())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun recordingCutShortIsReadableUpToItsLastCompleteBlock() {
|
||||
val file = MemoryFile()
|
||||
val writer = MkaWriter(file, null, 0, emptyList())
|
||||
val track = writer.addAudioTrack("mic", 16000, 1, true)
|
||||
// 2.5 s: clusters of a second each reach the file as they fill; the last half-second is still buffered.
|
||||
for (index in 0 until 50) writer.writeAudio(track, index * 50L, pcmBytes(800, 1, index), 0, 1600)
|
||||
val whole = MkaReader(file.input())
|
||||
assertEquals("mic", whole.tracks.single().name)
|
||||
var count = 0
|
||||
while (whole.next() != null) count++
|
||||
assertEquals(40, count)
|
||||
assertEquals(2000L, whole.durationMs)
|
||||
// A crash mid-write tears the last cluster: its complete blocks still read.
|
||||
val torn = MkaReader(file.input(file.bytes.size - 1000))
|
||||
count = 0
|
||||
while (torn.next() != null) count++
|
||||
assertEquals(39, count)
|
||||
assertEquals(1950L, torn.durationMs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsOtherFiles() {
|
||||
val wav = MemoryFile()
|
||||
wav.write(BinaryEncoding.wavHeader(0, 16000, 1), 0, 44)
|
||||
assertFailsWith<IllegalArgumentException> { MkaReader(wav.input()) }
|
||||
assertFailsWith<IllegalArgumentException> { MkaReader(MemoryFile().input()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun paddedRegionsFillTheirReservedSpaceWithAVoidElement() {
|
||||
assertNull(EbmlWriter.padded(ByteArray(10), 9))
|
||||
assertNull(EbmlWriter.padded(ByteArray(10), 11))
|
||||
assertEquals(listOf(1, 2, 0xEC, 0x80), EbmlWriter.padded(byteArrayOf(1, 2), 4)!!.map { it.toInt() and 0xff })
|
||||
val long = EbmlWriter.padded(ByteArray(0), 300)!!
|
||||
assertEquals(300, long.size)
|
||||
// An eight-byte size field: 300 - 9 = 0x123.
|
||||
assertEquals(listOf(0xEC, 0x01, 0, 0, 0, 0, 0, 0x01, 0x23), long.take(9).map { it.toInt() and 0xff })
|
||||
}
|
||||
}
|
||||
|
||||
class MicRecordingTest {
|
||||
private val warnings = mutableListOf<String>()
|
||||
|
||||
private fun recorder(file: MemoryFile, config: String? = "{\"mics\":4}") =
|
||||
MicRecordingWriter(file, config, 1_700_000_000_000, testPlatform()) { warnings.add(it) }
|
||||
|
||||
@Test
|
||||
fun recordsEveryStreamWithItsMetadataAndReplaysTheSources() {
|
||||
val file = MemoryFile()
|
||||
val writer = recorder(file)
|
||||
for (index in 0 until 4) {
|
||||
val left = chunk(MicStream.LEFT, 5000 + index * 50L, 100 + index, channels = 2)
|
||||
left.sequence = 7 + index
|
||||
left.deviceTickMs = 900_000 + index * 50L
|
||||
left.hasDirection = true
|
||||
left.angleDegrees = -12
|
||||
left.ssr = 40
|
||||
left.truncated = index == 3
|
||||
writer.write(left)
|
||||
val feed = SingleTempleMixer().mix(left)!!
|
||||
writer.write(feed)
|
||||
val right = chunk(MicStream.RIGHT, 5010 + index * 50L, 200 + index, channels = 2)
|
||||
right.sequence = 3 + index
|
||||
right.deviceTickMs = 40_000 + index * 50L
|
||||
writer.write(right)
|
||||
}
|
||||
// The capture went back to the mixed stream part-way through.
|
||||
writer.event("{\"event\":\"capture\",\"mics\":1}")
|
||||
val mono = chunk(MicStream.MONO, 5200, 55)
|
||||
mono.hasDirection = true
|
||||
mono.angleDegrees = 30
|
||||
mono.ssr = 9
|
||||
writer.write(mono)
|
||||
assertEquals(250L, writer.finish())
|
||||
assertFalse(writer.failed)
|
||||
assertTrue(warnings.isEmpty(), warnings.toString())
|
||||
|
||||
val mka = MkaReader(file.input())
|
||||
// What a player picks by default is a mixed track.
|
||||
assertEquals(listOf("mix", "mic", "left temple", "right temple", "frames"), mka.tracks.map { it.name })
|
||||
|
||||
val reader = MicRecordingReader(file.input())
|
||||
assertEquals("{\"mics\":4}", reader.configJson)
|
||||
assertEquals("mic + left temple 2ch + right temple 2ch", reader.description)
|
||||
assertEquals(250L, reader.durationMs)
|
||||
val chunks = drain(reader)
|
||||
// The mix is derived again on a replay, so only the sources come back.
|
||||
assertEquals(4, chunks.count { it.stream == MicStream.LEFT })
|
||||
assertEquals(4, chunks.count { it.stream == MicStream.RIGHT })
|
||||
assertEquals(1, chunks.count { it.stream == MicStream.MONO })
|
||||
val left = chunks.filter { it.stream == MicStream.LEFT }
|
||||
assertEquals(listOf(0L, 50L, 100L, 150L), left.map { it.timeMs })
|
||||
assertEquals(listOf(7, 8, 9, 10), left.map { it.sequence })
|
||||
assertEquals(900_050L, left[1].deviceTickMs)
|
||||
assertEquals(2, left[0].channels)
|
||||
assertEquals(listOf<Short>(100, 101, 100, 101), left[0].pcm.take(4))
|
||||
assertTrue(left.all { it.hasDirection && it.angleDegrees == -12 && it.ssr == 40 })
|
||||
assertEquals(listOf(false, false, false, true), left.map { it.truncated })
|
||||
val right = chunks.filter { it.stream == MicStream.RIGHT }
|
||||
assertEquals(listOf(10L, 60L, 110L, 160L), right.map { it.timeMs })
|
||||
assertFalse(right[0].hasDirection)
|
||||
val replayedMono = chunks.single { it.stream == MicStream.MONO }
|
||||
assertEquals(200L, replayedMono.timeMs)
|
||||
assertEquals(30, replayedMono.angleDegrees)
|
||||
assertEquals(800, replayedMono.frames)
|
||||
|
||||
reader.seekTo(100)
|
||||
assertEquals(100L, reader.next()!!.timeMs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun chunksHeldUpInDeliveryLandWhereTheyWereCaptured() {
|
||||
val file = MemoryFile()
|
||||
val writer = recorder(file, null)
|
||||
// A temple's ticks say when each chunk was captured. The first ten arrive as captured;
|
||||
// the link then stalls for a second and delivers the next twenty at once.
|
||||
for (index in 0 until 30) {
|
||||
val arrival = if (index < 10) 1000 + index * 50L else maxOf(2500L, 1000 + index * 50L)
|
||||
val chunk = chunk(MicStream.LEFT, arrival, index)
|
||||
chunk.deviceTickMs = 77_000 + index * 50L
|
||||
writer.write(chunk)
|
||||
}
|
||||
// Two chunks were lost on the way (their ticks are skipped): a gap, not a splice.
|
||||
val late = chunk(MicStream.LEFT, 2650, 99)
|
||||
late.deviceTickMs = 77_000 + 32 * 50L
|
||||
writer.write(late)
|
||||
writer.finish()
|
||||
val times = drain(MicRecordingReader(file.input())).map { it.timeMs }
|
||||
assertEquals((0 until 30).map { it * 50L } + 1600L, times)
|
||||
assertNull(MicRecordingReader(file.input()).configJson)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aSourceWithoutAClockIsPlacedBySampleCount() {
|
||||
val file = MemoryFile()
|
||||
val writer = recorder(file)
|
||||
for (index in 0 until 10) {
|
||||
// Arrivals jitter around the capture cadence; the stream's samples are contiguous.
|
||||
val jitter = if (index % 3 == 1) 30L else 0L
|
||||
writer.write(chunk(MicStream.MONO, 400 + index * 50L + jitter, index))
|
||||
}
|
||||
// The decoder reported one packet missing before this chunk.
|
||||
val afterLoss = chunk(MicStream.MONO, 400 + 11 * 50L, 11)
|
||||
afterLoss.lostFramesBefore = 800
|
||||
writer.write(afterLoss)
|
||||
// A capture that resumes much later is placed by its arrival, not spliced on.
|
||||
writer.write(chunk(MicStream.MONO, 60_000, 12))
|
||||
writer.finish()
|
||||
val chunks = drain(MicRecordingReader(file.input()))
|
||||
assertEquals((0 until 10).map { it * 50L } + 550L + 59_600L, chunks.map { it.timeMs })
|
||||
assertEquals(800, chunks[10].lostFramesBefore)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aForeignFilePlaysItsFirstTrackAsTheMicrophone() {
|
||||
val file = MemoryFile()
|
||||
val mka = MkaWriter(file, null, 0, emptyList())
|
||||
val track = mka.addAudioTrack("Stereo", 16000, 2, false)
|
||||
// One long block of stereo frames [100, 300].
|
||||
val samples = ShortArray(2000 * 2) { if (it % 2 == 0) 100 else 300 }
|
||||
mka.writeAudio(track, 0, AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size), 0, samples.size * 2)
|
||||
mka.finish()
|
||||
val reader = MicRecordingReader(file.input())
|
||||
assertEquals("mic 2ch", reader.description)
|
||||
val chunks = drain(reader)
|
||||
assertEquals(listOf(800, 800, 400), chunks.map { it.frames })
|
||||
assertEquals(listOf(0L, 50L, 100L), chunks.map { it.timeMs })
|
||||
assertTrue(chunks.all { it.stream == MicStream.MONO && it.channels == 1 && it.pcm.all { sample -> sample == 200.toShort() } })
|
||||
|
||||
val fast = MemoryFile()
|
||||
val other = MkaWriter(fast, null, 0, emptyList())
|
||||
other.writeAudio(other.addAudioTrack("mic", 48000, 1, true), 0, pcmBytes(480, 1, 1), 0, 960)
|
||||
other.finish()
|
||||
val error = assertFailsWith<IllegalArgumentException> { MicRecordingReader(fast.input()) }
|
||||
assertTrue(error.message!!.contains("48000 Hz"), error.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theFeedFollowsTheLeftTempleAndFallsBackToTheRight() {
|
||||
val mixer = SingleTempleMixer()
|
||||
// Until the left temple is heard from, the right one feeds.
|
||||
assertNotNull(mixer.mix(chunk(MicStream.RIGHT, 0, 10, channels = 2)))
|
||||
val left = chunk(MicStream.LEFT, 20, 100, channels = 2)
|
||||
left.deviceTickMs = 5
|
||||
val feed = mixer.mix(left)!!
|
||||
assertEquals(MicStream.MIX, feed.stream)
|
||||
assertEquals(1, feed.channels)
|
||||
assertEquals(800, feed.frames)
|
||||
// Channels [100, 101] average to 100.
|
||||
assertTrue(feed.pcm.all { it == 100.toShort() })
|
||||
assertEquals(MicStream.LEFT, feed.clockStream)
|
||||
assertEquals(5L, feed.deviceTickMs)
|
||||
assertNull(mixer.mix(chunk(MicStream.RIGHT, 60, 10)))
|
||||
assertNull(mixer.mix(chunk(MicStream.RIGHT, 300, 10)))
|
||||
// The left temple went quiet: the right stands in until it is back.
|
||||
assertNotNull(mixer.mix(chunk(MicStream.RIGHT, 320, 10)))
|
||||
assertNotNull(mixer.mix(chunk(MicStream.LEFT, 330, 100)))
|
||||
assertNull(mixer.mix(chunk(MicStream.RIGHT, 340, 10)))
|
||||
assertNull(mixer.mix(chunk(MicStream.MONO, 350, 10)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun decodesTheTemplesStreamFrames() {
|
||||
val frame = micStreamFrame(sequence = 0x1234, tick = 0x89ABCDEFL, channels = 2, samples = shortArrayOf(1, -2, 3, -4), angle = -45, ssr = 300, truncated = true)
|
||||
val decoded = MicArrayProtocol.decodeFrame(frame)!!
|
||||
assertEquals(0x1234, decoded.sequence)
|
||||
assertEquals(0x89ABCDEFL, decoded.deviceTickMs)
|
||||
assertEquals(2, decoded.channels)
|
||||
assertEquals(16000, decoded.sampleRate)
|
||||
assertEquals(-45, decoded.angleDegrees)
|
||||
assertEquals(300, decoded.ssr)
|
||||
assertTrue(decoded.truncated)
|
||||
assertEquals(listOf<Short>(1, -2, 3, -4), MicArrayProtocol.pcm16(frame, decoded)!!.toList())
|
||||
assertNull(MicArrayProtocol.decodeFrame(ByteArray(205)))
|
||||
// A payload length beyond the packet is cut to what arrived.
|
||||
val short = frame.copyOf(frame.size - 4)
|
||||
assertEquals(listOf<Short>(1, -2), MicArrayProtocol.pcm16(short, MicArrayProtocol.decodeFrame(short)!!)!!.toList())
|
||||
assertEquals(listOf(0x4d, 0x43, 1, 4), MicArrayProtocol.control(MicArrayProtocol.OP_RENEW).map { it.toInt() })
|
||||
assertTrue(MicArrayProtocol.isConfigure(CONFIGURE_RECORD))
|
||||
assertFalse(MicArrayProtocol.isConfigure(MicArrayProtocol.control(MicArrayProtocol.OP_STOP)))
|
||||
}
|
||||
}
|
||||
|
||||
/** A mic_control CONFIGURE record: codec front end, both mics, raw PCM16 at 16 kHz, hardware armed. */
|
||||
internal val CONFIGURE_RECORD = byteArrayOf(0x4d, 0x43, 1, 1, 0, 3, 1, 0, 160.toByte(), 0, 0, 0, 2)
|
||||
|
||||
/** An 'SM' stream frame as a temple sends it. */
|
||||
internal fun micStreamFrame(
|
||||
sequence: Int,
|
||||
tick: Long,
|
||||
channels: Int,
|
||||
samples: ShortArray,
|
||||
angle: Int = 0,
|
||||
ssr: Int = 0,
|
||||
truncated: Boolean = false,
|
||||
): ByteArray {
|
||||
val payload = AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size)
|
||||
val header = ByteArray(MicArrayProtocol.HEADER_BYTES)
|
||||
fun le16(at: Int, value: Int) {
|
||||
header[at] = value.toByte()
|
||||
header[at + 1] = (value shr 8).toByte()
|
||||
}
|
||||
header[0] = 'S'.code.toByte()
|
||||
header[1] = 'M'.code.toByte()
|
||||
header[2] = 1
|
||||
header[3] = (if (truncated) 0x82 else 0x02).toByte()
|
||||
le16(4, sequence)
|
||||
le16(6, (tick and 0xffff).toInt())
|
||||
le16(8, ((tick shr 16) and 0xffff).toInt())
|
||||
header[10] = channels.toByte()
|
||||
le16(11, 160)
|
||||
header[13] = 0
|
||||
header[14] = 1
|
||||
le16(15, angle)
|
||||
le16(17, ssr)
|
||||
le16(19, payload.size)
|
||||
return header + payload
|
||||
}
|
||||
|
||||
class VoiceCaptureSourcesTest {
|
||||
private class Events : FaceclawVoiceControllerListener {
|
||||
val statuses = mutableListOf<String?>()
|
||||
val transcripts = mutableListOf<Pair<String?, Boolean>>()
|
||||
val pcmChunks = mutableListOf<ByteArray>()
|
||||
val replay = mutableListOf<Pair<String?, Long>>()
|
||||
val stoppedLatch = Latch(1, testPlatform())
|
||||
override fun onStatus(status: String?) { statuses.add(status) }
|
||||
override fun onTranscript(text: String?, isFinal: Boolean) { transcripts.add(text to isFinal) }
|
||||
override fun onStopped(captureId: Int) { stoppedLatch.countDown() }
|
||||
override fun onPcm(pcm16le: ByteArray?) { pcmChunks.add(pcm16le!!) }
|
||||
override fun onFrameMeta(angleDegrees: Int, ssr: Int) {}
|
||||
override fun onSpeechEnd() {}
|
||||
override fun onSpeakerVerified(isWearer: Boolean, similarity: Float) {}
|
||||
override fun onReplay(event: String?, positionMs: Long, durationMs: Long) { replay.add(event to positionMs) }
|
||||
}
|
||||
|
||||
private class FakeLink : MicArrayLink {
|
||||
var listener: FaceclawAudioPacketListener? = null
|
||||
val controls = mutableListOf<String>()
|
||||
override fun startForwarding(listener: FaceclawAudioPacketListener): Boolean { this.listener = listener; return true }
|
||||
override fun stopForwarding() { listener = null }
|
||||
override fun isForwarding(): Boolean = listener != null
|
||||
override fun sendControl(record: ByteArray, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean {
|
||||
controls.add(label + ":" + record[3] + (if (leftTemple) "L" else "") + (if (rightTemple) "R" else ""))
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private class FakeHost(val link: FakeLink? = null, val recording: MemoryFile? = null) : VoiceCaptureHost {
|
||||
override val dispatcher = CallbackDispatcher { it() }
|
||||
override fun onSessionStarting() {}
|
||||
override fun isG2SessionReady(): Boolean = link != null
|
||||
override fun isG2AudioCaptureActive(): Boolean = link?.isForwarding() == true
|
||||
override fun g2AudioSource(): PacketAudioSource? = null
|
||||
override fun createPacketDecoder(): AudioPacketDecoder = error("no stock stream in these tests")
|
||||
override fun openPhoneMic(): PcmAudioSource? = null
|
||||
override fun hasTranscriberModel(kind: VoiceModelKind): Boolean = true
|
||||
override fun loadTranscriber(kind: VoiceModelKind): OfflineTranscriber = object : OfflineTranscriber {
|
||||
override fun recognize(samples: FloatArray, count: Int): String = "x"
|
||||
override fun release() {}
|
||||
}
|
||||
override fun speakerEmbedder(modelPath: String): SpeakerEmbedder = error("no verification in these tests")
|
||||
override fun micArrayLink(): MicArrayLink? = link
|
||||
override fun openRecording(path: String): SeekableInput = if (path == "missing") error("no such file") else recording!!.input()
|
||||
}
|
||||
|
||||
private val logs = mutableListOf<String>()
|
||||
|
||||
private fun session(host: VoiceCaptureHost, name: String) =
|
||||
VoiceCaptureSession(host, testPlatform(), name, { logs.add("I $it") }, { logs.add("W $it") }, { m, e -> logs.add("E $m ${e.message}") })
|
||||
|
||||
private fun waitFor(timeoutMs: Long, condition: () -> Boolean): Boolean {
|
||||
val deadline = testPlatform().elapsedRealtimeMs() + timeoutMs
|
||||
while (!condition()) {
|
||||
if (testPlatform().elapsedRealtimeMs() > deadline) return false
|
||||
sleepMs(5)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun monoRecording(chunks: Int): MemoryFile {
|
||||
val file = MemoryFile()
|
||||
val writer = MicRecordingWriter(file, null, 0, testPlatform())
|
||||
for (index in 0 until chunks) writer.write(chunk(MicStream.MONO, index * 50L, 1000 + index))
|
||||
writer.finish()
|
||||
return file
|
||||
}
|
||||
|
||||
@Test
|
||||
fun micArrayCaptureArmsTheTemplesRecordsThemAndTranscribesTheFeed() {
|
||||
val link = FakeLink()
|
||||
val host = FakeHost(link)
|
||||
val events = Events()
|
||||
val session = session(host, "voice-test-array")
|
||||
val file = MemoryFile()
|
||||
val recorder = MicRecordingWriter(file, "{}", 0, testPlatform())
|
||||
session.setListener(events)
|
||||
session.setMicArray(CONFIGURE_RECORD)
|
||||
session.setRecorder(recorder)
|
||||
session.start("cloud", 1)
|
||||
assertTrue(waitFor(3000) { link.controls.isNotEmpty() })
|
||||
assertTrue(session.isCapturing())
|
||||
val listener = link.listener!!
|
||||
for (index in 0 until 5) {
|
||||
val left = ShortArray(1600) { if (it % 2 == 0) 100 else 300 }
|
||||
listener.onAudioPacket(micStreamFrame(index, 1000 + index * 50L, 2, left, angle = 20, ssr = 7), "L", 500 + index * 50L)
|
||||
listener.onAudioPacket(micStreamFrame(index, 9000 + index * 50L, 2, ShortArray(1600) { 50 }), "R", 505 + index * 50L)
|
||||
}
|
||||
// A stock-format packet from before arming took effect is not audio.
|
||||
listener.onAudioPacket(ByteArray(205), "L", 760)
|
||||
assertTrue(waitFor(3000) { events.pcmChunks.size >= 5 })
|
||||
session.stop()
|
||||
assertTrue(events.stoppedLatch.await(3000))
|
||||
assertEquals(listOf("configure:1LR"), link.controls.take(1))
|
||||
assertEquals("stop:3LR", link.controls.last())
|
||||
assertFalse(link.isForwarding())
|
||||
assertFalse(session.isCapturing())
|
||||
// The feed is the left temple's microphones averaged.
|
||||
assertEquals(5, events.pcmChunks.size)
|
||||
assertEquals(1600, events.pcmChunks[0].size)
|
||||
assertTrue(AudioSegmentation.pcm16leToFloat(events.pcmChunks[0]).all { it == 200 / 32768f })
|
||||
assertTrue(events.statuses.contains("Listening (cloud)..."))
|
||||
|
||||
recorder.finish()
|
||||
assertEquals(listOf("mix", "left temple", "right temple", "frames"), MkaReader(file.input()).tracks.map { it.name })
|
||||
val recorded = drain(MicRecordingReader(file.input()))
|
||||
assertEquals(5, recorded.count { it.stream == MicStream.LEFT })
|
||||
assertEquals(5, recorded.count { it.stream == MicStream.RIGHT })
|
||||
val left = recorded.first { it.stream == MicStream.LEFT }
|
||||
assertEquals(2, left.channels)
|
||||
assertEquals(1000L, left.deviceTickMs)
|
||||
assertEquals(20, left.angleDegrees)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun micArrayCaptureNeedsGlassesThatHaveTheChannel() {
|
||||
val events = Events()
|
||||
val session = session(FakeHost(), "voice-test-array-none")
|
||||
session.setListener(events)
|
||||
session.setMicArray(CONFIGURE_RECORD)
|
||||
session.start("cloud", 1)
|
||||
assertEquals<List<String?>>(listOf("Voice control needs an active G2 connection."), events.statuses)
|
||||
// A record that is not a CONFIGURE leaves the capture on the mixed stream.
|
||||
val mixed = Events()
|
||||
session.setListener(mixed)
|
||||
session.setMicArray(MicArrayProtocol.control(MicArrayProtocol.OP_STOP))
|
||||
session.setUsePhoneMic(true)
|
||||
session.start("cloud", 2)
|
||||
assertTrue(mixed.stoppedLatch.await(3000))
|
||||
assertEquals<List<String?>>(listOf("Could not start the phone microphone."), mixed.statuses)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replayPlaysARecordingThroughThePipelineInRealTime() {
|
||||
val host = FakeHost(recording = monoRecording(30))
|
||||
val events = Events()
|
||||
val session = session(host, "voice-test-replay")
|
||||
session.setListener(events)
|
||||
// A replay is never recorded again.
|
||||
val copy = MemoryFile()
|
||||
val recorder = MicRecordingWriter(copy, null, 0, testPlatform())
|
||||
session.setRecorder(recorder)
|
||||
session.setReplay("recording.mka", 1000)
|
||||
val startedAt = testPlatform().elapsedRealtimeMs()
|
||||
session.start("onboard", 5)
|
||||
assertTrue(events.stoppedLatch.await(5000))
|
||||
val elapsed = testPlatform().elapsedRealtimeMs() - startedAt
|
||||
// Half a second of audio, paced as it was captured.
|
||||
assertTrue(elapsed >= 400, "replay took $elapsed ms")
|
||||
assertEquals(10, events.pcmChunks.size)
|
||||
assertEquals(1020.toShort(), AudioSegmentation.pcm16le(events.pcmChunks[0][0], events.pcmChunks[0][1]))
|
||||
assertEquals("start" to 1000L, events.replay.first())
|
||||
assertEquals("end" to 1500L, events.replay.last())
|
||||
assertEquals(VoiceCaptureSession.REPLAY_FINISHED_STATUS, events.statuses.last())
|
||||
assertEquals("x" to true, events.transcripts.last())
|
||||
assertFalse(session.isCapturing())
|
||||
recorder.finish()
|
||||
assertTrue(MkaReader(copy.input()).tracks.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replayJumpsAndPauses() {
|
||||
val host = FakeHost(recording = monoRecording(200))
|
||||
val events = Events()
|
||||
val session = session(host, "voice-test-replay-seek")
|
||||
session.setListener(events)
|
||||
session.setReplay("recording.mka", 0)
|
||||
session.start("onboard", 6)
|
||||
assertTrue(waitFor(3000) { events.pcmChunks.size >= 2 })
|
||||
session.setReplayPaused(true)
|
||||
sleepMs(100)
|
||||
val heldAt = events.pcmChunks.size
|
||||
sleepMs(200)
|
||||
assertEquals(heldAt, events.pcmChunks.size)
|
||||
assertTrue(session.isCapturing())
|
||||
// Jump to the last quarter-second while paused, then go on.
|
||||
session.seekReplay(9750)
|
||||
session.setReplayPaused(false)
|
||||
assertTrue(events.stoppedLatch.await(5000))
|
||||
assertTrue(events.replay.contains("seek" to 9750L), events.replay.toString())
|
||||
assertEquals("end" to 10_000L, events.replay.last())
|
||||
assertEquals(heldAt + 5, events.pcmChunks.size)
|
||||
// What played before the jump ended as its own utterance.
|
||||
assertEquals(2, events.transcripts.count { it.second })
|
||||
val last = events.pcmChunks.last()
|
||||
assertEquals(1199.toShort(), AudioSegmentation.pcm16le(last[0], last[1]))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replayOfAnUnreadableFileReportsAndStops() {
|
||||
val events = Events()
|
||||
val session = session(FakeHost(), "voice-test-replay-missing")
|
||||
session.setListener(events)
|
||||
session.setReplay("missing", 0)
|
||||
session.start("cloud", 7)
|
||||
assertTrue(events.stoppedLatch.await(3000))
|
||||
assertEquals<List<String?>>(listOf("Could not open the recording: no such file"), events.statuses)
|
||||
assertEquals(listOf<Pair<String?, Long>>("error" to 0L), events.replay)
|
||||
}
|
||||
}
|
||||
@@ -64,10 +64,12 @@ class ResourceCacheTest {
|
||||
|
||||
@Test fun lruEvictsByBytesAndProtectsAllCurrentFrameHits() {
|
||||
val cache = ResourceCacheState()
|
||||
// Five of these fill the 320 KiB arena; a sixth needs an eviction.
|
||||
val a = resource(60000, 1); val b = resource(60000, 2); val c = resource(60000, 3); val d = resource(60000, 4)
|
||||
assertContentEquals(intArrayOf(0, 1, 2), cache.prepare(listOf(a, b, c)))
|
||||
val e = resource(60000, 5); val f = resource(60000, 6)
|
||||
assertContentEquals(intArrayOf(0, 1, 2, 3, 4), cache.prepare(listOf(a, b, c, e, f)))
|
||||
cache.drainCommands(3600)
|
||||
cache.prepare(listOf(a)); cache.drainCommands(3600) // b is oldest now.
|
||||
cache.prepare(listOf(a, e, f)); cache.drainCommands(3600) // b is oldest now.
|
||||
assertContentEquals(intArrayOf(1, 2), cache.prepare(listOf(d, c))) // protect later hit c before d.
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertContentEquals(byteArrayOf(22, 1, 0, 1, 0), commands.first())
|
||||
@@ -75,10 +77,10 @@ class ResourceCacheTest {
|
||||
assertEquals(0, cache.resourceId(a)); assertEquals(2, cache.resourceId(c))
|
||||
assertEquals(1, cache.resourceId(d))
|
||||
assertContentEquals(d.bytes, uploaded(commands).getValue(1))
|
||||
val tooLarge = cache.prepare(listOf(a, c, d, b))
|
||||
val tooLarge = cache.prepare(listOf(a, c, d, e, f, b))
|
||||
assertEquals(-1, tooLarge.last())
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
assertEquals(3, cache.residentCount())
|
||||
assertEquals(5, cache.residentCount())
|
||||
}
|
||||
|
||||
@Test fun idPressureUsesLruEvenWhenBytesAreAvailable() {
|
||||
@@ -96,15 +98,15 @@ class ResourceCacheTest {
|
||||
|
||||
@Test fun rejectedPlansRestoreResidencyAndLru() {
|
||||
val cache = ResourceCacheState()
|
||||
val resources = (0..3).map { resource(65536, it) }
|
||||
cache.prepare(resources.take(3)); cache.drainCommands(3600)
|
||||
val resources = (0..5).map { resource(60000, it) } // five fit, the sixth evicts
|
||||
assertContentEquals(intArrayOf(0, 1, 2, 3, 4), cache.prepare(resources.take(5))); cache.drainCommands(3600)
|
||||
val rollback = cache.checkpoint()
|
||||
cache.prepare(listOf(resources[3]))
|
||||
cache.prepare(listOf(resources[5]))
|
||||
assertEquals(-1, cache.resourceId(resources[0]))
|
||||
rollback()
|
||||
assertEquals(0, cache.resourceId(resources[0])); assertEquals(-1, cache.resourceId(resources[3]))
|
||||
assertEquals(0, cache.resourceId(resources[0])); assertEquals(-1, cache.resourceId(resources[5]))
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
cache.prepare(listOf(resources[3]))
|
||||
cache.prepare(listOf(resources[5]))
|
||||
assertContentEquals(byteArrayOf(22, 1, 0, 0, 0), cache.drainCommands(3600).first())
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ class VoiceCaptureSessionTest {
|
||||
override fun onFrameMeta(angleDegrees: Int, ssr: Int) { frameMeta.add(angleDegrees to ssr) }
|
||||
override fun onSpeechEnd() { speechEnds++ }
|
||||
override fun onSpeakerVerified(isWearer: Boolean, similarity: Float) { verified = isWearer to similarity }
|
||||
val replay = mutableListOf<Pair<String?, Long>>()
|
||||
override fun onReplay(event: String?, positionMs: Long, durationMs: Long) { replay.add(event to positionMs) }
|
||||
}
|
||||
|
||||
/** Scripted phone mic: each entry is a chunk amplitude; after the script the read fails (-1). */
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
import platform.Foundation.NSTemporaryDirectory
|
||||
import platform.Foundation.NSUUID
|
||||
|
||||
class IosMicRecorderTest {
|
||||
@Test
|
||||
fun recordsPcmToAMatroskaFileThroughThePosixFileAdapters() {
|
||||
val path = NSTemporaryDirectory() + NSUUID().UUIDString + ".mka"
|
||||
try {
|
||||
val recorder = assertNotNull(IosMicRecordings.open(path, "{\"platform\":\"ios\"}"))
|
||||
// 2.5 s in 50 ms chunks: enough for whole clusters to reach the file before finish().
|
||||
for (index in 0 until 50) {
|
||||
val samples = ShortArray(800) { (index * 10 + it % 7).toShort() }
|
||||
recorder.append(AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size).data())
|
||||
}
|
||||
assertFalse(recorder.failed)
|
||||
val length = recorder.finish()
|
||||
assertTrue(length >= 2500, "recorded $length ms")
|
||||
assertEquals(fileLength(path), openFileInput(path).length)
|
||||
|
||||
val reader = MicRecordingReader(openFileInput(path))
|
||||
assertEquals("{\"platform\":\"ios\"}", reader.configJson)
|
||||
assertEquals("mic", reader.description)
|
||||
var chunks = 0
|
||||
var last: MicChunk? = null
|
||||
while (true) {
|
||||
last = reader.next() ?: break
|
||||
assertEquals(800, last.frames)
|
||||
assertEquals((chunks * 10).toShort(), last.pcm[0])
|
||||
chunks++
|
||||
}
|
||||
assertEquals(50, chunks)
|
||||
reader.close()
|
||||
} finally {
|
||||
deleteFile(path)
|
||||
}
|
||||
assertNull(IosMicRecordings.open("/nonexistent-directory/x.mka", null))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user