Synchronized hit-note WAV playing timing

This commit is contained in:
Gordon Weeks 2026-01-31 14:58:12 -08:00
parent 727fd97f39
commit a0fab24f28

View File

@ -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) static size_t pick_track_by_note_count(const Song& song)
{ {
size_t best = 0; size_t best = 0;
@ -255,6 +262,7 @@ public:
std::vector<Glyph> chart; std::vector<Glyph> chart;
std::vector<Glyph*> spawned; std::vector<Glyph*> spawned;
std::unordered_set<Glyph*> completed_notes; std::unordered_set<Glyph*> completed_notes;
std::vector<PendingSound> pending_sounds;
float song_time = 0.0f; float song_time = 0.0f;
float chart_time_offset = 0.0f; float chart_time_offset = 0.0f;
int score = 0; int score = 0;
@ -293,6 +301,7 @@ public:
dev_auto_hit_mode = false; dev_auto_hit_mode = false;
spawned.clear(); spawned.clear();
completed_notes.clear(); completed_notes.clear();
pending_sounds.clear();
for (int i = 0; i < LANE_COUNT; i++) for (int i = 0; i < LANE_COUNT; i++)
{ {
press_flash_timer[i] = 0.0f; press_flash_timer[i] = 0.0f;
@ -446,9 +455,9 @@ public:
hit_flash_timer[n->lane] = PRESS_FLASH_DURATION; hit_flash_timer[n->lane] = PRESS_FLASH_DURATION;
spawned.erase(it); spawned.erase(it);
completed_notes.insert(n); 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]) 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); float y_n = glyph_y(*n);
for (auto it2 = spawned.begin(); it2 != spawned.end();) for (auto it2 = spawned.begin(); it2 != spawned.end();)
{ {
@ -457,6 +466,9 @@ public:
fabsf(glyph_y(*other) - y_n) <= SIMULTANEOUS_NOTE_Y_TOLERANCE) fabsf(glyph_y(*other) - y_n) <= SIMULTANEOUS_NOTE_Y_TOLERANCE)
{ {
completed_notes.insert(other); 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); it2 = spawned.erase(it2);
} }
else else
@ -504,6 +516,19 @@ public:
song_time += delta_time; 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; float last_note_time = 0.0f;
for (const auto& n : chart) for (const auto& n : chart)
{ {