如何将双声道音频转换为具有控制每个声道音量的功能的单声道?

时间:2019-06-05 13:27:55

标签: javascript web-audio-api wavesurfer.js

我正在尝试在两个耳机中收听2声道音频(左右耳机中的音频相同)。

当前情况:

滑条居中-效果很好(两个耳机都有两个通道)。

滑条在右侧-效果很好(两个耳机的右声道)。

滑块在左侧-不起作用(仅左侧耳机中的左声道)。

const splitter = wavesurfer.backend.ac.createChannelSplitter(2);
const merger = wavesurfer.backend.ac.createChannelMerger(2);
const leftGain = wavesurfer.backend.ac.createGain();
const rightGain = wavesurfer.backend.ac.createGain();
const panner = wavesurfer.backend.ac.createPanner();

splitter.connect(leftGain, 0);
splitter.connect(rightGain, 1);

leftGain.connect(merger, 0, 0);
rightGain.connect(merger, 0, 1);

merger.connect(panner);

let slider = document.querySelector('#Slider');
$(slider ).change(function () {
    rightGain.gain.value = Number(slider.value);
    leftGain.gain.value = 1- (Number(slider.value));
})

wavesurfer.backend.setFilters([splitter, leftGain, rightGain, merger]);

当滑块在左侧时,我只想听两副耳机的左声道。 有人可以帮我吗?

Problem

1 个答案:

答案 0 :(得分:0)

调用setFilters()时,waversurfer.js将以一条简单的链连接所有提供的节点。就您而言,这意味着它将创建其他连接,如下所示:

splitter.connect(leftGain);
leftGain.connect(rightGain);
rightGain.connect(merger);

这可能不是您想要的。但是可以利用这种行为。我对您的示例做了一些修改。

const input = wavesurfer.backend.ac.createGain();
const splitter = wavesurfer.backend.ac.createChannelSplitter(2);
const merger = wavesurfer.backend.ac.createChannelMerger(2);
const leftGain = wavesurfer.backend.ac.createGain();
const rightGain = wavesurfer.backend.ac.createGain();

// This will make sure that a mono signal gets upmixed to stereo.
// If you always have stereo sound you can remove it.
input.channelCountMode = 'explicit';

// It is only necessary to connect the right channel
// because this is the one which needs optional parameters.
splitter.connect(rightGain, 1);
rightGain.connect(merger);
rightGain.connect(merger, 0, 1);

// Only the one connection which needs an optional parameter
// needs to be done for the left channel
leftGain.connect(merger, 0, 1);

// wavesufer.js will connect everything else.
wavesurfer.backend.setFilters([ input, splitter, leftGain, merger ]);

我还添加了另一个GainNode作为第一个节点,以确保在单声道的情况下将信号上混为立体声。我删除了PannerNode,因为它在您的示例中没有使用。