https://github.com/audiojs/audio-encode
Any format audio encoders
https://github.com/audiojs/audio-encode
Last synced: about 1 month ago
JSON representation
Any format audio encoders
- Host: GitHub
- URL: https://github.com/audiojs/audio-encode
- Owner: audiojs
- License: mit
- Created: 2026-03-19T05:40:53.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2026-04-20T21:49:12.000Z (about 1 month ago)
- Last Synced: 2026-04-20T23:34:49.212Z (about 1 month ago)
- Language: JavaScript
- Size: 101 KB
- Stars: 6
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# audio-encode [](https://github.com/audiojs/audio-encode/actions/workflows/test.js.yml)
Encode raw audio samples to any format.
JS / WASM – no ffmpeg, no native bindings, works in both node and browser.
Small API, minimal size, near-native performance, stream encoding.
[](https://npmjs.org/package/encode-audio/)
```js
import encode from 'encode-audio';
const buf = await encode.wav(channelData, { sampleRate: 44100 });
```
#### Supported formats:
| Format | Package | Engine |
|--------|---------|--------|
| WAV | [@audio/encode-wav](https://npmjs.com/package/@audio/encode-wav) | JS |
| MP3 | [@audio/encode-mp3](https://npmjs.com/package/@audio/encode-mp3) | WASM |
| OGG Vorbis | [@audio/encode-ogg](https://npmjs.com/package/@audio/encode-ogg) | WASM |
| Opus | [@audio/encode-opus](https://npmjs.com/package/@audio/encode-opus) | WASM |
| FLAC | [@audio/encode-flac](https://npmjs.com/package/@audio/encode-flac) | WASM |
| AIFF | [@audio/encode-aiff](https://npmjs.com/package/@audio/encode-aiff) | JS |
### Whole-file encode
Specify the format as method name. Input is _Float32Array[]_ (one per channel), a single _Float32Array_ (mono), or an [AudioBuffer](https://npmjs.com/package/audio-buffer).
```js
import encode from 'encode-audio';
const wav = await encode.wav(channelData, { sampleRate: 44100 });
const aiff = await encode.aiff(channelData, { sampleRate: 44100 });
const mp3 = await encode.mp3(channelData, { sampleRate: 44100, bitrate: 128 });
const ogg = await encode.ogg(channelData, { sampleRate: 44100, quality: 5 });
const flac = await encode.flac(channelData, { sampleRate: 44100 });
const opus = await encode.opus(channelData, { sampleRate: 48000, bitrate: 96 });
```
### Chunked encoding
Call with just options (no data) to create a streaming encoder:
```js
import encode from 'encode-audio';
const enc = await encode.mp3({ sampleRate: 44100, bitrate: 128 });
const a = await enc(chunk1); // Uint8Array
const b = await enc(chunk2);
const c = await enc(null); // end of stream — flush + free
// explicit control: enc.flush(), enc.free()
```
### Streaming
Pass an async iterable as data — returns an async generator:
```js
import encode from 'encode-audio'
for await (let buf of encode.mp3(audioSource, { sampleRate: 44100, bitrate: 128 })) {
// buf is Uint8Array
}
```
Works with any async iterable source.
### Options
| Option | Description | Applies to |
|--------|-------------|------------|
| `sampleRate` | Output sample rate (required) | all |
| `bitrate` | Target bitrate in kbps | mp3, opus |
| `quality` | Quality 0–10 (VBR) | ogg, mp3 |
| `channels` | Output channel count | all |
| `bitDepth` | Bit depth: 16 or 32 (wav), 16 or 24 (aiff, flac) | wav, aiff, flac |
| `compression` | FLAC compression level 0–8 | flac |
| `application` | `'audio'`, `'voip'`, or `'lowdelay'` | opus |
### Metadata
Splice tags, pictures, markers and regions into encoded bytes. Available for `wav`, `mp3`, `flac`.
```js
import encode from 'encode-audio'
import { wav } from 'encode-audio/meta'
let bytes = await encode.wav(channelData, { sampleRate: 44100 })
let out = wav(bytes, {
meta: { title: 'Hare Krishna', artist: 'Prabhupada', year: '1966' },
markers: [{ sample: 44100, label: 'verse' }],
regions: [{ sample: 88200, length: 44100, label: 'chorus' }]
})
```
Each codec sub-package also exposes its writer directly:
```js
import { writeMeta } from '@audio/encode-mp3/meta'
let tagged = writeMeta(mp3Bytes, { meta: { title: 'foo' } })
```
## See also
* [audio-decode](https://github.com/audiojs/audio-decode) – decode any audio format to raw samples.
* [wasm-media-encoders](https://github.com/arseneyr/wasm-media-encoders) – compact WASM MP3 & Vorbis encoders.
* [AudioEncoder](https://developer.mozilla.org/en-US/docs/Web/API/AudioEncoder) – native WebCodecs encoder API.
## License
[MIT](LICENSE)