Audio Stream

pipeline pipeline

The Audio Stream pipeline is a threaded pipeline that plays audio segments. This pipeline is designed to run on local machines given that it requires access to write to an output device.

Example

The following shows a simple example using this pipeline.

  1. from txtai.pipeline import AudioStream
  2. # Create and run pipeline
  3. audio = AudioStream()
  4. audio(data)

This pipeline may require additional system dependencies. See this section for more.

See the link below for a more detailed example.

NotebookDescription
Speech to Speech RAG ▶️Full cycle speech to speech workflow with RAGOpen In Colab

Configuration-driven example

Pipelines are run with Python or configuration. Pipelines can be instantiated in configuration using the lower case name of the pipeline. Configuration-driven pipelines are run with workflows or the API.

config.yml

  1. # Create pipeline using lower case class name
  2. audiostream:
  3. # Run pipeline with workflow
  4. workflow:
  5. audiostream:
  6. tasks:
  7. - action: audiostream

Run with Workflows

  1. from txtai import Application
  2. # Create and run pipeline with workflow
  3. app = Application("config.yml")
  4. list(app.workflow("audiostream", [["numpy data", "sample rate"]]))

Run with API

  1. CONFIG=config.yml uvicorn "txtai.api:app" &
  2. curl \
  3. -X POST "http://localhost:8000/workflow" \
  4. -H "Content-Type: application/json" \
  5. -d '{"name":"audiostream", "elements":[["numpy data", "sample rate"]]}'

Methods

Python documentation for the pipeline.

__init__(rate=None)

Creates an AudioStream pipeline.

Parameters:

NameTypeDescriptionDefault
rate

optional target sample rate, otherwise uses input target rate with each audio segment

None

Source code in txtai/pipeline/audio/audiostream.py

  1. 30
  2. 31
  3. 32
  4. 33
  5. 34
  6. 35
  7. 36
  8. 37
  9. 38
  10. 39
  11. 40
  12. 41
  13. 42
  14. 43
  15. 44
  16. 45
  17. 46
  18. 47
  19. 48
  20. 49
  21. 50
  22. 51
  1. def init(self, rate=None):
  2. “””
  3. Creates an AudioStream pipeline.
  4. Args:
  5. rate: optional target sample rate, otherwise uses input target rate with each audio segment
  6. “””
  7. if not AUDIOSTREAM:
  8. raise ImportError(
  9. (
  10. AudioStream pipeline is not available - install pipeline extra to enable.
  11. Also check that the portaudio system library is available.”
  12. )
  13. )
  14. # Target sample rate
  15. self.rate = rate
  16. self.queue = Queue()
  17. self.thread = Thread(target=self.play)
  18. self.thread.start()

__call__(segment)

Queues audio segments for the audio player.

Parameters:

NameTypeDescriptionDefault
segment

(audio, sample rate)|list

required

Returns:

TypeDescription

segment

Source code in txtai/pipeline/audio/audiostream.py

  1. 53
  2. 54
  3. 55
  4. 56
  5. 57
  6. 58
  7. 59
  8. 60
  9. 61
  10. 62
  11. 63
  12. 64
  13. 65
  14. 66
  15. 67
  16. 68
  17. 69
  18. 70
  19. 71
  1. def call(self, segment):
  2. “””
  3. Queues audio segments for the audio player.
  4. Args:
  5. segment: (audio, sample rate)|list
  6. Returns:
  7. segment
  8. “””
  9. # Convert single element to list
  10. segments = [segment] if isinstance(segment, tuple) else segment
  11. for x in segments:
  12. self.queue.put(x)
  13. # Return single element if single element passed in
  14. return segments[0] if isinstance(segment, tuple) else segments