0

Lets say I have a source node that's connected to the destination node. Even if the audio is mono, I want to be able to control each ear's volume independently, like I can do when I have stereo audio with splitter and merger node.

Already tried to use splitter and merger nodes on the mono source node, but right channel comes out empty.

example for stereo:

var audioCtx = new AudioContext();
var source = audioCtx.createMediaElementSource(myAudio);
var gainNodeL = audioCtx.createGain();
var gainNodeR = audioCtx.createGain();
var splitter = audioCtx.createChannelSplitter(2);
var merger = audioCtx.createChannelMerger(2);

source.connect(splitter);
splitter.connect(gainNodeL, 0);
splitter.connect(gainNodeR, 1);

gainNodeL.connect(merger, 0, 0);
gainNodeR.connect(merger, 0, 1);

merger.connect(audioCtx.createMediaStreamDestination());

When I do this with mono audio, the right channel comes out empty.

2

1 Answer 1

4

If a signal is only mono (or in other words its channelCount is 1) the ChannelSplitterNode is not necessary. I modified the example a bit. It does now split the mono signal of an Oscillator.

var audioCtx = new AudioContext();
var oscillator = audioCtx.createOscillator();
var gainNodeL = audioCtx.createGain();
var gainNodeR = audioCtx.createGain();
var merger = audioCtx.createChannelMerger(2);

oscillator.connect(gainNodeL);
oscillator.connect(gainNodeR);

gainNodeL.connect(merger, 0, 0);
gainNodeR.connect(merger, 0, 1);

merger.connect(audioCtx.destination);

oscillator.start();

function left () {
    gainNodeL.gain.value = 1;
    gainNodeR.gain.value = 0;
}

function right () {
    gainNodeL.gain.value = 0;
    gainNodeR.gain.value = 1;
}

function center () {
    gainNodeL.gain.value = 1;
    gainNodeR.gain.value = 1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

It does work with oscillator node, but source node cant connect to 2 nodes, it throws an exception
Hi @AlonDayan, which error do you get? Replacing the oscillator with a MediaElementSourceNode works for me in Firefox and Chrome. In theory it should be okay to connect a node to as many other nodes as you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.