简体   繁体   English

Web Audio API将单声道拆分为立体声

[英]Web Audio API split mono to stereo

Lets say I have a source node that's connected to the destination node. 可以说我有一个连接到目标节点的源节点。 Even if the audio is mono, I want to be able to control each ear's volume independently, like I can do when I have stereo audio with splitter and merger node. 即使音频是单声道,我也希望能够独立控制每只耳朵的音量,就像在使用带有分离器和合并节点的立体声音频时所做的那样。

Already tried to use splitter and merger nodes on the mono source node, but right channel comes out empty. 已经尝试在单源节点上使用拆分器和合并节点,但是正确的通道显示为空。

example for stereo: 立体声示例:

 var audioCtx = new AudioContext(); var source = audioCtx.createMediaElementSource(myAudio); var gainNodeL = audioCtx.createGain(); var gainNodeR = audioCtx.createGain(); var splitter = audioCtx.createChannelSplitter(2); var merger = audioCtx.createChannelMerger(2); source.connect(splitter); splitter.connect(gainNodeL, 0); splitter.connect(gainNodeR, 1); gainNodeL.connect(merger, 0, 0); gainNodeR.connect(merger, 0, 1); merger.connect(audioCtx.createMediaStreamDestination()); 

When I do this with mono audio, the right channel comes out empty. 当我用单声道音频执行此操作时,右声道会空出。

If a signal is only mono (or in other words its channelCount is 1) the ChannelSplitterNode is not necessary. 如果信号仅是单声道的(换句话说,其channelCount为1),则不需要ChannelSplitterNode。 I modified the example a bit. 我对示例进行了一些修改。 It does now split the mono signal of an Oscillator. 现在,它确实分离了振荡器的单声道信号。

var audioCtx = new AudioContext();
var oscillator = audioCtx.createOscillator();
var gainNodeL = audioCtx.createGain();
var gainNodeR = audioCtx.createGain();
var merger = audioCtx.createChannelMerger(2);

oscillator.connect(gainNodeL);
oscillator.connect(gainNodeR);

gainNodeL.connect(merger, 0, 0);
gainNodeR.connect(merger, 0, 1);

merger.connect(audioCtx.destination);

oscillator.start();

function left () {
    gainNodeL.gain.value = 1;
    gainNodeR.gain.value = 0;
}

function right () {
    gainNodeL.gain.value = 0;
    gainNodeR.gain.value = 1;
}

function center () {
    gainNodeL.gain.value = 1;
    gainNodeR.gain.value = 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM