Web音频5.1(6声道)输出

时间:2014-11-28 18:24:40

标签: javascript web-audio

我正在尝试通过增益控制将立体声音频通过channelsplitter到6个通道,然后再回到channelMerger,以控制5.1集的所有6个通道。该套装通过HDMI连接,并且窗口正确输出到所有6个通道(您可以让所有6个扬声器分别播放声音的屏幕)。

我能找到的唯一例子就是这段代码:

if (context.destination.maxChannelCount >= 6) {
    context.destination.channelCount = 6;
}
else {
    context.destination.channelCount = 2;
}

初始化audiocontext时,我的channelCount默认为2,maxChannelCount为6。

我使用以下代码在中间创建拆分器,合并和增益:

if (context.destination.maxChannelCount >= 6) {
    context.destination.channelCount = 6;
}
else {
    context.destination.channelCount = 2;
}

context.destination.channelCountMode = "explicit";
context.destination.channelInterpretation = "discrete";

var ammount = context.destination.channelCount;

console.log('Ammount of channels:',ammount); //this outputs 6

window.channelSplitter = context.createChannelSplitter(ammount);
window.channelMerger = context.createChannelMerger(ammount);

postGain.connect(channelSplitter); //postGain is the last node of the audio system
channelMerger.connect(context.destination);
window.channelGains = [];
for(i=0;i<ammount;i++){
    channelGains[i] = context.createGain();

    channelSplitter.connect(channelGains[i],i,0);
    channelGains[i].connect(channelMerger,0,i);
}

我在chrome(39.0.2171.71 m)中试过这个,其中maxChannelCount是6. Firefox输出2.

编辑: 在摆弄了channelSplitter之后,我发现除了前两个之外的所有输出都保持沉默。根据{{​​3}},使用channelinterpretation'speaker'时,这是正确的。 这意味着我需要自己填充频道,可能是使用spec所描述的算法。我仍然需要检查chrome是否正确输出所有6个频道。

1 个答案:

答案 0 :(得分:4)

问题是由于channelSplitter没有在所有通道上输出(我预期)。虽然根据规范这种行为是正确的。因此我们需要将自己的立体声写入5.1 upmixer。但是为了测试声音输出我使用左声道,我能够独立输出到所有6个声道。我只遇到一个问题。 connect方法的输入参数(第3个)不确定输出的顺序,如here所示。调用.connect()的顺序决定了输出的顺序。但是,第三个数字需要存在,并且与我调用.connect的其他时间不同(但在输入数量的范围内)。

另一件事我注意到合并和拆分器上的channelCount等于2.但是我发现它does not matter when having channelCountMode set to max

除了上述所有内容之外,我还摆弄了所有这些设置,但他们对此没有任何影响。唯一重要的是将目标节点上的channelCount设置为maxChannelCount。

但是,当未将所有通道连接到通道合并器时,合并会根据these规则对输入信号进行混合。将channelInterpretation设置为显式时,您仍然可以获得上混结果,而根据规范,它应该填充通道,并使每个通道都没有输入为空(因此只将第一个通道输出到左通道,并保持正确的输出静音)。

下面是摆放立体声频道的片段。

onload = function(){
  window.context = new AudioContext();
  window.source = context.createMediaElementSource(document.getElementById('player'));
  window.splitter = context.createChannelSplitter(2);
  source.connect(splitter);
  window.merger = context.createChannelMerger(2);

  window.leftGain = context.createGain();
  window.rightGain = context.createGain();

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

  
  //--------try (de)commenting things below-------
  leftGain.connect(merger, 0, 0);
  rightGain.connect(merger, 0, 1);
  //merger.channelInterpretation = 'discrete' //doesn't seem to have influcence..
  //merger.channelCountMode = 'explicit' //makes everything only output to the left channel
  
  //-------until here------
  
  merger.connect(context.destination);

  var leftRange = document.querySelector('#left');
  var rightRange = document.querySelector('#right');
  leftRange.oninput = function(){leftGain.gain.value = this.value}
  rightRange.oninput = function(){rightGain.gain.value = this.value}
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JS Bin</title>
  </head>
  <body>
    <audio src="http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg" controls autoplay id="player" preload></audio><br />
    <label for="left">Left channel</label>
    <input type="range" min=0 max=1 step=.01 value=1 id="left" /><br />
    <label for="right">Right channel</label>
    <input type="range" min=0 max=1 step=.01 value=1 id="right" />
  </body>
</html>