159 lines
6.2 KiB
Plaintext
159 lines
6.2 KiB
Plaintext
#! 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)));
|
||
|
|
}
|