Files
audio/tests/wav.sakura
T
2026-11-11 11:49:04 -06:00

145 lines
6.4 KiB
Plaintext

#! Contains the tests of WAV: sounds written and read back, as integers and as
#! floats, files of each sample size, more channels mixed down, a `data` chunk
#! longer than the file, and what isn't read.
import std::io;
import std::math;
import std::testing;
import malunal::audio;
# A WAV file of samples given as their bytes.
def wav_of(tag: u16, channels: u32, rate: u32, bits: u32, data: span<u8>, extensible: bool = false, mask: u32 = 0): vector<u8> {
var out := vector::<u8>::new();
val fmt_size: u32 = extensible ? 40 : 16;
text(&mut out, "RIFF");
number(&mut out, 4 + 8 + fmt_size + 8 + data.length as u32, 4);
text(&mut out, "WAVEfmt ");
number(&mut out, fmt_size, 4);
number(&mut out, extensible ? 0xFFFE : tag as u32, 2);
number(&mut out, channels, 2);
number(&mut out, rate, 4);
number(&mut out, rate * channels * bits / 8, 4);
number(&mut out, channels * bits / 8, 2);
number(&mut out, bits, 2);
if extensible {
number(&mut out, 22, 2);
number(&mut out, bits, 2);
number(&mut out, mask, 4);
number(&mut out, tag as u32, 2);
for b in [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71] {
out.append(b as u8);
}
}
text(&mut out, "data");
number(&mut out, data.length as u32, 4);
out.extend(data);
return out;
}
def text(out: &mut vector<u8>, value: strview): void {
for c in value {
out.append(c as u8);
}
}
def number(out: &mut vector<u8>, value: u32, bytes: u32): void {
for i in 0..bytes {
out.append((value >> (i * 8)) as u8);
}
}
@test def writes_and_reads_sounds_back(t: &mut tester): void {
val tone := sound::tone(440, 0.05, 44100);
val floats := audio::wav::encode(&tone, true) else (_) { t.expects(false); return; };
val back := audio::decode(floats.span) else (_) { t.expects(false); return; };
t.expects(back.rate == 44100 && back.channels == 1 && back.frames == tone.frames);
t.expects(apart(back.samples.span, tone.samples.span) == 0);
val whole := audio::wav::encode(&tone) else (_) { t.expects(false); return; };
t.expects(whole.length == 44 + tone.frames * 2);
val rounded := audio::decode(whole.span) else (_) { t.expects(false); return; };
t.expects(apart(rounded.samples.span, tone.samples.span) < 1.0 / 32767);
# Through a file.
audio::save(&tone, ".blossom/written.wav") else (_) { t.expects(false); return; };
val loaded := audio::load(".blossom/written.wav") else (_) { t.expects(false); return; };
t.expects(loaded.frames == tone.frames);
t.expects(fails_writing(audio::save(&tone, ".blossom/written.ogg"), audio_error::unrecognized));
}
@test def reads_each_sample_size(t: &mut tester): void {
# 8 bits, unsigned: 0, 255, and 128 are -1, nearly 1, and 0.
val eight_data: u8[3] = [0, 255, 128];
val eight := wav_of(1, 1, 8000, 8, eight_data[:]);
val read8 := audio::decode(eight.span) else (_) { t.expects(false); return; };
t.expects(read8.samples[0] == -1 && math::abs(read8.samples[1] - 127.0 / 128) < 1e-7 && read8.samples[2] == 0);
# 16 bits: -32768 and 16384.
val sixteen_data: u8[4] = [0x00, 0x80, 0x00, 0x40];
val sixteen := wav_of(1, 2, 8000, 16, sixteen_data[:]);
val read16 := audio::decode(sixteen.span) else (_) { t.expects(false); return; };
t.expects(read16.channels == 2 && read16.samples[0] == -1 && read16.samples[1] == 0.5);
# 24 bits: -8388608 and 4194304.
val twenty_four_data: u8[6] = [0x00, 0x00, 0x80, 0x00, 0x00, 0x40];
val twenty_four := wav_of(1, 1, 8000, 24, twenty_four_data[:]);
val read24 := audio::decode(twenty_four.span) else (_) { t.expects(false); return; };
t.expects(read24.samples[0] == -1 && read24.samples[1] == 0.5);
# 32 bits, in an extensible header.
val thirty_two_data: u8[4] = [0x00, 0x00, 0x00, 0xC0];
val thirty_two := wav_of(1, 1, 8000, 32, thirty_two_data[:], true, 4);
val read32 := audio::decode(thirty_two.span) else (_) { t.expects(false); return; };
t.expects(read32.samples[0] == -0.5);
# 64-bit floats: 0.25.
val doubles_data: u8[8] = [0, 0, 0, 0, 0, 0, 0xD0, 0x3F];
val doubles := wav_of(3, 1, 8000, 64, doubles_data[:]);
val read64 := audio::decode(doubles.span) else (_) { t.expects(false); return; };
t.expects(read64.samples[0] == 0.25);
}
@test def mixes_more_channels_down_as_their_mask_places_them(t: &mut tester): void {
# Four channels, front left and right and back left and right, as the mask
# says: a sample in the back left is heard in the left alone.
val data: u8[8] = [0, 0, 0, 0, 0x00, 0x40, 0, 0];
val quad := wav_of(1, 4, 8000, 16, data[:], true, 0x33);
val read := audio::decode(quad.span) else (_) { t.expects(false); return; };
t.expects(read.channels == 2 && read.frames == 1);
t.expects(read.samples[0] > 0.2 && read.samples[1] == 0);
}
@test def reads_a_data_chunk_longer_than_the_file_as_far_as_it_goes(t: &mut tester): void {
val cut_data: u8[6] = [0x00, 0x40, 0x00, 0x40, 0x00, 0x40];
var cut := wav_of(1, 1, 8000, 16, cut_data[:]);
cut.truncate();
val read := audio::decode(cut.span) else (_) { t.expects(false); return; };
t.expects(read.frames == 2);
}
@test def refuses_what_it_does_not_read(t: &mut tester): void {
# ADPCM, and a file of no `fmt `.
val zeros: u8[3] = [0, 0, 0];
val adpcm := wav_of(2, 1, 8000, 4, zeros[0:2]);
t.expects(fails(audio::decode(adpcm.span), audio_error::unsupported));
var headless := vector::<u8>::new();
text(&mut headless, "RIFF\x0C\x00\x00\x00WAVEdata\x00\x00\x00\x00");
t.expects(fails(audio::decode(headless.span), audio_error::malformed));
val wrong := wav_of(1, 2, 8000, 12, zeros[:]);
t.expects(fails(audio::decode(wrong.span), audio_error::unsupported));
}
# Whether writing failed with an error.
def fails_writing(made: result<void, audio_error>, why: audio_error): bool {
if made is error(e) {
return e == why;
}
return false;
}
@test def keeps_where_a_sound_loops_in_a_sampler_chunk(t: &mut tester): void {
var looped := sound::tone(440, 0.1);
looped.loop_points = loop_range{ start = 480, end = 4000 };
val bytes := audio::wav::encode(&looped) else (e) { t.expects(false); return; };
val read := audio::decode(bytes.span) else (e) { t.expects(false); return; };
t.expects(read.loop_points.start == 480 && read.loop_points.end == 4000 && read.frames == looped.frames);
val plain := audio::wav::encode(&sound::tone(440, 0.1)) else (e) { t.expects(false); return; };
val unlooped := audio::decode(plain.span) else (e) { t.expects(false); return; };
t.expects(unlooped.loop_points.end == 0 && plain.length == 44 + looped.frames * 2);
}