Initial Commit
This commit is contained in:
36 files changed
+4431
No files matched your search
@@ -0,0 +1,10 @@
|
||||
#! Contains the `main` that runs audio's tests, each in a copy of the program of
|
||||
#! its own. `blossom test` runs them, and `-- --only TEXT` those whose name
|
||||
#! holds the text.
|
||||
|
||||
import system;
|
||||
import std::testing;
|
||||
|
||||
def main(args: span<strview>): void {
|
||||
exit(tester::run(args));
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
#! Contains the tests of `mixer`: voices mixed at their gains, panned, ramped
|
||||
#! as they change, ended or looped at their sounds' ends, resampled and
|
||||
#! pitched, faded, on buses, and streamed as they play.
|
||||
|
||||
import std::math;
|
||||
import std::testing;
|
||||
import malunal::audio;
|
||||
|
||||
# A sound of one channel, every sample the same.
|
||||
def flat(rate: u32, frames: u64, value: f32): sound {
|
||||
var samples := vector::<f32>::new();
|
||||
samples.resize(frames, value);
|
||||
return sound{ rate = rate, channels = 1, samples = move(samples) };
|
||||
}
|
||||
|
||||
# Mixes some frames.
|
||||
def mixed(m: &mut mixer, frames: u64): vector<f32> {
|
||||
var out := vector::<f32>::new();
|
||||
out.resize(frames * 2);
|
||||
m.mix(out.slice());
|
||||
return out;
|
||||
}
|
||||
|
||||
@test def mixes_a_mono_voice_at_its_gain_and_pan(t: &mut tester): void {
|
||||
var m := mixer::new(48000);
|
||||
val id := m.add(flat(48000, 1000, 0.5));
|
||||
val v := m.play(id);
|
||||
val middle := mixed(&mut m, 100);
|
||||
# The constant power law: -3 dB each side at the middle.
|
||||
t.expects(math::abs(middle[0] - 0.5 * 0.70710677) < 1e-6 && math::abs(middle[199] - middle[198]) < 1e-6);
|
||||
|
||||
# Panned all left, ramped there across the next frames.
|
||||
m.set_pan(v, -1);
|
||||
val left := mixed(&mut m, 100);
|
||||
t.expects(math::abs(left[198] - 0.5) < 1e-6 && math::abs(left[199]) < 1e-6);
|
||||
t.expects(left[1] > 0 && left[1] < middle[1]);
|
||||
val held := mixed(&mut m, 100);
|
||||
t.expects(math::abs(held[0] - 0.5) < 1e-6 && held[1] == 0);
|
||||
|
||||
# Two voices add.
|
||||
m.set_pan(v, 0);
|
||||
_ = mixed(&mut m, 10);
|
||||
_ = m.play(id);
|
||||
val two := mixed(&mut m, 10);
|
||||
t.expects(math::abs(two[0] - 2 * 0.5 * 0.70710677) < 1e-6);
|
||||
t.expects(m.voice_count() == 2);
|
||||
}
|
||||
|
||||
@test def pans_a_stereo_voice_by_turning_down_one_side(t: &mut tester): void {
|
||||
var m := mixer::new(48000);
|
||||
val pairs: f32[6] = [0.5, 0.25, 0.5, 0.25, 0.5, 0.25];
|
||||
val id := m.add(sound::of(48000, 2, pairs[:]));
|
||||
_ = m.play(id, playback{ pan = 0.5 });
|
||||
val out := mixed(&mut m, 2);
|
||||
t.expects(math::abs(out[0] - 0.25) < 1e-6 && math::abs(out[1] - 0.25) < 1e-6);
|
||||
t.expects(m.add(sound::of(48000, 3, pairs[0:3])).is_none);
|
||||
}
|
||||
|
||||
@test def ends_voices_at_their_ends_and_loops_those_that_loop(t: &mut tester): void {
|
||||
var m := mixer::new(48000);
|
||||
val id := m.add(flat(48000, 100, 0.5));
|
||||
val once := m.play(id, playback{ pan = -1 });
|
||||
val out := mixed(&mut m, 150);
|
||||
t.expects(out[2 * 99] == 0.5 && out[2 * 100] == 0 && out[2 * 149] == 0);
|
||||
t.expects(!m.playing(once) && m.voice_count() == 0);
|
||||
|
||||
val looped := m.play(id, playback{ pan = -1, looping = true });
|
||||
val again := mixed(&mut m, 250);
|
||||
t.expects(again[2 * 150] == 0.5 && again[2 * 249] == 0.5 && m.playing(looped));
|
||||
t.expects(math::abs(m.position(looped) - 50.0 / 48000) < 1e-9);
|
||||
m.stop(looped);
|
||||
t.expects(!m.playing(looped));
|
||||
# A voice ended names none after, though its place is played again.
|
||||
val next := m.play(id);
|
||||
t.expects(m.playing(next) && !m.playing(looped));
|
||||
}
|
||||
|
||||
@test def resamples_and_pitches_as_it_plays(t: &mut tester): void {
|
||||
# A sine of 1 kHz at 24 kHz, played at 48 kHz: as long again, and as near the
|
||||
# same sine as cubic interpolation reaches.
|
||||
var m := mixer::new(48000);
|
||||
val id := m.add(sound::tone(1000, 0.05, 24000, 0.5));
|
||||
val v := m.play(id, playback{ pan = -1 });
|
||||
val out := mixed(&mut m, 2500);
|
||||
t.expects(!m.playing(v) && out[2 * 2390] != 0 && out[2 * 2405] == 0);
|
||||
var worst: f32 = 0;
|
||||
for i in 300u..2000u {
|
||||
val want := 0.5 * math::sin(math::tau * 1000 * i as f64 / 48000) as f32;
|
||||
worst = math::max(worst, math::abs(out[2 * i] - want));
|
||||
}
|
||||
t.expects(worst < 0.01);
|
||||
|
||||
# At twice its pitch, it plays at its own rate, as long as it is.
|
||||
val fast := m.play(id, playback{ pan = -1, pitch = 2 });
|
||||
val quick := mixed(&mut m, 1300);
|
||||
t.expects(!m.playing(fast) && quick[2 * 1100] != 0 && quick[2 * 1205] == 0);
|
||||
}
|
||||
|
||||
@test def multiplies_by_buses_and_the_master(t: &mut tester): void {
|
||||
var m := mixer::new(48000);
|
||||
val id := m.add(flat(48000, 100, 0.5));
|
||||
m.set_bus(3, 0.5);
|
||||
m.set_master(0.5);
|
||||
_ = m.play(id, playback{ pan = -1, bus = 3 });
|
||||
val out := mixed(&mut m, 4);
|
||||
t.expects(math::abs(out[0] - 0.125) < 1e-6 && m.bus(3) == 0.5 && m.master == 0.5);
|
||||
}
|
||||
|
||||
@test def fades_voices_in_and_out(t: &mut tester): void {
|
||||
var m := mixer::new(48000);
|
||||
val id := m.add(flat(48000, 10000, 0.5));
|
||||
val v := m.play(id, playback{ pan = -1, fade_in = 0.01 });
|
||||
val rising := mixed(&mut m, 480);
|
||||
t.expects(rising[0] < 0.01 && math::abs(rising[2 * 479] - 0.5) < 1e-5 && rising[2 * 240] < 0.3);
|
||||
m.stop(v, 0.01);
|
||||
val falling := mixed(&mut m, 480);
|
||||
t.expects(falling[0] > 0.49 && falling[2 * 479] < 1e-5 && !m.playing(v));
|
||||
}
|
||||
|
||||
@test def plays_a_stream_as_it_plays_the_sound_whole(t: &mut tester): void {
|
||||
val made := made_vorbis(2, 0);
|
||||
var m := mixer::new(48000);
|
||||
val streamed := m.add_streamed(made.bytes.view()) else (e) { t.expects(false); return; };
|
||||
var whole := audio::decode(made.bytes.view()) else (e) { t.expects(false); return; };
|
||||
val frames := whole.frames;
|
||||
val decoded := m.add(move(whole));
|
||||
t.expects(math::abs(m.seconds(streamed) - m.seconds(decoded)) < 1e-9);
|
||||
|
||||
val a := m.play(streamed);
|
||||
val from_stream := mixed(&mut m, frames + 10);
|
||||
t.expects(!m.playing(a));
|
||||
val b := m.play(decoded);
|
||||
val from_sound := mixed(&mut m, frames + 10);
|
||||
t.expects(!m.playing(b));
|
||||
t.expects(apart(from_stream.view(), from_sound.view()) < 1e-6);
|
||||
|
||||
# Looped, it plays its first frames again after its last.
|
||||
val c := m.play(streamed, playback{ looping = true });
|
||||
val round := mixed(&mut m, frames + 50);
|
||||
t.expects(m.playing(c));
|
||||
t.expects(math::abs(round[2 * (frames + 40)] - from_sound[2 * 40]) < 1e-6);
|
||||
|
||||
m.remove(streamed);
|
||||
t.expects(!m.playing(c) && !m.holds(streamed) && m.play(streamed).is_none);
|
||||
}
|
||||
|
||||
@test def makes_a_sound_another_under_its_id(t: &mut tester): void {
|
||||
var m := mixer::new(48000);
|
||||
val id := m.add(flat(48000, 100, 0.5));
|
||||
val v := m.play(id, playback{ pan = -1 });
|
||||
t.expects(m.replace(id, flat(48000, 100, 0.25)) && !m.playing(v));
|
||||
_ = m.play(id, playback{ pan = -1 });
|
||||
val out := mixed(&mut m, 2);
|
||||
t.expects(out[0] == 0.25 && sound_id::of_bits(id.bits).equals(&id));
|
||||
val made := made_vorbis(1, 0);
|
||||
t.expects(m.replace_streamed(id, made.bytes.view()) is value(_));
|
||||
t.expects(math::abs(m.seconds(id) - made.expected.length as f64 / 48000) < 1e-9);
|
||||
t.expects(!m.replace(sound_id{}, flat(48000, 1, 0)));
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#! Contains the tests of `audio_output`: one of no device, mixed as the test
|
||||
#! asks, and the system's default device, where there is one.
|
||||
|
||||
import std::testing;
|
||||
import std::thread;
|
||||
import std::time;
|
||||
import malunal::audio;
|
||||
|
||||
@test def mixes_for_no_device_as_asked(t: &mut tester): void {
|
||||
var out := audio_output::headless(44100);
|
||||
t.expects(!out.has_device && out.rate() == 44100);
|
||||
val id := out.add(sound::tone(440, 0.1, 44100));
|
||||
val v := out.play(id, playback{ pan = -1 });
|
||||
var frames := vector::<f32>::new();
|
||||
frames.resize(2 * 441);
|
||||
out.render(frames.slice());
|
||||
t.expects(out.played == 441 && frames[2 * 200] != 0 && frames[2 * 200 + 1] == 0);
|
||||
t.expects(out.playing(v));
|
||||
out.stop(v);
|
||||
t.expects(!out.playing(v));
|
||||
}
|
||||
|
||||
@test def plays_to_the_default_device_where_there_is_one(t: &mut tester): void {
|
||||
var out := audio_output::open() else (e) {
|
||||
# A machine of no audio device, as a build server, passes.
|
||||
t.expects(e == audio_error::no_device);
|
||||
return;
|
||||
};
|
||||
t.expects(out.has_device && out.rate() >= 8000);
|
||||
# Silent, so the test isn't heard.
|
||||
val id := out.add(sound::tone(440, 0.05, out.rate(), 0));
|
||||
_ = out.play(id);
|
||||
val started := time::instant::now();
|
||||
while out.played < 2000 && started.elapsed().compare(&time::seconds(2)) < 0 {
|
||||
thread::sleep(time::milliseconds(10));
|
||||
}
|
||||
t.expects(out.played >= 2000);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
#! Contains how the tests write what they encode: bits as Vorbis packs them,
|
||||
#! and Ogg's pages, each laced and checked by its CRC, worked out bit by bit as
|
||||
#! RFC 3533 defines it, apart from how the decoder works it out.
|
||||
|
||||
# Bits written as Vorbis packs them: each number's lowest bit first.
|
||||
struct bit_writer {
|
||||
var bytes: vector<u8>;
|
||||
var held: u32;
|
||||
var count: u32;
|
||||
}
|
||||
|
||||
def bit_writer::new(): bit_writer {
|
||||
return bit_writer{ bytes = vector::<u8>::new() };
|
||||
}
|
||||
|
||||
def bit_writer::put(&mut this, value: u32, width: u32): void {
|
||||
for i in 0..width {
|
||||
this.held |= ((value >> i) & 1) << this.count;
|
||||
this.count++;
|
||||
if this.count == 8 {
|
||||
this.bytes.append(this.held as u8);
|
||||
this.held = 0;
|
||||
this.count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# A codeword, its first bit highest, as a codebook's are read.
|
||||
def bit_writer::code(&mut this, codeword: u32, length: u32): void {
|
||||
for i in 0..length {
|
||||
this.put((codeword >> (length - 1 - i)) & 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
def bit_writer::text(&mut this, text: strview): void {
|
||||
for i in 0..text.length {
|
||||
this.put(text[i] as u32, 8);
|
||||
}
|
||||
}
|
||||
|
||||
def bit_writer::finish(&mut this): vector<u8> {
|
||||
if this.count > 0 {
|
||||
this.bytes.append(this.held as u8);
|
||||
}
|
||||
return move(this.bytes);
|
||||
}
|
||||
|
||||
# A packet's lacing: 255 for each whole 255 bytes, then what's left.
|
||||
def lace(into: &mut vector<u8>, length: u64): void {
|
||||
var left := length;
|
||||
while left >= 255 {
|
||||
into.append(255);
|
||||
left -= 255;
|
||||
}
|
||||
into.append(left as u8);
|
||||
}
|
||||
|
||||
# A page of up to two packets.
|
||||
def page_of(out: &mut vector<u8>, flags: u8, granule: u64, sequence: u32, one: span<u8>, two: span<u8>): void {
|
||||
var lacing := vector::<u8>::new();
|
||||
var body := vector::<u8>::new();
|
||||
lace(&mut lacing, one.length);
|
||||
body.extend(one);
|
||||
if two.length > 0 {
|
||||
lace(&mut lacing, two.length);
|
||||
body.extend(two);
|
||||
}
|
||||
page_laced(out, flags, granule, sequence, lacing.view(), body.view());
|
||||
}
|
||||
|
||||
# A page, its CRC worked out bit by bit, as RFC 3533 defines it.
|
||||
def page_laced(out: &mut vector<u8>, flags: u8, granule: u64, sequence: u32, lacing: span<u8>, body: span<u8>): void {
|
||||
val start := out.length;
|
||||
for c in "OggS" {
|
||||
out.append(c as u8);
|
||||
}
|
||||
out.append(0);
|
||||
out.append(flags);
|
||||
for i in 0u..8u {
|
||||
out.append((granule >> (i * 8) as u32) as u8);
|
||||
}
|
||||
put_u32(out, 0x51A7);
|
||||
put_u32(out, sequence);
|
||||
put_u32(out, 0);
|
||||
out.append(lacing.length as u8);
|
||||
out.extend(lacing);
|
||||
out.extend(body);
|
||||
var crc: u32 = 0;
|
||||
for i in start..out.length {
|
||||
crc ^= (out[i] as u32) << 24;
|
||||
for _ in 0..8 {
|
||||
val high := (crc & 0x80000000) != 0;
|
||||
crc = (crc & 0x7FFFFFFF) << 1;
|
||||
if high {
|
||||
crc ^= 0x04C11DB7;
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 0u..4u {
|
||||
out[start + 22 + i] = (crc >> (i * 8) as u32) as u8;
|
||||
}
|
||||
}
|
||||
|
||||
def put_u32(out: &mut vector<u8>, value: u32): void {
|
||||
for i in 0u..4u {
|
||||
out.append((value >> (i * 8) as u32) as u8);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#! 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("mp3") 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);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
#! Contains the frames a stream the tests make should decode to, worked out as
|
||||
#! the spec defines them, directly: each block's spectrum, its residue, the
|
||||
#! coupled channels taken apart; its inverse MDCT as its sum; windowed, the
|
||||
#! window's slopes short beside a short block; and laid at its place on one
|
||||
#! timeline, read from the first block's middle to the last's.
|
||||
|
||||
import std::math;
|
||||
|
||||
# The frames a stream of `pattern`'s blocks decodes to, each packet's residue
|
||||
# given, `trimmed` frames cut from its end.
|
||||
def defined_frames(channels: u32, trimmed: u64, residues: &vector<vector<f32>>): vector<f32> {
|
||||
val sizes: u64[2] = [64, 256];
|
||||
val count := 9u;
|
||||
var centers := vector::<u64>::new();
|
||||
var center := sizes[pattern[0] ? 1 : 0] / 2;
|
||||
for k in 0..count {
|
||||
if k > 0 {
|
||||
center += sizes[pattern[k - 1] ? 1 : 0] / 4 + sizes[pattern[k] ? 1 : 0] / 4;
|
||||
}
|
||||
centers.append(center);
|
||||
}
|
||||
val length := centers[count - 1] + 128;
|
||||
var timeline := vector::<f64>::new();
|
||||
timeline.resize(length * channels as u64);
|
||||
for k in 0..count {
|
||||
val long := pattern[k];
|
||||
val n := sizes[long ? 1 : 0];
|
||||
val half := n / 2;
|
||||
val start := centers[k] - half;
|
||||
for c in 0..channels as u64 {
|
||||
var spectrum := vector::<f64>::new();
|
||||
spectrum.resize(half);
|
||||
for i in 0..half {
|
||||
if channels == 1 {
|
||||
spectrum[i] = residues[k][i] as f64;
|
||||
} else {
|
||||
# Taken apart, as the magnitude and angle were coupled.
|
||||
val m := residues[k][2 * i] as f64;
|
||||
val a := residues[k][2 * i + 1] as f64;
|
||||
var mag := m;
|
||||
var ang := a;
|
||||
if m > 0 {
|
||||
if a > 0 { ang = m - a; } else { ang = m; mag = m + a; }
|
||||
} else {
|
||||
if a > 0 { ang = m + a; } else { ang = m; mag = m - a; }
|
||||
}
|
||||
spectrum[i] = c == 0 ? mag : ang;
|
||||
}
|
||||
}
|
||||
val before_short := long && k > 0 && !pattern[k - 1];
|
||||
val after_short := long && k + 1 < count && !pattern[k + 1];
|
||||
for t in 0..n {
|
||||
var sum: f64 = 0;
|
||||
for i in 0..half {
|
||||
sum += spectrum[i] * math::cos(math::tau / n as f64 * (t as f64 + 0.5 + n as f64 / 4) * (i as f64 + 0.5));
|
||||
}
|
||||
timeline[(start + t) * channels as u64 + c] += sum * window_at(n, t, before_short, after_short);
|
||||
}
|
||||
}
|
||||
}
|
||||
val first := centers[0];
|
||||
val total := centers[count - 1] - first - trimmed;
|
||||
var expected := vector::<f32>::new();
|
||||
for f in 0..total {
|
||||
for c in 0..channels as u64 {
|
||||
expected.append(timeline[(first + f) * channels as u64 + c] as f32);
|
||||
}
|
||||
}
|
||||
return expected;
|
||||
}
|
||||
|
||||
# Vorbis's window, at a sample of a block of a size, its slopes short beside a
|
||||
# short block, as the spec defines it.
|
||||
def window_at(n: u64, t: u64, before_short: bool, after_short: bool): f64 {
|
||||
val short: u64 = 64;
|
||||
val half := n / 2;
|
||||
var rise_from: u64 = 0;
|
||||
var rise := half;
|
||||
if before_short {
|
||||
rise_from = n / 4 - short / 4;
|
||||
rise = short / 2;
|
||||
}
|
||||
var fall_from := half;
|
||||
var fall := half;
|
||||
if after_short {
|
||||
fall_from = n * 3 / 4 - short / 4;
|
||||
fall = short / 2;
|
||||
}
|
||||
if t < rise_from {
|
||||
return 0;
|
||||
}
|
||||
if t < rise_from + rise {
|
||||
val s := math::sin(((t - rise_from) as f64 + 0.5) / rise as f64 * math::pi / 2);
|
||||
return math::sin(math::pi / 2 * s * s);
|
||||
}
|
||||
if t < fall_from {
|
||||
return 1;
|
||||
}
|
||||
if t < fall_from + fall {
|
||||
val s := math::sin(((t - fall_from) as f64 + 0.5) / fall as f64 * math::pi / 2 + math::pi / 2);
|
||||
return math::sin(math::pi / 2 * s * s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
#! Contains Ogg Vorbis streams made by a small encoder of the tests' own, and the
|
||||
#! frames each should decode to, as `spec.sakura` works them out.
|
||||
#!
|
||||
#! A stream's setup has a book of two classes and a book of 16 vectors of two
|
||||
#! dimensions, a floor of type 1 whose posts are both 255, a curve of 1, so each
|
||||
#! spectrum is its residue, partitions of 8 values, each silent or coded, and
|
||||
#! blocks of 64 and 256 samples. Two channels are coupled, and coded by a
|
||||
#! residue of type 2; one is coded by a residue of type 1.
|
||||
|
||||
import std::math;
|
||||
import malunal::audio;
|
||||
|
||||
# What a stream made by the tests holds.
|
||||
struct made_stream {
|
||||
var bytes: vector<u8>;
|
||||
var expected: vector<f32>; # The frames it decodes to, a frame's channels together.
|
||||
var channels: u32;
|
||||
}
|
||||
|
||||
# The blocks a stream is made of: long or short, in order.
|
||||
const pattern: bool[9] = [true, true, false, false, true, false, true, true, false];
|
||||
|
||||
# A coded partition's entries, and whether a partition is coded.
|
||||
def entry_at(packet: u64, partition: u64, j: u64): u32 {
|
||||
return ((packet * 7 + partition * 5 + j * 3) % 16) as u32;
|
||||
}
|
||||
|
||||
def coded(packet: u64, partition: u64): bool {
|
||||
return (packet + partition) % 3 != 0;
|
||||
}
|
||||
|
||||
# The values a vector entry stands for: a grid of four from -1.5, twice over.
|
||||
def entry_value(entry: u32, dimension: u32): f32 {
|
||||
val index := dimension == 0 ? entry % 4 : (entry / 4) % 4;
|
||||
return -1.5 + index as f32;
|
||||
}
|
||||
|
||||
# Makes a stream of one or two channels, `trimmed` frames cut from its end by
|
||||
# its last page's granule position.
|
||||
# @param floor_zero Whether its floor says it's of type 0, which isn't read.
|
||||
def made_vorbis(channels: u32, trimmed: u64, floor_zero: bool = false): made_stream {
|
||||
val sizes: u64[2] = [64, 256];
|
||||
val count := 9u;
|
||||
|
||||
# The headers.
|
||||
var id := bit_writer::new();
|
||||
id.put(1, 8);
|
||||
id.text("vorbis");
|
||||
id.put(0, 32);
|
||||
id.put(channels, 8);
|
||||
id.put(48000, 32);
|
||||
id.put(0, 32);
|
||||
id.put(0, 32);
|
||||
id.put(0, 32);
|
||||
id.put(6 | (8 << 4), 8);
|
||||
id.put(1, 8);
|
||||
var comment := bit_writer::new();
|
||||
comment.put(3, 8);
|
||||
comment.text("vorbis");
|
||||
comment.put(0, 32);
|
||||
comment.put(0, 32);
|
||||
comment.put(1, 8);
|
||||
var setup := bit_writer::new();
|
||||
setup.put(5, 8);
|
||||
setup.text("vorbis");
|
||||
setup.put(1, 8); # two books
|
||||
setup.put(0x564342, 24); # book 0: the classes
|
||||
setup.put(1, 16);
|
||||
setup.put(2, 24);
|
||||
setup.put(0, 2);
|
||||
setup.put(0, 5);
|
||||
setup.put(0, 5);
|
||||
setup.put(0, 4);
|
||||
setup.put(0x564342, 24); # book 1: the vectors
|
||||
setup.put(2, 16);
|
||||
setup.put(16, 24);
|
||||
setup.put(0, 2);
|
||||
for _ in 0..16 {
|
||||
setup.put(3, 5);
|
||||
}
|
||||
setup.put(1, 4);
|
||||
setup.put(0x80000000 | (787 << 21) | 3, 32); # minimum -1.5
|
||||
setup.put((788 << 21) | 1, 32); # delta 1
|
||||
setup.put(1, 4);
|
||||
setup.put(0, 1);
|
||||
for v in 0u..4u {
|
||||
setup.put(v as u32, 2);
|
||||
}
|
||||
setup.put(0, 6); # one time transform
|
||||
setup.put(0, 16);
|
||||
setup.put(0, 6); # one floor
|
||||
setup.put(floor_zero ? 0 : 1, 16);
|
||||
setup.put(0, 5);
|
||||
setup.put(0, 2);
|
||||
setup.put(7, 4);
|
||||
setup.put(0, 6); # one residue
|
||||
setup.put(channels == 2 ? 2 : 1, 16);
|
||||
setup.put(0, 24);
|
||||
setup.put(channels * 128, 24);
|
||||
setup.put(7, 24);
|
||||
setup.put(1, 6);
|
||||
setup.put(0, 8);
|
||||
setup.put(0, 4);
|
||||
setup.put(1, 4);
|
||||
setup.put(1, 8);
|
||||
setup.put(0, 6); # one mapping
|
||||
setup.put(0, 16);
|
||||
setup.put(0, 1);
|
||||
if channels == 2 {
|
||||
setup.put(1, 1);
|
||||
setup.put(0, 8);
|
||||
setup.put(0, 1);
|
||||
setup.put(1, 1);
|
||||
} else {
|
||||
setup.put(0, 1);
|
||||
}
|
||||
setup.put(0, 2);
|
||||
setup.put(0, 24);
|
||||
setup.put(1, 6); # two modes
|
||||
for long in 0u..2u {
|
||||
setup.put(long as u32, 1);
|
||||
setup.put(0, 32);
|
||||
setup.put(0, 8);
|
||||
}
|
||||
setup.put(1, 1);
|
||||
|
||||
# The packets, and the residue each codes.
|
||||
var packets := vector::<vector<u8>>::new();
|
||||
var residues := vector::<vector<f32>>::new();
|
||||
for k in 0..count {
|
||||
val long := pattern[k];
|
||||
val half := sizes[long ? 1 : 0] / 2;
|
||||
var w := bit_writer::new();
|
||||
w.put(0, 1);
|
||||
w.put(long ? 1 : 0, 1);
|
||||
if long {
|
||||
w.put(k == 0 || pattern[k - 1] ? 1 : 0, 1);
|
||||
w.put(k + 1 == count || pattern[k + 1] ? 1 : 0, 1);
|
||||
}
|
||||
for _ in 0..channels {
|
||||
w.put(1, 1);
|
||||
w.put(255, 8);
|
||||
w.put(255, 8);
|
||||
}
|
||||
val size := half * channels as u64;
|
||||
var residue := vector::<f32>::new();
|
||||
residue.resize(size);
|
||||
for p in 0..size / 8 {
|
||||
val on := coded(k, p);
|
||||
w.code(on ? 1 : 0, 1);
|
||||
continue if !on;
|
||||
for j in 0u..4u {
|
||||
val e := entry_at(k, p, j);
|
||||
w.code(e, 4);
|
||||
residue[p * 8 + j * 2] = entry_value(e, 0);
|
||||
residue[p * 8 + j * 2 + 1] = entry_value(e, 1);
|
||||
}
|
||||
}
|
||||
packets.append(w.finish());
|
||||
residues.append(move(residue));
|
||||
}
|
||||
|
||||
var expected := defined_frames(channels, trimmed, &residues);
|
||||
|
||||
# The pages: the identification alone, the comment and setup, then the audio
|
||||
# three packets to a page, each page's granule position the frames decoded
|
||||
# once its last packet is.
|
||||
var bytes := vector::<u8>::new();
|
||||
var sequence: u32 = 0;
|
||||
val ident := id.finish();
|
||||
page_of(&mut bytes, 2, 0, sequence, ident.view(), span::<u8>::empty());
|
||||
sequence++;
|
||||
val comments := comment.finish();
|
||||
val setups := setup.finish();
|
||||
page_of(&mut bytes, 0, 0, sequence, comments.view(), setups.view());
|
||||
sequence++;
|
||||
var decoded: u64 = 0;
|
||||
var k: u64 = 0;
|
||||
while k < count {
|
||||
val end := math::min(k + 3, count);
|
||||
var body := vector::<u8>::new();
|
||||
var lacing := vector::<u8>::new();
|
||||
for j in k..end {
|
||||
if j > 0 {
|
||||
decoded += sizes[pattern[j - 1] ? 1 : 0] / 4 + sizes[pattern[j] ? 1 : 0] / 4;
|
||||
}
|
||||
lace(&mut lacing, packets[j].length);
|
||||
body.extend(packets[j].view());
|
||||
}
|
||||
val last := end == count;
|
||||
page_laced(&mut bytes, last ? 4 : 0, last ? decoded - trimmed : decoded, sequence, lacing.view(), body.view());
|
||||
sequence++;
|
||||
k = end;
|
||||
}
|
||||
return made_stream{ bytes = move(bytes), expected = move(expected), channels = channels };
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#! Contains what the tests of `audio` share: whether a result failed as it
|
||||
#! should, and how far one run of samples strays from another. The Ogg Vorbis
|
||||
#! streams they decode are made in `streams.sakura`.
|
||||
|
||||
import std::math;
|
||||
import malunal::audio;
|
||||
|
||||
# Whether a result failed with an error.
|
||||
def fails<T>(made: result<T, audio_error>, why: audio_error): bool {
|
||||
if made is error(e) {
|
||||
return e == why;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
# How far one run of samples strays from another at most, or 1000 where they're
|
||||
# of other lengths.
|
||||
def apart(a: span<f32>, b: span<f32>): f32 {
|
||||
return 1000 if a.length != b.length;
|
||||
var most: f32 = 0;
|
||||
for i in 0..a.length {
|
||||
most = math::max(most, math::abs(a[i] - b[i]));
|
||||
}
|
||||
return most;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#! Contains the tests of Ogg Vorbis: streams the tests make decoded to the
|
||||
#! frames the spec defines, coupled channels and single, short blocks and long,
|
||||
#! cut where their granule positions say; read in pieces and from the start
|
||||
#! again; and broken streams refused.
|
||||
#!
|
||||
#! Real encoders' files were checked apart from these: 63 of them, from 1 KB to
|
||||
#! 9 MB, decoded as libvorbis decodes them, every sample within 5e-7.
|
||||
|
||||
import std::testing;
|
||||
import malunal::audio;
|
||||
|
||||
@test def decodes_two_coupled_channels_as_the_spec_defines(t: &mut tester): void {
|
||||
val made := made_vorbis(2, 0);
|
||||
val read := audio::decode(made.bytes.view()) else (e) { t.expects(false); return; };
|
||||
t.expects(read.rate == 48000 && read.channels == 2);
|
||||
t.expects(read.frames == made.expected.length / 2);
|
||||
t.expects(apart(read.samples.view(), made.expected.view()) < 1e-4);
|
||||
}
|
||||
|
||||
@test def decodes_one_channel_as_the_spec_defines(t: &mut tester): void {
|
||||
val made := made_vorbis(1, 0);
|
||||
val read := audio::decode(made.bytes.view()) else (e) { t.expects(false); return; };
|
||||
t.expects(read.channels == 1 && read.frames == made.expected.length);
|
||||
t.expects(apart(read.samples.view(), made.expected.view()) < 1e-4);
|
||||
}
|
||||
|
||||
@test def cuts_the_frames_its_last_granule_position_leaves_out(t: &mut tester): void {
|
||||
val made := made_vorbis(2, 37);
|
||||
val read := audio::decode(made.bytes.view()) else (e) { t.expects(false); return; };
|
||||
t.expects(read.frames == made.expected.length / 2);
|
||||
t.expects(apart(read.samples.view(), made.expected.view()) < 1e-4);
|
||||
val opened := audio::vorbis::decoder::open(made.bytes.view()) else (e) { t.expects(false); return; };
|
||||
t.expects(opened.frames == read.frames && opened.rate == 48000 && opened.channels == 2);
|
||||
}
|
||||
|
||||
@test def reads_in_any_pieces_and_from_the_start_again(t: &mut tester): void {
|
||||
val made := made_vorbis(2, 5);
|
||||
var reading := audio::vorbis::decoder::open(made.bytes.view()) else (e) { t.expects(false); return; };
|
||||
for _ in 0..2 {
|
||||
var all := vector::<f32>::new();
|
||||
var piece := vector::<f32>::new();
|
||||
piece.resize(2 * 13);
|
||||
while true {
|
||||
val count := reading.read(piece.slice()) else (e) { t.expects(false); return; };
|
||||
break if count == 0;
|
||||
for i in 0..count * 2 {
|
||||
all.append(piece[i]);
|
||||
}
|
||||
}
|
||||
t.expects(apart(all.view(), made.expected.view()) < 1e-4);
|
||||
reading.rewind();
|
||||
}
|
||||
}
|
||||
|
||||
@test def refuses_broken_streams(t: &mut tester): void {
|
||||
# A byte of a page changed, so its CRC is wrong.
|
||||
var changed := made_vorbis(1, 0);
|
||||
changed.bytes[changed.bytes.length - 3] ^= 0x10;
|
||||
t.expects(fails(audio::decode(changed.bytes.view()), audio_error::malformed));
|
||||
|
||||
# Cut short, partway through its last page.
|
||||
var cut := made_vorbis(1, 0);
|
||||
cut.bytes.truncate();
|
||||
t.expects(fails(audio::decode(cut.bytes.view()), audio_error::malformed));
|
||||
|
||||
# A floor of type 0.
|
||||
val old := made_vorbis(1, 0, true);
|
||||
t.expects(fails(audio::decode(old.bytes.view()), audio_error::unsupported));
|
||||
|
||||
# An Ogg file of something besides Vorbis.
|
||||
var other := vector::<u8>::new();
|
||||
page_of(&mut other, 2, 0, 0, "\x80theora", span::<u8>::empty());
|
||||
t.expects(fails(audio::decode(other.view()), audio_error::unrecognized));
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
#! 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.view()) else (_) { t.expects(false); return; };
|
||||
t.expects(back.rate == 44100 && back.channels == 1 && back.frames == tone.frames);
|
||||
t.expects(apart(back.samples.view(), tone.samples.view()) == 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.view()) else (_) { t.expects(false); return; };
|
||||
t.expects(apart(rounded.samples.view(), tone.samples.view()) < 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.view()) 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.view()) 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.view()) 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.view()) 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.view()) 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.view()) 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.view()) 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.view()), 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.view()), audio_error::malformed));
|
||||
val wrong := wav_of(1, 2, 8000, 12, zeros[:]);
|
||||
t.expects(fails(audio::decode(wrong.view()), 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;
|
||||
}
|
||||
Reference in new issue
Block a user