Add two-pass speech recognition Android/iOS demo (#304)

This commit is contained in:
Fangjun Kuang
2023-09-12 15:40:16 +08:00
committed by GitHub
parent 8982984ea2
commit debab7c091
97 changed files with 3546 additions and 57 deletions

View File

@@ -21,10 +21,6 @@ private const val REQUEST_RECORD_AUDIO_PERMISSION = 200
class MainActivity : AppCompatActivity() {
private val permissions: Array<String> = arrayOf(Manifest.permission.RECORD_AUDIO)
// If there is a GPU and useGPU is true, we will use GPU
// If there is no GPU and useGPU is true, we won't use GPU
private val useGPU: Boolean = true
private lateinit var model: SherpaOnnx
private var audioRecord: AudioRecord? = null
private lateinit var recordButton: Button
@@ -91,7 +87,7 @@ class MainActivity : AppCompatActivity() {
audioRecord!!.startRecording()
recordButton.setText(R.string.stop)
isRecording = true
model.reset()
model.reset(true)
textView.text = ""
lastText = ""
idx = 0
@@ -125,25 +121,31 @@ class MainActivity : AppCompatActivity() {
while (model.isReady()) {
model.decode()
}
val isEndpoint = model.isEndpoint()
val text = model.text
var textToDisplay = lastText;
if(text.isNotBlank()) {
if (lastText.isBlank()) {
textToDisplay = "${idx}: ${text}"
} else {
textToDisplay = "${lastText}\n${idx}: ${text}"
}
}
if (isEndpoint) {
model.reset()
if (text.isNotBlank()) {
lastText = "${lastText}\n${idx}: ${text}"
textToDisplay = lastText;
idx += 1
}
}
runOnUiThread {
val isEndpoint = model.isEndpoint()
val text = model.text
if(text.isNotBlank()) {
if (lastText.isBlank()) {
textView.text = "${idx}: ${text}"
} else {
textView.text = "${lastText}\n${idx}: ${text}"
}
}
if (isEndpoint) {
model.reset()
if (text.isNotBlank()) {
lastText = "${lastText}\n${idx}: ${text}"
idx += 1
}
}
textView.text = textToDisplay
}
}
}

View File

@@ -77,7 +77,7 @@ class SherpaOnnx(
acceptWaveform(ptr, samples, sampleRate)
fun inputFinished() = inputFinished(ptr)
fun reset() = reset(ptr)
fun reset(recreate: Boolean = false) = reset(ptr, recreate = recreate)
fun decode() = decode(ptr)
fun isEndpoint(): Boolean = isEndpoint(ptr)
fun isReady(): Boolean = isReady(ptr)
@@ -99,7 +99,7 @@ class SherpaOnnx(
private external fun acceptWaveform(ptr: Long, samples: FloatArray, sampleRate: Int)
private external fun inputFinished(ptr: Long)
private external fun getText(ptr: Long): String
private external fun reset(ptr: Long)
private external fun reset(ptr: Long, recreate: Boolean)
private external fun decode(ptr: Long)
private external fun isEndpoint(ptr: Long): Boolean
private external fun isReady(ptr: Long): Boolean