# pcm-convert

> Convert PCM audio data between formats

Latest version **3.2.0** (published 2026-08-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install pcm-convert
pnpm add pcm-convert
yarn add pcm-convert
bun add pcm-convert
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2026-08-29 |
| First published | 2017-06-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 30.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 34 |
| Author | Dima Yv |
| Maintainers | dfcreative, dy |
| Keywords | audio, pcm, audiojs, dsp |

## Links

- npm: https://www.npmjs.com/package/pcm-convert
- Repository: https://github.com/audiojs/pcm-convert
- Homepage: https://github.com/audiojs/pcm-convert#readme
- Issues: https://github.com/audiojs/pcm-convert/issues
- Funding: https://github.com/sponsors/audiojs
- npm.io page: https://npm.io/package/pcm-convert

## Dependencies (1)

- [@audio/resample-polyphase](https://npm.io/package/@audio/resample-polyphase.md) ^1.0.0

## Recent versions

- 3.2.0 (latest) — 2026-08-29
- 3.1.1 — 2026-03-31
- 3.1.0 — 2026-03-26
- 3.0.2 — 2026-03-25
- 3.0.1 — 2026-03-23
- 3.0.0 — 2026-03-23
- 2.0.0 — 2026-03-16
- 1.6.5 — 2018-01-06
- 1.6.4 — 2017-09-25
- 1.6.3 — 2017-07-19
- 1.6.2 — 2017-07-19
- 1.6.1 — 2017-07-19
- 1.6.0 — 2017-07-01
- 1.5.3 — 2017-06-29
- 1.5.2 — 2017-06-29
- … 12 more at https://npm.io/package/pcm-convert/versions

## README

# pcm-convert [![stable](https://img.shields.io/badge/stability-stable-green.svg)](http://github.com/badges/stability-badges)

Convert PCM audio data between formats — dtype, layout, endianness, container, and sample rate (polyphase FIR anti-aliased resampling via [`@audio/resample-polyphase`](https://github.com/audiojs/resample)). ESM.

[![npm install pcm-convert](https://nodei.co/npm/pcm-convert.png?mini=true)](https://npmjs.org/package/pcm-convert/)

```js
import convert, { parse, detect, stringify, sampleRate } from 'pcm-convert'

// dtype conversion
convert(new Float32Array([1, -1, 0.5]), 'int16')

// sample-rate conversion (rates on both sides → resampled, anti-aliased)
convert(samples, 'float32 44100', 'float32 48000')
convert(samples, 'int16 stereo interleaved 48000', 'float32 16000')  // the 48k → 16k voice-agent path

// interleaved → planar, dtype change
convert(new Uint8Array([127, 200, 127, 200]), 'uint8 stereo interleaved', 'float32 planar')

// endianness swap
convert(new Float32Array([1, .5, -.5, -1]), 'le', 'be')

// planar channel arrays → interleaved (replaces pcm-encode)
convert([new Float32Array([1, 0, -1]), new Float32Array([0, 0.5, -0.5])], 'int16 interleaved')

// AudioBuffer input (auto-detected)
convert(audioBuffer, 'int16 interleaved')

// any format → AudioBuffer (browser-native; Node.js auto-discovers audio-buffer package if installed)
convert(new Int16Array([...]), { dtype: 'int16', channels: 2, interleaved: true, sampleRate: 44100 }, 'audiobuffer')

// write into existing buffer
convert(new Uint8Array([0, 255]), new Uint16Array(2))

// object format
convert(data, { dtype: 'float32', channels: 2, interleaved: false }, { dtype: 'int16', interleaved: true })

// format utilities
parse('float32 stereo planar 44100')  // → { dtype: 'float32', channels: 2, interleaved: false, sampleRate: 44100 }
stringify({ dtype: 'float32', channels: 2, interleaved: false })  // → 'float32 stereo planar'
detect(new Int16Array(4))  // → { dtype: 'int16' }
detect([new Float32Array(4), new Float32Array(4)])  // → { dtype: 'float32', channels: 2, interleaved: false }
```

## API

### `convert(src, from?, to?, dst?)`

Converts `src` from format `from` to format `to`. If `from` is omitted, it is detected from `src`. If `dst` is provided, result is written into it.

**Source types**: `Float32Array`, `Float64Array`, `Int8/16/32Array`, `Uint8/16/32Array`, `Array` (float -1..1), `Float32Array[]` (planar channels), `AudioBuffer` (Web Audio API), `ArrayBuffer`, `Buffer` (Node.js).

### Format

String (`'float32 stereo planar 44100'`) or object (`{ dtype, channels, interleaved, endianness, sampleRate, container }`). Tokens in any order; commas, semicolons, underscores accepted as separators.

| Field | Tokens / values |
|---|---|
| `dtype` | `float32`, `float64`, `float`, `int8`, `int16`, `int32`, `int`, `uint8`, `uint16`, `uint32`, `uint` |
| `channels` | `mono`, `stereo`, `quad`, `2.1`, `5.1`, `N-channel` — or any number in object format |
| `interleaved` | `interleaved`, `planar` — or boolean in object format |
| `endianness` | `le`, `be` |
| `sampleRate` | `8000` `11025` `16000` `22050` `44100` `48000` `88200` `96000` `176400` `192000` `352800` `384000` — or any number in object format |
| `container` | `array`, `arraybuffer`, `buffer` (Node.js), `audiobuffer` |

In string format, bare numbers are matched against the sample rate whitelist. Use `N-channel` for arbitrary channel counts.

When both `from.sampleRate` and `to.sampleRate` are present and differ, samples are resampled — rational polyphase FIR (Kaiser-windowed, ~85 dB stopband), output length `round(n · to/from)`. Equal or absent rates pass through untouched. `AudioBuffer` sources carry their own rate, so `convert(audioBuffer, 'audiobuffer 48000')` resamples and stamps the target rate.

Object aliases: `type` → `dtype`, `numberOfChannels` → `channels`, `rate` → `sampleRate`.

### `parse(fmt)` → descriptor  •  `detect(data)` → descriptor  •  `stringify(descriptor, omit?)` → string

### `sampleRate` — standard sample rates array

## Absorbs

- [audio-format](https://npmjs.org/package/audio-format) → `parse`, `detect`, `stringify`
- [sample-rate](https://npmjs.org/package/sample-rate) → `sampleRate`
- pcm-encode → `convert([...channels], format)`

## License

MIT

<p align=center><a href="https://github.com/krishnized/license/">ॐ</a></p>

---
_Source: https://npm.io/package/pcm-convert · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
