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
- 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 final audio data, always call completeAudioSend() to signal that the input is complete. The previously registered ResultListener receives the recognition results.