-
Notifications
You must be signed in to change notification settings - Fork 111
/
midi_util.py
217 lines (181 loc) · 7.5 KB
/
midi_util.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
"""
Handles MIDI file loading
"""
import midi
import numpy as np
import os
from constants import *
def midi_encode(note_seq, resolution=NOTES_PER_BEAT, step=1):
"""
Takes a piano roll and encodes it into MIDI pattern
"""
# Instantiate a MIDI Pattern (contains a list of tracks)
pattern = midi.Pattern()
pattern.resolution = resolution
# Instantiate a MIDI Track (contains a list of MIDI events)
track = midi.Track()
# Append the track to the pattern
pattern.append(track)
play = note_seq[:, :, 0]
replay = note_seq[:, :, 1]
volume = note_seq[:, :, 2]
# The current pattern being played
current = np.zeros_like(play[0])
# Absolute tick of last event
last_event_tick = 0
# Amount of NOOP ticks
noop_ticks = 0
for tick, data in enumerate(play):
data = np.array(data)
if not np.array_equal(current, data):# or np.any(replay[tick]):
noop_ticks = 0
for index, next_volume in np.ndenumerate(data):
if next_volume > 0 and current[index] == 0:
# Was off, but now turned on
evt = midi.NoteOnEvent(
tick=(tick - last_event_tick) * step,
velocity=int(volume[tick][index[0]] * MAX_VELOCITY),
pitch=index[0]
)
track.append(evt)
last_event_tick = tick
elif current[index] > 0 and next_volume == 0:
# Was on, but now turned off
evt = midi.NoteOffEvent(
tick=(tick - last_event_tick) * step,
pitch=index[0]
)
track.append(evt)
last_event_tick = tick
elif current[index] > 0 and next_volume > 0 and replay[tick][index[0]] > 0:
# Handle replay
evt_off = midi.NoteOffEvent(
tick=(tick- last_event_tick) * step,
pitch=index[0]
)
track.append(evt_off)
evt_on = midi.NoteOnEvent(
tick=0,
velocity=int(volume[tick][index[0]] * MAX_VELOCITY),
pitch=index[0]
)
track.append(evt_on)
last_event_tick = tick
else:
noop_ticks += 1
current = data
tick += 1
# Turn off all remaining on notes
for index, vol in np.ndenumerate(current):
if vol > 0:
# Was on, but now turned off
evt = midi.NoteOffEvent(
tick=(tick - last_event_tick) * step,
pitch=index[0]
)
track.append(evt)
last_event_tick = tick
noop_ticks = 0
# Add the end of track event, append it to the track
eot = midi.EndOfTrackEvent(tick=noop_ticks)
track.append(eot)
return pattern
def midi_decode(pattern,
classes=MIDI_MAX_NOTES,
step=None):
"""
Takes a MIDI pattern and decodes it into a piano roll.
"""
if step is None:
step = pattern.resolution // NOTES_PER_BEAT
# Extract all tracks at highest resolution
merged_replay = None
merged_volume = None
for track in pattern:
# The downsampled sequences
replay_sequence = []
volume_sequence = []
# Raw sequences
replay_buffer = [np.zeros((classes,))]
volume_buffer = [np.zeros((classes,))]
for i, event in enumerate(track):
# Duplicate the last note pattern to wait for next event
for _ in range(event.tick):
replay_buffer.append(np.zeros(classes))
volume_buffer.append(np.copy(volume_buffer[-1]))
# Buffer & downscale sequence
if len(volume_buffer) > step:
# Take the min
replay_any = np.minimum(np.sum(replay_buffer[:-1], axis=0), 1)
replay_sequence.append(replay_any)
# Determine volume by max
volume_sum = np.amax(volume_buffer[:-1], axis=0)
volume_sequence.append(volume_sum)
# Keep the last one (discard things in the middle)
replay_buffer = replay_buffer[-1:]
volume_buffer = volume_buffer[-1:]
if isinstance(event, midi.EndOfTrackEvent):
break
# Modify the last note pattern
if isinstance(event, midi.NoteOnEvent):
pitch, velocity = event.data
volume_buffer[-1][pitch] = velocity / MAX_VELOCITY
# Check for replay_buffer, which is true if the current note was previously played and needs to be replayed
if len(volume_buffer) > 1 and volume_buffer[-2][pitch] > 0 and volume_buffer[-1][pitch] > 0:
replay_buffer[-1][pitch] = 1
# Override current volume with previous volume
volume_buffer[-1][pitch] = volume_buffer[-2][pitch]
if isinstance(event, midi.NoteOffEvent):
pitch, velocity = event.data
volume_buffer[-1][pitch] = 0
# Add the remaining
replay_any = np.minimum(np.sum(replay_buffer, axis=0), 1)
replay_sequence.append(replay_any)
volume_sequence.append(volume_buffer[0])
replay_sequence = np.array(replay_sequence)
volume_sequence = np.array(volume_sequence)
assert len(volume_sequence) == len(replay_sequence)
if merged_volume is None:
merged_replay = replay_sequence
merged_volume = volume_sequence
else:
# Merge into a single track, padding with zeros of needed
if len(volume_sequence) > len(merged_volume):
# Swap variables such that merged_notes is always at least
# as large as play_sequence
tmp = replay_sequence
replay_sequence = merged_replay
merged_replay = tmp
tmp = volume_sequence
volume_sequence = merged_volume
merged_volume = tmp
assert len(merged_volume) >= len(volume_sequence)
diff = len(merged_volume) - len(volume_sequence)
merged_replay += np.pad(replay_sequence, ((0, diff), (0, 0)), 'constant')
merged_volume += np.pad(volume_sequence, ((0, diff), (0, 0)), 'constant')
merged = np.stack([np.ceil(merged_volume), merged_replay, merged_volume], axis=2)
# Prevent stacking duplicate notes to exceed one.
merged = np.minimum(merged, 1)
return merged
def load_midi(fname):
p = midi.read_midifile(fname)
cache_path = os.path.join(CACHE_DIR, fname + '.npy')
try:
note_seq = np.load(cache_path)
except Exception as e:
# Perform caching
os.makedirs(os.path.dirname(cache_path), exist_ok=True)
note_seq = midi_decode(p)
np.save(cache_path, note_seq)
assert len(note_seq.shape) == 3, note_seq.shape
assert note_seq.shape[1] == MIDI_MAX_NOTES, note_seq.shape
assert note_seq.shape[2] == 3, note_seq.shape
assert (note_seq >= 0).all()
assert (note_seq <= 1).all()
return note_seq
if __name__ == '__main__':
# Test
# p = midi.read_midifile("out/test_in.mid")
p = midi.read_midifile("out/test_in.mid")
p = midi_encode(midi_decode(p))
midi.write_midifile("out/test_out.mid", p)