GizStack
View .mdOpen in ChatGPTOpen in Claude

gizlang/wav

WAV files (RIFF WAVE), read and written. Every program that reads or writes one takes it from here.

gizlang
import * as Wav from "gizlang/wav";

Types

Sound

gizlang
type Sound = { rate: i64; channels: i64; samples: Vec<f32>; }

Sound: channels interleaved samples in [-1, 1] a frame, rate frames a second.

  • rate: i64
  • channels: i64
  • samples: Vec<f32>

Functions

decode

gizlang
function decode(data: bytes): Sound throws string

The sound of a WAV file (PCM of 8, 16, 24 or 32 bits, or IEEE floats of 32 or 64 bits, also in WAVE_FORMAT_EXTENSIBLE), as floats in [-1, 1) the way libsndfile reads them (a signed integer over 2^(bits - 1); 8 bits are unsigned around 128). Throws why DATA is not one.

Effects: fail, alloc.

encode

gizlang
function encode(samples: Vec<f32>, rate: i64, channels: i64 = 1): bytes

A WAV file of 16-bit PCM of SAMPLES (CHANNELS interleaved) at RATE frames a second, as libsndfile writes floats: x 32767 rounded, clipped to [-1, 1]. The form a browser's decodeAudioData and every player read.

Effects: fail, alloc.