Files
2026-11-14 15:37:01 -06:00

105 lines
4.7 KiB
Plaintext

#! Contains the tests of FLAC: streams the tests make, of every kind of
#! subframe, channel assignment, and residual, decoded to exactly the samples
#! they were made of; of 24 bits, wasted bits, and blocks of every size; of six
#! channels mixed down; sought by a seek table and by searching; their loop
#! points read of their comments; and broken streams refused.
#!
#! Real encoders' files were checked apart from these: 31 of them, each decoded
#! to the samples its STREAMINFO's MD5 is of.
import std::testing;
import malunal::audio;
def no_comments(): span<strview> {
return span::<strview>::blank();
}
@test def decodes_every_kind_of_subframe_exactly(t: &mut tester): void {
val plans := every_kind(24, 576, 2);
val made := made_flac(2, 16, plans.span, false, false, no_comments());
val read := audio::decode(made.bytes.span) else (e) { t.expects(false); return; };
t.expects(read.rate == 48000 && read.channels == 2 && read.frames == 24 * 576);
t.expects(apart(read.samples.span, made.expected.span) == 0);
}
@test def decodes_24_bits_wasted_and_blocks_of_every_size(t: &mut tester): void {
val sizes: u64[6] = [300, 1000, 64, 2048, 96, 777];
val kinds: coding[2] = [coding::fixed, coding::lpc];
var plans := vector::<frame_plan>::new();
var first: u64 = 0;
for i in 0..12u {
val block := sizes[i % 6];
plans.append(frame_plan{
first = first, block = block, kind = kinds[i % 2], order = 2 + (i % 3) as u32, wasted = 3,
partitions = block % 4 == 0 ? 2u : 0u, escape = i == 5,
});
first += block;
}
val made := made_flac(1, 24, plans.span, true, false, no_comments());
val read := audio::decode(made.bytes.span) else (e) { t.expects(false); return; };
t.expects(read.channels == 1 && read.frames == first);
t.expects(apart(read.samples.span, made.expected.span) == 0);
}
@test def mixes_six_channels_down(t: &mut tester): void {
val plans := every_kind(8, 512, 6);
val made := made_flac(6, 16, plans.span, false, false, no_comments());
val read := audio::decode(made.bytes.span) else (e) { t.expects(false); return; };
t.expects(read.channels == 2 && read.frames == 8 * 512);
var speakers: speaker[6];
for c in 0..6u {
speakers[c] = audio::standard_speaker(6, c as u32);
}
var two := vector::<f32>::new();
two.resize(read.frames * 2);
audio::downmix(made.expected.span, read.frames, speakers[:], two.slice);
t.expects(apart(read.samples.span, two.span) < 1e-6);
}
@test def seeks_by_its_table_and_by_searching_its_headers(t: &mut tester): void {
for table in [false, true] {
val plans := every_kind(200, 1152, 2);
val made := made_flac(2, 16, plans.span, false, table, no_comments());
var reading := audio::flac::decoder::open(made.bytes.span) else (e) { t.expects(false); return; };
val total := reading.frames;
t.expects(total == 200 * 1152);
var piece := vector::<f32>::new();
piece.resize(3000 * 2);
val targets: u64[8] = [0, 1, 1151, 1152, 50000, 123456, 230399, 230400];
for &target in targets {
t.expects(reading.seek(target) is value(_));
val count := reading.read(piece.slice) else (e) { t.expects(false); return; };
val wanted: u64 = total - target < 3000 ? total - target : 3000;
t.expects(count == wanted);
t.expects(apart(piece.span[0:count * 2], made.expected.span[target * 2:count * 2]) == 0);
}
}
}
@test def reads_where_it_loops_of_its_comments(t: &mut tester): void {
val comments: strview[3] = ["TITLE=tone", "LOOPSTART=1000", "loopend=5000"];
val made := made_flac(1, 16, every_kind(10, 1024, 1).span, false, false, comments[:]);
val read := audio::decode(made.bytes.span) else (e) { t.expects(false); return; };
t.expects(read.loop_points.start == 1000 && read.loop_points.end == 5000);
val plain := made_flac(1, 16, every_kind(4, 1024, 1).span, false, false, no_comments());
val unlooped := audio::decode(plain.bytes.span) else (e) { t.expects(false); return; };
t.expects(unlooped.loop_points.start == 0 && unlooped.loop_points.end == 0);
}
@test def refuses_broken_flac(t: &mut tester): void {
# A byte of its last frame changed, so its CRC-16 is wrong.
var changed := made_flac(2, 16, every_kind(4, 576, 2).span, false, false, no_comments());
changed.bytes[changed.bytes.length - 5] ^= 0x10;
t.expects(fails(audio::decode(changed.bytes.span), audio_error::malformed));
# Cut short, partway through its last frame.
var cut := made_flac(2, 16, every_kind(4, 576, 2).span, false, false, no_comments());
cut.bytes.resize(cut.bytes.length - 40);
t.expects(fails(audio::decode(cut.bytes.span), audio_error::malformed));
# Not FLAC.
var other := made_flac(1, 16, every_kind(1, 576, 1).span, false, false, no_comments());
other.bytes[3] = 'X' as u8;
t.expects(fails(audio::decode(other.bytes.span), audio_error::unrecognized));
}