MediaStreamAudioDestinationNode 始终为立体声;忽略 AudioNodeOptions

MediaStreamAudioDestinationNode is always Stereo; AudioNodeOptions ignored

我正在尝试创建带有 MediaStreamAudioDestinationNode 的 1ch(单声道)MediaStreamTrack。按照标准,这应该是可以的。

const ctx = new AudioContext();
const destinationNode = new MediaStreamAudioDestinationNode(ctx, {
    channelCount: 1,
    channelCountMode: 'explicit',
    channelInterpretation: 'speakers',
});
await ctx.resume(); // doesn't make a difference

// this fails
expect(destinationNode.stream.getAudioTracks()[0].getSettings().channelCount).equal(1);

结果:

我做错了什么?


编辑: 显然 firefox 和 chrome 实际上都产生单声道,他们只是没有告诉你真相。这是 Typescript 的解决方案:

async function getNumChannelsInTrack(track: MediaStreamTrack): Promise<number> {
    // unfortunately, we can't use track.getSettings().channelCount, because
    // - Firefox 90 returns {} from getSettings() => see: https://bugzilla.mozilla.org/show_bug.cgi?id=1307808
    // - Chrome 92 always reports 2 channels, even if that's incorrect => see: https://bugs.chromium.org/p/chromium/issues/detail?id=1044645
    // Workaround: Record audio and look at the recorded buffer to determine the number of audio channels in the buffer.

    const stream = new MediaStream();
    stream.addTrack(track);
    const mediaRecorder = new MediaRecorder(stream);

    mediaRecorder.start();

    return new Promise<number>((resolve) => {
        setTimeout(() => {
            mediaRecorder.stop();
            mediaRecorder.ondataavailable = async ({ data }) => {
                const offlineAudioContext = new OfflineAudioContext({
                    length: 1,
                    sampleRate: 48000,
                });
                const audioBuffer = await offlineAudioContext.decodeAudioData(
                    await data.arrayBuffer()
                );
                resolve(audioBuffer.numberOfChannels);
            };
        }, 1000);
    });
}

我不认为你做错了什么。这是 Chrome 中的一个已知问题 (https://bugs.chromium.org/p/chromium/issues/detail?id=1044645),目前尚未解决。

我认为在 Firefox 中它甚至没有实现。此错误 (https://bugzilla.mozilla.org/show_bug.cgi?id=1307808) 表明 getSettings() 到目前为止,只有 returns 那些可以更改的值。

我认为如果您 star/follow 这些问题或对它们发表评论以确保它们不会被遗忘,将会很有帮助。