forked from boostcampaitech5/level3_cv_finalproject-cv-08
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_score.py
51 lines (41 loc) · 1.96 KB
/
generate_score.py
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
from music21 import converter, stream, chord, clef, instrument, note
def generate_two_hand_score(output_roll_midi_path):
roll_score = converter.parse(output_roll_midi_path)
pitch_threshold = 80
right_hand_score = stream.Score()
left_hand_score = stream.Score()
for element in roll_score.chordify().recurse():
if isinstance(element, chord.Chord):
if element.quarterLength > 4.0:
element.augmentOrDiminish(4.0 / (element.quarterLength + 1e-5), inPlace=True)
notes = [pitch.midi for pitch in element.pitches]
avg_note = sum(notes) // len(notes)
if avg_note < pitch_threshold:
left_hand_score.append(element)
right_hand_score.append(note.Rest(quarterLength=element.quarterLength))
else:
right_hand_score.append(element)
left_hand_score.append(note.Rest(quarterLength=element.quarterLength))
left_hand_score.makeMeasures(inPlace=True)
left_hand_score.insert(0, instrument.Piano())
right_hand_score.makeMeasures(inPlace=True)
right_hand_score.insert(0, instrument.Piano())
right_hand_part = stream.Part()
right_hand_part.append(clef.TrebleClef())
right_hand_part.append(right_hand_score)
left_hand_part = stream.Part()
left_hand_part.append(clef.BassClef())
left_hand_part.append(left_hand_score)
combined_score = stream.Score()
combined_score.insert(0, right_hand_part)
combined_score.insert(0, left_hand_part)
return combined_score
def generate_score(output_roll_midi_path):
roll_score = converter.parse(output_roll_midi_path)
score = stream.Score()
for element in roll_score.chordify().recurse():
if isinstance(element, chord.Chord):
if element.quarterLength > 4.0:
element.augmentOrDiminish(4.0 / (element.quarterLength + 1e-5), inPlace=True)
score.append(element)
return score