Class

AudioMerger

AudioMerger(optionsopt)

AudioMerger - Combines multiple audio streams into a single output

This class provides functionality to merge multiple audio streams (e.g., from different participants or sources) into a single audio track. Each stream can have its volume adjusted independently. This is useful for scenarios like mixing local audio with screen share audio or combining multiple participant audio streams.

Constructor

# new AudioMerger(optionsopt)

Parameters:
Name Type Attributes Description
options Object <optional>

Configuration options

audioContext AudioContext <optional>

An existing AudioContext to use. If not provided, a new AudioContext will be created.

Example
// Create the merger
const merger = new AudioMerger();

// Initialize with the local video stream
merger.initializeWithVideoStream(localVideoStream);

// Add additional audio streams
await merger.addStream(screenShareAudioStream);

// Adjust volume of a stream (0-1 range)
merger.adjustVolume(screenShareAudioStream, 0.5);

// Get the merged track for the producer
const mergedTrack = merger.getTrackForProducer({ audioGainControl });

// Clean up when done
merger.dispose();

Methods

# async addStream(stream) → {Promise.<void>}

Adds an audio stream to the merger

The stream's audio will be mixed with all other streams in the merger.

Parameters:
Name Type Description
stream MediaStream

The audio stream to add (must contain at least one audio track)

If the stream is invalid or has no audio tracks

Error
Promise.<void>
Example
// Add screen share audio
const screenStream = await navigator.mediaDevices.getDisplayMedia({ audio: true });
await merger.addStream(screenStream);

# adjustVolume(stream, volume)

Adjusts the volume of a specific stream in the merger

Parameters:
Name Type Description
stream MediaStream | string

The stream to adjust, or 'initial' for the initial stream

volume number

The volume level (0.0 = muted, 1.0 = full volume)

If volume is not a number between 0 and 1

Error

If the stream is not found in the merger

Error
Example
// Lower the screen share audio volume
merger.adjustVolume(screenShareStream, 0.3);

// Mute the initial stream
merger.adjustVolume('initial', 0);

# dispose()

Disposes of the merger and releases all resources

This stops all MediaStreamTracks, disconnects all audio nodes, and clears all references. Call this when you're done using the merger to prevent memory leaks and release hardware resources (microphone, etc.).

Example
// Clean up when leaving a room
merger.dispose();

# getTrackForProducer(options) → {MediaStreamTrack}

Gets the merged audio track for use with a producer

Optionally connects an audio gain control node to the output chain.

Parameters:
Name Type Attributes Description
options Object

Options for getting the track

audioGainControl GainNode <optional>

An optional GainNode to connect to the output for volume control

The merged audio track

MediaStreamTrack
Example
const gainNode = audioContext.createGain();
const mergedTrack = merger.getTrackForProducer({ audioGainControl: gainNode });
await producer.replaceTrack({ track: mergedTrack });

# initializeWithVideoStream(videoStream)

Initializes the merger with the primary video stream

This should be called first to set up the initial audio source, typically the local microphone audio from a video stream.

Parameters:
Name Type Description
videoStream MediaStream

The video stream containing the audio track to use as the initial source

If videoStream is not provided

Error

If the video stream does not contain an audio track

Error
Example
const localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
merger.initializeWithVideoStream(localStream);

# removeStream(stream)

Removes an audio stream from the merger

The stream will be disconnected and its audio will no longer be part of the merged output.

Parameters:
Name Type Description
stream MediaStream

The stream to remove (cannot be 'initial')

If attempting to remove the initial stream

Error

If the stream is not found in the merger

Error
Example
merger.removeStream(screenShareAudioStream);