Files
top-octane/tests/test_audio.cpp
T

280 lines
12 KiB
C++
Raw Normal View History

2026-11-30 23:22:00 +02:00
#include "assets/music_bank.hpp"
2026-11-30 22:31:51 +02:00
#include "audio/audio_engine.hpp"
2026-11-30 23:22:00 +02:00
#include "audio/opl3_synth.hpp"
#include "audio/sf_synth.hpp"
2026-11-30 22:31:51 +02:00
#include "core/config.hpp"
#include "core/vfs.hpp"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace top_octane;
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::cerr << "FAIL [" << __FILE__ << ":" << __LINE__ << "]: " #cond << "\n"; \
return 1; \
} \
} while (0)
int main() {
Vfs vfs;
CHECK(vfs.init(HIOCTANE_DEFAULT_DATA_DIR));
GameConfig cfg;
audio::AudioEngine eng;
CHECK(eng.init(vfs, cfg, /*open_device=*/false));
struct BufferStats {
float peak = 0.0f;
float rms = 0.0f;
bool finite_ok = true;
};
auto measure_buffer = [](const std::vector<float>& buf, const char* label) -> BufferStats {
BufferStats st;
double energy = 0.0;
for (float s : buf) {
if (!std::isfinite(s) || std::abs(s) > 1.05f) {
std::cerr << "Bad sample in " << label << ": " << s << "\n";
st.finite_ok = false;
return st;
}
st.peak = std::max(st.peak, std::abs(s));
energy += static_cast<double>(s) * s;
}
st.rms = static_cast<float>(std::sqrt(energy / static_cast<double>(buf.size())));
std::cout << " " << label << ": peak=" << st.peak << " rms=" << st.rms << "\n";
return st;
};
constexpr int kRenderFrames = 48000 * 4; // 4.0 seconds stereo @ 48 kHz (covers lead-in ticks)
std::vector<float> pcm(kRenderFrames * 2, 0.0f);
// 1. OPL3 Backend (SAMPLE.OPL + TGAME1.XMI)
eng.play_music(audio::MusicTrackId::Game1, true);
eng.mix_offline(pcm.data(), kRenderFrames);
BufferStats opl_stats = measure_buffer(pcm, "OPL3 Music (TGAME1.XMI)");
CHECK(opl_stats.finite_ok && opl_stats.peak > 0.01f && opl_stats.rms >= 0.025f);
// 2. SoundFont Backend (BULLFROG.SBK + TGAME1.WTB)
cfg.music_backend = MusicBackend::SoundFont;
eng.apply_config(cfg, true);
eng.mix_offline(pcm.data(), kRenderFrames);
BufferStats sf_stats = measure_buffer(pcm, "SoundFont Music (TGAME1.WTB + BULLFROG.SBK)");
CHECK(sf_stats.finite_ok && sf_stats.peak > 0.01f && sf_stats.rms >= 0.015f);
// 3. 3D Spatial SFX + Continuous Engine Mixing + Music Balance Ratio
eng.stop_music();
eng.set_engine_state(true, 0.85f, true, 0.3f);
eng.play_sfx_3d("MISSILE.WAV", Vec3(5, 0, 0), Vec3(0, 0, 0), Vec3(1, 0, 0), 0.48f, 1.0f, 0.18f);
eng.play_sfx("EXPLODE.WAV", 0.60f, 1.0f, -0.5f, false, 0.10f);
std::vector<float> sfx_pcm(24000 * 2, 0.0f);
eng.mix_offline(sfx_pcm.data(), 24000);
BufferStats sfx_stats = measure_buffer(sfx_pcm, "3D SFX + Engine Loop");
CHECK(sfx_stats.finite_ok && sfx_stats.peak > 0.01f && sfx_stats.rms > 0.002f);
CHECK(sfx_stats.rms / opl_stats.rms < 6.0f);
CHECK(sfx_stats.rms / sf_stats.rms < 6.0f);
// 4. Bank 3 WAV loudness normalization spread check
{
float min_rms = 1e6f;
float max_rms = 0.0f;
float max_peak = 0.0f;
int active_count = 0;
for (const auto& smp : eng.sound_bank().gameplay_sfx) {
if (smp.name == "NULL.WAV" || smp.pcm_mono.size() < 512) continue;
++active_count;
CHECK(smp.peak <= 0.90f);
CHECK(smp.rms > 0.05f);
min_rms = std::min(min_rms, smp.rms);
max_rms = std::max(max_rms, smp.rms);
max_peak = std::max(max_peak, smp.peak);
}
float spread = max_rms / std::max(min_rms, 1e-6f);
std::cout << " Bank 3 WAV stats: count=" << active_count
<< " min_rms=" << min_rms << " max_rms=" << max_rms
<< " ratio=" << spread << " max_peak=" << max_peak << "\n";
CHECK(active_count >= 25);
CHECK(spread < 2.8f);
}
// 5. Spam & SFX bus limiter stress test (cooled slices + uncapped single-buffer burst)
{
eng.stop_music();
eng.set_engine_state(true, 0.85f, true, 0.0f);
std::vector<float> stress_pcm;
stress_pcm.reserve((10 * 480 + 24000) * 2);
std::vector<float> slice(480 * 2, 0.0f);
for (int s = 0; s < 10; ++s) {
for (int k = 0; k < 6; ++k) {
eng.play_sfx("MINISHOT.WAV", 0.35f, 1.0f, 0.0f, false, 0.08f);
}
for (int k = 0; k < 3; ++k) {
eng.play_sfx("EXPLODE.WAV", 0.60f, 1.0f, 0.0f, false, 0.10f);
}
eng.mix_offline(slice.data(), 480);
stress_pcm.insert(stress_pcm.end(), slice.begin(), slice.end());
}
std::vector<float> tail(24000 * 2, 0.0f);
eng.mix_offline(tail.data(), 24000);
stress_pcm.insert(stress_pcm.end(), tail.begin(), tail.end());
BufferStats cooled_stats = measure_buffer(stress_pcm, "60x Minishot + 30x Explode Stress");
CHECK(cooled_stats.finite_ok && cooled_stats.peak <= 0.99f && cooled_stats.rms < 0.45f);
std::vector<float> burst(24000 * 2, 0.0f);
for (int k = 0; k < 30; ++k) {
eng.play_sfx("EXPLODE.WAV", 0.60f, 1.0f, 0.0f, false, 0.0f);
}
eng.mix_offline(burst.data(), 24000);
BufferStats burst_stats = measure_buffer(burst, "30x Uncapped Explode Voice-Cap Burst");
CHECK(burst_stats.finite_ok && burst_stats.peak <= 0.99f && burst_stats.rms < 0.45f);
}
2026-11-30 23:22:00 +02:00
// 6. BULLFROG.SBK generator accuracy & synth dynamics / filter / 5-track checks
{
assets::SoundFontBank sbk;
CHECK(sbk.load(vfs, "SOUND/BULLFROG.SBK"));
CHECK(!sbk.is_sf2);
const auto* pr2 = sbk.find_preset(0, 2); // 22LOOP (op55=7200 -> -1200 cents)
CHECK(pr2 && !pr2->zones.empty());
CHECK(pr2->zones[0].sample_pitch_cents == -1200);
const auto* pr117 = sbk.find_preset(0, 117); // AgogoLoTone (op36=2034, no op37, op38=1192)
CHECK(pr117 && !pr117->zones.empty());
CHECK(pr117->zones[0].vol_decay_sec > 3.0f && pr117->zones[0].vol_decay_sec < 3.5f);
CHECK(pr117->zones[0].vol_sustain_db > 85.0f);
CHECK(pr117->zones[0].vol_release_sec > 1.8f && pr117->zones[0].vol_release_sec < 2.2f);
const auto* pr0 = sbk.find_preset(0, 0); // basic kit -> HCL909 key 42
CHECK(pr0);
const assets::SoundFontZone* hcl = nullptr;
for (const auto& z : pr0->zones) {
if (42 >= z.key_lo && 42 <= z.key_hi) {
hcl = &z;
break;
}
}
CHECK(hcl != nullptr);
CHECK(hcl->attenuation_db > 2.4f && hcl->attenuation_db < 2.9f);
CHECK(hcl->pan_tenths_pct == 16);
float prev_fc = 100000.0f;
for (uint16_t p = 119; p <= 127; ++p) {
const auto* pr = sbk.find_preset(0, p);
CHECK(pr && !pr->zones.empty());
const auto& z = pr->zones[0];
CHECK(z.has_filter && z.filter_q_db > 10.0f && z.chorus_send > 0.9f);
CHECK(z.filter_fc_hz < prev_fc);
prev_fc = z.filter_fc_hz;
}
CHECK(prev_fc > 850.0f && prev_fc < 980.0f);
// Concave dynamics check on Opl3Synth and SfSynth (vel, CC 7, CC 11)
assets::OplTimbreBank opl_bank;
CHECK(opl_bank.load(vfs, "SOUND/SAMPLE.OPL"));
audio::Opl3Synth opl_dyn(&opl_bank, 48000);
audio::SfSynth sf_dyn(&sbk, &opl_bank, 48000);
auto render_single_rms = [](audio::IMidiSynth& syn, uint8_t ch, uint8_t prog,
bool user_bank, uint8_t vel, uint8_t cc7, uint8_t cc11) -> float {
syn.reset();
syn.program_change(ch, prog);
if (user_bank) syn.control_change(ch, 119, 1);
syn.control_change(ch, 7, cc7);
syn.control_change(ch, 11, cc11);
syn.note_on(ch, 48, vel);
std::vector<float> buf(12000 * 2, 0.0f); // 0.25s
syn.render(buf.data(), 12000);
double e = 0.0;
for (float s : buf) e += static_cast<double>(s) * s;
return static_cast<float>(std::sqrt(e / static_cast<double>(buf.size())));
};
auto check_dyn = [&](audio::IMidiSynth& syn, uint8_t prog, bool user_bank, const char* label) {
float full = render_single_rms(syn, 0, prog, user_bank, 127, 127, 127);
float half_v = render_single_rms(syn, 0, prog, user_bank, 64, 127, 127);
float half_c7 = render_single_rms(syn, 0, prog, user_bank, 127, 64, 127);
float half_c11 = render_single_rms(syn, 0, prog, user_bank, 127, 127, 64);
std::cout << " Dynamics [" << label << "]: full=" << full
<< " vel64=" << half_v << " cc7_64=" << half_c7 << " cc11_64=" << half_c11 << "\n";
if (!(full > 0.01f && half_v / full < 0.35f &&
half_c7 / full < 0.38f && half_c11 / full < 0.38f)) {
return false;
}
return true;
};
CHECK(check_dyn(opl_dyn, 38, false, "OPL3 prog38"));
CHECK(check_dyn(sf_dyn, 33, false, "SF GM prog33"));
CHECK(check_dyn(sf_dyn, 118, true, "SF BULLFROG prog118"));
// Acid filter spectral brightness contrast: prog 119 (8 kHz) vs prog 127 (912 Hz)
auto measure_hf_ratio = [&](uint8_t prog) -> float {
sf_dyn.reset();
sf_dyn.control_change(0, 119, 1);
sf_dyn.program_change(0, prog);
sf_dyn.note_on(0, 36, 120);
std::vector<float> buf(12000 * 2, 0.0f);
sf_dyn.render(buf.data(), 12000);
double sig_e = 0.0, diff_e = 0.0;
for (size_t i = 2; i < buf.size(); i += 2) {
float d = buf[i] - buf[i - 2];
sig_e += static_cast<double>(buf[i]) * buf[i];
diff_e += static_cast<double>(d) * d;
}
return static_cast<float>(std::sqrt(diff_e / std::max(sig_e, 1e-12)));
};
float hf_119 = measure_hf_ratio(119);
float hf_127 = measure_hf_ratio(127);
std::cout << " Acid filter HF ratio: prog119=" << hf_119 << " prog127=" << hf_127 << "\n";
CHECK(hf_119 > hf_127 * 2.5f);
// Render all 5 tracks across both OPL3 and SoundFont backends
constexpr audio::MusicTrackId kTracks[5] = {
audio::MusicTrackId::TitleIntro,
audio::MusicTrackId::Game1,
audio::MusicTrackId::Game2,
audio::MusicTrackId::Game3,
audio::MusicTrackId::Game4
};
std::vector<float> trk_pcm(48000 * 3 * 2, 0.0f);
for (int b = 0; b < 2; ++b) {
cfg.music_backend = (b == 0) ? MusicBackend::Opl3 : MusicBackend::SoundFont;
eng.apply_config(cfg, true);
for (int t = 0; t < 5; ++t) {
eng.play_music(kTracks[t], true);
eng.mix_offline(trk_pcm.data(), 48000 * 3);
BufferStats st = measure_buffer(
trk_pcm, (b == 0) ? "All-tracks OPL3" : "All-tracks SoundFont");
CHECK(st.finite_ok && st.peak > 0.01f && st.rms >= 0.008f);
}
// Dedicated Menu track: immediate downbeat energy + CC 116..117 loop wrap past 27s
eng.play_music(audio::MusicTrackId::Menu, true);
std::vector<float> menu_2s(48000 * 2 * 2, 0.0f);
eng.mix_offline(menu_2s.data(), 48000 * 2);
BufferStats menu_start = measure_buffer(
menu_2s, (b == 0) ? "Menu immediate 2s OPL3" : "Menu immediate 2s SoundFont");
CHECK(menu_start.finite_ok && menu_start.peak <= 1.0f &&
menu_start.rms >= ((b == 0) ? 0.03f : 0.025f));
std::vector<float> menu_1s(48000 * 2, 0.0f);
for (int sec = 2; sec < 29; ++sec) {
eng.mix_offline(menu_1s.data(), 48000);
}
BufferStats menu_loop = measure_buffer(
menu_1s, (b == 0) ? "Menu looped 29s OPL3" : "Menu looped 29s SoundFont");
CHECK(menu_loop.finite_ok && menu_loop.peak <= 1.0f &&
menu_loop.rms >= ((b == 0) ? 0.03f : 0.025f));
2026-11-30 23:22:00 +02:00
}
eng.stop_music();
}
2026-11-30 22:31:51 +02:00
std::cout << "[PASS] AudioEngine, OPL3 FM, SoundFont SBK, Normalization & Limiter validated\n";
return 0;
}