stream_compressed_audio

Transcribe contiguous chunks of non PCM format audio

This event is an alternative for sending audio chunks of non PCM format audio. Compressed audio formats like mp4 or ogg are supported. The event payload is the following:

  • Audio samples in binary format
  • MIME type

Some example code:

path = '/test_audio.wav'
chunkSize = 1024 /* size of chunk in bytes */

const response = await fetch(path)
const blob = await response.blob()

const splitBlob = (file, chunkSize) => {
  let startPointer = 0
  let endPointer = file.size
  const cSize = Math.min(file.size, chunkSize)
  const chunks = []
  while (startPointer < endPointer) {
    let newStartPointer = startPointer + cSize;
    chunks.push(file.slice(startPointer, newStartPointer))
    startPointer = newStartPointer
  }
  return chunks
}

const blobChunks = splitBlob(blob, 1024)

for (const chunk of blobChunks) {
  socketLocal.emit('stream_compressed_audio', chunk, blob.type)
}

The response to this event is identical to the response to the stream_audio_samples event, i.e. a chronologically ordered sequence of recognition and partial_recognition events.

For Web environments according to the type of generated chunks stream_audio_samples should use ScriptProcessorNode and stream_compressed_audio should use MediaRecorder. Experiments show that MediaRecorder is preferred because it produces much less audio distortion.