npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@_mehrad/lamejs

v2.0.0

Published

MP3 (CBR) encoder in TypeScript, ported from LAME. ESM only, tree-shakeable, no dependencies.

Readme

lamejs

CBR MP3 encoder for 16-bit PCM, written in TypeScript. It started as a port of LAME (via jump3r and zhuker/lamejs).

  • ESM only, with no dependencies and no side effects at import time ("sideEffects": false).
  • Tree-shakeable. Importing only WavHeader costs about 0.5 kB, and the encoder is about 97 kB minified (31 kB gzip).
  • Load it lazily with await import(...). Nothing runs until you construct an encoder.
  • Its output is byte-for-byte identical to lamejs 1.2.1 for the same input (see Tests).

Install

npm install @_mehrad/lamejs

Use

import { Mp3Encoder } from '@_mehrad/lamejs';

const encoder = new Mp3Encoder(2, 44100, 128); // channels, sample rate, kbps
const chunks: Int8Array[] = [];
for (let i = 0; i < left.length; i += 1152)
  chunks.push(encoder.encodeBuffer(left.subarray(i, i + 1152), right.subarray(i, i + 1152)));
chunks.push(encoder.flush());

const mp3 = new Blob(chunks, { type: 'audio/mpeg' });

To load the encoder lazily, so it ends up in its own chunk and is fetched only when someone records:

const { Mp3Encoder } = await import('@_mehrad/lamejs');

Encoding is synchronous CPU work, at roughly 35× real time for 44.1 kHz stereo on a laptop. For long recordings, run it in a module worker:

// mp3.worker.ts
import { Mp3Encoder } from '@_mehrad/lamejs';
onmessage = ({ data: { left, right, sampleRate } }) => {
  const enc = new Mp3Encoder(right ? 2 : 1, sampleRate, 128);
  const out = [enc.encodeBuffer(left, right), enc.flush()];
  postMessage(new Blob(out, { type: 'audio/mpeg' }));
};

// app
const worker = new Worker(new URL('./mp3.worker.ts', import.meta.url), { type: 'module' });

API

new Mp3Encoder(channels: 1 | 2, sampleRate: number, kbps: number)

  • Sample rates other than the MPEG rates (8–48 kHz) are resampled.
  • The bitrate is rounded to the nearest legal value for the output rate.
  • Throws RangeError for invalid arguments.

encodeBuffer(left: Int16Array, right?: Int16Array): Int8Array

  • Any chunk size works. right is required for stereo.
  • Returns the MP3 bytes ready so far, which may be empty.
  • The returned array is a copy you can keep.

flush(): Int8Array

  • Encodes what is still buffered and returns the final bytes.

WavHeader.readHeader(view: DataView): WavHeader | undefined

  • Reads channels, sampleRate, dataOffset and dataLen from a PCM WAV file.

What it does not do

The encoder runs the same fixed configuration lamejs 1.x used: CBR, plain stereo or mono, LAME quality 3, no bit reservoir, no ID3 or Xing/VBR tag. The VBR/ABR, joint stereo, ID3, ReplayGain and decoder code was unreachable through that API and has been removed. If you need any of it, port it from the Java reference in src/main/java or from git history.

Migrating from 1.x

  • ESM only: require('@_mehrad/lamejs') and the lame.all.js / lame.min.js globals are gone. Use import.
  • The constructor arguments are required and validated (1.x logged a warning and fell back to mono 44.1 kHz).
  • Stereo encodeBuffer throws if right is missing or a different length. 1.x silently encoded garbage.
  • WavHeader.readHeader returns undefined when there is no data chunk instead of reading past the end. An unsupported fmt chunk throws an Error instead of a string.

Tests

npm test     # build + node --test
npm run bench
npm run size # gzip size of the bundled encoder

The tests compare SHA-1 hashes of the encoded output with hashes from the original lamejs 1.2.1 JavaScript. That covers 54 synthetic cases (mono/stereo, 8–96 kHz, 7–320 kbps, odd chunk sizes, resampling) and the real recordings in testdata/ at 64 and 128 kbps.

License

LGPL-3.0, as LAME.