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 --- tools/tracker_compiler.cc | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'tools/tracker_compiler.cc') diff --git a/tools/tracker_compiler.cc b/tools/tracker_compiler.cc index 59d4187..314099d 100644 --- a/tools/tracker_compiler.cc +++ b/tools/tracker_compiler.cc @@ -99,7 +99,7 @@ struct Sample { }; struct Event { - float beat; + float unit_time; // Unit-less time within pattern std::string sample_name; float volume, pan; }; @@ -107,6 +107,7 @@ struct Event { struct Pattern { std::string name; std::vector events; + float unit_length; // Pattern duration in units (default 1.0 for 4-beat patterns) }; struct Trigger { @@ -177,6 +178,14 @@ int main(int argc, char** argv) { } else if (cmd == "PATTERN") { Pattern p; ss >> p.name; + p.unit_length = 1.0f; // Default: 1 unit = 4 beats + // Check for optional LENGTH parameter + std::string next_token; + if (ss >> next_token) { + if (next_token == "LENGTH") { + ss >> p.unit_length; + } + } current_section = "PATTERN:" + p.name; patterns.push_back(p); pattern_map[p.name] = patterns.size() - 1; @@ -184,18 +193,18 @@ int main(int argc, char** argv) { current_section = "SCORE"; } else { if (current_section.rfind("PATTERN:", 0) == 0) { - // Parse event: beat, sample, vol, pan + // Parse event: unit_time, sample, vol, pan Event e; - float beat; + float unit_time; std::stringstream ss2(line); - ss2 >> beat; + ss2 >> unit_time; char comma; ss2 >> comma; std::string sname; ss2 >> sname; if (sname.back() == ',') sname.pop_back(); - e.beat = beat; + e.unit_time = unit_time; e.sample_name = sname; ss2 >> e.volume >> comma >> e.pan; @@ -276,7 +285,7 @@ int main(int argc, char** argv) { // For now, assume sample_map is used for both generated and asset // samples. This will need refinement if asset samples are not in // sample_map directly. - fprintf(out_file, " { %.1ff, %d, %.1ff, %.1ff },\n", e.beat, + fprintf(out_file, " { %.2ff, %d, %.1ff, %.1ff },\n", e.unit_time, sample_map[e.sample_name], e.volume, e.pan); } fprintf(out_file, "};\n"); @@ -285,8 +294,8 @@ int main(int argc, char** argv) { fprintf(out_file, "const TrackerPattern g_tracker_patterns[] = {\n"); for (const auto& p : patterns) { - fprintf(out_file, " { PATTERN_EVENTS_%s, %zu, 4.0f }, // %s\n", - p.name.c_str(), p.events.size(), p.name.c_str()); + fprintf(out_file, " { PATTERN_EVENTS_%s, %zu, %.2ff }, // %s\n", + p.name.c_str(), p.events.size(), p.unit_length, p.name.c_str()); } fprintf(out_file, "};\n"); fprintf(out_file, "const uint32_t g_tracker_patterns_count = %zu;\n\n", -- cgit v1.2.3