// Audio smoke tests against the user's ROM (TBG_ROM=path). Exit code = failure count. #include #include #include #include #include #include #include "audio/sound.hpp" #include "audio/sound_ops.hpp" namespace { int g_failures = 0; #define CHECK(cond) \ do { \ if (!(cond)) { \ std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ ++g_failures; \ } \ } while (0) double rms(const std::vector& v) { double s = 0; for (std::int16_t x : v) s += static_cast(x) * x; return std::sqrt(s / static_cast(v.size())); } std::vector render(tbg::audio::Sound& snd, int seconds) { std::vector out(static_cast(2 * 32000 * seconds)); for (std::size_t off = 0; off < out.size(); off += 2 * 800) snd.render(out.data() + off, 800); return out; } } // namespace int main() { const char* path = std::getenv("TBG_ROM"); if (!path) { std::printf("audio tests skipped (set TBG_ROM)\n"); return 0; } std::ifstream f(path, std::ios::binary); const std::vector rom{std::istreambuf_iterator(f), {}}; CHECK(tbg::audio::Sound::supported(rom)); if (!tbg::audio::Sound::supported(rom)) return g_failures; // The driver boots and the boot sequence is the IPL upload + init (88:84B9, cmd $00/$01/$19). tbg::audio::SoundSim sim(rom); sim.boot(); CHECK(!sim.ops().empty() && sim.ops().front().kind == tbg::audio::ApuOp::Kind::Ipl); tbg::audio::Sound snd(rom); CHECK(rms(render(snd, 1)) < 1.0); // silence before any song snd.play_song(0x02); // Mirurun's stage theme const auto music = render(snd, 3); CHECK(rms(music) > 300); snd.sfx(0x05); // 4-line clear over the music const auto with_sfx = render(snd, 1); CHECK(rms(with_sfx) > 300); snd.stop(); render(snd, 3); CHECK(rms(render(snd, 1)) < 50); // faded out if (g_failures == 0) std::printf("all audio tests passed\n"); return g_failures; }