@echogarden/wave-codec

中文文档
0.4.0发布于 2026/07/23MITTypeScriptES Module零依赖近 30 天下载 2.1万

A fully-featured WAVE format encoder and decoder. Written in pure TypeScript.

原始使用文档
文档目录5 节
  1. Installation
  2. Basic usage
  3. Encoding and decoding to and from a raw data buffer
  4. Converting between 32-bit float channels and data buffers
  5. License

WAVE encoder and decoder

Fully-featured encoder and decoder for the standard WAVE format.

  • Written in pure TypeScript
  • Supports all sample formats and bit-depths included in the core WAVE standard, including GUID-based sub-formats for PCM and Float sample formats, and the 8-bit A-law and Mu-law codecs
  • Ignores unsupported or unknown sub-chunks like LIST, fact, plst and junk
  • Supports reading and writing WAVE buffers larger than 4 GiB, by interpreting a chunk of length 2^32 - 1 to extend up to the end of the buffer (even if it's much larger). This is compatible with FFMpeg's input and output approach for long or streaming WAVE data
  • Runs on Node.js, Deno, Bun, browsers
  • No dependencies

Installation

npm install @echogarden/wave-codec

Basic usage

Encodes and decodes raw audio data given as a channel array of 32-bit float samples:

import { encodeWaveFromFloat32Channels, decodeWaveToFloat32Channels } from '@echogarden/wave-codec'

// A stereo pair with a few silent samples
const float32Channels = [
    new Float32Array([0, 0, 0, 0]),
    new Float32Array([0, 0, 0, 0]),
]

const waveData = encodeWaveFromFloat32Channels(float32Channels, 44100)

const decodedChannels = decodeWaveToFloat32Channels(waveData)

Encoding and decoding to and from a raw data buffer

The data is given as a Uin8Array, and may contain any sample format supported by the WAVE standard, like 8-bit 16-bit, 24-bit, 32-bit integer raw integer PCM, 32-bit or 64-bit float, Alaw or Mulaw. Multiple channels should be interleaved.

import { encodeWaveFromDataBuffer, decodeWaveToDataBuffer, SampleFormat } from '@echogarden/wave-codec'

const audioBuffer = new Uint8Array([0, 0, 0, 0])

const waveData = encodeWaveFromBuffer(
    audioBuffer, // Audio data
    44100, // Sample rate
    1, // Channel count
    16, // Bit depth, can be 8, 16, 24, or 32 for PCM, 32 or 64 for Float, 8 for Alaw and Mulaw
    SampleFormat.PCM, // Sample format, can be PCM, Float, Alaw or Mulaw
    0 // Speaker position mask (used for multichannel audio)
)

const {	decodedAudioBuffer,
        sampleRate,
        channelCount,
        bitDepth,
        sampleFormat,
        speakerPositionMask } = decodeWaveToBuffer(waveData)

Converting between 32-bit float channels and data buffers

These methods are used internally to convert any supported sample format to and from 32-bit float channels, and are exposed as utility methods.

32-bit float channel array -> audio data buffer:

float32ChannelsToBuffer(
    audioChannels: Float32Array[],
    targetBitDepth: BitDepth = 16,
    targetSampleFormat: SampleFormat = SampleFormat.PCM): Uint8Array

Audio data buffer -> 32-bit float channel array:

bufferToFloat32Channels(
    audioBuffer: Uint8Array,
    channelCount: number,
    sourceBitDepth: BitDepth,
    sourceSampleFormat: SampleFormat): Float32Array[]

License

MIT

版本与发布记录

@echogarden/wave-codec 共发布 8 个版本,首次发布于 2024/11/28;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.4.0latest2026/07/233 个月前
0.3.32025/05/101.5 年前
0.3.22025/05/051.5 年前
0.3.12025/05/041.5 年前
0.3.02025/05/041.5 年前
0.2.12024/12/121.9 年前

关于 @echogarden/wave-codec 的常见问题

@echogarden/wave-codec 是什么?

A fully-featured WAVE format encoder and decoder. Written in pure TypeScript.

如何安装 @echogarden/wave-codec?

在项目目录运行 npm install @echogarden/wave-codec,或使用 pnpm add / yarn add / bun add。

@echogarden/wave-codec 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@echogarden/wave-codec 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@echogarden/wave-codec 可以商用吗?

@echogarden/wave-codec 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@echogarden/wave-codec 的下载量有多少?

根据 npm 官方下载统计,@echogarden/wave-codec 在 2026/10/13 – 2026/11/11 这 30 天内被下载 21,464 次,日均约 715 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@echogarden/wave-codec 还在维护吗?

最新版本 0.4.0 发布于 2026/07/23(3 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/11/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。