86 lines
3.6 KiB
Plaintext
86 lines
3.6 KiB
Plaintext
#! Contains the tests of MP3: files of the tests' own, `tests/files`, tones
|
|||
|
|
#! LAME encoded of sines, decoded to as many frames as the tones had, LAME's
|
||
|
|
#! delays cut, near their samples; sought anywhere, exactly as decoded from
|
||
|
|
#! the start; and what isn't MP3 refused.
|
||
|
|
#!
|
||
|
|
#! Real encoders' files were checked apart from these: 73 of them, and 54 LAME
|
||
|
|
#! made of every rate of MPEG-1, 2, and 2.5, of one channel and two, of 16 to
|
||
|
|
#! 160 kilobits a second, each decoded to as many frames as FFmpeg decodes it
|
||
|
|
#! to, every sample within 6.4e-6 of FFmpeg's.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
import std::math;
|
||
|
|
import std::testing;
|
||
|
|
import malunal::audio;
|
||
|
|
|
||
|
|
# A file of the tests', its bytes, or none where it can't be read.
|
||
|
|
def test_file(name: strview): vector<u8> {
|
||
|
|
var made := vector::<u8>::new();
|
||
|
|
val at := path::parse($"tests/files/{name}") else (_) { return made; };
|
||
|
|
val read := io::read_file(&at) else (_) { return made; };
|
||
|
|
made.extend(read.span);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# How far a decoded tone strays from the sines of 0.4 it was made of, each
|
||
|
|
# channel its own, as the root of their squares' mean, past its first and last
|
||
|
|
# 2000 frames, where an encoder's filters ring.
|
||
|
|
def tone_error(read: &sound, frequencies: span<f64>): f64 {
|
||
|
|
val width := read.channels as u64;
|
||
|
|
var sum: f64 = 0;
|
||
|
|
var count: f64 = 0;
|
||
|
|
for i in 2000..read.frames - 2000 {
|
||
|
|
for c in 0..width {
|
||
|
|
val want := 0.4 * math::sin(math::tau * frequencies[c] * i as f64 / read.rate as f64);
|
||
|
|
val off := read.samples[i * width + c] as f64 - want;
|
||
|
|
sum += off * off;
|
||
|
|
count += 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return math::sqrt(sum / count);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def decodes_mp3_to_the_tone_it_was_made_of(t: &mut tester): void {
|
||
|
|
val stereo := test_file("tone.mp3");
|
||
|
|
val found := audio::format_of(stereo.span) else { t.expects(false); return; };
|
||
|
|
t.expects(found == format::mp3);
|
||
|
|
val read := audio::decode(stereo.span) else (e) { t.expects(false); return; };
|
||
|
|
t.expects(read.rate == 44100 && read.channels == 2 && read.frames == 66150);
|
||
|
|
val both: f64[2] = [440, 660];
|
||
|
|
# LAME's own noise strays 0.014; a frame early or late, 0.026.
|
||
|
|
t.expects(tone_error(&read, both[:]) < 0.02);
|
||
|
|
|
||
|
|
val mono := test_file("tone22.mp3");
|
||
|
|
val low := audio::decode(mono.span) else (e) { t.expects(false); return; };
|
||
|
|
t.expects(low.rate == 22050 && low.channels == 1 && low.frames == 22050);
|
||
|
|
val one: f64[1] = [330];
|
||
|
|
t.expects(tone_error(&low, one[:]) < 0.02);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def seeks_mp3_exactly(t: &mut tester): void {
|
||
|
|
val bytes := test_file("tone.mp3");
|
||
|
|
val whole := audio::decode(bytes.span) else (e) { t.expects(false); return; };
|
||
|
|
var reading := audio::mp3::decoder::open(bytes.span) else (e) { t.expects(false); return; };
|
||
|
|
var piece := vector::<f32>::new();
|
||
|
|
piece.resize(2000 * 2);
|
||
|
|
val total := whole.frames;
|
||
|
|
val targets: u64[6] = [0, 1, 1000, 30000, 66149, 66150];
|
||
|
|
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 < 2000 ? total - target : 2000;
|
||
|
|
t.expects(count == wanted);
|
||
|
|
t.expects(apart(piece.span[0:count * 2], whole.samples.span[target * 2:count * 2]) == 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def refuses_what_isnt_mp3(t: &mut tester): void {
|
||
|
|
var bytes := test_file("tone.mp3");
|
||
|
|
# An ID3 tag before nothing of MP3's.
|
||
|
|
val tagged: u8[16] = [73, 68, 51, 3, 0, 0, 0, 0, 0, 4, 97, 98, 99, 100, 101, 102];
|
||
|
|
t.expects(fails(audio::decode(tagged[:]), audio_error::unrecognized));
|
||
|
|
# Cut short, it gives what it has.
|
||
|
|
bytes.resize(bytes.length / 2);
|
||
|
|
val half := audio::decode(bytes.span) else (e) { t.expects(false); return; };
|
||
|
|
t.expects(half.frames > 20000 && half.frames < 40000);
|
||
|
|
}
|