Files
siegeandClaude Opus 5.5 806835a0f9 M3: tbg_audio — original music/SFX from the ROM via the game's own SPC driver
- external/snes_spc: blargg's snes_spc 0.9.0 (LGPL-2.1), built as a shared
  library so it stays separately linkable
- src/audio: 65816 interpreter running the ROM's bank-88 sound code with the
  APU primitives hooked (op generator), SPC host with the real handshakes,
  Sound API (play_song/sfx/fade/danger/restore, thread-safe command queue,
  render 32 kHz stereo)
- tools/snd_validate: all 64 song op lists byte-identical to snd_build_spc.py;
  renders sample-identical to the validated reference host
- tests/audio_tests (TBG_ROM)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 07:39:51 -05:00

67 lines
2.3 KiB
C++

// Audio smoke tests against the user's ROM (TBG_ROM=path). Exit code = failure count.
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <vector>
#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<std::int16_t>& v) {
double s = 0;
for (std::int16_t x : v) s += static_cast<double>(x) * x;
return std::sqrt(s / static_cast<double>(v.size()));
}
std::vector<std::int16_t> render(tbg::audio::Sound& snd, int seconds) {
std::vector<std::int16_t> out(static_cast<std::size_t>(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<std::uint8_t> rom{std::istreambuf_iterator<char>(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;
}