app/src/main/kotlin/com/honjimaku/subrep/Recognizer.kt (962 bytes)
1 package com.honjimaku.subrep 2 3 /** Turns one piece of speech into text: Whisper on the phone, or [CloudRecognizer]. */ 4 interface Recognizer : AutoCloseable { 5 6 /** The language found in the last piece, "ja", "ru", ..., or "" when not known. */ 7 val detectedLanguage: String 8 9 /** The text of [samples]: 16 kHz mono, from -1 to 1. [language] is a code, or "auto". */ 10 fun transcribe(samples: FloatArray, language: String): String 11 } 12 13 /** Recognition on subread.space. Each piece costs its length in seconds from the hours of the account. */ 14 class CloudRecognizer(private val cloud: Cloud, private val onSecondsLeft: (Long) -> Unit) : Recognizer { 15 16 override val detectedLanguage: String = "" 17 18 override fun transcribe(samples: FloatArray, language: String): String { 19 val answer = cloud.transcribe(Pcm.toBytes(samples), language) 20 onSecondsLeft(answer.secondsLeft) 21 return answer.text 22 } 23 24 override fun close() = Unit 25 }