|
|
from pathlib import Path |
|
|
import librosa |
|
|
import csv |
|
|
import json |
|
|
import numpy as np |
|
|
import music21 as m21 |
|
|
|
|
|
CSV_FILENAME = "./Edit_Data.csv" |
|
|
SR = 44100 |
|
|
CROSS_FADE_SAMPLES = int(SR / 10) |
|
|
|
|
|
def load_data(): |
|
|
|
|
|
csv_path = Path(__file__).parent / Path(CSV_FILENAME) |
|
|
|
|
|
with open(csv_path) as csv_file: |
|
|
csv_reader = csv.DictReader(csv_file, delimiter=',') |
|
|
return list(csv_reader) |
|
|
|
|
|
def load_syncpoints(score): |
|
|
json_path = Path(__file__).parent / Path("../syncpoints/") / score.with_suffix(".json").name |
|
|
return json.loads(json_path.read_text()) |
|
|
|
|
|
def get_nearest_measure(syncpoints, sample): |
|
|
for measure_no, time_s in syncpoints: |
|
|
if np.isclose(sample / SR, time_s, atol=0.6): |
|
|
return measure_no |
|
|
return None |
|
|
|
|
|
def remove_measure_ranges(score, ranges): |
|
|
for part in score.parts: |
|
|
measures_to_remove = [] |
|
|
for measure in part.getElementsByClass(m21.stream.Measure): |
|
|
if any(start <= measure.number <= end for start, end in ranges): |
|
|
measures_to_remove.append(measure) |
|
|
|
|
|
for measure in measures_to_remove: |
|
|
part.remove(measure) |
|
|
|
|
|
return score |
|
|
|
|
|
musicxml_files = sorted((Path(__file__).parent / Path("../musicxml/")).glob("*.xml")) |
|
|
|
|
|
for row in load_data(): |
|
|
edits = [] |
|
|
|
|
|
score = musicxml_files[int(row['Track ID']) - 1] |
|
|
num_edits = int(row['Num Edits']) |
|
|
adjust_samples = int(row['Adjust Samples']) |
|
|
|
|
|
|
|
|
for n in range(num_edits): |
|
|
edit_idx = str(n + 1) |
|
|
edit_ref = int(row['Edit ' + edit_idx + ' Ref']) |
|
|
edit_start = int(row['Edit ' + edit_idx + ' Start']) |
|
|
edit_dur = int(row['Edit ' + edit_idx + ' Duration']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
source_start = edit_start - edit_ref - adjust_samples |
|
|
source_end = source_start + edit_dur |
|
|
|
|
|
edits.append([source_start, source_end, edit_start, (edit_start+edit_dur), row['Track ID']]) |
|
|
|
|
|
if len(edits) > 1: |
|
|
seen_sections = [] |
|
|
repeated_measures = [] |
|
|
syncpoints = load_syncpoints(score) |
|
|
for edit in edits: |
|
|
source_section_times = (edit[0], edit[1]) |
|
|
if source_section_times in seen_sections: |
|
|
target_section_times = (edit[2], edit[3]) |
|
|
|
|
|
|
|
|
measure_from = get_nearest_measure(syncpoints, edit[2]) |
|
|
measure_to = get_nearest_measure(syncpoints, edit[3]) |
|
|
|
|
|
|
|
|
print(f"{score.name} {measure_from} to {measure_to} ({(edit[3] - edit[2]) / SR / 60:.2f} minutes)") |
|
|
|
|
|
repeated_measures.append((measure_from, measure_to)) |
|
|
|
|
|
seen_sections.append(source_section_times) |
|
|
|
|
|
m21_score = m21.converter.parse(score) |
|
|
remove_measure_ranges(m21_score, repeated_measures) |
|
|
else: |
|
|
m21_score = m21.converter.parse(score) |
|
|
|
|
|
|
|
|
new_folder = Path(__file__).parent / Path("../musicxml_no_repeats/") |
|
|
new_folder.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
new_score_path = new_folder / Path(f"{row['Track ID']}.xml") |
|
|
m21_score.write('musicxml', fp=new_score_path) |