createWavHeader
Creates a standard 44-byte WAV (RIFF) header for raw PCM audio data.
function createWavHeader(
dataLength,
sampleRate,
numChannels,
bitsPerSample): ArrayBuffer;
Defined in: src/utils/audio.ts:252
Creates a standard 44-byte WAV (RIFF) header for raw PCM audio data.
Parameters
| Parameter | Type | Description |
|---|---|---|
dataLength | number | The length of the raw PCM data in bytes (excluding header). |
sampleRate | number | The audio sample rate in Hz (e.g., 16000, 44100). |
numChannels | number | The number of audio channels (1 for mono, 2 for stereo). |
bitsPerSample | number | The bit depth per sample (e.g., 16, 24). |
Returns
ArrayBuffer
A 44-byte ArrayBuffer containing the WAV header.
Remarks
The header conforms to the WAV file format specification with:
- RIFF chunk descriptor
fmtsub-chunk (PCM format, AudioFormat = 1)datasub-chunk header
This header can be prepended to raw PCM data to create a valid WAV file.
Example
const pcmData = new Int16Array(16000); // 1 second of 16-bit mono at 16 kHz
const header = createWavHeader(pcmData.byteLength, 16000, 1, 16);
// Concatenate header + data for a valid WAV file
See
createAudioBlob for a convenience function that combines header and data.