Spaces:
Sleeping
Sleeping
File size: 17,478 Bytes
fe7e262 | 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | import collections
import json
from itertools import chain
from pathlib import Path
import miditoolkit
import numpy as np
from . import assets
from .utils import load_config
DEFAULT_SUBBEAT_RANGE = np.arange(0, 64, dtype=int)
DEFAULT_PIANO_RANGE = np.arange(21, 109, dtype=int)
DEFAULT_VELOCITY_BINS = np.linspace(0, 124, 31 + 1, dtype=int) # midi velocity: 0~127
LS_DEFAULT_VELOCITY = 80
DEFAULT_BPM_BINS = np.linspace(32, 224, 64 + 1, dtype=int)
DEFAULT_DURATION_RANGE = np.arange(1, 1 + 32, dtype=int)
DEFAULT_CHORD_ROOTS = [
"A",
"A#",
"B",
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
]
DEFAULT_CHORD_QUALITY = [
"+",
"/o7",
"7",
"M",
"M7",
"m",
"m7",
"o",
"o7",
"sus2",
"sus4",
]
VOCAB_SIZE = 500
def gen_vocab():
spec = [f"spec_{t}" for t in ["pad", "bos", "eos", "unk", "mask", "ss", "se"]]
bar = ["bar_start", "bar_end"] + [f"bar_{i}" for i in range(1, 5)] + ["bar_N"]
position = [f"position_{i}" for i in DEFAULT_SUBBEAT_RANGE]
chord = ["chord_N_N"]
for root in DEFAULT_CHORD_ROOTS:
for quality in DEFAULT_CHORD_QUALITY:
chord.append(f"chord_{root}_{quality}")
tempo = [f"tempo_{i}" for i in DEFAULT_BPM_BINS]
pitch = [f"pitch_{i}" for i in DEFAULT_PIANO_RANGE]
duration = [f"duration_{i}" for i in DEFAULT_DURATION_RANGE]
velocity = [f"velocity_{i}" for i in DEFAULT_VELOCITY_BINS]
vocab = spec + bar + position + chord + tempo + pitch + duration + velocity
vocab = vocab + ["reserved"] * (VOCAB_SIZE - len(vocab))
return vocab
class Event:
def __init__(
self,
etype,
value,
):
self.etype = etype
self.value = value
def init_check(self):
if self.etype == "spec":
assert self.value.split("_")[0] in ["spec"], f"{self.etype}: {self.value}"
elif self.etype == "bar":
assert self.value.split("_")[0] in ["bar"], f"{self.etype}: {self.value}"
elif self.etype == "metric":
assert self.value.split("_")[0] in [
"position",
"chord",
"tempo",
], f"{self.etype}: {self.value}"
elif self.etype == "note":
assert self.value.split("_")[0] in [
"pitch",
"duration",
"velocity",
], f"{self.etype}: {self.value}"
else:
raise ValueError(f"Unknown etype: {self.etype}")
def unwrap(self, vtype):
return vtype(self.value.split("_")[1])
def __repr__(self):
return f"Event({self.etype}: {self.value})"
def __eq__(self, other):
if not isinstance(other, Event):
return False
return self.etype == other.etype and self.value == other.value
class Tokenizer:
def __init__(self, vocab_file=None, beat_div=None, ticks_per_beat=None):
vocab_file = assets.vocab_file() if vocab_file is None else vocab_file
if beat_div is None or ticks_per_beat is None:
config_file = assets.config_file()
hp = load_config(config_file)
beat_div = hp.beat_div
ticks_per_beat = hp.ticks_per_beat
self.vocab = Vocab(vocab_file)
self.beat_div = beat_div
self.ticks_per_beat = ticks_per_beat
def get_song_from_midi(self, midi):
song = midi_to_song(midi, self.beat_div)
song["events"] = song_to_events(song)
song["ls_events"] = extract_leadsheet_from_events(
song["events"], song["metadata"]["beat_per_bar"], self.beat_div
)
return song
def events_to_midi(self, events):
midi = events_to_midi(events, self.ticks_per_beat, self.beat_div)
return midi
def e2i(self, e):
return self.vocab.t2i[e.value]
def i2e(self, eid):
token = self.vocab.i2t[eid]
if token.startswith("spec"):
return Event("spec", token)
elif token.startswith("bar"):
return Event("bar", token)
elif token.split("_")[0] in ["position", "chord", "tempo"]:
return Event("metric", token)
elif token.split("_")[0] in ["pitch", "duration", "velocity"]:
return Event("note", token)
else:
raise ValueError(f"Unknown token: {token}")
def get_bar_ranges(self, events, from_start=True):
return get_bar_ranges(events, from_start)
@staticmethod
def get_tempo_event(bpm):
tempo = DEFAULT_BPM_BINS[np.argmin(abs(DEFAULT_BPM_BINS - bpm))]
return Event("metric", f"tempo_{tempo}")
@staticmethod
def get_duration_event(duration):
duration = DEFAULT_DURATION_RANGE[np.argmin(abs(DEFAULT_DURATION_RANGE - duration))]
return Event("note", f"duration_{duration}")
@staticmethod
def get_velocity_event(velocity):
velocity = DEFAULT_VELOCITY_BINS[np.argmin(abs(DEFAULT_VELOCITY_BINS - velocity))]
return Event("note", f"velocity_{velocity}")
class Vocab:
def __init__(self, vacab_file):
self.i2t = json.loads(vacab_file.read_text())
self.t2i = {}
for i, t in enumerate(self.i2t):
self.t2i[t] = i
def len(self):
return len(self.i2t)
def __len__(self):
return len(self.i2t)
def __repr__(self):
out = []
for i, t in enumerate(self.i2t):
out.append(f"{i}: {t}")
return "\n".join(out)
def midi_to_song(midi_obj, beat_div):
assert midi_obj.ticks_per_beat % beat_div == 0
grid_resol = midi_obj.ticks_per_beat // beat_div
# load notes
instr_notes = collections.defaultdict(list)
for instr in midi_obj.instruments:
for note in instr.notes:
instr_notes[instr.name].append(note)
instr_notes[instr.name].sort(key=lambda x: x.start)
# load chords
chords = []
for marker in midi_obj.markers:
if marker.text.split("_")[0] != "global" and "Boundary" not in marker.text.split("_")[0]:
chords.append(marker)
chords.sort(key=lambda x: x.time)
# load tempos
tempos = midi_obj.tempo_changes
tempos.sort(key=lambda x: x.time)
# load labels
labels = []
for marker in midi_obj.markers:
if "Boundary" in marker.text.split("_")[0]:
labels.append(marker)
labels.sort(key=lambda x: x.time)
# load global bpm
global_bpm = None
for marker in midi_obj.markers:
if marker.text.split("_")[0] == "global" and marker.text.split("_")[1] == "bpm":
global_bpm = int(marker.text.split("_")[2])
# process notes
intsr_gird = dict()
for key in instr_notes.keys():
notes = instr_notes[key]
note_grid = collections.defaultdict(list)
for note in notes:
# quantize start
quant_time = round(note.start / grid_resol)
# duration
note_duration = note.end - note.start
duration = round(note_duration / grid_resol)
duration = max(duration, 1) # dur >= 1
# append
note_grid[quant_time].append(
{
"note": note,
"pitch": note.pitch,
"duration": duration,
"velocity": note.velocity,
}
)
# sort
for time in note_grid.keys():
note_grid[time].sort(key=lambda x: -x["pitch"])
# set to track
intsr_gird[key] = note_grid.copy()
# process chords
chord_grid = collections.defaultdict(list)
for chord in chords:
quant_time = round(chord.time / grid_resol)
# chord_grid[quant_time] = [chord] # NOTE: only one chord per time
chord_grid[quant_time].append(chord)
# process tempo
tempo_grid = collections.defaultdict(list)
for tempo in tempos:
quant_time = round(tempo.time / grid_resol)
# tempo.tempo = DEFAULT_BPM_BINS[np.argmin(abs(DEFAULT_BPM_BINS-tempo.tempo))]
tempo_grid[quant_time] = [tempo] # NOTE: only one tempo per time
all_bpm = [tempo[0].tempo for _, tempo in tempo_grid.items()]
assert len(all_bpm) > 0, " No tempo changes in midi file."
average_bpm = sum(all_bpm) / len(all_bpm)
if global_bpm is None:
global_bpm = average_bpm
# process boundary
label_grid = collections.defaultdict(list)
for label in labels:
quant_time = round(label.time / grid_resol)
label_grid[quant_time] = [label]
# collect
song_data = {
"notes": intsr_gird,
"chords": chord_grid,
"tempos": tempo_grid,
"labels": label_grid,
"metadata": {
"global_bpm": global_bpm,
"average_bpm": average_bpm,
"beat_div": beat_div,
"beat_per_bar": midi_obj.time_signature_changes[0].numerator,
},
}
return song_data
def song_to_events(song):
beat_div = song["metadata"]["beat_div"]
beat_per_bar = song["metadata"]["beat_per_bar"]
grid_per_bar = beat_div * beat_per_bar
events = [Event("spec", "spec_ss")]
global_tempo = DEFAULT_BPM_BINS[
np.argmin(abs(DEFAULT_BPM_BINS - song["metadata"]["global_bpm"]))
]
events.append(Event("metric", f"tempo_{global_tempo}"))
max_grid = list(chain(song["tempos"].keys(), song["chords"].keys()))
for _, v in song["notes"].items():
max_grid.extend(v.keys())
max_grid = max(max_grid)
for bar_i in range(0, max_grid + 1, grid_per_bar):
events.append(Event("bar", "bar_start"))
for i in range(bar_i, min(bar_i + grid_per_bar, max_grid + 1)):
pos = Event("metric", f"position_{i-bar_i}")
tmp = []
empty = True
if i in song["chords"]:
chord_items = song["chords"][i][0].text.split("_")
chord = f"{chord_items[0]}_{chord_items[1]}"
tmp.append(Event("metric", f"chord_{chord}"))
empty = False
if i in song["tempos"]:
tempo = DEFAULT_BPM_BINS[
np.argmin(abs(DEFAULT_BPM_BINS - song["tempos"][i][0].tempo))
]
tmp.append(Event("metric", f"tempo_{tempo}"))
empty = False
for _, instr in song["notes"].items():
if i in instr:
for note in instr[i]:
duration = DEFAULT_DURATION_RANGE[
np.argmin(abs(DEFAULT_DURATION_RANGE - note["duration"]))
]
velocity = DEFAULT_VELOCITY_BINS[
np.argmin(abs(DEFAULT_VELOCITY_BINS - note["velocity"]))
]
tmp.append(Event("note", f'pitch_{note["pitch"]}'))
tmp.append(Event("note", f"duration_{duration}"))
tmp.append(Event("note", f"velocity_{velocity}"))
empty = False
if not empty:
events.append(pos)
events.extend(tmp)
events.append(Event("bar", "bar_end"))
events.append(Event("spec", "spec_se"))
return events
def events_to_midi(events, ticks_per_beat, grid_div):
bar_ranges = get_bar_ranges(events, from_start=False)
midi = miditoolkit.MidiFile()
midi.ticks_per_beat = ticks_per_beat
track = miditoolkit.Instrument(program=0, is_drum=False, name="piano")
midi.instruments = [track]
bar_tick = 0
subbeat_tick = 0
pitch, velocity, duration = 0, 0, 0
bar_len = 4
for i, (start, end) in enumerate(bar_ranges):
assert ticks_per_beat % grid_div == 0
ticks_per_subbeat = ticks_per_beat // grid_div
for event in events[start:end]:
if event.etype == "spec":
pass
elif event.etype == "bar":
if event.value in ["bar_start", "bar_end"]:
pass
else:
try:
bar_len = int(event.value.split("_")[1])
except ValueError:
assert event.value == "bar_N"
elif event.etype == "metric":
v = event.value
if v.startswith("position"):
pos = int(v.split("_")[1])
subbeat_tick = pos * ticks_per_subbeat
elif v.startswith("tempo"):
tempo = int(v.split("_")[1])
m = miditoolkit.TempoChange(time=bar_tick + subbeat_tick, tempo=tempo)
midi.tempo_changes.append(m)
elif v.startswith("chord"):
pass
else:
raise ValueError(f"Unknown metric: {v}")
elif event.etype == "note":
v = event.value
if v.startswith("pitch"):
pitch = int(v.split("_")[1])
elif v.startswith("duration"):
duration = int(v.split("_")[1]) * ticks_per_subbeat
elif v.startswith("velocity"):
velocity = int(v.split("_")[1])
n = miditoolkit.Note(
start=bar_tick + subbeat_tick,
end=bar_tick + subbeat_tick + duration,
pitch=pitch,
velocity=velocity,
)
midi.instruments[0].notes.append(n)
else:
raise ValueError(f"Unknown note: {v}")
else:
raise ValueError(f"Unknown event: {type(event)}")
bar_tick += ticks_per_beat * bar_len
return midi
def extract_leadsheet_from_events(
events, beat_per_bar, beat_div, cover_beat=0, min_pitch=60, no_chord=False
):
# algorithm:
# - skyline
# - filter out notes with pitch < 60
grids = []
for event in events:
if event.etype == "bar":
for _ in range(beat_per_bar * beat_div):
grids.append(list())
grid_idx = 0
bar_count = 0
subbeat = 0
note_tmp = []
first_tempo = True
for event in events:
if event.etype == "spec":
grids[grid_idx].append(event)
elif event.etype == "bar":
if event.value == "bar_end":
bar_count += 1
grid_idx = bar_count * (beat_per_bar * beat_div)
subbeat = 0
grids[grid_idx].append(event)
elif event.etype == "metric":
if event.value.startswith("position"):
subbeat = int(event.value.split("_")[1])
if not event.value.startswith("tempo"):
grids[grid_idx + subbeat].append(event)
else: # tempo
if first_tempo:
grids[grid_idx + subbeat].append(event)
first_tempo = False
elif event.etype == "note":
note_tmp.append(event)
if event.value.startswith("velocity"):
pitch = note_tmp[0].value.split("_")[1]
if int(pitch) >= min_pitch: # only keep notes with pitch >= min_pitch
grids[grid_idx + subbeat].append(note_tmp[0])
grids[grid_idx + subbeat].append(note_tmp[1])
grids[grid_idx + subbeat].append(
Event("note", f"velocity_{LS_DEFAULT_VELOCITY}")
)
note_tmp = []
# select the highest note
for i, grid in enumerate(grids):
notes = [e for e in grid if e.etype == "note"]
notes = [(notes[i], notes[i + 1], notes[i + 2]) for i in range(0, len(notes), 3)]
notes.sort(key=lambda x: -x[0].unwrap(int)) # sort by pitch
grids[i] = [e for e in grid if not e.etype == "note"]
if len(notes) > 0:
grids[i].extend(notes[0])
# remove useless metric
for i, grid in enumerate(grids):
if len(grid) == 0 or not grid[-1].etype == "metric":
continue
metric = grid[-1]
assert metric.etype == "metric"
if metric.value.startswith("position"):
grids[i].pop()
assert len(grid) == 0 or grid[-1].etype == "bar"
return list(chain(*grids))
def get_bar_ranges(events, from_start=True):
bar_idx_list = []
for i, event in enumerate(events):
if event.etype == "bar" and event.value == "bar_start":
bar_idx_list.append(i)
bar_idx_list = bar_idx_list + [len(events)]
if from_start:
bar_idx_list[0] = 0 # the first bar starts at 0
bar_ranges = []
for i in range(len(bar_idx_list) - 1):
bar_ranges.append((bar_idx_list[i], bar_idx_list[i + 1]))
return bar_ranges
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
cmd_gen_vocab = subparsers.add_parser("gen_vocab")
cmd_gen_vocab.add_argument("--output_file", type=Path, required=True)
ca = parser.parse_args()
if ca.command is None:
parser.print_help()
exit()
elif ca.command == "gen_vocab":
vocab = gen_vocab()
ca.output_file.write_text(json.dumps(vocab, indent=2))
vocab = Vocab(ca.output_file)
print(vocab)
print("vocab size:", vocab.len())
else:
raise ValueError(f"Unknown command: {ca.command}")
|