wav
import wav from "wav";
import fs from "fs";
import io from "io";
var samples: [480000]i16; // 5 s of 48 kHz stereo
fn main() -> i32 {
let data: string = fs.read_file("ding.wav") or { return 1; };
let w: wav.Info = wav.info(data) or {
io.println("not a WAV file we can read: {err}");
return 1;
};
io.println("{w.rate} Hz, {w.channels} channels, {w.frames} frames");
let frames: i64 = wav.decode_into(data, samples[0..480000]) or { return 1; };
io.println("decoded {frames} frames");
return 0;
}wav reads and writes WAV files and nothing else: it allocates nothing,
touches no files and needs no audio system, so it works anywhere,
freestanding code included. To play what you read, use audio, whose
decode_wav is built on this module.
| Function | Does |
|---|---|
info(data) -> Info or WavError | What the file holds |
decode_into(data, out) -> i64 or WavError | The samples as 16-bit integers into out, as many whole frames as fit; how many frames |
encode_i16(samples, rate, channels, out) -> i64 or WavError | A whole 16-bit WAV file of samples into out; its length |
encoded_len(frames, channels) -> i64 | How long encode_i16’s file is: HEADER_LEN + 2 bytes a sample |
write_header(out, rate, channels, bits, is_float, frames) | The 44 bytes (HEADER_LEN) before the samples, for 8, 16, 24 or 32-bit PCM or 32-bit float |
pub struct Info {
rate: i64; // frames a second
channels: i64;
bits: i64; // bits a sample in the file
is_float: bool; // 32-bit IEEE floats rather than integers
frames: i64;
data_at: i64; // where the samples are: data[data_at..data_at + data_len]
data_len: i64;
}
pub enum WavError : u8 {
Invalid = 1: "not a WAV file";
Unsupported = 2: "a WAV encoding wav does not support";
}decode_into writes the samples interleaved, as the file has them:
frame n starts at out[n * channels]. Size out from info:
frames * channels samples hold the whole file. Every format becomes
16-bit:
| In the file | As 16-bit |
|---|---|
| 8-bit PCM (unsigned) | re-centred: 128 is 0 |
| 16-bit PCM | as it is |
| 24- and 32-bit PCM | the top 16 bits |
| 32-bit float | scaled by 32767, clipped to ±32767 |
WAVE_FORMAT_EXTENSIBLE files work too, and any number of channels.
Chunks other than fmt and data are skipped. A file cut short gives
the frames it still has. Compressed WAV (ADPCM, A-law, mu-law) and 64-bit
float are Unsupported.
Writing
encode_i16 writes a whole file into a buffer you give it, sized with
encoded_len; save it with fs.write_file:
import wav from "wav";
import fs from "fs";
var file: [96044]u8; // encoded_len(48000, 1): one second of 48 kHz mono
fn save(samples: []const i16) -> i32 {
let n: i64 = wav.encode_i16(samples, 48000, 1, file[0..96044]) or { return 1; };
fs.write_file("take.wav", file[0..n]) or { return 1; };
return 0;
}When you write as you record and don’t know the length yet, write a
header for 0 frames, then the samples (little-endian), then
write_header again with the count over the first HEADER_LEN bytes.
write_header takes 8, 16, 24 or 32-bit PCM (is_float false) or 32-bit
float (is_float true); anything else is Unsupported, as is more than
4 GiB of samples. A buffer shorter than what is written is a broken
contract and traps. To record, see audio.
Last modified: