两个单声道音频流,一个在右扬声器上播放,另一个在左扬声器上并行播放

时间:2018-10-24 15:34:54

标签: javascript audio stream

我有两个要并行播放的单声道音频流(mp3文件),right.mp3仅应在右声道(扬声器)听到,left.mp3仅在左声道(扬声器)听到。我怎样才能做到这一点? 我已经尝试过合并,请参见下文。

var contextL = new AudioContext() || new webkitAudioContext(),
    requestL = new XMLHttpRequest();
    requestL.open("GET", "./left.mp3", true);
    requestL.responseType = "arraybuffer";
    requestL.onload = function(){
        contextL.decodeAudioData(requestL.response, onDecodedL);
    }

var contextR = new AudioContext() || new webkitAudioContext(),
    requestR = new XMLHttpRequest();

    requestR.open("GET", "./right.mp3", true);
    requestR.responseType = "arraybuffer";
    requestR.onload = function(){
        contextR.decodeAudioData(requestR.response, onDecodedR);
    }


function onDecodedL(buffer){
    var bufferSourceL = contextL.createBufferSource();
        bufferSourceL.buffer = buffer;
    var splitterL   = contextL.createChannelSplitter(2);
        bufferSourceL.connect(splitterL);
    var mergerL     = contextL.createChannelMerger(2);
    var gainL       = contextL.createGain();
        gainL.value = 0;
        splitterL.connect(gainL, 0);
        bufferSourceL.start();
}   

function onDecodedR(buffer){
    var bufferSourceR = contextR.createBufferSource();
        bufferSourceR.buffer = buffer;  
    var splitterR = contextR.createChannelSplitter(2);
        bufferSourceR.connect(splitterR);
    var mergerR = contextR.createChannelMerger(2);
    var gainR = contextR.createGain();
        gainR.value = 0;
        splitterR.connect(gainR,1);
        bufferSourceR.start();
} 

0 个答案:

没有答案