I'm using javascript, and I'm trying to write a program that will, among other things, play an mp3 through either the left or the right speaker (it will vary), but not both. However, I want it to be absolutely silent on the other speaker.
In the past, I have found ways to play a sound loudly through one speaker, and although it should be silent in the other speaker, it always plays through (albeit quietly). I thought it was a fault of my computer, until I found a youtube Left Right speaker test, that correctly plays through just one speaker.
I have searched for hours, trying pannerNodes, splitter, but whatever I do, it never seems to work correctly (or at all).
Any help would be greatly appreciated!
Ideally, here's what I'd like to add on to:
sound1 = new Audio();
sound1.src = 'pizza.mp3';
sound1.play();
If code can be added on to make that play through only the left (or right) speaker, that would be awesome. And if that's possible, then read no further!
I have tried splitter code, which I got online, and it is supposed to be able to mute the left or the right, but it seems like it'll only play at the left volume (or, more accurately, whichever one I attach to the 0-spot of "splitter.connect(something,0);" Also, it plays through both speakers at the left volume. It may be because I'm using an oscillator as a test (wasn't sure how to attach an mp3 to it), or it could be I'm doing something drastically wrong. Like I said, hours of trying various things, and audio in javascript is not my strength.
Below is what I tried to use based off of something I read from: MarijnS95. However, like I said, I cannot get it to play an .mp3, and it'll always play at the left volume, but through both speakers. original post
tone = new AudioContext();
tone.createGain = tone.createGain||tone.createGainNode; //fallback for gain naming
gainL = tone.createGain();
gainR = tone.createGain();
splitter = this.tone.createChannelSplitter(2);
//Connect splitter' outputs to each Gain Nodes
splitter.connect(gainR, 1);
splitter.connect(gainL, 0);
//Connect to source
source = tone.createOscillator();
//Connect the source to the splitter
source.connect(splitter,0,0);
//Connect Left and Right Nodes to the output
//Assuming stereo as initial status
gainL.connect(tone.destination, 0);
gainR.connect(tone.destination, 0);
//Mute left channel and set the right gain to normal
gainL.gain.value = 1;
gainR.gain.value = 0;
source.start();
Hope that makes some sense.
Thanks in advance!