Transcribe Multiple Streams

In this example, we will transcribe multiple streams concurrently. The motivation for this example is to transcribe multiple live streams in real-time with low latency, such as radio channels, TV channels and phone or video calls. However, for demonstration purposes we are going to read the audio data from file.

Channel

Channel is a simple wrapper class that stores all the information required to transcribed a particular channel. In this example, we read the audio data from file. In real-world applications, the source of audio data could come from a TV or radio channel.

class Channel:
    def __init__(self, idx: int, client: Client) -> None:
        self._idx = idx
        self._client = client

    def iter_audio(self) -> Iterable[bytes]:
        with open(TEST_AUDIO_LONG_RAW, "rb") as fh:
            while True:
                audio = fh.read(1024)
                if len(audio) == 0:
                    break
                yield audio

    def run(self) -> None:
        for result in transcribe_stream(
            self.iter_audio(),
            self._client,
            audio_format="pcm_s16le",
            sample_rate_hertz=16000,
            num_audio_channels=1,
        ):
            words = " ".join([w.text for w in result.words])
            print(f"Channel {self._idx}: {words}")

Putting it together

The glue code that remains:

  1. Create a single Client that is shared among all the channels.

  2. Create and start all the channels in its own thread.

  3. Join all the threads.

def main():
    # Create a single client for all channels / threads.
    with Client() as client:
        # Create channels and start threads that run Channel.run().
        threads = []
        for channel_num in range(NUM_CHANNELS):
            channel = Channel(channel_num, client)
            thread = threading.Thread(target=channel.run)
            thread.start()
            threads.append(thread)

        # Join threads.
        for thread in threads:
            thread.join()

if __name__ == "__main__":
    main()

The program will concurrenlty transcribe all channels. The print outs from different channels may be interleaved.

Run!

examples/transcribe_multiple_streams.py GitHub

python3 transcribe_multiple_live_streams.py