Randy
Randy

Reputation: 495

Play an Oscillation In a Single Channel

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?

Upvotes: 4

Views: 1949

Answers (1)

cwilso
cwilso

Reputation: 13908

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 );

Upvotes: 3

Related Questions