#pragma once #include "assets/music_bank.hpp" #include "assets/sound_bank.hpp" #include "audio/opl3_synth.hpp" #include "audio/sf_synth.hpp" #include "audio/xmidi_player.hpp" #include "core/config.hpp" #include "core/math.hpp" #include "core/vfs.hpp" #include #include #include #include #include #include namespace top_octane::audio { enum class MusicTrackId { TitleIntro = 0, // TINTRO2 Game1 = 1, // TGAME1 Game2 = 2, // TGAME2 Game3 = 3, // TGAME3 Game4 = 4, // TGAME4 Menu = 5, // Dedicated Menu loop (downbeat of TINTRO2 / optional tmenu.wav) }; class AudioEngine { public: static constexpr int kSampleRate = 48000; static constexpr int kMaxSfxVoices = 32; static constexpr int kMaxSameSampleVoices = 3; static constexpr int kCmdQueueSize = 256; AudioEngine() = default; ~AudioEngine(); AudioEngine(const AudioEngine&) = delete; AudioEngine& operator=(const AudioEngine&) = delete; // Loads assets (SOUND.DAT, MUSIC.DAT, SAMPLE.OPL, BULLFROG.SBK) & opens SDL audio if open_device==true bool init(const Vfs& vfs, const GameConfig& cfg, bool open_device = true); void shutdown(); void apply_config(const GameConfig& cfg, bool window_focused = true); void play_music(MusicTrackId track, bool loop = true); void stop_music(); // Gameplay / intro SFX triggers (thread-safe lock-free push) void play_sfx(const std::string& wav_name, float gain = 1.0f, float pitch = 1.0f, float pan = 0.0f, bool is_intro_bank = false, float min_interval_sec = 0.05f); void play_sfx_3d(const std::string& wav_name, const Vec3& src_pos, const Vec3& listener_pos, const Vec3& listener_right, float gain = 1.0f, float pitch = 1.0f, float min_interval_sec = 0.05f); // Continuous player engine pitch/volume modulation void set_engine_state(bool active, float speed_ratio, bool boosting, float scrape_amount); // Offline render hook for deterministic unit testing void mix_offline(float* stereo_out, int frames); const assets::SoundBank& sound_bank() const { return sound_bank_; } const assets::OplTimbreBank& opl_bank() const { return opl_bank_; } const assets::SoundFontBank& sf_bank() const { return sf_bank_; } const assets::XmiSequence& xmi_sequence(MusicBackend b, MusicTrackId t) const; private: enum class CmdType : uint8_t { PlaySfx = 0, PlayMusic, StopMusic, SetBackend, }; struct AudioCommand { CmdType type = CmdType::PlaySfx; int sample_idx = -1; bool intro_bank = false; float gain = 1.0f; float pitch = 1.0f; float pan = 0.0f; float min_interval_sec = 0.05f; MusicTrackId track = MusicTrackId::Game1; bool loop = true; MusicBackend backend = MusicBackend::Opl3; }; struct SfxVoice { bool active = false; const assets::SoundSample* sample = nullptr; int sample_idx = -1; bool intro_bank = false; uint64_t start_seq = 0; double pos = 0.0; double step = 1.0; float gain_l = 0.0f; float gain_r = 0.0f; }; bool push_command(const AudioCommand& cmd); void drain_commands(); void start_active_track(); void render_internal(float* stereo_out, int frames); int find_sfx_index(const std::string& name, bool intro_bank) const; static void sdl_audio_callback(void* userdata, uint8_t* stream, int len); SDL_AudioDeviceID dev_id_ = 0; bool initialized_ = false; assets::SoundBank sound_bank_; assets::MusicArchive music_archive_; assets::OplTimbreBank opl_bank_; assets::SoundFontBank sf_bank_; assets::SoundFontBank custom_sf_bank_; std::string loaded_custom_sf_path_; bool has_custom_sf_ = false; // Pre-parsed sequences for 2 banks (Adlib .XMI and AWE32 .WTB) x 5 songs std::array opl_seqs_{}; std::array wt_seqs_{}; std::array custom_wav_tracks_{}; Opl3Synth opl_synth_; SfSynth sf_synth_; XmidiPlayer xmi_player_; MusicBackend active_backend_ = MusicBackend::Opl3; MusicBackend requested_backend_ = MusicBackend::Opl3; MusicTrackId active_track_ = MusicTrackId::TitleIntro; bool active_loop_ = true; double custom_music_pos_ = 0.0; bool custom_music_playing_ = false; std::array sfx_voices_{}; uint64_t mix_sample_clock_ = static_cast(kSampleRate) * 60ull; uint64_t voice_seq_ = 0; std::array last_play_sample_gameplay_{}; std::array last_play_sample_intro_{}; const assets::SoundSample* engine_sample_ = nullptr; const assets::SoundSample* scrape_sample_ = nullptr; double engine_pos_ = 0.0; double scrape_pos_ = 0.0; // Lock-free SPSC command ring buffer (game thread -> audio thread) std::array cmd_ring_{}; std::atomic cmd_head_{0}; std::atomic cmd_tail_{0}; // Atomic mix parameters std::atomic master_vol_{0.85f}; std::atomic sfx_vol_{0.80f}; std::atomic music_vol_{0.75f}; std::atomic muted_{false}; std::atomic engine_active_{false}; std::atomic engine_speed_ratio_{0.0f}; std::atomic engine_boosting_{false}; std::atomic scrape_amount_{0.0f}; }; } // namespace top_octane::audio