Updated the streaming to avoid the double ups in the output...looks better now...
This commit is contained in:
@@ -11,7 +11,7 @@ android {
|
|||||||
applicationId = "net.mmanningau.speechtokeyboard"
|
applicationId = "net.mmanningau.speechtokeyboard"
|
||||||
minSdk = 28
|
minSdk = 28
|
||||||
targetSdk = 36
|
targetSdk = 36
|
||||||
versionCode = 8
|
versionCode = 9
|
||||||
versionName = "1.0"
|
versionName = "1.0"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
|||||||
@@ -156,26 +156,28 @@ class TestModelActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
private fun stopRecording() {
|
private fun stopRecording() {
|
||||||
isRecording = false
|
isRecording = false
|
||||||
recordingThread?.join() // Wait for loop to finish
|
recordingThread?.join()
|
||||||
micButton.clearColorFilter()
|
micButton.clearColorFilter()
|
||||||
outputText.text = "$committedText [Stopped]"
|
|
||||||
|
// Just show what we have, don't overwrite with "[Stopped]"
|
||||||
|
// to prevent visual jarring.
|
||||||
|
outputText.append("\n[Stopped]")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun processAudioLoop() {
|
private fun processAudioLoop() {
|
||||||
val sampleRate = 16000
|
val sampleRate = 16000
|
||||||
val bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT)
|
val bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT)
|
||||||
|
|
||||||
|
// 1. GUARD CLAUSE: Unpack nullables safely
|
||||||
|
// If recognizer or stream are null, we stop immediately.
|
||||||
|
// This creates 'localRec' and 'localStream' which are GUARANTEED non-null.
|
||||||
|
val localRec = recognizer ?: return
|
||||||
|
val localStream = stream ?: return
|
||||||
|
|
||||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- FIX START ---
|
|
||||||
// Capture global variables into local non-null variables.
|
|
||||||
// If either is null, we just exit the loop safely.
|
|
||||||
val activeStream = stream ?: return
|
|
||||||
val activeRecognizer = recognizer ?: return
|
|
||||||
// --- FIX END ---
|
|
||||||
|
|
||||||
val record = AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize)
|
val record = AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize)
|
||||||
record.startRecording()
|
record.startRecording()
|
||||||
|
|
||||||
@@ -186,28 +188,27 @@ class TestModelActivity : AppCompatActivity() {
|
|||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
val samples = FloatArray(ret) { buffer[it] / 32768.0f }
|
val samples = FloatArray(ret) { buffer[it] / 32768.0f }
|
||||||
|
|
||||||
// Use 'activeStream' and 'activeRecognizer' (No ? needed anymore)
|
// 2. Use the LOCAL (non-null) variables
|
||||||
activeStream.acceptWaveform(samples, sampleRate)
|
localStream.acceptWaveform(samples, sampleRate)
|
||||||
|
|
||||||
while (activeRecognizer.isReady(activeStream)) {
|
while (localRec.isReady(localStream)) {
|
||||||
activeRecognizer.decode(activeStream)
|
localRec.decode(localStream)
|
||||||
}
|
}
|
||||||
|
|
||||||
val text = activeRecognizer.getResult(activeStream).text
|
val text = localRec.getResult(localStream).text
|
||||||
|
val isEndpoint = localRec.isEndpoint(localStream)
|
||||||
|
|
||||||
if (text.isNotEmpty()) {
|
if (text.isNotEmpty()) {
|
||||||
val cleanText = text.lowercase()
|
val cleanText = text.lowercase()
|
||||||
|
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
outputText.text = "$committedText $cleanText"
|
if (isEndpoint) {
|
||||||
}
|
|
||||||
|
|
||||||
if (activeRecognizer.isEndpoint(activeStream)) {
|
|
||||||
if (cleanText.isNotBlank()) {
|
|
||||||
committedText += "$cleanText "
|
committedText += "$cleanText "
|
||||||
|
outputText.text = committedText
|
||||||
sendToPico("$cleanText ")
|
sendToPico("$cleanText ")
|
||||||
|
localRec.reset(localStream)
|
||||||
// Reset the stream
|
} else {
|
||||||
activeRecognizer.reset(activeStream)
|
outputText.text = "$committedText $cleanText"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user