arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
MixGain< T > Struct Template Reference

Mixing of multiple audio input streams into a single output stream. More...

#include <AudioOutput.h>

Public Member Functions

 MixGain ()=default
 
 MixGain (float f)
 
scale (T sample) const
 

Public Attributes

float value = 0
 

Detailed Description

template<typename T>
struct audio_tools::MixGain< T >

Mixing of multiple audio input streams into a single output stream.

The OutputMixer allows you to combine multiple audio streams by summing their samples with configurable weights per channel. Each input stream is buffered independently using ring buffers, and the mixer outputs the combined result when all buffers have sufficient data available.

Features:

Auto Index Functionality: By default, auto-indexing is enabled (setAutoIndex(true)). When using the basic write() method without specifying a stream index, the mixer automatically:

  1. Writes data to the current stream index (starting at 0)
  2. Increments to the next stream index after each write
  3. Automatically calls flushMixer() after writing to the last stream
  4. Resets the index back to 0 for the next cycle

This enables simple round-robin writing where you just call write() repeatedly and the mixer handles stream distribution and output flushing automatically.

Usage Examples:

Auto Index Mode (default):

OutputMixer<int16_t> mixer(Serial, 3); // 3 input streams to Serial output
mixer.begin(1024); // 1KB buffer per stream
// Simple round-robin writing - auto-increments stream index
mixer.write(audio_data1, length); // -> stream 0, index = 1
mixer.write(audio_data2, length); // -> stream 1, index = 2
mixer.write(audio_data3, length); // -> stream 2, auto-flush, index = 0
// Process repeats automatically
static HardwareSerial Serial
Definition Arduino.h:179
Definition AudioOutput.h:411

Manual Index Mode:

mixer.begin(1024);
mixer.setAutoIndex(false); // Disable auto-indexing
mixer.setWeight(0, 0.8f); // Stream 0 at 80% volume
mixer.setWeight(1, 1.0f); // Stream 1 at 100% volume
mixer.setWeight(2, 0.5f); // Stream 2 at 50% volume
// Write data to specific streams manually
mixer.write(0, audio_data1, length); // Write to stream 0
mixer.write(1, audio_data2, length); // Write to stream 1
mixer.write(2, audio_data3, length); // Write to stream 2
mixer.flushMixer(); // Manually trigger mix and output
Note
By default uses RingBuffer as the buffer type. Buffer type can be customized using setCreateBufferCallback().
All input streams must have the same sample format (bit depth, sample rate).
The mixer normalizes output by dividing by the total weight sum.
In auto-index mode, ensure you write to all streams in each cycle for proper mixing.
Author
Phil Schatzmann
Template Parameters
TAudio sample data type (e.g., int16_t, int32_t, float)

Applies a normalized [0,1] mixing gain to a full-range sample T, used by OutputMixer and InputMixer. The default (float) multiply works for any T, including T=float. Specialized for T=int16_t/int24_t/int32_t under PREFER_FIXEDPOINT to go through q1_14_t::scale() (integer-only) instead of a per-sample float multiply – the same technique used for VolumeStream's fixed-point volume path.

Constructor & Destructor Documentation

◆ MixGain() [1/2]

template<typename T >
MixGain ( )
default

◆ MixGain() [2/2]

template<typename T >
MixGain ( float  f)
inline

Member Function Documentation

◆ scale()

template<typename T >
T scale ( sample) const
inline

Member Data Documentation

◆ value

template<typename T >
float value = 0

The documentation for this struct was generated from the following file: