【问题标题】:WEBRTC: Is it possible to control volume per audio track when we have audio streaming?WEBRTC:当我们有音频流时,是否可以控制每个音轨的音量?
【发布时间】:2020-10-25 21:26:50
【问题描述】:

我的目标是什么?

控制每个音轨的音量,这样我就可以在较低的音量下播放背景声音,在较高的音量下播放其他声音。

场景

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

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

  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

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

【问题讨论】:

    标签: javascript webrtc web-audio-api


    【解决方案1】:

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

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

    【讨论】:

    • 有道理,我现在就试试看!谢谢!
    • 我试过了,它改善了一点,谢谢!它现在减小音量并增加,但它永远不会达到 0 音量,比如说,当它达到零时,它似乎减少了 50% 的音量。如果您有任何线索,请告诉我
    • 我找到了一个解决方案,虽然有点奇怪,但它正在工作,我正在让我的输入范围从 -100 到 100。这样 -1 是静音,1 是 100%体积。再次感谢您带领我走上这条路!
    • 您是否仍然有绕过GainNode 的直接连接?当您将gain 设置为 -1 时,您实际上会翻转波形,但不会更改其幅度。如果然后将翻转的信号与未修改的信号相乘,您最终将没有声音,因为它们相互抵消。这可能是您需要将范围扩大到 -1 的原因。
    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 2014-02-25
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    相关资源
    最近更新 更多