Amazon Transcribe streaming waiting for incoming transcription after terminating input audio stream

49 views Asked by At

I am streaming microphone audio to Amazon Transcribe for dictation. Currently when the user lets go of the transcription button, all transcription that has yet to be returned from AWS transcribe is lost, even though the audio has already been streamed. I would like to have some way of waiting for or knowing when all the already streamed audio has been processed and returned.

I currently have a function that is passed into the AudioStream prop of StartMedicalStreamTranscriptionCommand that looks like this:

// Generator function to get audio stream in correct format for AWS Transcribe
async function* getAudioStream(
  audioDataIterator: AsyncIterable<MessageEvent<AudioStreamWorkletProcessorMessage>>,
  abortSignal: AbortSignal
) {
  for await (const { data } of audioDataIterator) {
    yield { AudioEvent: { AudioChunk: data.pcmData } };

    if (abortSignal.aborted) {
      break;
    }
  }

  // Wait for a few seconds to allow AWS Transcribe to finish processing audio
  // after user has let go of the dictation button
  await new Promise((r) => setTimeout(r, 3000));
}

When the transcription is aborted, I want all already processed transcription data to still be sent back. When aborted all transcription that is yet to be returned from AWS Transcribe is lost. Currently, I have added a band-aid fix for this by waiting an arbitrary number of seconds before returning from the function, which allows for the remaining transcriptions to come through.

Is there a more proper way to handle this situation?

0

There are 0 answers