audio-decode is a JavaScript/WASM library designed for decoding various audio formats into raw PCM samples, suitable for both Node.js and browser environments. Currently at version 3.9.3, it maintains an active release cadence with frequent updates introducing new codec support, performance improvements, and API refinements. A key differentiator is its independence from external tools like FFmpeg, relying solely on JS and WASM implementations for a small footprint and near-native performance. It supports a wide array of formats including MP3, WAV, OGG Vorbis, FLAC, Opus, M4A/AAC, and more, offering both whole-file and chunked/streaming decoding capabilities. The library provides a unified API, and individual codec modules can be selectively loaded in browsers to optimize bundle size, making it highly versatile for diverse audio processing needs.
npm install audio-decodeVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to decode an audio file from a buffer and access its `channelData` and `sampleRate`.
Update import statements to ESM (`import decode from 'audio-decode'`) and adjust code to expect a plain object `{ channelData: Float32Array[], sampleRate: number }` instead of an `AudioBuffer`.Replace `decode.mp3(myBuffer)` with `decode(myBuffer)`. For chunked decoding, first get a decoder instance: `let dec = await decode.mp3(); await dec(chunk);`.
Change calls from `myDecoder.decode(chunk)` to `myDecoder(chunk)`.
Migrate streaming logic to use `for await (let chunk of decode.mp3(stream))` or obtain a chunked decoder with `let dec = await decode.mp3()` and feed it chunks.
When using selective loading in browsers via import maps, ensure the paths reflect the new naming convention (e.g., `@audio/decode-mp3`).
Ensure your project is configured for ESM. For Node.js, add `"type": "module"` to your `package.json` or save your file with a `.mjs` extension. If using an older Node.js environment, `audio-decode` v3.x is not compatible.
Update your code to destructure the result directly or access properties on the returned plain object: `const { channelData, sampleRate } = await decode(buf);`.Verify that the input `buffer` actually contains valid audio data for one of the supported formats. If you are certain of the format, consider using a format-specific decoder (e.g., `decode.mp3()`) for better error diagnostics, or ensure the necessary codec sub-package is available (e.g., in a browser import map).