From a0fab24f28b129e7c41af1c7c2c39a91ded6b3f2 Mon Sep 17 00:00:00 2001 From: Gordon Weeks <627684+gcweeks@users.noreply.github.com> Date: Sat, 31 Jan 2026 14:58:12 -0800 Subject: [PATCH] Synchronized hit-note WAV playing timing --- src/samples/ghhb_game.h | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/samples/ghhb_game.h b/src/samples/ghhb_game.h index 0a87fab..30f2c13 100644 --- a/src/samples/ghhb_game.h +++ b/src/samples/ghhb_game.h @@ -147,6 +147,13 @@ struct Glyph } }; +struct PendingSound +{ + float play_time = 0.0f; + int instrument_slot = 0; + int octave = 0; +}; + static size_t pick_track_by_note_count(const Song& song) { size_t best = 0; @@ -255,6 +262,7 @@ public: std::vector chart; std::vector spawned; std::unordered_set completed_notes; + std::vector pending_sounds; float song_time = 0.0f; float chart_time_offset = 0.0f; int score = 0; @@ -293,6 +301,7 @@ public: dev_auto_hit_mode = false; spawned.clear(); completed_notes.clear(); + pending_sounds.clear(); for (int i = 0; i < LANE_COUNT; i++) { press_flash_timer[i] = 0.0f; @@ -446,9 +455,9 @@ public: hit_flash_timer[n->lane] = PRESS_FLASH_DURATION; spawned.erase(it); completed_notes.insert(n); - printf("note lane: %d, note octave: %d\n", n->lane, n->octave); + float hit_line_time = n->time + chart_time_offset; if (note_sounds_loaded[n->instrument_slot][n->octave]) - PlaySound(note_sounds[n->instrument_slot][n->octave]); + pending_sounds.push_back({hit_line_time, n->instrument_slot, n->octave}); float y_n = glyph_y(*n); for (auto it2 = spawned.begin(); it2 != spawned.end();) { @@ -457,6 +466,9 @@ public: fabsf(glyph_y(*other) - y_n) <= SIMULTANEOUS_NOTE_Y_TOLERANCE) { completed_notes.insert(other); + float other_hit_line_time = other->time + chart_time_offset; + if (note_sounds_loaded[other->instrument_slot][other->octave]) + pending_sounds.push_back({other_hit_line_time, other->instrument_slot, other->octave}); it2 = spawned.erase(it2); } else @@ -504,6 +516,19 @@ public: song_time += delta_time; } + for (auto it = pending_sounds.begin(); it != pending_sounds.end();) + { + if (song_time >= it->play_time) + { + PlaySound(note_sounds[it->instrument_slot][it->octave]); + it = pending_sounds.erase(it); + } + else + { + ++it; + } + } + float last_note_time = 0.0f; for (const auto& n : chart) {