summaryrefslogtreecommitdiff
path: root/convert_track.py
blob: ec9d62cee588b8be08de7a32a602b833ae5f4442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
"""Convert .track files from beat-based to unit-less timing."""

import re
import sys

def convert_beat_to_unit(beat_str):
    """Convert beat value to unit-less (beat / 4)."""
    beat = float(beat_str)
    unit = beat / 4.0
    return f"{unit:.2f}"

def process_line(line):
    """Process a single line, converting beat values."""
    line = line.rstrip('\n')

    # Skip comments and empty lines
    if not line.strip() or line.strip().startswith('#'):
        return line

    # PATTERN line - add LENGTH 1.0
    if line.strip().startswith('PATTERN '):
        # Check if LENGTH already exists
        if ' LENGTH ' in line:
            return line
        parts = line.split()
        if len(parts) >= 2:
            return f"PATTERN {parts[1]} LENGTH 1.0"
        return line

    # Event line (starts with a number)
    match = re.match(r'^(\s*)([0-9.]+),\s*(.+)$', line)
    if match:
        indent, beat_str, rest = match.groups()
        unit_str = convert_beat_to_unit(beat_str)
        return f"{indent}{unit_str}, {rest}"

    return line

def main():
    if len(sys.argv) != 3:
        print(f"Usage: {sys.argv[0]} <input.track> <output.track>")
        sys.exit(1)

    input_file = sys.argv[1]
    output_file = sys.argv[2]

    with open(input_file, 'r') as f:
        lines = f.readlines()

    # Add header comment about timing
    output_lines = []
    output_lines.append("# Enhanced Demo Track - Progressive buildup with varied percussion\n")
    output_lines.append("# Features acceleration/deceleration with diverse samples and melodic progression\n")
    output_lines.append("#\n")
    output_lines.append("# TIMING: Unit-less (1 unit = 4 beats at 120 BPM = 2 seconds)\n")
    output_lines.append("# Pattern events use unit-less time (0.0-1.0 for 4-beat pattern)\n")
    output_lines.append("# Score triggers use unit-less time\n")

    # Skip old header lines
    start_idx = 0
    for i, line in enumerate(lines):
        if line.strip() and not line.strip().startswith('#'):
            start_idx = i
            break

    # Process rest of file
    for line in lines[start_idx:]:
        output_lines.append(process_line(line) + '\n')

    with open(output_file, 'w') as f:
        f.writelines(output_lines)

    print(f"Converted {input_file} -> {output_file}")

if __name__ == '__main__':
    main()