diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-08 11:13:53 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-08 11:13:53 +0100 |
| commit | 392d03c0c05f24be3210a04d9a50cd9714d1e265 (patch) | |
| tree | 3c4985879bdf614536020f2061c299ecf442677f /src/audio/tracker.cc | |
| parent | d2d20763ac61f59187d261bb7d6dedcab525bc54 (diff) | |
refactor(audio): Finalize audio sync, update docs, and clean up test artifacts
- Implemented sample-accurate audio-visual synchronization by using the hardware audio clock as the master time source.
- Ensured tracker updates and visual rendering are slaved to the stable audio clock.
- Corrected to accept and use delta time for sample-accurate event scheduling.
- Updated all relevant tests (, , , , ) to use the new delta time parameter.
- Added function.
- Marked Task #71 as completed in .
- Updated to reflect the audio system's current status.
- Created a handoff document: .
- Removed temporary peak log files (, ).
Diffstat (limited to 'src/audio/tracker.cc')
| -rw-r--r-- | src/audio/tracker.cc | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/audio/tracker.cc b/src/audio/tracker.cc index 2bb4159..42e074d 100644 --- a/src/audio/tracker.cc +++ b/src/audio/tracker.cc @@ -215,19 +215,22 @@ static void trigger_note_event(const TrackerEvent& event, start_offset_samples); } -void tracker_update(float music_time_sec) { +void tracker_update(float music_time_sec, float dt_music_sec) { // Unit-less timing: 1 unit = 4 beats (by convention) const float BEATS_PER_UNIT = 4.0f; const float unit_duration_sec = (BEATS_PER_UNIT / g_tracker_score.bpm) * 60.0f; + const float end_music_time = music_time_sec + dt_music_sec; + const float tempo_scale = synth_get_tempo_scale(); + // Step 1: Process new pattern triggers while (g_last_trigger_idx < g_tracker_score.num_triggers) { const TrackerPatternTrigger& trigger = g_tracker_score.triggers[g_last_trigger_idx]; const float trigger_time_sec = trigger.unit_time * unit_duration_sec; - if (trigger_time_sec > music_time_sec) + if (trigger_time_sec > end_music_time) break; // Add this pattern to active patterns list @@ -243,11 +246,7 @@ void tracker_update(float music_time_sec) { } // Step 2: Update all active patterns and trigger individual events - // NOTE: We trigger events immediately when their time passes (no sample - // offsets) This gives ~16ms quantization (60fps) which is acceptable Sample - // offsets don't work with tempo scaling because music_time and render_time - // are in different time domains (tempo-scaled vs physical) - + // Sample-accurate timing: Calculate offset relative to music_time_sec for (int i = 0; i < MAX_SPECTROGRAMS; ++i) { if (!g_active_patterns[i].active) continue; @@ -255,26 +254,31 @@ void tracker_update(float music_time_sec) { ActivePattern& active = g_active_patterns[i]; const TrackerPattern& pattern = g_tracker_patterns[active.pattern_id]; - // Calculate elapsed unit-less time since pattern started - const float elapsed_music_time = music_time_sec - active.start_music_time; - const float elapsed_units = elapsed_music_time / unit_duration_sec; - // Trigger all events that have passed their unit time while (active.next_event_idx < pattern.num_events) { const TrackerEvent& event = pattern.events[active.next_event_idx]; + const float event_music_time = + active.start_music_time + event.unit_time * unit_duration_sec; - if (event.unit_time > elapsed_units) + if (event_music_time > end_music_time) break; // This event hasn't reached its time yet - // Trigger this event immediately (no sample offset) - // Timing quantization: ~16ms at 60fps, acceptable for rhythm - trigger_note_event(event, 0); + // Sample-accurate timing: + // Offset = (music_time_delta / tempo_scale) * sample_rate + int sample_offset = 0; + if (event_music_time > music_time_sec) { + sample_offset = (int)((event_music_time - music_time_sec) / + tempo_scale * 32000.0f); + } + trigger_note_event(event, sample_offset); active.next_event_idx++; } // Pattern remains active until full duration elapses - if (elapsed_units >= pattern.unit_length) { + const float pattern_end_time = + active.start_music_time + pattern.unit_length * unit_duration_sec; + if (pattern_end_time <= end_music_time) { active.active = false; } } |
