From 726ae79dd3ba8f368d3a671f371e747c33195edd Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 7 Feb 2026 19:22:43 +0100 Subject: refactor(audio): Convert tracker to unit-less timing system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes tracker timing from beat-based to unit-less system to separate musical structure from BPM-dependent playback speed. TIMING CONVENTION: - 1 unit = 4 beats (by convention) - Conversion: seconds = units * (4 / BPM) * 60 - At 120 BPM: 1 unit = 2 seconds BENEFITS: - Pattern structure independent of BPM - BPM changes only affect playback speed, not structure - Easier pattern composition (0.00-1.00 for typical 4-beat pattern) - Fixes issue where patterns played for 2s instead of expected duration DATA STRUCTURES (tracker.h): - TrackerEvent.beat → TrackerEvent.unit_time - TrackerPattern.num_beats → TrackerPattern.unit_length - TrackerPatternTrigger.time_sec → TrackerPatternTrigger.unit_time RUNTIME (tracker.cc): - Added BEATS_PER_UNIT constant (4.0) - Convert units to seconds at playback time using BPM - Pattern remains active for full unit_length duration - Fixed premature pattern deactivation bug COMPILER (tracker_compiler.cc): - Parse LENGTH parameter from PATTERN lines (defaults to 1.0) - Parse unit_time instead of beat values - Generate code with unit-less timing ASSETS: - test_demo.track: converted to unit-less (8 score triggers) - music.track: converted to unit-less (all patterns) - Events: beat/4 conversion (e.g., beat 2.0 → unit 0.50) - Score: seconds/unit_duration (e.g., 4s → 2.0 units at 120 BPM) VISUALIZER (track_visualizer/index.html): - Parse LENGTH parameter and BPM directive - Convert unit-less time to seconds for rendering - Update tick positioning to use unit_time - Display correct pattern durations DOCUMENTATION (doc/TRACKER.md): - Added complete .track format specification - Timing conversion reference table - Examples with unit-less timing - Pattern LENGTH parameter documentation FILES MODIFIED: - src/audio/tracker.{h,cc} (data structures + runtime conversion) - tools/tracker_compiler.cc (parser + code generation) - assets/{test_demo,music}.track (converted to unit-less) - tools/track_visualizer/index.html (BPM-aware rendering) - doc/TRACKER.md (format documentation) - convert_track.py (conversion utility script) TEST RESULTS: - test_demo builds and runs correctly - demo64k builds successfully - Generated code verified (unit-less values in music_data.cc) Co-Authored-By: Claude Sonnet 4.5 --- src/audio/tracker.cc | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/audio/tracker.cc') diff --git a/src/audio/tracker.cc b/src/audio/tracker.cc index 7ad5a67..9ae772e 100644 --- a/src/audio/tracker.cc +++ b/src/audio/tracker.cc @@ -212,19 +212,24 @@ static void trigger_note_event(const TrackerEvent& event) { } void tracker_update(float music_time_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; + // 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]; - if (trigger.time_sec > music_time_sec) + const float trigger_time_sec = trigger.unit_time * unit_duration_sec; + if (trigger_time_sec > music_time_sec) break; // Add this pattern to active patterns list const int slot = get_free_pattern_slot(); if (slot != -1) { g_active_patterns[slot].pattern_id = trigger.pattern_id; - g_active_patterns[slot].start_music_time = trigger.time_sec; + g_active_patterns[slot].start_music_time = trigger_time_sec; g_active_patterns[slot].next_event_idx = 0; g_active_patterns[slot].active = true; } @@ -233,8 +238,6 @@ void tracker_update(float music_time_sec) { } // Step 2: Update all active patterns and trigger individual events - const float beat_duration = 60.0f / g_tracker_score.bpm; - for (int i = 0; i < MAX_SPECTROGRAMS; ++i) { if (!g_active_patterns[i].active) continue; @@ -242,15 +245,15 @@ void tracker_update(float music_time_sec) { ActivePattern& active = g_active_patterns[i]; const TrackerPattern& pattern = g_tracker_patterns[active.pattern_id]; - // Calculate elapsed beats since pattern started + // Calculate elapsed unit-less time since pattern started const float elapsed_music_time = music_time_sec - active.start_music_time; - const float elapsed_beats = elapsed_music_time / beat_duration; + const float elapsed_units = elapsed_music_time / unit_duration_sec; - // Trigger all events that have passed their beat time + // 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]; - if (event.beat > elapsed_beats) + if (event.unit_time > elapsed_units) break; // This event hasn't reached its time yet // Trigger this event as an individual voice @@ -259,8 +262,8 @@ void tracker_update(float music_time_sec) { active.next_event_idx++; } - // If all events have been triggered, mark pattern as complete - if (active.next_event_idx >= pattern.num_events) { + // Pattern remains active until full duration elapses + if (elapsed_units >= pattern.unit_length) { active.active = false; } } -- cgit v1.2.3