-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotMeasure.py
executable file
·62 lines (55 loc) · 1.95 KB
/
plotMeasure.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
#!/usr/bin/env python3
import mido
import numpy as np
import matplotlib.pyplot as plt
from collections import defaultdict
from events import EventQue
def plot_midi_events(events,title=None):
# Prepare data for plotting
ranges = events.channel_note_ranges()
colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray']
y = 0
y_values = []
y_labels = []
channels = sorted(ranges.keys())
for ch_idx,channel in enumerate(channels):
for nn_idx in range(ranges[channel][1] - ranges[channel][0] + 1):
nn = ranges[channel][0] + nn_idx
if nn in events.events[channel].keys():
y_values.append(y)
y_labels.append('Ch %d:% 3d' % (channel, nn))
x_range = events.events[channel][nn]
if isinstance(x_range, list):
for range_element in x_range:
start = range_element[0]
end = range_element[1]
if not end:
plt.scatter(start, y, marker='D', color=colors[ch_idx])
else:
plt.barh(y, end - start, left=start, height=0.1, color=colors[ch_idx])
if x_range:
y += 1
if title:
plt.title(title)
plt.xlim([0,events.duration])
plt.yticks(y_values, y_labels)
plt.xlabel('Time')
plt.tight_layout()
plt.show()
def plot_midi(f, title=None):
m = None
if isinstance(f, str):
m = mido.MidiFile(f)
elif isinstance(f, mido.MidiFile):
m = f
elif isinstance(f, mido.MidiTrack):
m = mido.MidiFile()
m.tracks.append(f)
else:
raise TypeError
eventq = EventQue(m)
plot_midi_events(eventq, title)
if __name__ == '__main__':
mf = mido.MidiFile('demo/redeemed-melody.mid')
eventq = EventQue(mf)
plot_midi_events(eventq)