#! Contains the tests of `mixer`: voices mixed at their gains, panned, ramped #! as they change, ended or looped at their sounds' ends or loop points, #! resampled and pitched, faded, on buses, streamed as they play, and sought. 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::::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 { var out := vector::::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.span) else (e) { t.expects(false); return; }; var whole := audio::decode(made.bytes.span) 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.span, from_sound.span) < 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.span) 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))); } # A sound of one channel, each frame its place over 1000. def ramp(frames: u64, looped: loop_range): sound { var samples := vector::::new(); for i in 0..frames { samples.append(i as f32 / 1000); } return sound{ rate = 48000, channels = 1, samples = move(samples), loop_points = looped }; } @test def loops_between_its_sounds_loop_points(t: &mut tester): void { var m := mixer::new(48000); val id := m.add(ramp(1000, loop_range{ start = 200, end = 600 })); val v := m.play(id, playback{ pan = -1, looping = true }); val out := mixed(&mut m, 1400); t.expects(math::abs(out[2 * 599] - 0.599) < 1e-6 && math::abs(out[2 * 600] - 0.2) < 1e-6); t.expects(math::abs(out[2 * 999] - 0.599) < 1e-6 && math::abs(out[2 * 1000] - 0.2) < 1e-6); t.expects(math::abs(m.position(v) - 200.0 / 48000) < 1e-9); # Not looping, it plays to its end. m.stop(v); val once := m.play(id, playback{ pan = -1 }); val whole := mixed(&mut m, 1001); t.expects(math::abs(whole[2 * 999] - 0.999) < 1e-6 && !m.playing(once)); # Streamed, it loops so too, as the decoded sound does. val comments: strview[2] = ["LOOPSTART=1000", "LOOPEND=5000"]; val made := made_flac(2, 16, every_kind(10, 1024, 2).span, false, false, comments[:]); val streamed := m.add_streamed(made.bytes.span) else (e) { t.expects(false); return; }; val decoded := m.add(audio::decode(made.bytes.span) else (e) { t.expects(false); return; }); m.stop_all(); val a := m.play(streamed, playback{ looping = true }); val from_stream := mixed(&mut m, 13000); m.stop(a); val b := m.play(decoded, playback{ looping = true }); val from_sound := mixed(&mut m, 13000); t.expects(apart(from_stream.span, from_sound.span) == 0); t.expects(from_sound[2 * 5000] == made.expected[2 * 1000] && from_sound[2 * 9000] == made.expected[2 * 1000]); t.expects(math::abs(m.position(b) - 1000.0 / 48000) < 1e-9); } @test def seeks_decoded_and_streamed_voices(t: &mut tester): void { val made := made_flac(2, 16, every_kind(100, 1152, 2).span, false, true, no_comments()); var m := mixer::new(48000); val streamed := m.add_streamed(made.bytes.span) else (e) { t.expects(false); return; }; val decoded := m.add(audio::decode(made.bytes.span) else (e) { t.expects(false); return; }); for &id in [streamed, decoded] { val v := m.play(id); _ = mixed(&mut m, 100); t.expects(m.seek(v, 1.0) is value(_)); val out := mixed(&mut m, 2000); t.expects(apart(out.span, made.expected.span[48000 * 2:4000]) == 0); t.expects(math::abs(m.position(v) - (48000.0 + 2000) / 48000) < 1e-9); m.stop(v); } } @test def delays_voices_and_moves_silent_ones_on_unheard(t: &mut tester): void { var m := mixer::new(48000); val id := m.add(ramp(1000, loop_range{})); # Heard 0.002 s, 96 frames, late. _ = m.play(id, playback{ pan = -1, delay = 0.002 }); val late := mixed(&mut m, 200); t.expects(late[2 * 95] == 0 && late[2 * 96] == 0 && math::abs(late[2 * 97] - 0.001) < 1e-6); m.stop_all(); # Silent, it moves on as it would play, and is heard from there. val v := m.play(id, playback{ pan = -1, silent = true }); val quiet := mixed(&mut m, 300); t.expects(quiet[2 * 299] == 0 && math::abs(m.position(v) - 300.0 / 48000) < 1e-9); m.change(v, playback{ pan = -1 }); val heard := mixed(&mut m, 300); t.expects(math::abs(heard[2 * 299] - 0.599) < 1e-6 && heard[0] < 0.3); }