创建具有 4 个通道的 audioWorkletNode?

creating an audioWorkletNode with 4 channels?

我正在开发一个 mod 播放器,它是一个使用 webaudio/audioWorkletNode 具有 4 个不同音轨(通道)的音频文件。

我使用 2 声道(立体声)音频节点使其正常工作:

问题是我想分析并显示每个轨道的波形显示(因此应该有 4 个不同的分析器)。

我想创建一个将 outputChannelCount 设置为 [4] 的 audioWorkletNode,将分析器连接到节点的四个通道中的每一个,然后使用 channelMerger 将其混合成 2 个立体声通道。

所以我使用了下面的代码,期望它创建一个有 4 个通道的节点:

let node = new AudioWorkletNode(context, 'processor', { outputChannelCount: [4] });

但是outputChannelCount参数好像被忽略了。无论我指定什么,最后都设置为2个通道。

有没有其他方法,或者我必须自己处理分析,使用我自己的分析器?

我终于找到了一种混合所有四个通道并将每个通道传递给自己的分析器的方法:

this.context.audioWorklet.addModule(`js/${soundProcessor}`).then(() => 
{
    this.splitter = this.context.createChannelSplitter(4);
    // Use 4 inputs that will be used to send each track's data to a separate analyser
    // NOTE: what should we do if we support more channels (and different mod formats)?
    this.workletNode = new AudioWorkletNode(this.context, 'mod-processor', {
            outputChannelCount: [1, 1, 1, 1],
            numberOfInputs: 0,
            numberOfOutputs: 4
    });

    this.workletNode.port.onmessage = this.handleMessage.bind(this);
    this.postMessage({
        message: 'init',
        mixingRate: this.mixingRate
    });
    this.workletNode.port.start();

    // create four analysers and connect each worklet's input to one
    this.analysers = new Array();

    for (let i = 0; i < 4; ++i) {
        const analyser = this.context.createAnalyser();
        analyser.fftSize = 256;// Math.pow(2, 11);
        analyser.minDecibels = -90;
        analyser.maxDecibels = -10;
        analyser.smoothingTimeConstant = 0.65;
        this.workletNode.connect(analyser, i, 0);
        this.analysers.push(analyser);
    }

    this.merger = this.context.createChannelMerger(4);

    // merge the channel 0+3 in left channel, 1+2 in right channel
    this.workletNode.connect(this.merger, 0, 0);
    this.workletNode.connect(this.merger, 1, 1);
    this.workletNode.connect(this.merger, 2, 1);
    this.workletNode.connect(this.merger, 3, 0);
    this.merger.connect(this.context.destination);
});

我基本上创建了一个具有 4 个输出的新节点,并将输出用作通道。为了产生立体声输出,我可以使用通道合并。瞧!

可以在此处找到该应用程序的完整源代码:https://warpdesign.github.io/modplayer-js/