package com.cottondb.voiceimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.buildJsonArrayimport kotlinx.serialization.json.buildJsonObjectimport 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. */dataclassWord(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`. */dataclassHeard(val pass: String,val final: Boolean,val text: String,val words: List<Word>,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. */interfaceLivePass {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. */interfaceSecondPass {/** 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<String, List<Word>>?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. */classDictation(privateval live: LivePass,privateval second: SecondPass?,privateval language: String?,privateval later: (() -> Unit) -> Unit, level: Levelling? = null,privateval say: (Heard) -> Unit,) {privateval leveller = level?.let(::Leveller)privateval rate = 16/** The session's audio from sample [heldFrom] on, for the second pass. */privatevar held = FloatArray(16000 * 30)privatevar heldCount = 0privatevar heldFrom = 0Lfun 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) ?: heardif (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() }privatefun 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 }privatefun heard(h: Heard) { say(h)if (!h.final || second == null || h.text.isBlank()) returnval 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@laterval 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. */classLeveller(privateval levelling: Levelling) {privatevar peak = 0ffun 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 samplesval gain = minOf(levelling.toPeak / peak, levelling.maxGain)returnFloatArray(samples.size) { (samples[it] * gain).coerceIn(-1f, 1f) } }}