forked from Blendman974/faceclaw
Multi-mic support and replay mode for Transcribe app
This commit is contained in:
1 parent
c162c8e633
commit
9372b2742c
45 files changed
+3926
-288
No files matched your search
@@ -36,7 +36,7 @@ function harness() {
|
||||
static open(name) {
|
||||
recordingAttempts++;
|
||||
if (failRecordings) return null;
|
||||
const r = new FakeRecorder(); r.name = name; r.path = `/Downloads/${name}.wav`; r.pcm = []; r.closed = false; recordings.push(r); return r;
|
||||
const r = new FakeRecorder(); r.name = name; r.path = `/Downloads/${name}.mka`; r.pcm = []; r.closed = false; recordings.push(r); return r;
|
||||
}
|
||||
append(bytes) { this.pcm.push(...bytes); }
|
||||
close() { this.closed = true; }
|
||||
@@ -49,7 +49,7 @@ function harness() {
|
||||
setTimeout: fn => { timers.set(fn, fn); return fn; }, clearTimeout: fn => timers.delete(fn),
|
||||
NSData: { dataWithBytesLength: (b, n) => Buffer.from(b, 0, n) }, interop: { handleof: b => b, bufferFromData: d => d },
|
||||
require: name => name === './cloud-stt-provider' ? provider : name === './speech-analyzer-stt.ios' ? analyzer
|
||||
: name === './wav-recorder.ios' ? { WavRecorder: FakeRecorder } : {} };
|
||||
: name === './mic-recorder.ios' ? { MicRecorder: FakeRecorder } : {} };
|
||||
vm.runInNewContext(ts.transpileModule(fs.readFileSync('app/native/voice-control.ios.ts', 'utf8'), {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox);
|
||||
const bridge = new sandbox.exports.IosVoiceControlBridge();
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ const { loader } = require('./helpers/load-typescript.cjs');
|
||||
const load = loader();
|
||||
const { TranscriptModel, TranscriptScroll, transcriptLayout, wrapTranscribeText } = load('app/apps/transcribe/transcript-model.ts');
|
||||
const { SpeechPauseDetector } = load('app/native/speech-pause.ts');
|
||||
const { formatReplayTime, stepReplaySeconds } = load('app/apps/transcribe/replay-time.ts');
|
||||
const { TimedTranscript } = load('app/native/transcript-format.ts');
|
||||
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
|
||||
|
||||
@@ -128,3 +129,33 @@ test('pause detector uses 1.5 seconds of PCM, fires once per pause, and ignores
|
||||
d.accept(pcm(3000));
|
||||
assert.equal(d.accept(pcm(0, 1500)), true);
|
||||
});
|
||||
|
||||
test('a replay jump leaves its time on a line of its own and starts a new paragraph', () => {
|
||||
const m = new TranscriptModel();
|
||||
m.mark('[0:30]');
|
||||
assert.equal(m.text, '[0:30]');
|
||||
m.accept({ text: 'After the jump', isFinal: false });
|
||||
assert.equal(m.text, '[0:30]\nAfter the jump');
|
||||
// What was still being spoken when the replay jumped is kept, ahead of the mark.
|
||||
m.mark('[1:15]');
|
||||
m.accept({ text: 'Later.', isFinal: true });
|
||||
m.accept({ text: 'More.', isFinal: true });
|
||||
assert.equal(m.text, '[0:30]\nAfter the jump\n[1:15]\nLater. More.');
|
||||
});
|
||||
|
||||
test('replay times read as m:ss, with hours from an hour on', () => {
|
||||
assert.equal(formatReplayTime(0), '0:00');
|
||||
assert.equal(formatReplayTime(83_999), '1:23');
|
||||
assert.equal(formatReplayTime(3_600_000), '1:00:00');
|
||||
assert.equal(formatReplayTime(83_000, true), '0:01:23');
|
||||
assert.equal(formatReplayTime(-5), '0:00');
|
||||
});
|
||||
|
||||
test('the time picker steps carry between fields and stay inside the recording', () => {
|
||||
assert.equal(stepReplaySeconds(59, 'seconds', 1, 600), 60);
|
||||
assert.equal(stepReplaySeconds(60, 'seconds', -1, 600), 59);
|
||||
assert.equal(stepReplaySeconds(30, 'minutes', 2, 600), 150);
|
||||
assert.equal(stepReplaySeconds(590, 'minutes', 1, 600), 600);
|
||||
assert.equal(stepReplaySeconds(30, 'minutes', -1, 600), 0);
|
||||
assert.equal(stepReplaySeconds(100, 'hours', 1, 7300), 3700);
|
||||
});
|
||||
@@ -164,6 +164,8 @@ function androidBridge(modules = {}) {
|
||||
setListener(listener) { this.listener = listener; },
|
||||
setCommunicator() {}, setUsePhoneMic() {}, setEndpointing() {},
|
||||
setNoiseSuppression() {}, setBeamFilter() {}, clearSpeakerVerification() {}, setOnboardModelKind() {},
|
||||
setMicArray() {}, setReplay() {}, setRecordingEnabled(enabled) { this.recordingEnabled = enabled; },
|
||||
noteRecordingEvent(json) { (this.recordingEvents ??= []).push(JSON.parse(json)); },
|
||||
start(_mode, id) { this.captureId = id; this.active = true; this.starts++; },
|
||||
stop() { this.active = false; },
|
||||
isCapturing() { return this.active; },
|
||||
@@ -217,24 +219,23 @@ test('a shared capture restarts for a holder that needs another provider, and is
|
||||
assert.equal(bridge.holdsCapture('ptt'), true);
|
||||
});
|
||||
|
||||
test('Android continuous captures record to the requested WAV, and report one that cannot be created', () => {
|
||||
test('Android continuous captures record to the requested file, and report one that cannot be created', () => {
|
||||
const recordings = [];
|
||||
let fail = false, attempts = 0;
|
||||
class FakeRecorder {
|
||||
static open(name) {
|
||||
attempts++;
|
||||
if (fail) return null;
|
||||
const r = new FakeRecorder(); r.name = name; r.path = `/Download/${name}.wav`; r.pcm = []; r.closed = false; recordings.push(r); return r;
|
||||
}
|
||||
append(pcm) { this.pcm.push(...pcm); }
|
||||
close() { this.closed = true; }
|
||||
}
|
||||
const { bridge, controller } = androidBridge({
|
||||
'./wav-recorder': { WavRecorder: FakeRecorder },
|
||||
'./mic-recording': { newRecordingPath: name => `/Download/${name}.mka` },
|
||||
'./speech-pause': { SpeechPauseDetector: class { reset() {} accept() { return false; } } },
|
||||
'../util/array-util': { toUint8Array: pcm => Uint8Array.from(pcm) },
|
||||
'../util/audio-level': { pcmLevel: () => 0 },
|
||||
});
|
||||
// The controller writes the file itself, from the capture's own thread.
|
||||
controller.startRecording = (path, json) => {
|
||||
attempts++;
|
||||
if (fail) return false;
|
||||
recordings.push({ path, info: JSON.parse(json), closed: false });
|
||||
return true;
|
||||
};
|
||||
controller.stopRecording = () => { recordings.at(-1).closed = true; return 0; };
|
||||
const options = { provider: 'onboard' };
|
||||
let changes = 0; bridge.onRecordingChange(() => changes++);
|
||||
bridge.recordTo('transcribe-1');
|
||||
@@ -242,20 +243,24 @@ test('Android continuous captures record to the requested WAV, and report one th
|
||||
// Push-to-talk on a capture of its own is not recorded, and the file waits for audio to save.
|
||||
bridge.startPushToTalk(options);
|
||||
controller.listener.onPcm([9, 9]);
|
||||
assert.equal(attempts, 0);
|
||||
assert.deepEqual([attempts, controller.recordingEnabled], [0, false]);
|
||||
bridge.startContinuousCapture(options);
|
||||
controller.listener.onPcm([1, 2]);
|
||||
controller.listener.onPcm([3, 4]);
|
||||
assert.deepEqual(recordings.map(r => [r.name, r.pcm, r.closed]), [['transcribe-1', [1, 2, 3, 4], false]]);
|
||||
// The file outlives a capture: one restarted after the glasses reconnect keeps appending to it.
|
||||
assert.deepEqual(recordings.map(r => [r.path, r.info.recording, r.closed]), [['/Download/transcribe-1.mka', 'transcribe-1', false]]);
|
||||
assert.deepEqual([attempts, controller.recordingEnabled], [1, true]);
|
||||
// The file outlives a capture: one restarted after the glasses reconnect goes on in it,
|
||||
// with the restart noted, and nothing is recorded while no capture holds the mic.
|
||||
bridge.handleSessionEnded();
|
||||
assert.equal(controller.recordingEnabled, false);
|
||||
bridge.resumeCapture(options);
|
||||
controller.listener.onPcm([5]);
|
||||
assert.deepEqual(recordings.map(r => [r.pcm, r.closed]), [[[1, 2, 3, 4, 5], false]]);
|
||||
assert.deepEqual([attempts, recordings[0].closed, controller.recordingEnabled], [1, false, true]);
|
||||
assert.deepEqual(controller.recordingEvents.map(event => [event.event, event.recording]), [['capture', 'transcribe-1']]);
|
||||
bridge.recordTo('');
|
||||
assert.deepEqual([recordings[0].closed, bridge.recording, changes], [true, false, 2]);
|
||||
controller.listener.onPcm([6]);
|
||||
assert.equal(recordings[0].pcm.length, 5);
|
||||
assert.equal(attempts, 1);
|
||||
|
||||
// One attempt per recording, not one per packet; another recording is a fresh attempt.
|
||||
fail = true; attempts = 0;
|
||||
@@ -269,5 +274,5 @@ test('Android continuous captures record to the requested WAV, and report one th
|
||||
bridge.recordTo('');
|
||||
bridge.recordTo('transcribe-2');
|
||||
controller.listener.onPcm([10]);
|
||||
assert.deepEqual([bridge.recording, bridge.recordingFailed, recordings.at(-1).pcm], [true, false, [10]]);
|
||||
assert.deepEqual([bridge.recording, bridge.recordingFailed, recordings.at(-1).path], [true, false, '/Download/transcribe-2.mka']);
|
||||
});
|
||||
@@ -0,0 +1,209 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { loader } = require('./helpers/load-typescript.cjs');
|
||||
|
||||
/** The Android voice bridge over a fake native controller that records what it is told. */
|
||||
function harness({ recordingOk = true, downloads = '/downloads' } = {}) {
|
||||
const controllers = [];
|
||||
class FakeController {
|
||||
constructor() {
|
||||
this.calls = []; this.events = []; this.capturing = false; this.recording = null;
|
||||
this.micArray = null; this.replay = null; this.recordingEnabled = null;
|
||||
controllers.push(this);
|
||||
// Setters the tests don't look at are only logged.
|
||||
return new Proxy(this, { get: (target, name) => name in target ? target[name] : (...args) => { target.calls.push([name, ...args]); } });
|
||||
}
|
||||
setListener(listener) { this.listener = listener; }
|
||||
setMicArray(record) { this.micArray = record; }
|
||||
setReplay(path, startMs) { this.replay = path === null ? null : { path, startMs }; }
|
||||
start(mode) { this.calls.push(['start', mode]); this.capturing = true; }
|
||||
stop() { this.calls.push(['stop']); this.capturing = false; }
|
||||
isCapturing() { return this.capturing; }
|
||||
startRecording(path, json) { if (recordingOk) this.recording = { path, info: JSON.parse(json) }; return recordingOk; }
|
||||
setRecordingEnabled(enabled) { this.recordingEnabled = enabled; }
|
||||
noteRecordingEvent(json) { this.events.push(JSON.parse(json)); }
|
||||
stopRecording() { this.calls.push(['stopRecording']); this.recording = null; return 0; }
|
||||
seekReplay(ms) { this.calls.push(['seekReplay', ms]); }
|
||||
setReplayPaused(paused) { this.calls.push(['setReplayPaused', paused]); }
|
||||
}
|
||||
const module = loader({
|
||||
global: { isAndroid: true },
|
||||
com: { faceclaw: { app: { FaceclawVoiceController: FakeController, FaceclawVoiceControllerListener: function (impl) { return impl; } } } },
|
||||
}, {
|
||||
'@nativescript/core': { Utils: { android: { getApplicationContext: () => ({}) } } },
|
||||
'./cloud-stt': { toJavaBytes: bytes => Array.from(bytes) },
|
||||
'./cloud-stt-provider': {
|
||||
createCloudSttClient: () => null,
|
||||
transcriptionKey: options => `${options.provider}:${options.language ?? ''}`,
|
||||
},
|
||||
'./mic-recording': { newRecordingPath: name => downloads === null ? null : `${downloads}/${name}.mka` },
|
||||
'../util/array-util': { toUint8Array: value => value },
|
||||
})('app/native/voice-control.ts');
|
||||
const options = (extra = {}) => ({
|
||||
communicator: {}, provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: '', sonioxApiKey: '', ...extra,
|
||||
});
|
||||
return { ...module, controllers, options, pcm: controller => controller.listener.onPcm(new Uint8Array(1600)) };
|
||||
}
|
||||
|
||||
const CONFIGURE_2 = Uint8Array.from([0x4d, 0x43, 1, 1, 1, 1, 1, 0, 160, 0, 0, 0, 2]);
|
||||
const CONFIGURE_4 = Uint8Array.from([0x4d, 0x43, 1, 1, 0, 3, 1, 0, 160, 0, 0, 0, 2]);
|
||||
|
||||
test('a capture of the temples\' microphones arms them, and another microphone count restarts the stream', () => {
|
||||
const h = harness();
|
||||
const bridge = h.voiceControlBridge;
|
||||
bridge.startContinuousCapture(h.options({ micArray: { configure: CONFIGURE_2 } }));
|
||||
const controller = h.controllers[0];
|
||||
assert.deepEqual(controller.micArray, Array.from(CONFIGURE_2));
|
||||
assert.equal(controller.replay, null);
|
||||
assert.deepEqual(controller.calls.filter(call => call[0] === 'start'), [['start', 'onboard']]);
|
||||
// Push-to-talk asks for the same thing while the window is open: it shares the stream.
|
||||
bridge.startPushToTalk(h.options({ micArray: { configure: CONFIGURE_2 } }));
|
||||
assert.equal(controller.calls.filter(call => call[0] === 'start').length, 1);
|
||||
bridge.stopPushToTalk();
|
||||
// The Microphones setting changed.
|
||||
bridge.retargetContinuousCapture(h.options({ micArray: { configure: CONFIGURE_4 } }));
|
||||
assert.deepEqual(controller.micArray, Array.from(CONFIGURE_4));
|
||||
assert.deepEqual(controller.calls.filter(call => call[0] === 'start' || call[0] === 'stop').map(call => call[0]), ['start', 'stop', 'start']);
|
||||
// Back to the mixed stream; the phone's microphone never gets an array.
|
||||
bridge.retargetContinuousCapture(h.options());
|
||||
assert.equal(controller.micArray, null);
|
||||
bridge.retargetContinuousCapture(h.options({ communicator: null, usePhoneMic: true, micArray: { configure: CONFIGURE_4 } }));
|
||||
assert.equal(controller.micArray, null);
|
||||
});
|
||||
|
||||
test('a recording opens with the first audio, describes its capture, and follows the capture across restarts', () => {
|
||||
const h = harness();
|
||||
const bridge = h.voiceControlBridge;
|
||||
const changes = [];
|
||||
bridge.onRecordingChange(() => changes.push(bridge.recording));
|
||||
bridge.recordTo('transcribe-1');
|
||||
assert.equal(bridge.recording, true);
|
||||
bridge.startContinuousCapture(h.options({ micArray: { configure: CONFIGURE_4 }, noiseSuppression: true, captureInfo: { mics: 4, glasses: 'G2' } }));
|
||||
const controller = h.controllers[0];
|
||||
// Nothing on disk until audio arrives: a capture that never starts leaves no file.
|
||||
assert.equal(controller.recording, null);
|
||||
assert.equal(controller.recordingEnabled, true);
|
||||
h.pcm(controller);
|
||||
assert.equal(controller.recording.path, '/downloads/transcribe-1.mka');
|
||||
const info = controller.recording.info;
|
||||
assert.equal(info.recording, 'transcribe-1');
|
||||
assert.equal(info.source, 'glasses');
|
||||
assert.equal(info.micArray, true);
|
||||
assert.equal(info.mics, 4);
|
||||
assert.equal(info.glasses, 'G2');
|
||||
assert.deepEqual(info.transcription, { provider: 'onboard', language: '' });
|
||||
assert.deepEqual(info.processing, { noiseSuppression: true, beamFilter: null });
|
||||
h.pcm(controller);
|
||||
assert.equal(controller.calls.filter(call => call[0] === 'stopRecording').length, 0);
|
||||
|
||||
// The capture restarts with other microphones: the same file goes on, with the change noted in it.
|
||||
bridge.retargetContinuousCapture(h.options({ captureInfo: { mics: 1, glasses: 'G2' } }));
|
||||
assert.equal(controller.events.length, 1);
|
||||
assert.equal(controller.events[0].event, 'capture');
|
||||
assert.equal(controller.events[0].mics, 1);
|
||||
assert.equal(controller.events[0].micArray, false);
|
||||
assert.equal(controller.recording.path, '/downloads/transcribe-1.mka');
|
||||
|
||||
// Save Recording was turned off.
|
||||
bridge.recordTo('');
|
||||
assert.equal(bridge.recording, false);
|
||||
assert.equal(controller.recording, null);
|
||||
assert.deepEqual(changes, [true, false]);
|
||||
});
|
||||
|
||||
test('push-to-talk on a capture of its own is not recorded', () => {
|
||||
const h = harness();
|
||||
const bridge = h.voiceControlBridge;
|
||||
bridge.recordTo('transcribe-1');
|
||||
bridge.startPushToTalk(h.options());
|
||||
const controller = h.controllers[0];
|
||||
assert.equal(controller.recordingEnabled, false);
|
||||
h.pcm(controller);
|
||||
assert.equal(controller.recording, null);
|
||||
// The Transcribe window joins the running stream: from here it is recorded.
|
||||
bridge.startContinuousCapture(h.options());
|
||||
assert.equal(controller.recordingEnabled, true);
|
||||
h.pcm(controller);
|
||||
assert.equal(controller.recording.path, '/downloads/transcribe-1.mka');
|
||||
bridge.stopContinuousCapture();
|
||||
assert.equal(controller.recordingEnabled, false);
|
||||
});
|
||||
|
||||
test('a recording that cannot be created is reported once', () => {
|
||||
for (const h of [harness({ recordingOk: false }), harness({ downloads: null })]) {
|
||||
const bridge = h.voiceControlBridge;
|
||||
bridge.recordTo('transcribe-1');
|
||||
bridge.startContinuousCapture(h.options());
|
||||
h.pcm(h.controllers[0]);
|
||||
assert.equal(bridge.recording, false);
|
||||
assert.equal(bridge.recordingFailed, true);
|
||||
}
|
||||
});
|
||||
|
||||
test('a replay runs on its own controller, follows its events, jumps while playing and restarts after the end', () => {
|
||||
const h = harness();
|
||||
const replay = new h.VoiceReplay('/downloads/talk.mka', () => h.options({ communicator: {}, micArray: { configure: CONFIGURE_4 } }));
|
||||
const seen = [];
|
||||
replay.onChange(jumpedToMs => seen.push([replay.state, replay.positionMs, jumpedToMs]));
|
||||
assert.equal(h.controllers.length, 0);
|
||||
replay.start();
|
||||
// The microphone's bridge has not been touched.
|
||||
assert.equal(h.controllers.length, 1);
|
||||
const controller = h.controllers[0];
|
||||
assert.deepEqual(controller.replay, { path: '/downloads/talk.mka', startMs: 0 });
|
||||
assert.equal(controller.micArray, null);
|
||||
assert.equal(replay.state, 'opening');
|
||||
const statuses = [];
|
||||
replay.events.onStatus(state => statuses.push(state.status));
|
||||
assert.equal(statuses.at(-1), 'Opening the recording...');
|
||||
|
||||
controller.listener.onReplay('start', 0, 60_000);
|
||||
assert.equal(replay.state, 'playing');
|
||||
assert.equal(replay.durationMs, 60_000);
|
||||
controller.listener.onReplay('position', 1000, 60_000);
|
||||
assert.equal(replay.positionMs, 1000);
|
||||
// A plain start and the ticking position are not jumps.
|
||||
assert.deepEqual(seen.map(entry => entry[2]), [undefined, undefined, undefined]);
|
||||
|
||||
replay.setPaused(true);
|
||||
assert.equal(replay.state, 'paused');
|
||||
replay.setPaused(false);
|
||||
replay.jumpTo(90_000); // past the end: held to the recording's length
|
||||
assert.deepEqual(controller.calls.filter(call => call[0] === 'seekReplay' || call[0] === 'setReplayPaused'),
|
||||
[['setReplayPaused', true], ['setReplayPaused', false], ['seekReplay', 60_000]]);
|
||||
replay.jumpTo(30_000);
|
||||
controller.listener.onReplay('seek', 30_000, 60_000);
|
||||
assert.deepEqual(seen.at(-1), ['playing', 30_000, 30_000]);
|
||||
|
||||
controller.listener.onReplay('end', 60_000, 60_000);
|
||||
controller.capturing = false;
|
||||
assert.equal(replay.state, 'ended');
|
||||
assert.equal(replay.positionMs, 60_000);
|
||||
replay.setPaused(true); // nothing to pause
|
||||
assert.equal(replay.state, 'ended');
|
||||
|
||||
// A jump once it has ended plays again from there.
|
||||
replay.jumpTo(45_000);
|
||||
assert.deepEqual(controller.replay, { path: '/downloads/talk.mka', startMs: 45_000 });
|
||||
assert.equal(replay.state, 'opening');
|
||||
controller.listener.onReplay('start', 45_000, 60_000);
|
||||
assert.deepEqual(seen.at(-1), ['playing', 45_000, 45_000]);
|
||||
|
||||
replay.close();
|
||||
assert.equal(controller.calls.at(-1)[0], 'stop');
|
||||
replay.start();
|
||||
assert.equal(controller.calls.at(-1)[0], 'stop');
|
||||
});
|
||||
|
||||
test('a recording that cannot be opened fails the replay', () => {
|
||||
const h = harness();
|
||||
const replay = new h.VoiceReplay('/downloads/missing.mka', () => h.options());
|
||||
replay.start();
|
||||
const controller = h.controllers[0];
|
||||
controller.listener.onStatus('Could not open the recording: no such file');
|
||||
controller.listener.onReplay('error', 0, 0);
|
||||
assert.equal(replay.state, 'failed');
|
||||
const statuses = [];
|
||||
replay.events.onStatus(state => statuses.push(state.status));
|
||||
assert.deepEqual(statuses, ['Could not open the recording: no such file']);
|
||||
});
|
||||
@@ -1,19 +0,0 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { loader } = require('./helpers/load-typescript.cjs');
|
||||
|
||||
test('the WAV header describes 16 kHz mono PCM16 and its data size', () => {
|
||||
const { wavHeader } = loader({ DataView }, {})('app/native/wav-header.ts');
|
||||
const header = Buffer.from(wavHeader(32000));
|
||||
assert.equal(header.length, 44);
|
||||
assert.equal(header.toString('ascii', 0, 4), 'RIFF');
|
||||
assert.equal(header.readUInt32LE(4), 36 + 32000);
|
||||
assert.equal(header.toString('ascii', 8, 16), 'WAVEfmt ');
|
||||
assert.equal(header.readUInt16LE(20), 1);
|
||||
assert.equal(header.readUInt16LE(22), 1);
|
||||
assert.equal(header.readUInt32LE(24), 16000);
|
||||
assert.equal(header.readUInt32LE(28), 32000);
|
||||
assert.equal(header.readUInt16LE(34), 16);
|
||||
assert.equal(header.toString('ascii', 36, 40), 'data');
|
||||
assert.equal(header.readUInt32LE(40), 32000);
|
||||
});
|
||||
Reference in new issue
Block a user