File size: 3,412 Bytes
4048d4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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():
    # Load data from CSV file
    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'])

        # not needed for our purpose here
        # if (n > 0):
        #     edit_start -= CROSS_FADE_SAMPLES
        #     edit_dur += CROSS_FADE_SAMPLES

        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])

                # lookup measure number via syncpoints file
                measure_from = get_nearest_measure(syncpoints, edit[2])
                measure_to = get_nearest_measure(syncpoints, edit[3])

                # print duration in minutes that were repeated
                print(f"{score.name} {measure_from} to {measure_to} ({(edit[3] - edit[2]) / SR / 60:.2f} minutes)")
                # collect measure ranges
                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)

    # make a new folder for the scores
    new_folder = Path(__file__).parent / Path("../musicxml_no_repeats/")
    new_folder.mkdir(parents=True, exist_ok=True)

    # save new score
    new_score_path = new_folder / Path(f"{row['Track ID']}.xml")
    m21_score.write('musicxml', fp=new_score_path)