arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
OutputMixer< T > Class Template Reference

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

#include <AudioOutput.h>

Inheritance diagram for OutputMixer< T >:
Print

Public Member Functions

 OutputMixer (Print &finalOutput, int outputStreamCount)
 
int available (int idx)
 Provides the available bytes in the buffer.
 
int availableForWrite () override
 Provides the bytes available to write for the current stream buffer.
 
int availableForWrite (int idx)
 Provides the bytes available to write for the indicated stream index.
 
int availablePercent (int idx)
 Provides the % fill level of the buffer for the indicated index.
 
int availableSamples ()
 Returns the minimum number of samples available across all buffers.
 
bool begin (int copy_buffer_size_bytes=DEFAULT_BUFFER_SIZE)
 Starts the processing.
 
void end ()
 Remove all input streams.
 
virtual void flush ()
 
void flushMixer ()
 Force output to final destination.
 
BaseBuffer< T > * getBuffer (int idx)
 Provides the write buffer for the indicated index.
 
void next ()
 Moves to the next mixing index.
 
void resize (int size)
 Resizes the buffer to the indicated number of bytes.
 
void setAutoIndex (bool flag)
 Automatically increment mixing index after each write.
 
void setCreateBufferCallback (BaseBuffer< T > *(*cb)(int size))
 Define callback to allocate custum buffer types.
 
void setIndex (int idx)
 Sets the Output Stream index.
 
void setOutput (Print &finalOutput)
 Sets the final output destination for mixed audio.
 
void setOutputCount (int count)
 Sets the number of input streams to mix.
 
void setWeight (int channel, float weight)
 
int size ()
 Number of stremams to which are mixed together.
 
size_t write (const uint8_t *data, size_t len) override
 
size_t write (int idx, const uint8_t *buffer_c, size_t bytes)
 Write the data for an individual stream idx which will be mixed together.
 
size_t write (uint8_t) override
 Single byte write - not supported, returns 0.
 
size_t writeSilence (int idx, size_t bytes)
 Writes silence to the specified stream buffer.
 
size_t writeSilence (size_t bytes)
 Writes silence to the current stream buffer.
 

Protected Member Functions

void allocate_buffers (int size)
 Allocates ring buffers for all input streams.
 
void free_buffers ()
 Releases memory for all ring buffers.
 
void update_total_weights ()
 Recalculates the total weights for normalization.
 

Static Protected Member Functions

static BaseBuffer< T > * create_buffer (int size)
 Creates a default ring buffer of the specified size.
 

Protected Attributes

int _timeout = 10
 
Vector< BaseBuffer< T > * > buffers {0}
 
BaseBuffer< T > *(* create_buffer_cb )(int size) = create_buffer
 
bool is_active = false
 
bool is_auto_index = true
 
Vector< T > output {0}
 
int output_count = 0
 
Printp_final_output = nullptr
 
void * p_memory = nullptr
 
int size_bytes = 0
 
int stream_idx = 0
 
float total_weights = 0.0
 
Vector< float > weights {0}
 

Detailed Description

template<typename T>
class audio_tools::OutputMixer< 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
Mixing of multiple audio input streams into a single output stream.
Definition AudioOutput.h:366

Manual Index Mode:

OutputMixer<int16_t> mixer(Serial, 3);
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)

Member Function Documentation

◆ availableForWrite()

template<typename T >
int availableForWrite ( )
inlineoverridevirtual

Provides the bytes available to write for the current stream buffer.

Reimplemented from Print.

◆ flush()

virtual void flush ( )
inlinevirtualinherited

◆ setWeight()

template<typename T >
void setWeight ( int  channel,
float  weight 
)
inline

Defines a new weight for the indicated channel: If you set it to 0.0 it is muted. The initial value is 1.0

◆ write()

template<typename T >
size_t write ( const uint8_t *  data,
size_t  len 
)
inlineoverridevirtual

Write the data from a simgle stream which will be mixed together (the stream idx is increased)

Reimplemented from Print.


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