package com.cottondb.voice import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.buildJsonArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put /** One word with its time in the session's audio (from the first sample fed), as voice.heard's `words` carries it. */ data class Word(val w: String, val startMs: Long, val endMs: Long, val conf: Float? = null) /** A pass's result over [fromMs, toMs) of the session's audio: voice.heard's request without `session`. */ data class Heard( val pass: String, val final: Boolean, val text: String, val words: List, val language: String?, val fromMs: Long, val toMs: Long, ) { fun json(): JsonObject = buildJsonObject { put("pass", pass) put("final", final) put("text", text) if (words.isNotEmpty()) put("words", buildJsonArray { words.forEach { w -> add(buildJsonObject { put("w", w.w); put("start_ms", w.startMs); put("end_ms", w.endMs); w.conf?.let { put("conf", it) } }) } }) language?.let { put("language", it) } put("from_ms", fromMs) put("to_ms", toMs) } } /** * The live pass: fed 16 kHz samples in order, it says what it hears as it hears it (final=false, the words may still * change) and settles each sentence (final=true) when it ends. Times are the session's, from its first sample. */ interface LivePass { fun accept(samples: FloatArray, heard: (Heard) -> Unit) /** The audio has ended: settle whatever is still open. */ fun finish(heard: (Heard) -> Unit) fun close() } /** The second pass: a larger model re-reads one settled sentence's audio. */ interface SecondPass { /** The sentence's text and words, times from the sentence's own first sample; null when it heard nothing. */ fun read(samples: FloatArray, language: String?): Pair>? fun close() } /** * One dictation inside Cotton Voice: the live pass over every sample as it arrives, and for every sentence the live * pass settles, the second pass over that sentence's audio, run after it on [later] (a thread of its own, so the live * words never wait for it). Audio is held only from the oldest sentence the second pass has not read yet, and * nothing at all once the session ends. */ class Dictation( private val live: LivePass, private val second: SecondPass?, private val language: String?, private val later: (() -> Unit) -> Unit, level: Levelling? = null, private val say: (Heard) -> Unit, ) { private val leveller = level?.let(::Leveller) private val rate = 16 /** The session's audio from sample [heldFrom] on, for the second pass. */ private var held = FloatArray(16000 * 30) private var heldCount = 0 private var heldFrom = 0L fun feed(pcm: ByteArray) { val samples = FloatArray(pcm.size / 2) { i -> ((pcm[2 * i].toInt() and 0xff) or (pcm[2 * i + 1].toInt() shl 8)).toShort() / 32768f } accept(samples) } fun accept(heard: FloatArray) { val samples = leveller?.level(heard) ?: heard if (second != null) hold(samples) live.accept(samples, ::heard) } fun finish(ended: () -> Unit) { live.finish(::heard) later { held = FloatArray(0); heldCount = 0; ended() } } fun close() { held = FloatArray(0) heldCount = 0 live.close() } private fun hold(samples: FloatArray) { if (heldCount + samples.size > held.size) held = held.copyOf(maxOf(held.size * 2, heldCount + samples.size)) samples.copyInto(held, heldCount) heldCount += samples.size } private fun heard(h: Heard) { say(h) if (!h.final || second == null || h.text.isBlank()) return val from = (h.fromMs * rate - heldFrom).toInt().coerceIn(0, heldCount) val to = (h.toMs * rate - heldFrom).toInt().coerceIn(from, heldCount) val sentence = held.copyOfRange(from, to) // Forget everything up to the sentence's end: the next sentence starts after it. held.copyInto(held, 0, to, heldCount) heldCount -= to heldFrom += to later { val read = second.read(sentence, h.language ?: language) ?: return@later val offset = h.fromMs say(Heard("second", true, read.first, read.second.map { it.copy(startMs = it.startMs + offset, endMs = it.endMs + offset) }, h.language ?: language, h.fromMs, h.toMs)) } } } /** * The catalogue's levelling over one stream, before a model that does not level its own input: while the loudest * sample so far stays under [Levelling.belowPeak], every sample is raised so that peak reads [Levelling.toPeak], by at * most [Levelling.maxGain]. A voice loud enough is left as it is. */ class Leveller(private val levelling: Levelling) { private var peak = 0f fun level(samples: FloatArray): FloatArray { for (x in samples) if (kotlin.math.abs(x) > peak) peak = kotlin.math.abs(x) if (peak == 0f || peak >= levelling.belowPeak) return samples val gain = minOf(levelling.toPeak / peak, levelling.maxGain) return FloatArray(samples.size) { (samples[it] * gain).coerceIn(-1f, 1f) } } }