环绕声(5.1)Web Audio支持

时间:2015-04-15 14:02:38

标签: javascript html5 audio web

我正在尝试创建简单的应用程序,以便在所有5.1声道中播放立体声/单声道音乐。在研究了Web音频规范之后,我花了4个小时来尝试编写代码。不幸的是我无处可去。音频仅播放2个频道。如果我设置merger.channelCountMode =“explicit”,它只能从中心通道播放。

如果我设置merger.channelInterpretation =“discrete”;它只能从左声道播放。我做错了什么?非常感谢你。 我的代码:

      var audio;

function PlaySurround(){
  context = new AudioContext();
  audio = new Audio();
  audio.src = "a.mp3";
  var source = context.createMediaElementSource(audio);
  context.destination.channelCount = 6;
  audio.currentTime = Math.random() * 200;
  
  
  //Create a splitter to "separete" the stereo audio data to two channels.
  var splitter = context.createChannelSplitter(6);
  splitter.channelCount = 6;
  //splitter.channelInterpretation = "discrete";
  //splitter.channelCountMode = "explicit";
              
  console.log(splitter);
  //Connect your source to the splitter (usually, you will do it with the last audio node before context destination)
  source.connect(splitter);
  
  //Create two gain nodes (one for each side of the stereo image)
  var panLeft = context.createGain();
  var panRight = context.createGain();
  
  var panLeftSurround = context.createGain();
  var panRightSurround = context.createGain();
  
  var panCenter = context.createGain();
  var panSubwoofer = context.createGain(); 
   
  //Connect the splitter channels to the Gain Nodes that we've just created
  splitter.connect(panLeft, 0);
  splitter.connect(panRight, 1);
  
  splitter.connect(panCenter, 2);
  splitter.connect(panSubwoofer, 3);
  
  splitter.connect(panLeftSurround, 4);
  splitter.connect(panRightSurround, 5);
  
  panLeft.gain.value = 1;
  panRight.gain.value = 1;
  
  panLeftSurround.gain.value = 1;
  panRightSurround.gain.value = 1;
  
  panCenter.gain.value = 1;
  panSubwoofer.gain.value = 1;
  
 
  //Create a merger node, to get both signals back together
  var merger = context.createChannelMerger(6);
  merger.channelCount = 6;
  //merger.channelInterpretation = "discrete";
  merger.channelCountMode = "explicit";
  
  console.log(merger);
  
  panLeft.connect(merger, 0, 0);
  panRight.connect(merger, 0, 1);
   
  panCenter.connect(merger, 0, 2);
  panSubwoofer.connect(merger, 0, 3);
      
  panLeftSurround.connect(merger, 0, 4);
  panRightSurround.connect(merger, 0, 5); 
  
  //Connect the Merger Node to the final audio destination (your speakers)
  
  merger.connect(context.destination);
  
  source.mediaElement.play();
}

0 个答案:

没有答案