Files
audio/tests/effects.sakura
T
2026-11-11 11:49:04 -06:00

101 lines
3.9 KiB
Plaintext

#! Contains the tests of effects: voices and buses filtered, low and high pass;
#! a voice sent to a bus of reverb, whose echoes go on after it ends and die
#! away; and the limiter, holding the mix at its ceiling, and passing what's
#! below it as it is, its lookahead late.
import std::math;
import std::testing;
import malunal::audio;
# The root of the mean of the squares of the left of frames, from one to
# another.
def left_level(frames: &vector<f32>, from: u64, to: u64): f32 {
var sum: f64 = 0;
for i in from..to {
sum += (frames[i * 2] * frames[i * 2]) as f64;
}
return math::sqrt(sum / (to - from) as f64) as f32;
}
@test def filters_voices_and_buses(t: &mut tester): void {
var m := mixer::new(48000);
val low := m.add(sound::tone(100, 1, 48000, 1));
val high := m.add(sound::tone(5000, 1, 48000, 1));
# A sine of 1 has a level of 0.707.
val v := m.play(high, playback{ pan = -1, low_pass = 500 });
val muffled := mixed(&mut m, 4800);
t.expects(left_level(&muffled, 2400, 4800) < 0.01);
m.change(v, playback{ pan = -1 });
val clear := mixed(&mut m, 4800);
t.expects(math::abs(left_level(&clear, 2400, 4800) - 0.707) < 0.01);
m.stop(v);
val w := m.play(low, playback{ pan = -1, high_pass = 1000 });
val thin := mixed(&mut m, 4800);
t.expects(left_level(&thin, 2400, 4800) < 0.01);
m.stop(w);
# A bus's filters put on every voice of it.
m.set_bus_effects(2, bus_effects{ low_pass = 500 });
t.expects(m.bus_effects(2).low_pass == 500);
_ = m.play(high, playback{ pan = -1, bus = 2 });
_ = m.play(low, playback{ pan = -1, bus = 2 });
val both := mixed(&mut m, 4800);
t.expects(math::abs(left_level(&both, 2400, 4800) - 0.707) < 0.03);
}
@test def sends_voices_to_a_bus_of_reverb(t: &mut tester): void {
var m := mixer::new(48000);
val impulse: f32[4] = [1, 0, 0, 0];
val click := m.add(sound::of(48000, 1, impulse[:]));
m.set_bus_effects(1, bus_effects{ reverb = reverb{ wet = 1, dry = 0, room = 0.8 } });
t.expects(m.bus_effects(1).reverb.wet == 1);
_ = m.play(click, playback{ pan = -1, send = 1, send_bus = 1 });
val heard := mixed(&mut m, 48000);
# The click, then its echoes, after it ended, dying away.
t.expects(heard[0] == 1 && m.voice_count() == 0);
val early := left_level(&heard, 2000, 6000);
val late := left_level(&heard, 40000, 44000);
t.expects(early > 0.001 && late < early / 4 && late > 0);
# Unsent, a voice has no echoes.
var plain := mixer::new(48000);
val again := plain.add(sound::of(48000, 1, impulse[:]));
plain.set_bus_effects(1, bus_effects{ reverb = reverb{ wet = 1, dry = 0 } });
_ = plain.play(again, playback{ pan = -1 });
val dry := mixed(&mut plain, 6000);
t.expects(left_level(&dry, 2000, 6000) == 0);
}
@test def limits_the_mix_to_its_ceiling(t: &mut tester): void {
var m := mixer::new(48000);
val loud := m.add(sound::tone(200, 1, 48000, 0.6));
m.set_limiter(limiter{ on = true, ceiling = 0.9, lookahead = 0.001 });
t.expects(m.limiting.on && m.limiting.ceiling == 0.9);
for _ in 0..3 {
_ = m.play(loud, playback{ pan = -1 });
}
# Three of 0.6 would reach 1.8: none past 0.9, and near it at the peaks.
val out := mixed(&mut m, 9600);
var most: f32 = 0;
for i in 0..9600u {
most = math::max(most, math::abs(out[i * 2]));
}
t.expects(most <= 0.9 + 1e-5 && most > 0.85);
# What's below the ceiling is as it was, 48 frames late.
var calm := mixer::new(48000);
val soft := calm.add(sound::tone(200, 1, 48000, 0.5));
calm.set_limiter(limiter{ on = true, ceiling = 0.9, lookahead = 0.001 });
_ = calm.play(soft, playback{ pan = -1 });
val late := mixed(&mut calm, 4800);
var unlimited := mixer::new(48000);
_ = unlimited.play(unlimited.add(sound::tone(200, 1, 48000, 0.5)), playback{ pan = -1 });
val plain := mixed(&mut unlimited, 4800);
var worst: f32 = 0;
for i in 48..4800u {
worst = math::max(worst, math::abs(late[i * 2] - plain[(i - 48) * 2]));
}
t.expects(worst < 1e-6 && late[0] == 0);
}