stt and mic capture live tested. capture is now async

This commit is contained in:
Joshua Perry 2024-10-23 18:31:04 +01:00
parent 83e3c53ebc
commit 68ddc43446
4 changed files with 32 additions and 10 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -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")
}

View File

@ -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)
}

View File

@ -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
}
}