4

I have been trying to get an oscillator sound to play in only one channel and I have not been able to get it to work.

I tried to use a panner node to set the position of the sound, but it still plays in both the channels, just not as loud in the more distant channel.

My latest attempt tried to use a channel merger but it still plays in both channels:

var audioContext = new webkitAudioContext();
var merger = audioContext.createChannelMerger();
merger.connect(audioContext.destination);

var osc = audioContext.createOscillator();
osc.frequency.value = 500;
osc.connect( merger, 0, 1 );
osc.start( audioContext.currentTime );
osc.stop( audioContext.currentTime + 2 );

How do you create an oscillator that only plays in a single channel?

Randy
  • 445
  • 5
  • 9

1 Answers1

3

Hmm, weird - appears to be a bug when an input of the Merger is unconnected. To work around, just connect a zeroed-out gain node to the other input, like so:

var audioContext = new webkitAudioContext();
var merger = audioContext.createChannelMerger(2);
merger.connect(audioContext.destination);

var osc = audioContext.createOscillator();
osc.frequency.value = 500;
osc.connect( merger, 0, 0 );

// workaround here:
var gain= audioContext.createGainNode();
gain.gain.value = 0.0;
osc.connect( gain );
gain.connect( merger, 0, 1 );
// end workaround

osc.start( audioContext.currentTime );
osc.stop( audioContext.currentTime + 2 );
cwilso
  • 13,610
  • 1
  • 30
  • 35
  • Would this be something to post as a bug to the chromium or webkit project? – Randy Jan 23 '13 at 00:45
  • Also, I am testing the idea of adding a zero-ed out gain node and I am still hearing a faint sound in the other channel...? Is there something else that would be causing it to carry over? – Randy Jan 23 '13 at 00:56
  • Chromium. And no, I don't know why it would carry over, other than crosstalk in the digital-to-analog converter circuitry or the analog jack/cable. Loud sine waves can do that, though. (60-cycle hum, anyone?) – cwilso Jan 24 '13 at 22:32
  • Hi, I'm just curious, what exactly are the extra arguments passed to connect()? Having trouble finding documentation on that. – Ben Davis Aug 22 '13 at 04:22
  • the extra arguments on connect() stand for channels indexes. see first paragraph here https://developer.mozilla.org/en-US/docs/Web/API/ChannelMergerNode#Example – gooleem Sep 20 '19 at 12:52