expo-speech-recognition 2.1.4 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # expo-speech-recognition
2
2
 
3
+ ## 2.1.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 9010d14: Android: Use `VOICE_RECOGNITION` audio source input by default and harden the record loop to gracefully handle read errors
8
+
3
9
  ## 2.1.4
4
10
 
5
11
  ### Patch Changes
@@ -7,7 +7,9 @@ import android.media.AudioRecord
7
7
  import android.media.MediaRecorder
8
8
  import android.os.ParcelFileDescriptor
9
9
  import android.os.ParcelFileDescriptor.AutoCloseOutputStream
10
+ import android.os.Build
10
11
  import android.util.Log
12
+ import androidx.annotation.RequiresApi
11
13
  import java.io.DataOutputStream
12
14
  import java.io.File
13
15
  import java.io.FileOutputStream
@@ -26,6 +28,7 @@ interface AudioRecorder {
26
28
  *
27
29
  * Once stopped, the recording stream is written to a wav file for external use
28
30
  */
31
+ @RequiresApi(Build.VERSION_CODES.GINGERBREAD)
29
32
  class ExpoAudioRecorder(
30
33
  private val context: Context,
31
34
  // Optional output file path
@@ -131,7 +134,7 @@ class ExpoAudioRecorder(
131
134
  @SuppressLint("MissingPermission")
132
135
  private fun createRecorder(): AudioRecord =
133
136
  AudioRecord(
134
- MediaRecorder.AudioSource.DEFAULT,
137
+ MediaRecorder.AudioSource.VOICE_RECOGNITION,
135
138
  sampleRateInHz,
136
139
  channelConfig,
137
140
  AudioFormat.ENCODING_PCM_16BIT,
@@ -194,23 +197,60 @@ class ExpoAudioRecorder(
194
197
 
195
198
  private fun streamAudioToPipe() {
196
199
  val tempFileOutputStream = FileOutputStream(tempPcmFile)
197
- val data = ByteArray(bufferSizeInBytes / 2)
200
+ val data = ByteArray(bufferSizeInBytes)
198
201
 
199
202
  while (isRecordingAudio) {
200
- val read = audioRecorder!!.read(data, 0, data.size)
201
- if (read > 0) {
202
- try {
203
- outputStream?.write(data, 0, read)
204
- outputStream?.flush()
203
+ val recorder = audioRecorder ?: break
204
+
205
+ val read =
206
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
207
+ // avoid empty reads
208
+ recorder.read(data, 0, data.size, AudioRecord.READ_BLOCKING)
209
+ } else {
210
+ recorder.read(data, 0, data.size)
211
+ }
212
+
213
+ when {
214
+ read > 0 -> {
215
+ try {
216
+ outputStream?.write(data, 0, read)
217
+ outputStream?.flush()
205
218
 
206
- // Write to the temp PCM file
207
- if (outputFilePath != null) {
208
- tempFileOutputStream.write(data, 0, read)
209
- tempFileOutputStream.flush()
219
+ // Write to the temp PCM file
220
+ if (outputFilePath != null) {
221
+ tempFileOutputStream.write(data, 0, read)
222
+ tempFileOutputStream.flush()
223
+ }
224
+ } catch (e: IOException) {
225
+ Log.e(TAG, "Failed to write to output stream", e)
226
+ e.printStackTrace()
210
227
  }
211
- } catch (e: IOException) {
212
- Log.e(TAG, "Failed to write to output stream", e)
213
- e.printStackTrace()
228
+ }
229
+
230
+ // (this should only happen on API 22 and below)
231
+ read == 0 -> {
232
+ try {
233
+ Thread.sleep(10)
234
+ } catch (_: InterruptedException) {}
235
+ }
236
+
237
+ read == AudioRecord.ERROR_DEAD_OBJECT -> {
238
+ Log.w(TAG, "AudioRecord returned ERROR_DEAD_OBJECT; breaking out of the loop")
239
+ // todo: we should probably emit an error event here
240
+ break
241
+ }
242
+
243
+ read == AudioRecord.ERROR_INVALID_OPERATION || read == AudioRecord.ERROR_BAD_VALUE -> {
244
+ Log.w(TAG, "AudioRecord read error: $read; backing off briefly")
245
+ try {
246
+ Thread.sleep(10)
247
+ } catch (_: InterruptedException) {}
248
+ }
249
+
250
+ else -> {
251
+ // todo: we should probably emit an error event here
252
+ Log.w(TAG, "AudioRecord read returned '$read'; breaking out of the loop")
253
+ break
214
254
  }
215
255
  }
216
256
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-speech-recognition",
3
- "version": "2.1.4",
3
+ "version": "2.1.5",
4
4
  "description": "Speech Recognition for React Native Expo projects",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",