|
| 1 | +# audio-encode [](https://github.com/audiojs/audio-encode/actions/workflows/test.js.yml) |
| 2 | + |
| 3 | +Encode raw audio samples to any format.<br> |
| 4 | +JS / WASM – no ffmpeg, no native bindings, works in both node and browser.<br> |
| 5 | +Small API, minimal size, near-native performance, stream encoding. |
| 6 | + |
| 7 | +[](https://npmjs.org/package/audio-encode/) |
| 8 | + |
| 9 | +```js |
| 10 | +import encode from 'audio-encode'; |
| 11 | + |
| 12 | +const buf = await encode.wav(channelData, { sampleRate: 44100 }); |
| 13 | +``` |
| 14 | + |
| 15 | +#### Supported formats: |
| 16 | + |
| 17 | +| Format | Package | Engine | |
| 18 | +|--------|---------|--------| |
| 19 | +| WAV | built-in | JS | |
| 20 | +| MP3 | [wasm-media-encoders](https://github.com/nicodemus26/wasm-media-encoders) | WASM | |
| 21 | +| OGG Vorbis | [wasm-media-encoders](https://github.com/nicodemus26/wasm-media-encoders) | WASM | |
| 22 | +| Opus | TBD | WASM | |
| 23 | +| FLAC | [libflac.js](https://github.com/nicodemus26/libflac.js) | WASM | |
| 24 | +| AAC / M4A | TBD | WASM | |
| 25 | +| AIFF | built-in | JS | |
| 26 | +| WebM | TBD | WASM | |
| 27 | + |
| 28 | +### Whole-file encode |
| 29 | + |
| 30 | +Specify the format as method name. Input is _Float32Array[]_ (one per channel) or a single _Float32Array_ (mono). |
| 31 | + |
| 32 | +```js |
| 33 | +import encode from 'audio-encode'; |
| 34 | + |
| 35 | +const wav = await encode.wav(channelData, { sampleRate: 44100 }); |
| 36 | +const mp3 = await encode.mp3(channelData, { sampleRate: 44100, bitrate: 128 }); |
| 37 | +const ogg = await encode.ogg(channelData, { sampleRate: 44100, quality: 5 }); |
| 38 | +``` |
| 39 | + |
| 40 | +### Stream encoding |
| 41 | + |
| 42 | +For chunk-by-chunk encoding, use `.stream()`: |
| 43 | + |
| 44 | +```js |
| 45 | +import encode from 'audio-encode'; |
| 46 | + |
| 47 | +const encoder = await encode.mp3.stream({ sampleRate: 44100, bitrate: 128 }); |
| 48 | + |
| 49 | +const a = await encoder.encode(chunk1); // Uint8Array |
| 50 | +const b = await encoder.encode(chunk2); |
| 51 | +const c = await encoder.encode(); // end of stream — flush + free |
| 52 | + |
| 53 | +// explicit methods |
| 54 | +// encoder.flush(), encoder.free() |
| 55 | +``` |
| 56 | + |
| 57 | +### Options |
| 58 | + |
| 59 | +| Option | Description | Applies to | |
| 60 | +|--------|-------------|------------| |
| 61 | +| `sampleRate` | Output sample rate (required) | all | |
| 62 | +| `bitrate` | Target bitrate in kbps | mp3, aac, opus | |
| 63 | +| `quality` | Quality 0–10 (VBR) | ogg, mp3 | |
| 64 | +| `channels` | Output channel count | all | |
| 65 | + |
| 66 | +### Custom encoders |
| 67 | + |
| 68 | +The `encode` registry is extensible: |
| 69 | + |
| 70 | +```js |
| 71 | +import encode from 'audio-encode'; |
| 72 | +encode.myformat = Object.assign( |
| 73 | + async (data, opts) => { /* ... */ }, |
| 74 | + { stream: async (opts) => ({ encode: chunk => ..., free() {} }) } |
| 75 | +); |
| 76 | +``` |
| 77 | + |
| 78 | +## See also |
| 79 | + |
| 80 | +* [audio-decode](https://github.com/audiojs/audio-decode) – decode any audio format to raw samples. |
| 81 | +* [wasm-media-encoders](https://github.com/nicodemus26/wasm-media-encoders) – compact WASM MP3 & Vorbis encoders. |
| 82 | +* [AudioEncoder](https://developer.mozilla.org/en-US/docs/Web/API/AudioEncoder) – native WebCodecs encoder API. |
| 83 | +* [ffmpeg.wasm](https://github.com/ffmpegwasm/ffmpeg.wasm) – full encoding/decoding library. |
| 84 | + |
| 85 | +## License |
| 86 | + |
| 87 | +[MIT](LICENSE) |
| 88 | + |
| 89 | +<a href="https://github.com/krishnized/license/">ॐ</a> |
0 commit comments