Skip to main content

Overview

AudioMixer uses the Web Audio API to combine multiple MediaStreams with audio tracks into a single mixed MediaStream. This is useful for scenarios like mixing multiple audio sources for recording or combining audio tracks for processing.

Constructor

Create a new AudioMixer instance.
The mixer starts in an uninitialized state. Add MediaStreams first, then call start() to begin mixing.

Methods

addMediaStream

Adds an audio MediaStream to be mixed. If the stream doesn’t contain audio tracks, a warning is logged but the stream is still added.
MediaStream
required
The MediaStream to be mixed. Should contain at least one audio track.
Example:
If a MediaStream without audio tracks is added, the mixer will log a warning: “Added MediaStream doesn’t contain audio tracks.”

start

Initializes the Web Audio graph and starts mixing all added MediaStreams. Returns the mixed output stream.
Returns: MediaStream containing the mixed audio, or null if no MediaStreams were added. How it works:
  1. Creates an AudioContext
  2. Creates a MediaStreamAudioDestinationNode for output
  3. Creates MediaStreamAudioSourceNode for each input stream
  4. Connects all source nodes to the destination node
  5. Returns the mixed MediaStream
Example:
If start() is called multiple times, it returns the existing mixed stream without recreating the audio graph.

reset

Disconnects all audio nodes and clears all references. Call this to clean up when done with the mixer.
This method:
  • Disconnects all MediaStreamAudioSourceNode instances
  • Clears the list of streams to mix
  • Cleans up the AudioContext and destination node
  • Resets the started state
Example:

Complete Example

Basic Audio Mixing

Mixing Remote Participant Audio

Browser Support

AudioMixer uses the Web Audio API, which is supported in all modern browsers:
  • Chrome/Edge 14+
  • Firefox 25+
  • Safari 6+
  • Opera 15+
AudioContext is automatically created with default sample rate. The mixer handles all audio graph setup internally.

Common Use Cases

  1. Recording multiple audio sources - Mix microphone + system audio for screen recordings
  2. Conference audio mixing - Combine all participant audio for processing
  3. Audio monitoring - Mix multiple inputs for real-time monitoring
  4. Custom audio processing - Pre-mix audio before applying effects

Important Notes

Always call reset() when done with the mixer to properly clean up AudioContext and disconnect nodes. This prevents memory leaks.
The mixer performs a simple mixing operation by connecting all sources to a single destination. Audio levels are not automatically adjusted - you may want to apply gain control for better balance.