Skip to main content

Sending Audio Data

To recognize PCM audio held by the app instead of the vehicle microphone, pass a ByteArray to sendAudio(). This feature is only available in ON_DEVICE mode. You can send the entire data at once or split it into multiple chunks for transmission.

note

The 3,200-byte chunks and 100 ms intervals in this document are examples of transmitting 16 kHz, 16-bit, mono PCM at real-time speed. If using a different audio format, calculate the chunk size to match 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 last audio data, be sure to call completeAudioSend() to indicate that the input has ended. The recognition result will be delivered to the pre-registered ResultListener.