-
Notifications
You must be signed in to change notification settings - Fork 4
/
melodic_features_extraction.py
268 lines (244 loc) · 10.8 KB
/
melodic_features_extraction.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
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
import os, sys
sys.path.append(r'G:\university\programming files\py files\musicpy\musicpy')
import musicpy as mp
import numpy as np
from copy import deepcopy as copy
from matplotlib import pyplot as plt
import math
class Melody:
def __init__(self, current_chord, filename=None, is_melody=False):
if not is_melody:
try:
current_chord_melody = mp.alg.split_melody(current_chord)
current_chord_melody.start_time = current_chord.start_time
except:
pass
self.current_chord = current_chord_melody
self.filename = filename
self.percentile = [1, 99]
self.octave_distribution_tol = 0.85
self.get_melodic_features()
def get_melodic_features(self):
self.pitch_intervals = [
self.current_chord.notes[i].degree -
self.current_chord.notes[i - 1].degree
for i in range(1, len(self.current_chord))
]
self.abs_pitch_intervals = [abs(i) for i in self.pitch_intervals]
current_degree = self.current_chord.get_degree()
current_degree_percentile = [
np.percentile(current_degree, self.percentile[0]),
np.percentile(current_degree, self.percentile[1])
]
current_degree_preprocess = [
i for i in current_degree if
current_degree_percentile[0] <= i <= current_degree_percentile[1]
]
self.pitch_mean = np.mean(current_degree_preprocess)
self.pitch_var = np.var(current_degree)
self.pitch_std = np.std(current_degree)
self.pitch_range = max(current_degree) - min(current_degree)
current_octave_distribution = [
(i, len([j for j in self.current_chord if j.num == i]) /
len(self.current_chord)) for i in range(9)
]
current_octave_distribution.sort(key=lambda s: s[1], reverse=True)
current_octave_counter = 0
self.pitch_octave_distribution = []
for each in current_octave_distribution:
current_octave_counter += each[1]
self.pitch_octave_distribution.append(each[0])
if current_octave_counter >= self.octave_distribution_tol:
break
self.pitch_octave_distribution.sort()
self.pitch_interval_mean = np.mean(self.abs_pitch_intervals)
self.pitch_interval_var = np.var(self.abs_pitch_intervals)
self.pitch_interval_std = np.std(self.abs_pitch_intervals)
self.pitch_interval_distribution = {
j: self.abs_pitch_intervals.count(i)
for i, j in mp.database.INTERVAL.items()
}
current_interval = self.current_chord.interval
self.interval_var = np.var(current_interval)
self.interval_std = np.std(current_interval)
current_interval_percentile = [
np.percentile(current_interval, self.percentile[0]),
np.percentile(current_interval, self.percentile[1])
]
current_interval_preprocess = [
i for i in current_interval if current_interval_percentile[0] <= i
<= current_interval_percentile[1]
]
self.interval_mean = np.mean(current_interval_preprocess)
current_duration = self.current_chord.get_duration()
self.duration_var = np.var(current_duration)
self.duration_std = np.std(current_duration)
current_duration_percentile = [
np.percentile(current_duration, self.percentile[0]),
np.percentile(current_duration, self.percentile[1])
]
current_duration_preprocess = [
i for i in current_duration if current_duration_percentile[0] <= i
<= current_duration_percentile[1]
]
self.duration_mean = np.mean(current_duration_preprocess)
def get_key_rate(self, current_scale, bar_range=None):
current_chord = self.current_chord
if bar_range is not None:
current_chord = self.current_chord.cut(*bar_range)
current_scale_names = current_scale.names()
current_scale_names = [
mp.database.standard_dict.get(i, i) for i in current_scale_names
]
current_chord_names = current_chord.names()
current_chord_names = [
mp.database.standard_dict.get(i, i) for i in current_chord_names
]
current_key_rate = len([
i for i in current_chord_names if i in current_scale_names
]) / len(current_chord_names)
return current_key_rate
def get_moving_melodic_features(self,
width=1,
bar_interval=1,
key_rate_scale=None):
temp = copy(self)
start = temp.current_chord.start_time
length = temp.current_chord.bars(
start_time=temp.current_chord.start_time)
results = []
i = start + bar_interval
reach_end = False
while True:
if i >= length:
i = length
reach_end = True
current_start = i - width
if current_start < 0:
current_start = 0
current_stop = i
current_melody = copy(temp)
current_melody.current_chord = current_melody.current_chord.cut(
current_start, current_stop)
current_melody.get_melodic_features()
if key_rate_scale is not None:
current_key_rate = current_melody.get_key_rate(key_rate_scale)
current_melody.key_rate = current_key_rate
results.append(current_melody)
if reach_end:
break
i += bar_interval
return results
def get_melody_from_file(current_file):
current = mp.read(current_file)
current_track = current.tracks[0]
current_track.start_time = current.start_times[0]
current_melody = Melody(current_track, filename=current_file)
return current_melody
def test_get_melodic_features_of_midi_files(midi_path):
files = [os.path.join(midi_path, i) for i in os.listdir(midi_path)]
melody_list = []
for each in files:
try:
current = mp.read(each)
except:
continue
if len(current) == 1:
current_track = current.tracks[0]
current_track.start_time = current.start_times[0]
current_melody = Melody(current_track, filename=each)
melody_list.append(current_melody)
return melody_list
def test_plot_moving_melodic_features(current_file,
current_width=4,
current_bar_interval=1 / 4,
feature='pitch_mean',
plot_bar_ticks=2,
key_rate_scale=None):
current = mp.read(current_file)
current_track = current.tracks[0]
current_track.start_time = current.start_times[0]
current_melody = Melody(current_track)
current_melody_length = current_melody.current_chord.bars(
current_melody.current_chord.start_time)
moving_melodic_features = current_melody.get_moving_melodic_features(
width=current_width,
bar_interval=current_bar_interval,
key_rate_scale=key_rate_scale)
features = [getattr(i, feature) for i in moving_melodic_features]
points = [
current_melody.current_chord.start_time + i * current_bar_interval
for i in range(
0,
math.ceil((current_melody_length -
current_melody.current_chord.start_time) /
current_bar_interval))
]
fig = plt.figure(figsize=(12, 8))
plt.plot(points, features)
plt.xlabel('bar')
if feature == 'key_rate':
plt.ylabel(f'{feature} ({key_rate_scale.get_scale_name()})')
else:
plt.ylabel(feature)
x_major_locator = plt.MultipleLocator(plot_bar_ticks)
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
plt.show()
def test_plot_multiple_moving_melodic_features(current_file,
current_width=4,
current_bar_interval=1 / 4,
features=['pitch_mean'],
plot_bar_ticks=2,
key_rate_scale=None):
current = mp.read(current_file)
current_track = current.tracks[0]
current_track.start_time = current.start_times[0]
current_melody = Melody(current_track)
current_melody_length = current_melody.current_chord.bars(
current_melody.current_chord.start_time)
fig, axes = plt.subplots(len(features), figsize=(12, 8))
if len(features) == 1:
axes = [axes]
if key_rate_scale is None:
moving_melodic_features = current_melody.get_moving_melodic_features(
width=current_width, bar_interval=current_bar_interval)
points = [
current_melody.current_chord.start_time + i * current_bar_interval
for i in range(
0,
math.ceil((current_melody_length -
current_melody.current_chord.start_time) /
current_bar_interval))
]
for j, feature in enumerate(features):
if key_rate_scale is not None:
moving_melodic_features = current_melody.get_moving_melodic_features(
width=current_width,
bar_interval=current_bar_interval,
key_rate_scale=key_rate_scale[j])
current_features = [
getattr(i, feature) for i in moving_melodic_features
]
axes[j].plot(points, current_features)
axes[j].set(xlabel='bar')
if feature == 'key_rate':
axes[j].set(
ylabel=f'{feature} ({key_rate_scale[j].get_scale_name()})')
else:
axes[j].set(ylabel=feature)
x_major_locator = plt.MultipleLocator(plot_bar_ticks)
axes[j].xaxis.set_major_locator(x_major_locator)
plt.show()
if __name__ == '__main__':
midi_path = r'G:\music project files\mp3 and midi files\midi files'
current_file = r'G:\music project files\mp3 and midi files\midi files\牧羊人的眼泪(又名沉睡国度)xin.mid'
test_plot_multiple_moving_melodic_features(current_file,
current_width=4,
current_bar_interval=1 / 4,
plot_bar_ticks=2,
features=[
'pitch_mean', 'pitch_var',
'pitch_interval_mean',
'pitch_interval_var'
])