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
- Kotlin
- Java
val pcmData = applicationContext.assets
.open("test_audio.pcm")
.use { it.readBytes() }
speechToText.sendAudio(pcmData)
speechToText.completeAudioSend()
byte[] pcmData;
try (InputStream input = getAssets().open("test_audio.pcm");
ByteArrayOutputStream output = new ByteArrayOutputStream()) {
byte[] buffer = new byte[8_192];
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
pcmData = output.toByteArray();
}
speechToText.sendAudio(pcmData);
speechToText.completeAudioSend();
Stream chunks
- Kotlin
- Java
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()
private static final int BYTES_PER_100_MS = 3_200;
byte[] buffer = new byte[BYTES_PER_100_MS];
try (InputStream input = getAssets().open("test_audio.pcm")) {
int read;
while ((read = input.read(buffer)) != -1) {
speechToText.sendAudio(Arrays.copyOf(buffer, read));
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.