Skip to main content

Sending audio data

To recognize PCM audio supplied by the app instead of using the vehicle microphone, pass a ByteArray to sendAudio(). This feature is available only in ON_DEVICE mode. You can send the entire buffer at once or stream it in multiple chunks.

info

The 3,200-byte chunk and 100 ms interval in this document are an example of streaming 16 kHz, 16-bit, mono PCM at real-time speed. For another audio format, calculate the chunk size that corresponds to 100 ms of that format.

Send a complete buffer​

val pcmData = applicationContext.assets
.open("test_audio.pcm")
.use { it.readBytes() }

speechToText.sendAudio(pcmData)
speechToText.completeAudioSend()

Stream chunks​

private const val BYTES_PER_100_MS = 3_200

val pcmData = applicationContext.assets
.open("test_audio.pcm")
.use { it.readBytes() }

pcmData.asList()
.chunked(BYTES_PER_100_MS)
.forEach { chunk ->
speechToText.sendAudio(chunk.toByteArray())
Thread.sleep(100L)
}

speechToText.completeAudioSend()

After sending the final audio data, always call completeAudioSend() to signal that the input is complete. The previously registered ResultListener receives the recognition results.