Skip to content

Commit 09bb004

Browse files
authored
Merge pull request #79 from mschroederi/mic-streaming-crash-r0.8
Move finishing transcription to recording thread
2 parents 5ac4e6b + 0d3c1ac commit 09bb004

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

android_mic_streaming/app/src/main/java/org/deepspeechdemo/MainActivity.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MainActivity : AppCompatActivity() {
2222

2323
// Change the following parameters regarding
2424
// what works best for your use case or your language.
25-
private val BEAM_WIDTH = 500
25+
private val BEAM_WIDTH = 500L
2626
private val LM_ALPHA = 0.931289039105002f
2727
private val LM_BETA = 1.1834137581510284f
2828

@@ -40,8 +40,7 @@ class MainActivity : AppCompatActivity() {
4040

4141
private fun checkAudioPermission() {
4242
// permission is automatically granted on sdk < 23 upon installation
43-
if (Build.VERSION.SDK_INT >= 23)
44-
{
43+
if (Build.VERSION.SDK_INT >= 23) {
4544
val permission = Manifest.permission.RECORD_AUDIO
4645

4746
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
@@ -55,14 +54,17 @@ class MainActivity : AppCompatActivity() {
5554

5655
while (isRecording) {
5756
recorder?.read(
58-
audioData,
59-
0,
60-
NUM_BUFFER_ELEMENTS
57+
audioData,
58+
0,
59+
NUM_BUFFER_ELEMENTS
6160
)
6261
model?.feedAudioContent(streamContext, audioData, audioData.size)
6362
val decoded = model?.intermediateDecode(streamContext)
6463
runOnUiThread { transcription.text = decoded }
6564
}
65+
val decoded = model?.finishStream(streamContext)
66+
runOnUiThread { transcription.text = decoded }
67+
recorder?.stop()
6668
}
6769

6870
private fun createModel(): Boolean {
@@ -102,11 +104,11 @@ class MainActivity : AppCompatActivity() {
102104

103105
if (recorder == null) {
104106
recorder = AudioRecord(
105-
MediaRecorder.AudioSource.VOICE_RECOGNITION,
106-
model.sampleRate(),
107-
RECORDER_CHANNELS,
108-
RECORDER_AUDIO_ENCODING,
109-
NUM_BUFFER_ELEMENTS * BYTES_PER_ELEMENT)
107+
MediaRecorder.AudioSource.VOICE_RECOGNITION,
108+
model.sampleRate(),
109+
RECORDER_CHANNELS,
110+
RECORDER_AUDIO_ENCODING,
111+
NUM_BUFFER_ELEMENTS * BYTES_PER_ELEMENT)
110112
}
111113

112114
recorder?.startRecording()
@@ -134,11 +136,6 @@ class MainActivity : AppCompatActivity() {
134136
private fun stopListening() {
135137
isRecording = false
136138
btnStartInference.text = "Start Recording"
137-
138-
val decoded = model?.finishStream(streamContext)
139-
transcription.text = decoded
140-
141-
recorder?.stop()
142139
}
143140

144141
fun onRecordClick(v: View?) {

0 commit comments

Comments
 (0)