129 lines · 6.2 KB · KotlinRaw
package com.cottondb.voiceimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testclass DictationTest { /** Says the words it is told at the samples it is told: "hello" tentative at 0.5 s, settled at 1 s (from 0 to 1000 ms). */ private class ScriptedLive : LivePass { var fed = 0 override fun accept(samples: FloatArray, heard: (Heard) -> Unit) { val before = fed fed += samples.size if (before < 8000 && fed >= 8000) heard(Heard("live", false, "hel", emptyList(), "en", 0, 500)) if (before < 16000 && fed >= 16000) heard(Heard("live", true, "hello", listOf(Word("hello", 100, 900)), "en", 0, 1000)) if (before < 32000 && fed >= 32000) heard(Heard("live", true, "world", emptyList(), "en", 1000, 2000)) } override fun finish(heard: (Heard) -> Unit) = heard(Heard("live", true, "end", emptyList(), "en", 2000, fed / 16L)) override fun close() {} } private class RecordingSecond : SecondPass { val read = mutableListOf<FloatArray>() override fun read(samples: FloatArray, language: String?): Pair<String, List<Word>>? { read += samples return "Hello." to listOf(Word("Hello.", 50, 800)) } override fun close() {} } @Test fun the_second_pass_reads_each_settled_sentence_after_the_live_words_with_session_times() { val second = RecordingSecond() val said = mutableListOf<Heard>() val jobs = mutableListOf<() -> Unit>() val d = Dictation(ScriptedLive(), second, "en", later = { jobs += it }) { said += it } // 1 s of audio whose sample i holds i, so the sentence handed to the second pass can be checked by value. d.accept(FloatArray(16000) { it.toFloat() }) assertEquals(listOf("hel", "hello"), said.map { it.text }) assertTrue("the second pass waits its turn: the live words never wait for it", second.read.isEmpty()) jobs.removeAt(0)() assertEquals(16000, second.read[0].size) assertEquals(0f, second.read[0][0]) val s = said.last() assertEquals("second", s.pass) assertEquals(true, s.final) assertEquals(0L to 1000L, s.fromMs to s.toMs) assertEquals("Hello.", s.text) // The next sentence's audio starts where the first ended. d.accept(FloatArray(16000) { 16000f + it }) jobs.removeAt(0)() assertEquals(16000f, second.read[1][0]) assertEquals(1000L, said.last().fromMs) assertEquals(1050L, said.last().words[0].startMs) } @Test fun finish_settles_the_open_sentence_then_ends_after_its_second_pass() { val said = mutableListOf<Heard>() val jobs = ArrayDeque<() -> Unit>() val d = Dictation(ScriptedLive(), RecordingSecond(), "en", later = { jobs += it }) { said += it } d.accept(FloatArray(4000)) var ended = false d.finish { ended = true } assertEquals("end", said.last().text) while (jobs.isNotEmpty()) jobs.removeFirst()() assertTrue(ended) assertEquals("second", said.last().pass) } @Test fun without_a_second_pass_only_the_live_words_are_said() { val said = mutableListOf<Heard>() val d = Dictation(ScriptedLive(), null, "te", later = { it() }) { said += it } d.feed(ByteArray(32000 * 2)) assertEquals(listOf("live", "live", "live"), said.map { it.pass }) } @Test fun pcm_is_read_as_16_bit_little_endian() { val got = mutableListOf<FloatArray>() val live = object : LivePass { override fun accept(samples: FloatArray, heard: (Heard) -> Unit) { got += samples } override fun finish(heard: (Heard) -> Unit) {} override fun close() {} } Dictation(live, null, null, later = { it() }) {}.feed(byteArrayOf(0x00, 0x40, 0x00, 0xC0.toByte())) assertEquals(0.5f, got[0][0]) assertEquals(-0.5f, got[0][1]) } @Test fun heard_crosses_in_voice_heard_shape() { val j = Heard("live", false, "hi there", listOf(Word("hi", 0, 200)), "en", 0, 400).json().toString() assertEquals("""{"pass":"live","final":false,"text":"hi there","words":[{"w":"hi","start_ms":0,"end_ms":200}],"language":"en","from_ms":0,"to_ms":400}""", j) } @Test fun words_start_at_a_marked_token_and_end_where_the_next_starts() { val w = wordsOf(arrayOf("▁HE", "LLO", "▁WOR", "LD"), floatArrayOf(0.1f, 0.2f, 0.5f, 0.6f), 1000, 1900, lower = true) assertEquals(listOf(Word("hello", 1100, 1500), Word("world", 1500, 1900)), w) assertEquals(emptyList<Word>(), wordsOf(arrayOf(" Hi"), floatArrayOf(), 0, 10, false)) } @Test fun a_planned_model_without_files_yet_stands_in_by_language() { val catalogue = """{"languages":["en"],"live":{"engine":"sherpa:nemotron"},"second":{"engine":"sherpa:parakeet"}, "models":{"nemotron":{"kind":"online-transducer","role":"live","languages":["en"],"files":[{"name":"encoder.int8.onnx","bytes":null,"sha256":null,"url":null}]}, "parakeet":{"kind":"offline-transducer","role":"second","languages":["en"],"files":[{"name":"encoder.int8.onnx","bytes":1,"sha256":"ab","url":"https://x/e"}]}}}""" val r = Plan.parse("s1", catalogue).resolve { false } assertEquals(StandIns.zipformerEn.id, r.live.id) assertEquals("parakeet", r.second!!.id) assertNull(r.vad) assertEquals(listOf(StandIns.zipformerEn.id, "parakeet"), r.missing.map { it.id }) assertEquals(false, r.runnable) val te = Plan.parse("s2", """{"languages":["te"],"live":{"engine":"sherpa:indic"},"second":{"engine":"sherpa:indic"}}""").resolve { true } assertEquals(StandIns.whisperTiny.id, te.live.id) assertNull("a second pass with the live model is no second pass", te.second) assertEquals(StandIns.sileroVad.id, te.vad!!.id) assertTrue(te.runnable) } @Test(expected = NoEngine::class) fun a_live_engine_that_is_not_sherpa_is_no_engine_here() { Plan.parse("s", """{"languages":["en"],"live":{"engine":"android.speech"}}""").resolve { true } }}