javascriptweb-audio-apiaudio-processingaudiobuffer

How to convert an AudioBuffer to a mp3 file?


Is there an easy way of doing that, or do I need to interleave the channels and create a DataView that contains a specific header format as well as the interleaved data?


Solution

  • For now, this is not possible on the web platform. We want to overhaul the decoding and encoding primitives so that they work both in real-time (like MediaRecorder, today) and offline (say, encode an OfflineAudioContext). We haven't even started standardizing this, we're working on finishing up the Web Audio API.

    However, thanks to emscripten, it's quite easy to do regardless, use a version of libmp3lame compiled to javascript of wasm, such as https://github.com/higuma/mp3-lame-encoder-js.

    I would recommend getting ArrayBuffers from your AudioBuffer, using getChannelData(channelIndex), transfer that to a web worker (without copying, so it's fast), and post the encoded blob back to the main thread when it's done.