有没有一种方法可以下载在特定页面上播放的所有音频流的音频文件?

时间:2020-04-18 18:01:12

标签: javascript html node.js html5-audio web-audio-api

我正在使用PizzicatoJS + HowlerJS构建应用程序。这些库实际上使我可以同时播放多个音频文件。想象一下4条音轨,每个音轨包含吉他,贝斯,鼓,人声等乐器。

使用PizzicatoJS的组功能或对我所有的l叫声音运行forEach循环并触发.play()时,一切都很好。但是,我想下载我从扬声器听到的最终声音。有办法解决这个问题吗?

我查看了OfflineAudioContext,但不确定如何使用它生成音频文件。看起来它需要一个<audio>标签之类的音频源。我想做的事可能吗?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我认为OfflineAudioContext可以帮助您解决用例。

假设您要创建一个长度为10秒的文件。它应该包含从开始到第二个8播放的声音。还有另一个应该在第二个5开始播放并持续到结束的声音。两种声音都已经是AudioBuffers(分别名为soundBufferanotherSoundBuffer)。

您可以按以下步骤安排和组合声音。

const sampleRate = 44100;
const offlineAudioContext = new OfflineAudioContext({
    length: sampleRate * 10,
    sampleRate
});

const soundSourceNode = new AudioBufferSourceNode({
    buffer: soundBuffer
});

soundSourceNode.start(0);
soundSourceNode.stop(8);
soundSourceNode.connect(offlineAudioContext.destination);

const anotherSoundSourceNode = new AudioBufferSourceNode({
    buffer: anotherSoundBuffer
});

anotherSoundSourceNode.start(5);
anotherSoundSourceNode.stop(10);
anotherSoundSourceNode.connect(offlineAudioContext.destination);

offlineAudioContext
    .startRendering()
    .then((audioBuffer) => {
        // save the resulting buffer as a file
    });

现在,您可以使用一个库将生成的AudioBuffer转换为编码的音频文件。例如,audiobuffer-to-wav这样的一个库。