diff --git a/.idea/misc.xml b/.idea/misc.xml index a1bcc4b..051fa4a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 9b7fabd..2ec23f2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,6 +27,8 @@ dependencies { //Java Native Access implementation("net.java.dev.jna:jna:5.15.0") implementation("net.java.dev.jna:jna-platform:5.15.0") + //Coroutines + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0") } diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 51b6871..9e10b57 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -9,7 +9,12 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.window.Window import androidx.compose.ui.window.application +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.async +import kotlinx.coroutines.runBlocking +@OptIn(DelicateCoroutinesApi::class) @Composable @Preview fun App() { @@ -17,7 +22,8 @@ fun App() { MaterialTheme { Button(onClick = { - text = "Hello, Desktop!" + val mic = Microphone() + GlobalScope.async { mic.startCapture() } }) { Text(text) } diff --git a/src/main/kotlin/Microphone.kt b/src/main/kotlin/Microphone.kt index 9f86878..86eeb7f 100644 --- a/src/main/kotlin/Microphone.kt +++ b/src/main/kotlin/Microphone.kt @@ -1,7 +1,10 @@ +import kotlinx.coroutines.coroutineScope import javax.sound.sampled.AudioFormat import javax.sound.sampled.AudioSystem import javax.sound.sampled.DataLine import javax.sound.sampled.TargetDataLine +import kotlinx.coroutines.launch + /** * TODO: Documentation */ @@ -17,9 +20,22 @@ class Microphone { /** * */ - fun startCapture() { + suspend fun startCapture() { + source.open(audioFormat, source.bufferSize) source.start() - //TODO: Start processing loop in new coroutine and check if it could be moved into own file + + val buffer = ByteArray(160000) + //TODO: Start loop in own coroutine + coroutineScope { + launch { + while (true) { + val audioBytes = source.read(buffer, 0, buffer.size) + val stt = STT() + val result = stt.parseBuffer(buffer, audioBytes) + println("Captured Speech: $result") + } + } + } } /** * @@ -37,12 +53,12 @@ class Microphone { * */ private fun getFormat(): AudioFormat { - return AudioFormat( //TODO: Get format settings from user settings + return AudioFormat( 16000.0f, // 16000 Required 16, - 2, + 1, true, - true + false ) } /** @@ -53,9 +69,7 @@ class Microphone { TargetDataLine::class.java, audioFormat ) - val source = AudioSystem.getLine(info) as TargetDataLine - source.open(audioFormat) - return source + return AudioSystem.getLine(info) as TargetDataLine } } \ No newline at end of file