如何使用 WebAudio API 声道分离器调整音轨的左增益或右增益?

How do I use the WebAudio API channel splitter for adjusting the Left or Right gain on an audio track?

<body>
  <audio id="myAudio" src="audio/Squirrel Nut Zippers - Trou Macacq.mp3"></audio>
</body>
<script>
  var myAudio = document.getElementById('myAudio')
  var context = new AudioContext();
  var audioSource = context.createMediaElementSource(myAudio)
  var splitter = context.createChannelSplitter(2);
  audioSource.connect(splitter);
  var merger = context.createChannelMerger(2)

  //REDUCE VOLUME OF LEFT CHANNEL ONLY
  var gainNode = context.createGain();
  gainNode.gain.setValueAtTime(0.5, context.currentTime);
  splitter.connect(gainNode, 0);

  //CONNECT SPLITTER BACK TO SECOND INPUT OF THE MERGER
  gainNode.connect(merger, 0, 1);
  splitter.connect(merger, 1, 0);

  var destination = context.createMediaStreamDestination();

  merger.connect(destination)

  myAudio.play()

</script>

我试图让通过左声道播放的音频静音,以便右声道的音频是唯一通过两个扬声器播放的音频。我的理解是,这是通过拆分通道,调整一个通道的增益,然后将它们重新合并在一起来完成的,但是我不确定如何做到这一点,需要帮助

这是使用您的框架的粗略草图:

var splitter = context.createChannelSplitter(2);
var merger = context.createChannelMerger(2);
// connect the merger to the destination now.
merger.connect(context.destination);

// Gain node to control the volume of each channel.
var gainL = context.createGain();
var gainR = context.createGain();

audioSource.connect(splitter);

// audioSource is split into its separate channels.  For each channel connect
// a different gain node so we can control the volume independently.
splitter.connect(gainL, 0, 0);
splitter.connect(gainR, 1, 0);

// Combine the output of each gain node back into one stream using the merger.
gainL.connect(merger, 0, 0);
gainR.connect(merger, 0, 1);

// Set the gain values for the left and right channels as desired.
gainL.gain.value = 0;
gainR.gain.value = 1;