2

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?

Maxime Dupré
  • 5,319
  • 7
  • 38
  • 72

1 Answers1

3

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.

padenot
  • 1,515
  • 8
  • 11
  • I have tried that project on Github and it is very slow, especially on mobile. You would think that it's only natural that if you have a decode method on the audio context, there is an encode method. – mydoghasworms Aug 27 '20 at 06:46
  • I have seen good reports about lamejs. It is an MP3 encoder written in Javascript (so no wasm which for me has resulted in it aborting because of virtual memory issues): https://github.com/zhuker/lamejs – mydoghasworms Aug 27 '20 at 06:53