Package Exports
- @echogarden/wave-codec
- @echogarden/wave-codec/dist/WaveCodec.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@echogarden/wave-codec) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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,plstandjunk - 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-codecBasic 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): Uint8ArrayAudio data buffer -> 32-bit float channel array:
bufferToFloat32Channels(
audioBuffer: Uint8Array,
channelCount: number,
sourceBitDepth: BitDepth,
sourceSampleFormat: SampleFormat): Float32Array[]License
MIT