-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
inputs.py
359 lines (313 loc) · 11.7 KB
/
inputs.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""Input interfaces.
Functions
---------
- load
- from_pretty_midi
- read
"""
import json
from pathlib import Path
from typing import Union
import numpy as np
from pretty_midi import PrettyMIDI
from .multitrack import DEFAULT_RESOLUTION, Multitrack
from .track import BinaryTrack, StandardTrack, Track
from .utils import reconstruct_sparse
__all__ = ["load", "from_pretty_midi", "read"]
def load(path: Union[str, Path]) -> Multitrack:
"""Load a NPZ file into a Multitrack object.
Supports only files previously saved by :func:`pypianoroll.save`.
Parameters
----------
path : str or Path
Path to the file to load.
See Also
--------
:func:`pypianoroll.save` : Save a Multitrack object to a NPZ file.
:func:`pypianoroll.read` : Read a MIDI file into a Multitrack
object.
"""
with np.load(path) as loaded:
if "info.json" not in loaded:
raise RuntimeError("Cannot find `info.json` in the NPZ file.")
# Load the info dictionary
info_dict = json.loads(loaded["info.json"].decode("utf-8"))
# Get the resolution
resolution = info_dict.get("resolution")
# Look for `beat_resolution` for backward compatibility
if resolution is None:
resolution = info_dict.get("beat_resolution")
if resolution is None:
raise RuntimeError(
"Cannot find `resolution` or `beat_resolution` in "
"`info.json`."
)
# Load the tracks
idx = 0
tracks = []
while str(idx) in info_dict:
name = info_dict[str(idx)].get("name")
program = info_dict[str(idx)].get("program")
is_drum = info_dict[str(idx)].get("is_drum")
pianoroll = reconstruct_sparse(loaded, "pianoroll_" + str(idx))
if pianoroll.dtype == np.bool_:
track: Track = BinaryTrack(
name=name,
program=program,
is_drum=is_drum,
pianoroll=pianoroll,
)
elif pianoroll.dtype == np.uint8:
track = StandardTrack(
name=name,
program=program,
is_drum=is_drum,
pianoroll=pianoroll,
)
else:
track = Track(
name=name,
program=program,
is_drum=is_drum,
pianoroll=pianoroll,
)
tracks.append(track)
idx += 1
return Multitrack(
name=info_dict["name"],
resolution=resolution,
tempo=loaded.get("tempo"),
beat=loaded.get("beat"),
downbeat=loaded.get("downbeat"),
tracks=tracks,
)
def from_pretty_midi(
midi: PrettyMIDI,
resolution: int = DEFAULT_RESOLUTION,
mode: str = "max",
algorithm: str = "normal",
collect_onsets_only: bool = False,
first_beat_time: float = None,
) -> Multitrack:
"""Return a Multitrack object converted from a PrettyMIDI object.
Parse a :class:`pretty_midi.PrettyMIDI` object. The data type of the
resulting piano rolls is automatically determined (int if 'mode' is
'sum' and np.uint8 if `mode` is 'max').
Parameters
----------
midi : :class:`pretty_midi.PrettyMIDI`
PrettyMIDI object to parse.
resolution : int, default: `pypianoroll.DEFAULT_RESOLUTION` (24)
Time steps per quarter note.
mode : {'max', 'sum'}, default: 'max'
Merging strategy for duplicate notes.
algorithm : {'normal', 'strict', 'custom'}, default: 'normal'
Algorithm for finding the location of the first beat (see
Notes).
collect_onsets_only : bool, default: False
True to collect only the onset of the notes (i.e. note on
events) in all tracks, where the note off and duration
information are discarded. False to parse regular piano rolls.
first_beat_time : float, optional
Location of the first beat, in sec. Required and only
effective when using 'custom' algorithm.
Returns
-------
:class:`pypianoroll.Multitrack`
Converted Multitrack object.
Notes
-----
There are three algorithms for finding the location of the first
beat:
- 'normal' : Estimate the location of the first beat using
:meth:`pretty_midi.PrettyMIDI.estimate_beat_start`.
- 'strict' : Set the location of the first beat to the time of the
first time signature change. Raise a RuntimeError if no time
signature change is found.
- 'custom' : Set the location of the first beat to the value of
argument `first_beat_time`. Raise a ValueError if
`first_beat_time` is not given.
If an incomplete beat before the first beat is found, an additional
beat will be added before the (estimated) beat starting time.
However, notes before the (estimated) beat starting time for more
than one beat are dropped.
"""
if mode not in ("max", "sum"):
raise ValueError("`mode` must be either 'max' or 'sum'.")
# Set first_beat_time for 'normal' and 'strict' modes
if algorithm == "normal":
if midi.time_signature_changes:
midi.time_signature_changes.sort(key=lambda x: x.time)
first_beat_time = midi.time_signature_changes[0].time
else:
first_beat_time = midi.estimate_beat_start()
elif algorithm == "strict":
if not midi.time_signature_changes:
raise RuntimeError(
"No time signature change event found. Unable to set beat "
"start time using 'strict' algorithm."
)
midi.time_signature_changes.sort(key=lambda x: x.time)
first_beat_time = midi.time_signature_changes[0].time
elif algorithm == "custom":
if first_beat_time is None:
raise TypeError(
"`first_beat_time` must be given when using 'custom' "
"algorithm."
)
if first_beat_time < 0.0:
raise ValueError("`first_beat_time` must be a positive number.")
else:
raise ValueError(
"`algorithm` must be one of 'normal', 'strict' or 'custom'."
)
# get tempo change event times and contents
tc_times, tempi = midi.get_tempo_changes()
arg_sorted = np.argsort(tc_times)
tc_times = tc_times[arg_sorted]
tempi = tempi[arg_sorted]
beat_times = midi.get_beats(first_beat_time)
if not beat_times.size:
raise ValueError("Cannot get beat timings to quantize the piano roll.")
beat_times.sort()
n_beats = len(beat_times)
n_time_steps = resolution * n_beats
# Parse downbeat array
if not midi.time_signature_changes:
# This probably won't happen as pretty_midi always add a 4/4 time
# signature at time 0
beat = None
downbeat = None
else:
beat = np.zeros((n_time_steps, 1), bool)
downbeat = np.zeros((n_time_steps, 1), bool)
beat[0] = True
downbeat[0] = True
start = 0
end = start
for idx, tsc in enumerate(midi.time_signature_changes):
start_idx = start * resolution
if idx + 1 < len(midi.time_signature_changes):
end += np.searchsorted(
beat_times[end:], midi.time_signature_changes[idx + 1].time
)
end_idx = end * resolution
else:
end_idx = n_time_steps
beat[start_idx:end_idx:resolution] = True
stride = tsc.numerator * resolution
downbeat[start_idx:end_idx:stride] = True
start = end
# Build tempo array
one_more_beat = 2 * beat_times[-1] - beat_times[-2]
beat_times_one_more = np.append(beat_times, one_more_beat)
bpm = 60.0 / np.diff(beat_times_one_more)
tempo = np.tile(bpm, (1, 24)).reshape(-1, 1)
# Parse the tracks
tracks = []
for instrument in midi.instruments:
if mode == "max":
pianoroll = np.zeros((n_time_steps, 128), np.uint8)
else:
pianoroll = np.zeros((n_time_steps, 128), int)
pitches = np.array(
[
note.pitch
for note in instrument.notes
if note.end > first_beat_time
]
)
note_on_times = np.array(
[
note.start
for note in instrument.notes
if note.end > first_beat_time
]
)
beat_indices = np.searchsorted(beat_times, note_on_times) - 1
remained = note_on_times - beat_times[beat_indices]
ratios = remained / (
beat_times_one_more[beat_indices + 1] - beat_times[beat_indices]
)
rounded = np.round((beat_indices + ratios) * resolution)
note_ons = rounded.astype(int)
if collect_onsets_only:
pianoroll[note_ons, pitches] = True
elif instrument.is_drum:
velocities = [
note.velocity
for note in instrument.notes
if note.end > first_beat_time
]
pianoroll[note_ons, pitches] = velocities
else:
note_off_times = np.array(
[
note.end
for note in instrument.notes
if note.end > first_beat_time
]
)
beat_indices = np.searchsorted(beat_times, note_off_times) - 1
remained = note_off_times - beat_times[beat_indices]
ratios = remained / (
beat_times_one_more[beat_indices + 1]
- beat_times[beat_indices]
)
note_offs = ((beat_indices + ratios) * resolution).astype(int)
for idx, start in enumerate(note_ons):
end = note_offs[idx]
velocity = instrument.notes[idx].velocity
if velocity < 1:
continue
if 0 < start < n_time_steps:
if pianoroll[start - 1, pitches[idx]]:
pianoroll[start - 1, pitches[idx]] = 0
if end < n_time_steps - 1:
if pianoroll[end, pitches[idx]]:
end -= 1
if mode == "max":
pianoroll[start:end, pitches[idx]] += velocity
else:
maximum = np.maximum(
pianoroll[start:end, pitches[idx]], velocity
)
pianoroll[start:end, pitches[idx]] = maximum
if mode == "max":
track: Track = StandardTrack(
name=str(instrument.name),
program=int(instrument.program),
is_drum=bool(instrument.is_drum),
pianoroll=pianoroll,
)
else:
track = Track(
name=str(instrument.name),
program=int(instrument.program),
is_drum=bool(instrument.is_drum),
pianoroll=pianoroll,
)
tracks.append(track)
return Multitrack(
resolution=resolution,
tempo=tempo,
beat=beat,
downbeat=downbeat,
tracks=tracks,
)
def read(path: Union[str, Path], **kwargs) -> Multitrack:
"""Read a MIDI file into a Multitrack object.
Parameters
----------
path : str or Path
Path to the file to read.
**kwargs
Keyword arguments to pass to
:func:`pypianoroll.from_pretty_midi`.
See Also
--------
:func:`pypianoroll.write` : Write a Multitrack object to a MIDI
file.
:func:`pypianoroll.load` : Load a NPZ file into a Multitrack object.
"""
return from_pretty_midi(PrettyMIDI(str(path)), **kwargs)