lamejs is a pure JavaScript MP3 encoder library, initially a rewrite of jump3r-code which itself was based on libmp3lame. It enables client-side and server-side (Node.js) encoding of raw PCM audio data (specifically Int16Array samples) into MP3 format. The project highlights its performance, claiming to be significantly faster than real-time on various machines and environments (browser and Node.js). The current stable version is 1.2.1, released after a considerable hiatus, primarily to address a TypeScript compatibility issue. Its release cadence is slow, suggesting a maintenance rather than actively developed status. Key differentiators include its pure JavaScript implementation, making it suitable for browser-based audio processing without WASM, and its reported high encoding speed. It's particularly useful for scenarios requiring on-the-fly MP3 generation from Web Audio API outputs or other PCM sources.
npm install lamejsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the Mp3Encoder, feed it audio samples in chunks, and then flush the buffer to finalize the MP3 data. It covers both mono encoding and the necessary steps to collect the encoded segments.
Ensure all audio input data is converted to `Int16Array` format before passing it to `encodeBuffer`. For Web Audio API `AudioBuffer`s, multiply by `32767` and `Math.min(1, Math.max(-1, value))` to clamp, then cast to `Int16Array`.
For TypeScript, use `import * as lamejs from 'lamejs';` or `import { Mp3Encoder } from 'lamejs';` for proper type inference and module resolution. Update `tsconfig.json` if needed to ensure correct `moduleResolution` (e.g., `NodeNext`).Always call `mp3encoder.flush()` once all `encodeBuffer()` calls are complete and append the returned `Int8Array` to your collected MP3 data.
When segmenting your audio samples, try to use a `sampleBlockSize` of 1152 for stereo (2*576) or 576 for mono, or at least a multiple of 576 where possible.
For CommonJS, use `const lamejs = require('lamejs');`. For ESM/TypeScript, use `import * as lamejs from 'lamejs';` or `import { Mp3Encoder } from 'lamejs';`.Convert `Float32Array` samples to `Int16Array` before passing them to `encodeBuffer`. Example: `const int16Samples = new Int16Array(float32Samples.length); for (let i = 0; i < float32Samples.length; i++) { int16Samples[i] = Math.max(-1, Math.min(1, float32Samples[i])) * 0x7FFF; }`Ensure `mp3encoder.flush()` is called after all `encodeBuffer` calls, and its `Int8Array` result is included in the final MP3 data array.
No dependency data recorded yet.