Audio Mixer

pipeline pipeline

The Audio Mixer pipeline mixes multiple audio streams into a single stream.

Example

The following shows a simple example using this pipeline.

  1. from txtai.pipeline import AudioMixer
  2. # Create and run pipeline
  3. mixer = AudioMixer()
  4. mixer(((audio1, rate1), (audio2, rate2)))

See the link below for a more detailed example.

NotebookDescription
Generative AudioStorytelling with generative audio workflowsOpen 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. audiomixer:
  3. # Run pipeline with workflow
  4. workflow:
  5. audiomixer:
  6. tasks:
  7. - action: audiomixer

Run with Workflows

  1. from txtai import Application
  2. # Create and run pipeline with workflow
  3. app = Application("config.yml")
  4. list(app.workflow("audiomixer", [[[audio1, rate1], [audio2, rate2]]]))

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":"audiomixer", "elements":[[[audio1, rate1], [audio2, rate2]]]}'

Methods

Python documentation for the pipeline.

__init__(rate=None)

Creates an AudioMixer pipeline.

Parameters:

NameTypeDescriptionDefault
rate

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

None

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

  1. 14
  2. 15
  3. 16
  4. 17
  5. 18
  6. 19
  7. 20
  8. 21
  9. 22
  10. 23
  11. 24
  12. 25
  13. 26
  1. def init(self, rate=None):
  2. “””
  3. Creates an AudioMixer pipeline.
  4. Args:
  5. rate: optional target sample rate, otherwise uses input target rate with each audio segment
  6. “””
  7. if not SCIPY:
  8. raise ImportError(‘AudioMixer pipeline is not available - install pipeline extra to enable.’)
  9. # Target sample rate
  10. self.rate = rate

__call__(segment, scale1=1, scale2=1)

Mixes multiple audio streams into a single stream.

Parameters:

NameTypeDescriptionDefault
segment

((audio1, sample rate), (audio2, sample rate))|list

required
scale1

optional scaling factor for segment1

1
scale2

optional scaling factor for segment2

1

Returns:

TypeDescription

list of (audio, sample rate)

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

  1. 28
  2. 29
  3. 30
  4. 31
  5. 32
  6. 33
  7. 34
  8. 35
  9. 36
  10. 37
  11. 38
  12. 39
  13. 40
  14. 41
  15. 42
  16. 43
  17. 44
  18. 45
  19. 46
  20. 47
  21. 48
  22. 49
  23. 50
  24. 51
  25. 52
  26. 53
  27. 54
  28. 55
  29. 56
  30. 57
  31. 58
  1. def call(self, segment, scale1=1, scale2=1):
  2. “””
  3. Mixes multiple audio streams into a single stream.
  4. Args:
  5. segment: ((audio1, sample rate), (audio2, sample rate))|list
  6. scale1: optional scaling factor for segment1
  7. scale2: optional scaling factor for segment2
  8. Returns:
  9. list of (audio, sample rate)
  10. “””
  11. # Convert single element to list
  12. segments = [segment] if isinstance(segment, tuple) else segment
  13. results = []
  14. for segment1, segment2 in segments:
  15. audio1, rate1 = segment1
  16. audio2, rate2 = segment2
  17. # Resample audio, as necessary
  18. target = self.rate if self.rate else rate1
  19. audio1 = Signal.resample(audio1, rate1, target)
  20. audio2 = Signal.resample(audio2, rate2, target)
  21. # Mix audio into single segment
  22. results.append((Signal.mix(audio1, audio2, scale1, scale2), target))
  23. # Return single element if single element passed in
  24. return results[0] if isinstance(segment, tuple) else results