Implementation:Neuml Txtai AudioStream
| Knowledge Sources | |
|---|---|
| Domains | Audio, Playback, Streaming |
| Last Updated | 2026-02-10 01:00 GMT |
Overview
Concrete tool for streaming audio playback to an output device provided by txtai.
Description
AudioStream is a threaded pipeline that streams audio segments to an output audio device. It runs a background playback thread that reads audio segments from an internal queue and plays them sequentially via the sounddevice library. Audio segments are optionally resampled to a target sample rate before playback. The pipeline is designed for local machines that have access to an audio output device. A special sentinel value AudioStream.COMPLETE can be queued to signal the end of the audio stream, and the wait() method blocks until all queued audio has been played.
Usage
Use AudioStream when you need to play audio in real-time on a local machine, such as playing back speech synthesis output from the TextToSpeech pipeline, building interactive voice applications, or streaming generated audio to speakers. It is designed for local execution environments with access to audio hardware.
Code Reference
Source Location
- Repository: Neuml_Txtai
- File: src/python/txtai/pipeline/audio/audiostream.py
Signature
class AudioStream(Pipeline):
COMPLETE = (1, None)
def __init__(self, rate=None)
def __call__(self, segment)
def wait(self)
Import
from txtai.pipeline.audio.audiostream import AudioStream
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| rate | int | No | Target sample rate for audio playback. When None, uses the sample rate from each input segment. Defaults to None. |
| segment | tuple or list | Yes | A single audio segment as (audio, sample_rate) or a list of such tuples. Each audio element is a NumPy array. |
Outputs
| Name | Type | Description |
|---|---|---|
| result | tuple(numpy.ndarray, int) | Returns the input segment unchanged when a single tuple is passed. Audio is queued for playback in the background. |
| results | list of tuple(numpy.ndarray, int) | Returns the list of input segments unchanged when a list is passed. |
Usage Examples
from txtai.pipeline import AudioStream, TextToSpeech
# Create pipelines
tts = TextToSpeech()
player = AudioStream()
# Generate and play speech
audio = tts("Hello, this is a test of audio streaming.")
player(audio)
# Signal end of stream and wait for playback to complete
player(AudioStream.COMPLETE)
player.wait()
# Play multiple segments
segments = [
tts("First sentence."),
tts("Second sentence.")
]
for segment in segments:
player(segment)
player(AudioStream.COMPLETE)
player.wait()