diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/track_visualizer/index.html | 71 | ||||
| -rw-r--r-- | tools/tracker_compiler.cc | 25 |
2 files changed, 69 insertions, 27 deletions
diff --git a/tools/track_visualizer/index.html b/tools/track_visualizer/index.html index 70a5fd8..4a613ec 100644 --- a/tools/track_visualizer/index.html +++ b/tools/track_visualizer/index.html @@ -161,7 +161,9 @@ const patterns = {}; const score = []; let currentPattern = null; + let currentPatternLength = 1.0; // Default: 1 unit let inScore = false; + let bpm = 120.0; // Default BPM for (let line of lines) { line = line.trim(); @@ -169,10 +171,28 @@ // Skip comments and empty lines if (line.startsWith('#') || line.length === 0) continue; - // Pattern definition + // BPM directive + if (line.startsWith('BPM ')) { + bpm = parseFloat(line.substring(4).trim()); + continue; + } + + // Pattern definition with optional LENGTH if (line.startsWith('PATTERN ')) { - currentPattern = line.substring(8).trim(); - patterns[currentPattern] = []; + const tokens = line.substring(8).trim().split(/\s+/); + currentPattern = tokens[0]; + currentPatternLength = 1.0; // Default + + // Check for LENGTH parameter + const lengthIdx = tokens.indexOf('LENGTH'); + if (lengthIdx !== -1 && lengthIdx + 1 < tokens.length) { + currentPatternLength = parseFloat(tokens[lengthIdx + 1]); + } + + patterns[currentPattern] = { + events: [], + unitLength: currentPatternLength + }; inScore = false; continue; } @@ -184,12 +204,12 @@ continue; } - // Pattern events (beat, sample, volume, pan) + // Pattern events (unit_time, sample, volume, pan) if (currentPattern && !inScore) { const parts = line.split(',').map(s => s.trim()); if (parts.length >= 2) { - patterns[currentPattern].push({ - beat: parseFloat(parts[0]), + patterns[currentPattern].events.push({ + unitTime: parseFloat(parts[0]), sample: parts[1], volume: parts.length > 2 ? parseFloat(parts[2]) : 1.0, pan: parts.length > 3 ? parseFloat(parts[3]) : 0.0 @@ -197,36 +217,44 @@ } } - // Score entries (time, pattern_name) + // Score entries (unit_time, pattern_name) if (inScore) { const parts = line.split(',').map(s => s.trim()); if (parts.length >= 2) { score.push({ - time: parseFloat(parts[0]), + unitTime: parseFloat(parts[0]), pattern: parts[1] }); } } } - return { patterns, score }; + return { patterns, score, bpm }; + } + + // Convert unit-less time to seconds + // 1 unit = 4 beats, at given BPM + function unitsToSeconds(units, bpm) { + const BEATS_PER_UNIT = 4.0; + const unitDurationSec = (BEATS_PER_UNIT / bpm) * 60.0; + return units * unitDurationSec; } - // Calculate pattern duration (max beat time) - function getPatternDuration(pattern) { - if (pattern.length === 0) return 4.0; // Default 4 beats - return Math.max(...pattern.map(e => e.beat)) + 1.0; + // Get pattern duration in seconds + function getPatternDuration(pattern, bpm) { + if (!pattern || !pattern.unitLength) return unitsToSeconds(1.0, bpm); + return unitsToSeconds(pattern.unitLength, bpm); } // Draw timeline function drawTimeline() { if (!trackData) return; - const { patterns, score } = trackData; + const { patterns, score, bpm } = trackData; // Find max time for canvas sizing const maxTime = score.length > 0 - ? Math.max(...score.map(s => s.time + getPatternDuration(patterns[s.pattern] || []))) + ? Math.max(...score.map(s => unitsToSeconds(s.unitTime, bpm) + getPatternDuration(patterns[s.pattern], bpm))) : 60; // Update canvas size @@ -246,8 +274,8 @@ // Group score entries by time for stacking const stackedPatterns = []; for (const entry of score) { - const startTime = entry.time; - const duration = getPatternDuration(patterns[entry.pattern] || []); + const startTime = unitsToSeconds(entry.unitTime, bpm); + const duration = getPatternDuration(patterns[entry.pattern], bpm); const endTime = startTime + duration; // Find stack level (avoid overlaps) @@ -261,13 +289,15 @@ stackLevel++; } + const pattern = patterns[entry.pattern]; stackedPatterns.push({ patternName: entry.pattern, startTime, endTime, duration, stackLevel, - events: patterns[entry.pattern] || [] + unitLength: pattern ? pattern.unitLength : 1.0, + events: pattern ? pattern.events : [] }); } @@ -385,7 +415,10 @@ ctx.strokeStyle = '#ffffff'; ctx.lineWidth = 2; for (const event of item.events) { - const tickX = x + event.beat * (PIXELS_PER_SECOND * zoomLevel) / beatsPerSecond; + // Convert unit_time to position within pattern + // event.unitTime ranges from 0 to item.unitLength + // width represents the full pattern duration + const tickX = x + (event.unitTime / item.unitLength) * width; const tickY = y + height - TICK_HEIGHT * verticalZoom; // Vertical tick mark 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<Event> 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", |
