- 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>
138 lines
5.9 KiB
C++
138 lines
5.9 KiB
C++
// Dev tool: checks the C++ sound op generator against re/tools/snd_build_spc.py and renders songs.
|
|
// usage: snd_validate ROM REFDIR [WAVDIR]
|
|
// REFDIR/song_XX.txt = `snd_build_spc.py ROM --song 0xXX --ops ...` (boot + cmd $13); missing files are skipped.
|
|
// With WAVDIR, every song is also rendered for 10 s through tbg::audio::Sound to WAVDIR/song_XX.wav.
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "audio/sound.hpp"
|
|
#include "audio/sound_ops.hpp"
|
|
|
|
namespace {
|
|
|
|
std::vector<std::uint8_t> slurp(const std::filesystem::path& p) {
|
|
std::ifstream f(p, std::ios::binary);
|
|
return {std::istreambuf_iterator<char>(f), {}};
|
|
}
|
|
|
|
void write_wav(const std::filesystem::path& path, const std::vector<std::int16_t>& s) {
|
|
std::ofstream f(path, std::ios::binary);
|
|
auto u32 = [&](std::uint32_t v) { f.write(reinterpret_cast<const char*>(&v), 4); };
|
|
auto u16 = [&](std::uint16_t v) { f.write(reinterpret_cast<const char*>(&v), 2); };
|
|
f.write("RIFF", 4);
|
|
u32(36 + static_cast<std::uint32_t>(s.size() * 2));
|
|
f.write("WAVEfmt ", 8);
|
|
u32(16), u16(1), u16(2), u32(32000), u32(32000 * 4), u16(4), u16(16);
|
|
f.write("data", 4);
|
|
u32(static_cast<std::uint32_t>(s.size() * 2));
|
|
f.write(reinterpret_cast<const char*>(s.data()), static_cast<std::streamsize>(s.size() * 2));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 3) {
|
|
std::fprintf(stderr, "usage: %s ROM REFDIR [WAVDIR]\n", argv[0]);
|
|
return 2;
|
|
}
|
|
const auto rom = slurp(argv[1]);
|
|
const std::filesystem::path ref = argv[2];
|
|
int same = 0, differ = 0, skipped = 0;
|
|
for (int song = 0; song < 0x40; ++song) {
|
|
char name[32];
|
|
std::snprintf(name, sizeof name, "song_%02X.txt", song);
|
|
const auto want = slurp(ref / name);
|
|
std::string got;
|
|
try {
|
|
tbg::audio::SoundSim sim(rom);
|
|
sim.boot();
|
|
sim.call(0x13, 0, static_cast<std::uint32_t>(song));
|
|
got = tbg::audio::format_ops(sim.ops());
|
|
} catch (const tbg::audio::Halt& e) {
|
|
got = std::string("HALT ") + e.what();
|
|
}
|
|
if (want.empty()) {
|
|
++skipped;
|
|
if (got.rfind("HALT", 0) != 0) std::printf("song %02X: no reference but C++ produced ops\n", song);
|
|
continue;
|
|
}
|
|
if (std::string(want.begin(), want.end()) == got) {
|
|
++same;
|
|
} else {
|
|
++differ;
|
|
std::printf("song %02X: op lists differ (%zu vs %zu bytes)\n", song, want.size(), got.size());
|
|
}
|
|
}
|
|
// A scene-like sequence: set switches (logo, title, select, map, stage, power sets) then SFX.
|
|
if (const auto want = slurp(ref / "seq.txt"); !want.empty()) {
|
|
tbg::audio::SoundSim sim(rom);
|
|
sim.boot();
|
|
for (std::uint32_t s : {0x24u, 0x20u, 0x11u, 0x17u, 0x02u, 0x0Eu, 0x1Bu, 0x33u, 0x02u}) sim.call(0x13, 0, s);
|
|
for (std::uint32_t e : {0x01u, 0x15u, 0x05u}) sim.call(0x14, 0, e);
|
|
const bool ok = std::string(want.begin(), want.end()) == tbg::audio::format_ops(sim.ops());
|
|
std::printf("sequence (9 songs + 3 SFX): %s\n", ok ? "identical" : "DIFFERS");
|
|
if (ok) ++same; else ++differ;
|
|
}
|
|
std::printf("op lists: %d identical, %d differ, %d without reference\n", same, differ, skipped);
|
|
|
|
// Sample-exact check of the host against snd_play's renders (re/runs/snd/songs, made without the filter).
|
|
for (int song : {0x02, 0x20, 0x24}) {
|
|
char name[32];
|
|
std::snprintf(name, sizeof name, "song_%02X.wav", song);
|
|
const auto ref_wav = slurp(ref.parent_path() / "songs" / name);
|
|
if (ref_wav.size() < 44 + 4 * 32000) continue;
|
|
tbg::audio::Sound snd(rom);
|
|
snd.set_filter(false);
|
|
snd.play_song(song);
|
|
std::vector<std::int16_t> got(2 * 32000 * 5);
|
|
for (std::size_t off = 0; off < got.size(); off += 2 * 640) snd.render(got.data() + off, 640);
|
|
std::size_t equal = 0, n = std::min(got.size(), (ref_wav.size() - 44) / 2);
|
|
for (std::size_t i = 0; i < n; ++i) {
|
|
const auto r = static_cast<std::int16_t>(ref_wav[44 + 2 * i] | ref_wav[45 + 2 * i] << 8);
|
|
equal += r == got[i];
|
|
}
|
|
std::printf("song %02X: %zu / %zu samples identical to snd_play (first 5 s)\n", song, equal, n);
|
|
}
|
|
|
|
if (argc > 3) {
|
|
const std::filesystem::path out = argv[3];
|
|
std::filesystem::create_directories(out);
|
|
tbg::audio::Sound snd(rom);
|
|
std::vector<std::int16_t> buf(2 * 640);
|
|
for (int song = 0; song < 0x40; ++song) {
|
|
snd.stop();
|
|
snd.render(buf.data(), 640);
|
|
snd.play_song(song);
|
|
std::vector<std::int16_t> wav;
|
|
double sum = 0;
|
|
double upload = 0;
|
|
for (int block = 0; block < 500; ++block) { // 500 x 640 frames = 10 s
|
|
snd.render(buf.data(), 640);
|
|
if (block == 0) upload = snd.last_command_ms();
|
|
wav.insert(wav.end(), buf.begin(), buf.end());
|
|
for (std::int16_t v : buf) sum += static_cast<double>(v) * v;
|
|
}
|
|
char name[32];
|
|
std::snprintf(name, sizeof name, "song_%02X.wav", song);
|
|
write_wav(out / name, wav);
|
|
std::printf("song %02X: upload+start %.1f ms, rms %.0f\n", song, upload, std::sqrt(sum / static_cast<double>(wav.size())));
|
|
}
|
|
// SFX over the battle music (song $02 = Mirurun's stage).
|
|
snd.play_song(0x02);
|
|
std::vector<std::int16_t> wav;
|
|
for (int block = 0; block < 400; ++block) {
|
|
if (block % 40 == 20) snd.sfx(std::vector<int>{0x01, 0x03, 0x15, 0x14, 0x04, 0x05, 0x07, 0x0E, 0x0A, 0x11}[static_cast<std::size_t>(block / 40)]);
|
|
snd.render(buf.data(), 640);
|
|
wav.insert(wav.end(), buf.begin(), buf.end());
|
|
}
|
|
write_wav(out / "sfx_over_02.wav", wav);
|
|
}
|
|
return differ == 0 ? 0 : 1;
|
|
}
|