Implementation:Neuml Txtai AudioMixer
| Knowledge Sources | |
|---|---|
| Domains | Audio, Signal Processing |
| Last Updated | 2026-02-10 01:00 GMT |
Overview
Concrete tool for mixing multiple audio streams into a single stream provided by txtai.
Description
AudioMixer is a pipeline that combines two audio segments into a single output audio stream. It accepts pairs of audio segments, each consisting of a NumPy array and sample rate. The pipeline automatically resamples audio segments to a common target sample rate when they differ. Audio segments are scaled using configurable scaling factors before mixing, and shorter segments are tiled (looped) to match the length of the longer segment. This enables overlaying background music with speech or combining any two audio sources.
Usage
Use AudioMixer when you need to combine two audio tracks into one, such as overlaying speech with background music, merging sound effects with narration, or blending any two audio signals. It handles sample rate mismatches and length differences automatically.
Code Reference
Source Location
- Repository: Neuml_Txtai
- File: src/python/txtai/pipeline/audio/audiomixer.py
Signature
class AudioMixer(Pipeline):
def __init__(self, rate=None)
def __call__(self, segment, scale1=1, scale2=1)
Import
from txtai.pipeline.audio.audiomixer import AudioMixer
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| rate | int | No | Target sample rate for output audio. When None, uses the sample rate of the first audio segment. Defaults to None. |
| segment | tuple or list | Yes | A tuple of two audio segments: ((audio1, rate1), (audio2, rate2)), or a list of such tuples for batch processing. |
| scale1 | float | No | Scaling factor applied to the first audio segment before mixing. Defaults to 1. |
| scale2 | float | No | Scaling factor applied to the second audio segment before mixing. Defaults to 1. |
Outputs
| Name | Type | Description |
|---|---|---|
| result | tuple(numpy.ndarray, int) | A tuple of (mixed audio waveform, sample rate) when a single segment tuple is passed. |
| results | list of tuple(numpy.ndarray, int) | A list of (mixed audio waveform, sample rate) tuples when a list of segment pairs is passed. |
Usage Examples
from txtai.pipeline import AudioMixer
import numpy as np
# Create an AudioMixer pipeline
mixer = AudioMixer()
# Mix two audio segments
audio1 = (np.random.randn(22050), 22050) # 1 second at 22050 Hz
audio2 = (np.random.randn(22050), 22050) # 1 second at 22050 Hz
mixed, rate = mixer((audio1, audio2))
# Mix with scaling factors (reduce volume of second track)
mixed, rate = mixer((audio1, audio2), scale1=1.0, scale2=0.3)
# Mix with a target sample rate
mixer_resampled = AudioMixer(rate=16000)
mixed, rate = mixer_resampled((audio1, audio2))
# Batch mix multiple pairs
results = mixer([(audio1, audio2), (audio1, audio2)])