WEBRTC:当我们有音频流时,是否可以控制每个音轨的音量?

WEBRTC: Is it possible to control volume per audio track when we have audio streaming?

我的目标是什么?

控制每个音轨的音量,这样我就可以将背景声音调小,其他声音调大。

场景

  1. 我有一个上传音频文件的页面,上传完成后会在 DOM.
  2. 上创建一个音频播放器
  3. 音频源已连接到 audioContext.destination 和 mediaStreamDestination。
  4. 音频流传输正常。

我为每个音频播放器添加了一个音量范围,我正在尝试控制音量。

  volumeControlInput.addEventListener("change", (event) => {
    const audioSource = audioSources[index];
    const gainNode = audioContext.createGain();

    // Connect source to a gain node
    audioSource.connect(gainNode);
    // Connect gain node to destination
    gainNode.connect(audioContext.destination);
    gainNode.connect(mediaStreamDestination); //this doesn't seem necessary but I tried with and without it and it doesn't change anything.

    const volume = event.target.value;
    const fraction = parseInt(volume) / parseInt(event.target.max); // max is 100
    // Using an x*x curve (x-squared) since simple linear (x) does not
    // sound as good.
    // I also tried setValueAtTime and there's no difference for me.
    gainNode.gain.value = fraction * fraction;
}

我要解决的问题是什么?

音量只会在我拖动音量范围输入时增加,它永远不会降低音量。

对我来说奇怪的是,这个例子在这里工作得很好https://webaudioapi.com/samples/volume/ 来源:https://github.com/borismus/webaudioapi.com/blob/master/content/posts/volume/volume-sample.js

我想知道这是否根本不可能,有什么想法吗?

您似乎在事件侦听器中创建了一个新的 GainNode。这样你就可以继续为每个新卷添加一个连接,但你永远不会删除以前的连接。这就解释了为什么您的信号只会越来越大。

我建议在事件侦听器之外创建 GainNode 及其连接。然后事件侦听器将只处理在 gain AudioParam.

上设置 value