Spaces:
Running
Running
File size: 5,185 Bytes
2469e25 | 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | """Write TJA on Model P's 48-slot beat lattice, including tempo changes."""
from __future__ import annotations
from collections import defaultdict
from functools import reduce
import math
import numpy as np
from chartgen.dataset import GRID
COURSES = {3: "Oni", 4: "Edit"}
NOTE_CODES = {0: "1", 1: "2", 2: "3", 3: "4", 4: "5"}
SLOT_EPSILON = 1e-5
def section_slots(sections, index, latest_ms):
start_ms, beat_ms, meter = sections[index, :3]
if index + 1 < len(sections):
end_ms = sections[index + 1, 0]
return max(1, math.ceil((end_ms - start_ms) * GRID / beat_ms - SLOT_EPSILON))
measure_slots = int(meter) * GRID
return max(1, math.ceil((latest_ms - start_ms + 0.1) * GRID / beat_ms / measure_slots)) * measure_slots
def position(sections, time_ms, latest_ms):
index = int(np.clip(np.searchsorted(sections[:, 0], time_ms + 1e-3, side="right") - 1, 0, len(sections) - 1))
start_ms, beat_ms = sections[index, :2]
n_slots = section_slots(sections, index, latest_ms)
slot = int(np.clip(round((time_ms - start_ms) * GRID / beat_ms), 0, n_slots - 1))
return index, slot
def measures(sections, notes, commands, latest_ms):
lines = []
previous_bpm = 60000 / sections[0, 1]
previous_measure = (4, 4)
lateness_ms = 0.0
current_scroll = 1.0
for index, (start_ms, beat_ms, meter, _, omit) in enumerate(sections):
if index and abs(lateness_ms) > 1e-6:
lines.append(f"#DELAY {-lateness_ms / 1000:.9f}")
bpm = 60000 / beat_ms
if abs(bpm - previous_bpm) > 1e-9:
lines.append(f"#BPMCHANGE {bpm:.9f}")
previous_bpm = bpm
n_slots = section_slots(sections, index, latest_ms)
regular = int(meter) * GRID
if omit:
lines.append("#BARLINEOFF")
for first in range(0, n_slots, regular):
stop = min(first + regular, n_slots)
count = stop - first
measure = (int(meter), 4)
if count != regular:
gcd = math.gcd(count, GRID * 4)
measure = count // gcd, GRID * 4 // gcd
if measure != previous_measure:
lines.append(f"#MEASURE {measure[0]}/{measure[1]}")
previous_measure = measure
occupied = (k for k in set(notes[index]) | set(commands[index]) if first <= k < stop)
step = reduce(math.gcd, (k - first for k in occupied), count)
row = ""
for k in range(first, stop, step):
event = notes[index].get(k)
controls = list(commands[index].get(k, []))
if event and abs(event[1] - current_scroll) > 1e-5:
current_scroll = event[1]
controls.append(f"#SCROLL {current_scroll:.6f}")
if controls:
if row:
lines.append(row)
row = ""
lines.extend(controls)
row += event[0] if event else "0"
lines.append(row + ",")
if omit:
lines.append("#BARLINEON")
if index + 1 < len(sections):
lateness_ms = start_ms + n_slots * beat_ms / GRID - sections[index + 1, 0]
return lines
def write_course(chart):
sections = chart.sections
# Track a chart's last possible roll end when sizing its final measure.
active_sections = np.searchsorted(sections[:, 0], chart.time + 1e-3, side="right") - 1
tails = chart.time + chart.length_beats * sections[active_sections, 1]
latest_ms = max(float(tails.max()), max((b for _, b in chart.kiai), default=0.0))
notes = defaultdict(dict)
controls = defaultdict(lambda: defaultdict(list))
for start, stop in chart.kiai:
a, x = position(sections, start, latest_ms)
b, y = position(sections, stop, latest_ms)
if (a, x) < (b, y):
controls[a][x].append("#GOGOSTART")
controls[b][y].append("#GOGOEND")
for t, ty, sv in zip(chart.time, chart.type, chart.sv):
section, slot = position(sections, t, latest_ms)
if slot in notes[section]:
raise RuntimeError("Two generated notes occupy the same TJA slot.")
notes[section][slot] = (NOTE_CODES[int(ty)], float(sv))
for i in np.flatnonzero(chart.type == 4):
start = position(sections, chart.time[i], latest_ms)
end = position(sections, tails[i], latest_ms)
section, slot = end
if start < end and slot not in notes[section]:
notes[section][slot] = ("8", 1.0)
else:
# A roll rounded to the same slot as its end cannot be encoded.
del notes[start[0]][start[1]]
return measures(sections, notes, controls, latest_ms)
def to_tja(charts, audio_name, title, artist):
sections = charts[3].sections
lines = [
f"TITLE:{title}", f"ARTIST:{artist}", f"WAVE:{audio_name}",
f"BPM:{60000 / sections[0, 1]:.9f}", f"OFFSET:{-sections[0, 0] / 1000:.9f}", "",
]
for tier in sorted(charts):
lines.extend([f"COURSE:{COURSES[tier]}", "LEVEL:10", "#START", *write_course(charts[tier]), "#END", ""])
return "\n".join(lines)
|