Updated to finally fix the double up text during the translastion at sometimes at the start of the second press of the microphone button

This commit is contained in:
2026-01-23 12:24:56 +11:00
parent cce093db4e
commit f17c6ab84e
13 changed files with 43 additions and 24 deletions

View File

@@ -11,7 +11,7 @@ android {
applicationId = "net.mmanningau.speechtokeyboard"
minSdk = 28
targetSdk = 36
versionCode = 9
versionCode = 10
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
@@ -25,6 +25,11 @@ android {
"proguard-rules.pro"
)
}
debug {
applicationIdSuffix = ".streaming"
// This changes the app name on your homescreen to "MyApp (Dev)"
resValue("string", "app_name", "Speech To Keyboard (Streaming)")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11

Binary file not shown.

After

Width:  |  Height:  |  Size: 411 KiB

View File

@@ -140,9 +140,12 @@ class TestModelActivity : AppCompatActivity() {
return
}
// Reset the stream for a new session
// Note: Sherpa streams can be persistent, but resetting ensures clean start
// If you want continuous conversation, don't reset 'committedText'
// FIX 1: CLEAR THE BUFFER
// This prevents the "ghost text" from the previous session appearing
// when you hit record again.
stream?.let { activeStream ->
recognizer?.reset(activeStream)
}
isRecording = true
micButton.setColorFilter(android.graphics.Color.RED)
@@ -168,9 +171,7 @@ class TestModelActivity : AppCompatActivity() {
val sampleRate = 16000
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.
// Guard clauses
val localRec = recognizer ?: return
val localStream = stream ?: return
@@ -188,7 +189,6 @@ class TestModelActivity : AppCompatActivity() {
if (ret > 0) {
val samples = FloatArray(ret) { buffer[it] / 32768.0f }
// 2. Use the LOCAL (non-null) variables
localStream.acceptWaveform(samples, sampleRate)
while (localRec.isReady(localStream)) {
@@ -201,13 +201,25 @@ class TestModelActivity : AppCompatActivity() {
if (text.isNotEmpty()) {
val cleanText = text.lowercase()
runOnUiThread {
if (isEndpoint) {
// FIX 2: THE ORDER OF OPERATIONS
// A. Update UI first
runOnUiThread {
committedText += "$cleanText "
outputText.text = committedText
sendToPico("$cleanText ")
}
// B. RESET IMMEDIATELY ON BACKGROUND THREAD
// We do this HERE, not inside runOnUiThread.
// This guarantees the stream is clean BEFORE the loop
// reads the next chunk of audio.
localRec.reset(localStream)
} else {
// Standard partial update
runOnUiThread {
outputText.text = "$committedText $cleanText"
}
}

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#0878F5</color>
</resources>