Add player name entry, dedicated menu music, and original Bullfrog menu fonts
This commit is contained in:
1 parent
c017d27b2a
commit
6c24f715a0
13 files changed
+835
-199
No files matched your search
+33
-28
@@ -18,6 +18,7 @@ int map_track_slot(MusicTrackId id) {
|
||||
case MusicTrackId::Game4: return 2;
|
||||
case MusicTrackId::TitleIntro: return 3;
|
||||
case MusicTrackId::Game3: return 4;
|
||||
case MusicTrackId::Menu: return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -29,6 +30,7 @@ const char* track_stem(MusicTrackId id) {
|
||||
case MusicTrackId::Game2: return "tgame2";
|
||||
case MusicTrackId::Game3: return "tgame3";
|
||||
case MusicTrackId::Game4: return "tgame4";
|
||||
case MusicTrackId::Menu: return "tmenu";
|
||||
}
|
||||
return "tgame1";
|
||||
}
|
||||
@@ -77,8 +79,8 @@ bool AudioEngine::init(const Vfs& vfs, const GameConfig& cfg, bool open_device)
|
||||
}
|
||||
}
|
||||
|
||||
// Optional custom WAV music tracks in <pref_dir>/music/*.wav
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
// Optional custom WAV music tracks in <pref_dir>/music/*.wav (including optional tmenu.wav)
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
MusicTrackId tid = static_cast<MusicTrackId>(i);
|
||||
std::filesystem::path wp = vfs.pref_dir() / "music" / (std::string(track_stem(tid)) + ".wav");
|
||||
std::vector<uint8_t> wdata;
|
||||
@@ -86,6 +88,11 @@ bool AudioEngine::init(const Vfs& vfs, const GameConfig& cfg, bool open_device)
|
||||
assets::decode_wav_bytes(wdata.data(), wdata.size(), custom_wav_tracks_[i]);
|
||||
}
|
||||
}
|
||||
if (custom_wav_tracks_[static_cast<int>(MusicTrackId::Menu)].pcm_mono.empty() &&
|
||||
!custom_wav_tracks_[static_cast<int>(MusicTrackId::TitleIntro)].pcm_mono.empty()) {
|
||||
custom_wav_tracks_[static_cast<int>(MusicTrackId::Menu)] =
|
||||
custom_wav_tracks_[static_cast<int>(MusicTrackId::TitleIntro)];
|
||||
}
|
||||
|
||||
opl_synth_.set_timbre_bank(&opl_bank_);
|
||||
opl_synth_.set_sample_rate(kSampleRate);
|
||||
@@ -237,6 +244,26 @@ bool AudioEngine::push_command(const AudioCommand& cmd) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void AudioEngine::start_active_track() {
|
||||
opl_synth_.reset();
|
||||
sf_synth_.reset();
|
||||
custom_music_playing_ = false;
|
||||
custom_music_pos_ = 0.0;
|
||||
|
||||
int idx = std::clamp(static_cast<int>(active_track_), 0, 5);
|
||||
int slot = map_track_slot(active_track_);
|
||||
if (active_backend_ == MusicBackend::Custom && !custom_wav_tracks_[idx].pcm_mono.empty()) {
|
||||
xmi_player_.stop();
|
||||
custom_music_playing_ = true;
|
||||
} else {
|
||||
uint32_t start_tick = (active_track_ == MusicTrackId::Menu) ? 1610u : 0u;
|
||||
const assets::XmiSequence* seq = (active_backend_ == MusicBackend::SoundFont)
|
||||
? &wt_seqs_[slot]
|
||||
: &opl_seqs_[slot];
|
||||
xmi_player_.set_sequence(seq, active_loop_, start_tick);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioEngine::drain_commands() {
|
||||
uint32_t tail = cmd_tail_.load(std::memory_order_relaxed);
|
||||
uint32_t head = cmd_head_.load(std::memory_order_acquire);
|
||||
@@ -298,20 +325,7 @@ void AudioEngine::drain_commands() {
|
||||
case CmdType::PlayMusic: {
|
||||
active_track_ = cmd.track;
|
||||
active_loop_ = cmd.loop;
|
||||
int slot = map_track_slot(cmd.track);
|
||||
opl_synth_.reset();
|
||||
sf_synth_.reset();
|
||||
custom_music_playing_ = false;
|
||||
custom_music_pos_ = 0.0;
|
||||
if (active_backend_ == MusicBackend::Custom &&
|
||||
!custom_wav_tracks_[static_cast<int>(cmd.track)].pcm_mono.empty()) {
|
||||
xmi_player_.stop();
|
||||
custom_music_playing_ = true;
|
||||
} else if (active_backend_ == MusicBackend::SoundFont) {
|
||||
xmi_player_.set_sequence(&wt_seqs_[slot], cmd.loop);
|
||||
} else {
|
||||
xmi_player_.set_sequence(&opl_seqs_[slot], cmd.loop);
|
||||
}
|
||||
start_active_track();
|
||||
break;
|
||||
}
|
||||
case CmdType::StopMusic:
|
||||
@@ -329,17 +343,7 @@ void AudioEngine::drain_commands() {
|
||||
sf_synth_.reset();
|
||||
custom_music_playing_ = false;
|
||||
if (was_playing) {
|
||||
int slot = map_track_slot(active_track_);
|
||||
if (active_backend_ == MusicBackend::Custom &&
|
||||
!custom_wav_tracks_[static_cast<int>(active_track_)].pcm_mono.empty()) {
|
||||
xmi_player_.stop();
|
||||
custom_music_playing_ = true;
|
||||
custom_music_pos_ = 0.0;
|
||||
} else if (active_backend_ == MusicBackend::SoundFont) {
|
||||
xmi_player_.set_sequence(&wt_seqs_[slot], active_loop_);
|
||||
} else {
|
||||
xmi_player_.set_sequence(&opl_seqs_[slot], active_loop_);
|
||||
}
|
||||
start_active_track();
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -368,7 +372,8 @@ void AudioEngine::render_internal(float* stereo_out, int frames) {
|
||||
// 1. Render Music into output chunk
|
||||
if (mvol > 0.001f) {
|
||||
if (custom_music_playing_) {
|
||||
const auto& tr = custom_wav_tracks_[static_cast<int>(active_track_)];
|
||||
int tidx = std::clamp(static_cast<int>(active_track_), 0, 5);
|
||||
const auto& tr = custom_wav_tracks_[tidx];
|
||||
if (!tr.pcm_mono.empty()) {
|
||||
double step = static_cast<double>(tr.sample_rate) / kSampleRate;
|
||||
for (int f = 0; f < cur; ++f) {
|
||||
|
||||
@@ -24,6 +24,7 @@ enum class MusicTrackId {
|
||||
Game2 = 2, // TGAME2
|
||||
Game3 = 3, // TGAME3
|
||||
Game4 = 4, // TGAME4
|
||||
Menu = 5, // Dedicated Menu loop (downbeat of TINTRO2 / optional tmenu.wav)
|
||||
};
|
||||
|
||||
class AudioEngine {
|
||||
@@ -101,6 +102,7 @@ private:
|
||||
|
||||
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);
|
||||
@@ -118,7 +120,7 @@ private:
|
||||
// Pre-parsed sequences for 2 banks (Adlib .XMI and AWE32 .WTB) x 5 songs
|
||||
std::array<assets::XmiSequence, 5> opl_seqs_{};
|
||||
std::array<assets::XmiSequence, 5> wt_seqs_{};
|
||||
std::array<assets::SoundSample, 5> custom_wav_tracks_{};
|
||||
std::array<assets::SoundSample, 6> custom_wav_tracks_{};
|
||||
|
||||
Opl3Synth opl_synth_;
|
||||
SfSynth sf_synth_;
|
||||
|
||||
@@ -9,12 +9,13 @@ void XmidiPlayer::set_sample_rate(int sample_rate) {
|
||||
samples_per_tick_ = static_cast<double>(sample_rate_) / 120.0;
|
||||
}
|
||||
|
||||
void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop) {
|
||||
void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop, uint32_t start_tick) {
|
||||
seq_ = seq;
|
||||
loop_ = loop;
|
||||
playing_ = (seq != nullptr && !seq->events.empty());
|
||||
event_cursor_ = 0;
|
||||
current_tick_ = 0;
|
||||
pending_start_tick_ = (seq && start_tick <= seq->total_ticks) ? start_tick : 0;
|
||||
sample_accum_ = 0.0;
|
||||
samples_per_tick_ = static_cast<double>(sample_rate_) / 120.0;
|
||||
for (auto& n : note_offs_) n = PendingNoteOff{};
|
||||
@@ -24,6 +25,7 @@ void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop) {
|
||||
void XmidiPlayer::stop() {
|
||||
playing_ = false;
|
||||
seq_ = nullptr;
|
||||
pending_start_tick_ = 0;
|
||||
for (auto& n : note_offs_) n = PendingNoteOff{};
|
||||
loop_depth_ = 0;
|
||||
}
|
||||
@@ -31,6 +33,29 @@ void XmidiPlayer::stop() {
|
||||
void XmidiPlayer::process_tick(IMidiSynth& synth) {
|
||||
if (!seq_ || !playing_) return;
|
||||
|
||||
const auto& evs = seq_->events;
|
||||
// Fast-forward controller/program/pitch-bend setup before requested start_tick
|
||||
if (pending_start_tick_ > 0) {
|
||||
while (event_cursor_ < evs.size() && evs[event_cursor_].tick < pending_start_tick_) {
|
||||
const auto& ev = evs[event_cursor_++];
|
||||
if (ev.status == 0xFFu) continue;
|
||||
uint8_t hi = ev.status & 0xF0u;
|
||||
uint8_t ch = ev.status & 0x0Fu;
|
||||
if (hi == 0xB0u) {
|
||||
if (ev.data1 != 116u && ev.data1 != 117u && ev.data1 != 120u && ev.data1 != 123u) {
|
||||
synth.control_change(ch, ev.data1, ev.data2);
|
||||
}
|
||||
} else if (hi == 0xC0u) {
|
||||
synth.program_change(ch, ev.data1);
|
||||
} else if (hi == 0xE0u) {
|
||||
int16_t bend14 = static_cast<int16_t>(((ev.data2 & 0x7Fu) << 7) | (ev.data1 & 0x7Fu)) - 8192;
|
||||
synth.pitch_bend(ch, bend14);
|
||||
}
|
||||
}
|
||||
current_tick_ = pending_start_tick_;
|
||||
pending_start_tick_ = 0;
|
||||
}
|
||||
|
||||
// Fire due note-offs
|
||||
for (auto& n : note_offs_) {
|
||||
if (n.active && current_tick_ >= n.off_tick) {
|
||||
@@ -48,7 +73,6 @@ void XmidiPlayer::process_tick(IMidiSynth& synth) {
|
||||
};
|
||||
|
||||
// Fire events at or before current_tick_
|
||||
const auto& evs = seq_->events;
|
||||
while (event_cursor_ < evs.size() && evs[event_cursor_].tick <= current_tick_) {
|
||||
const auto& ev = evs[event_cursor_++];
|
||||
if (ev.status == 0xFFu) {
|
||||
|
||||
@@ -14,7 +14,7 @@ public:
|
||||
static constexpr int kMaxActiveNotes = 128;
|
||||
|
||||
void set_sample_rate(int sample_rate);
|
||||
void set_sequence(const assets::XmiSequence* seq, bool loop = true);
|
||||
void set_sequence(const assets::XmiSequence* seq, bool loop = true, uint32_t start_tick = 0);
|
||||
void stop();
|
||||
bool is_playing() const { return seq_ != nullptr && playing_; }
|
||||
|
||||
@@ -45,6 +45,7 @@ private:
|
||||
|
||||
size_t event_cursor_ = 0;
|
||||
uint32_t current_tick_ = 0;
|
||||
uint32_t pending_start_tick_ = 0;
|
||||
double samples_per_tick_ = 400.0; // 48000 / 120 Hz default
|
||||
double sample_accum_ = 0.0;
|
||||
|
||||
|
||||
Reference in new issue
Block a user