61 lines
2.9 KiB
Plaintext
61 lines
2.9 KiB
Plaintext
#! Contains the tests of `audio`'s own functions: sounds made, which format
|
|
#! bytes, or a file's extension, say they're in, and channels mixed down.
|
|
|
|
import std::math;
|
|
import std::testing;
|
|
import malunal::audio;
|
|
|
|
@test def makes_tones(t: &mut tester): void {
|
|
val beep := sound::tone(1000, 0.25, 48000, 0.5);
|
|
t.expects(beep.rate == 48000 && beep.channels == 1 && beep.frames == 12000);
|
|
t.expects(math::abs(beep.seconds - 0.25) < 1e-9);
|
|
# Faded in from silence, and out to it, and as loud as asked between.
|
|
t.expects(beep.samples[0] == 0 && math::abs(beep.samples[11999]) < 0.01);
|
|
var loudest: f32 = 0;
|
|
for i in 0..beep.samples.length {
|
|
loudest = math::max(loudest, math::abs(beep.samples[i]));
|
|
}
|
|
t.expects(loudest > 0.49 && loudest <= 0.5);
|
|
val given: f32[4] = [0.5, -0.5, 0.25, -0.25];
|
|
val made := sound::of(22050, 2, given[:]);
|
|
t.expects(made.frames == 2 && made.samples[3] == -0.25);
|
|
}
|
|
|
|
@test def finds_each_format_by_its_first_bytes(t: &mut tester): void {
|
|
t.expects(audio::format_of("RIFF\x24\x00\x00\x00WAVEfmt ") is some(_));
|
|
t.expects(audio::format_of("RIFF\x24\x00\x00\x00WAVEfmt ").unwrap() == audio::format::wav);
|
|
t.expects(audio::format_of("OggS\x00\x02").unwrap() == audio::format::vorbis);
|
|
t.expects(audio::format_of("RIFF\x24\x00\x00\x00AVI ") is none);
|
|
t.expects(audio::format_of("ID3\x04") is none);
|
|
t.expects(fails(audio::decode("plain text"), audio_error::unrecognized));
|
|
t.expects(fails(audio::load("tests/missing.wav"), audio_error::unreadable));
|
|
}
|
|
|
|
@test def names_formats_by_their_extensions(t: &mut tester): void {
|
|
t.expects(audio::format_named("wav").unwrap() == audio::format::wav);
|
|
t.expects(audio::format_named("WAVE").unwrap() == audio::format::wav);
|
|
t.expects(audio::format_named("ogg").unwrap() == audio::format::vorbis);
|
|
t.expects(audio::format_named("Oga").unwrap() == audio::format::vorbis);
|
|
t.expects(audio::format_named("FLAC").unwrap() == audio::format::flac);
|
|
t.expects(audio::format_named("mp3").unwrap() == audio::format::mp3);
|
|
t.expects(audio::format_named("aiff") is none);
|
|
}
|
|
|
|
@test def mixes_more_channels_down_to_two(t: &mut tester): void {
|
|
# 5.1 in Windows's order: the front's sides whole, the center to both, the
|
|
# low frequencies left out, the backs to their sides.
|
|
val speakers: speaker[6] = [
|
|
speaker::front_left, speaker::front_right, speaker::center, speaker::low_frequency,
|
|
speaker::back_left, speaker::back_right,
|
|
];
|
|
val frame: f32[6] = [1, 0, 0, 1, 0, 0];
|
|
var two: f32[2];
|
|
audio::downmix(frame[:], 1, speakers[:], two[:]);
|
|
# Each side sums to 1 + 0.707 + 0.707, so is scaled by its inverse.
|
|
val scale := 1 / (1 + 2 * 0.70710677);
|
|
t.expects(math::abs(two[0] - scale) < 1e-6 && two[1] == 0);
|
|
val middle: f32[6] = [0, 0, 1, 0, 0, 0];
|
|
audio::downmix(middle[:], 1, speakers[:], two[:]);
|
|
t.expects(math::abs(two[0] - 0.70710677 * scale) < 1e-6 && math::abs(two[0] - two[1]) < 1e-9);
|
|
}
|