-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_helper.py
218 lines (180 loc) · 6.45 KB
/
midi_helper.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
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
import os
from math import floor
from shutil import move
import numpy as np
from music21 import converter, instrument, note, chord
from music21.chord import Chord
from music21.midi.translate import streamToMidiFile
from music21.note import Rest
from music21.pitch import PitchException, Pitch
from music21.stream import Part
import string
from random import choice
class Generate:
def __init__(self, segments, rules):
self.segments = segments
self.dict, self.alphabet = self.generate_dict()
self.rules = rules
self.axiom = self.alphabet[0]
def generate_dict(self):
dict = {}
alphabet = list(string.ascii_lowercase)
alphabet_used = []
for count, segment in enumerate(self.segments):
alphabet_used.append(alphabet[count])
dict[alphabet[count]] = segment
return dict, alphabet_used
def rewriting_system(self, axiom, n):
out = ""
for char in axiom:
if char in self.rules:
out += self.rules[char]
else:
out += char
n -= 1
if n > 0:
return self.rewriting_system(out, n)
return out
def convert_to_segments(self, melody):
states = {}
for key, _ in self.dict.items():
states[key] = 0
notes = []
durations = []
for char in melody:
if char in self.dict:
state = states[char]
curr_note = self.dict[char].get_notes()
curr_dur = self.dict[char].get_durations()
for i in range(state, state + 4):
notes.append(curr_note[i % len(curr_note)])
durations.append(curr_dur[i % len(curr_dur)])
states[char] += 4
return notes, durations
# Pretty generic rules - this needs serious work and testing
def generate_rules(self):
rules = {}
num_rules = floor(len(self.alphabet) / 4) + 1
for i in range(num_rules):
char = self.alphabet[i]
if i % 2 == 0 and i + 2 < len(self.alphabet):
rules[char] = "".join(self.alphabet)
else:
rules[char] = f"{char}{choice(self.alphabet)}"
print(rules)
self.rules = rules
class Segment:
def __init__(self, notes, filename, index, durations, key, do_prune, do_quantize):
self.notes = notes
self.durations = durations
self.filename = filename
self.index = index
self.key = key
self.part = self.__to_part()
if do_prune:
self.prune()
if do_quantize:
self.quantize()
def __to_part(self):
part = Part()
part.append(instrument.Piano())
for i in range(len(self.notes)):
note = self.notes[i]
duration = self.durations[i]
if note == "rest":
part.append(Rest(quarterLength=duration))
else:
part.append(Chord(note, quarterLength=duration))
return part
def write_to_midi(self, export=False, filename=None):
mf = streamToMidiFile(self.part)
if export:
mf.open(filename, 'wb')
else:
mf.open(f"tmp/{self.index}_{self.filename}", 'wb')
mf.write()
mf.close()
def play(self):
# Play midi file using timidity binary
self.write_to_midi()
try:
os.system(f"timidity -Os tmp/{self.index}_{self.filename}")
except Exception as e:
print(f"Music could not be played due to: {e}")
# Remove temporary midi file from /tmp
os.remove(f"tmp/{self.index}_{self.filename}")
def prune(self):
notes = self.notes
midi = []
for elem in notes:
if elem != "rest":
pitches = Chord(elem).pitches
avg = 0
for pitch in pitches:
avg += int(pitch.midi)
midi.append(avg / len(pitches))
distances = []
for (x, y) in zip(midi, midi[1:]):
distances.append(abs(floor(x - y)))
std = np.std(distances)
mean = np.mean(distances)
for i in range(len(distances)):
if distances[i] > mean + (2 * std):
notes[i - 1] = self.notes[i]
self.notes = notes
def quantize(self):
curr = 0
for i in range(len(self.durations)):
if curr % 4 == 0:
curr = self.durations[i]
elif ((curr + self.durations[i]) / 4) > 1:
self.durations[i] = 4 - curr
curr = 0
else:
curr += self.durations[i]
def get_notes(self):
return self.notes
def get_key(self):
return self.key
def get_durations(self):
return self.durations
class ExtractMidi:
def __init__(self, filename):
self._filename = filename
self._notes = []
self._durations = []
self.stream = converter.parse(self._filename)
self.instruments = instrument.partitionByInstrument(self.stream).parts
def parse_midi(self, inst='Piano'):
for part in self.instruments:
# select elements of only inst
if inst in str(part):
notes_to_parse = part.recurse()
for element in notes_to_parse:
# notes
if isinstance(element, note.Note):
self._notes.append(str(element.pitch))
self._durations.append(element.quarterLength)
# chords
elif isinstance(element, chord.Chord):
self._notes.append(' '.join(str(n.pitch)
for n in element))
self._durations.append(element.quarterLength)
# rests
elif isinstance(element, note.Rest):
self._notes.append(element.name)
self._durations.append(element.quarterLength)
def get_key(self):
return self.stream.analyze('key')
def get_instruments(self):
return self.instruments
def get_notes(self):
if not self._notes:
print("Midi has not been parsed")
return None
return self._notes
def get_durations(self):
if not self._durations:
print("Midi has not been parsed")
return None
return self._durations