pyquist 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyquist/__init__.py +40 -0
- pyquist/audio.py +505 -0
- pyquist/audio_test.py +447 -0
- pyquist/cli.py +102 -0
- pyquist/device.py +323 -0
- pyquist/device_test.py +508 -0
- pyquist/helper.py +133 -0
- pyquist/helper_test.py +107 -0
- pyquist/paths.py +13 -0
- pyquist/plot.py +285 -0
- pyquist/plot_test.py +190 -0
- pyquist/py.typed +0 -0
- pyquist/score.py +512 -0
- pyquist/score_test.py +476 -0
- pyquist/test_data/388954__fullmetaljedi__blues-riff-in-g-nylon.wav +0 -0
- pyquist/test_data/434013__mrpearch__drum-patern.mp3 +0 -0
- pyquist/test_data/ravel_bolero.mid +0 -0
- pyquist/web/__init__.py +7 -0
- pyquist/web/freesound.py +288 -0
- pyquist/web/theorytab.py +408 -0
- pyquist-0.1.0.dist-info/METADATA +180 -0
- pyquist-0.1.0.dist-info/RECORD +26 -0
- pyquist-0.1.0.dist-info/WHEEL +5 -0
- pyquist-0.1.0.dist-info/entry_points.txt +2 -0
- pyquist-0.1.0.dist-info/licenses/LICENSE +7 -0
- pyquist-0.1.0.dist-info/top_level.txt +1 -0
pyquist/score_test.py
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from .audio import Audio
|
|
6
|
+
from .paths import TEST_DATA_DIR
|
|
7
|
+
from .score import (
|
|
8
|
+
BasicMetronome,
|
|
9
|
+
Event,
|
|
10
|
+
Metronome,
|
|
11
|
+
MIDIMetronome,
|
|
12
|
+
Score,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
_BOLERO_MIDI = TEST_DATA_DIR / "ravel_bolero.mid"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _const_tone(duration, value, sample_rate=1000, **kwargs) -> Audio:
|
|
19
|
+
"""Test instrument: returns a flat-amplitude audio of ``duration`` seconds."""
|
|
20
|
+
return Audio(
|
|
21
|
+
np.full(int(duration * sample_rate), value, dtype=np.float32),
|
|
22
|
+
sample_rate=sample_rate,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _stereo_tone(duration, value, sample_rate=1000, **kwargs) -> Audio:
|
|
27
|
+
return Audio(
|
|
28
|
+
np.full((int(duration * sample_rate), 2), value, dtype=np.float32),
|
|
29
|
+
sample_rate=sample_rate,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class TestEvent(unittest.TestCase):
|
|
34
|
+
def test_construction_positional_and_keyword(self):
|
|
35
|
+
a = Event(0.5, {"pitch": 60})
|
|
36
|
+
b = Event(time=0.5, kwargs={"pitch": 60})
|
|
37
|
+
self.assertEqual(a, b)
|
|
38
|
+
self.assertEqual(a.time, 0.5)
|
|
39
|
+
self.assertEqual(a.kwargs, {"pitch": 60})
|
|
40
|
+
|
|
41
|
+
def test_unpacks_like_tuple(self):
|
|
42
|
+
event = Event(0.5, {"pitch": 60})
|
|
43
|
+
time, kwargs = event
|
|
44
|
+
self.assertEqual(time, 0.5)
|
|
45
|
+
self.assertEqual(kwargs, {"pitch": 60})
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class TestScoreListBehavior(unittest.TestCase):
|
|
49
|
+
def test_construction_from_iterable(self):
|
|
50
|
+
events = [Event(0.0, {}), Event(1.0, {})]
|
|
51
|
+
score = Score(events)
|
|
52
|
+
self.assertEqual(len(score), 2)
|
|
53
|
+
self.assertEqual(score[0], events[0])
|
|
54
|
+
|
|
55
|
+
def test_empty_construction(self):
|
|
56
|
+
self.assertEqual(len(Score()), 0)
|
|
57
|
+
|
|
58
|
+
def test_append_and_iterate(self):
|
|
59
|
+
score = Score()
|
|
60
|
+
score.append(Event(0.0, {}))
|
|
61
|
+
score.append(Event(1.0, {}))
|
|
62
|
+
times = [e.time for e in score]
|
|
63
|
+
self.assertEqual(times, [0.0, 1.0])
|
|
64
|
+
|
|
65
|
+
def test_slicing_returns_score(self):
|
|
66
|
+
score = Score([Event(t, {}) for t in (0.0, 1.0, 2.0)])
|
|
67
|
+
sliced = score[1:]
|
|
68
|
+
self.assertIsInstance(sliced, Score)
|
|
69
|
+
self.assertEqual(len(sliced), 2)
|
|
70
|
+
|
|
71
|
+
def test_addition_returns_score(self):
|
|
72
|
+
a = Score([Event(0.0, {})])
|
|
73
|
+
b = Score([Event(1.0, {})])
|
|
74
|
+
combined = a + b
|
|
75
|
+
self.assertIsInstance(combined, Score)
|
|
76
|
+
self.assertEqual(len(combined), 2)
|
|
77
|
+
# The whole point of UserList: this also works for 3+ scores.
|
|
78
|
+
c = Score([Event(2.0, {})])
|
|
79
|
+
big = a + b + c
|
|
80
|
+
self.assertIsInstance(big, Score)
|
|
81
|
+
self.assertEqual([e.time for e in big], [0.0, 1.0, 2.0])
|
|
82
|
+
|
|
83
|
+
def test_iadd_returns_score(self):
|
|
84
|
+
a = Score([Event(0.0, {})])
|
|
85
|
+
a += Score([Event(1.0, {})])
|
|
86
|
+
self.assertIsInstance(a, Score)
|
|
87
|
+
self.assertEqual(len(a), 2)
|
|
88
|
+
|
|
89
|
+
def test_multiplication_returns_score(self):
|
|
90
|
+
a = Score([Event(0.0, {})])
|
|
91
|
+
repeated = a * 3
|
|
92
|
+
self.assertIsInstance(repeated, Score)
|
|
93
|
+
self.assertEqual(len(repeated), 3)
|
|
94
|
+
|
|
95
|
+
def test_copy_returns_score(self):
|
|
96
|
+
a = Score([Event(0.0, {})])
|
|
97
|
+
self.assertIsInstance(a.copy(), Score)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class TestScoreProperties(unittest.TestCase):
|
|
101
|
+
def test_start_end_duration(self):
|
|
102
|
+
score = Score(Event(t, {}) for t in (0.5, 1.5, 3.0))
|
|
103
|
+
self.assertEqual(score.start_time, 0.5)
|
|
104
|
+
self.assertEqual(score.end_time, 3.0)
|
|
105
|
+
self.assertEqual(score.duration, 2.5)
|
|
106
|
+
|
|
107
|
+
def test_single_event(self):
|
|
108
|
+
score = Score([Event(2.0, {})])
|
|
109
|
+
self.assertEqual(score.start_time, 2.0)
|
|
110
|
+
self.assertEqual(score.end_time, 2.0)
|
|
111
|
+
self.assertEqual(score.duration, 0.0)
|
|
112
|
+
|
|
113
|
+
def test_unordered_events(self):
|
|
114
|
+
# start_/end_time use min/max, not first/last.
|
|
115
|
+
score = Score([Event(5.0, {}), Event(1.0, {}), Event(3.0, {})])
|
|
116
|
+
self.assertEqual(score.start_time, 1.0)
|
|
117
|
+
self.assertEqual(score.end_time, 5.0)
|
|
118
|
+
|
|
119
|
+
def test_empty_score_properties_raise(self):
|
|
120
|
+
empty = Score()
|
|
121
|
+
with self.assertRaises(ValueError):
|
|
122
|
+
empty.start_time
|
|
123
|
+
with self.assertRaises(ValueError):
|
|
124
|
+
empty.end_time
|
|
125
|
+
with self.assertRaises(ValueError):
|
|
126
|
+
empty.duration
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class TestScoreSegment(unittest.TestCase):
|
|
130
|
+
def test_offset_and_duration_default_relativizes(self):
|
|
131
|
+
# Default: event times are shifted so the segment starts at 0.
|
|
132
|
+
score = Score(Event(t, {}) for t in (0.0, 0.5, 1.0, 1.5, 2.0))
|
|
133
|
+
sliced = score.segment(offset=0.5, duration=1.0)
|
|
134
|
+
self.assertEqual([e.time for e in sliced], [0.0, 0.5])
|
|
135
|
+
|
|
136
|
+
def test_relativize_false_preserves_original_times(self):
|
|
137
|
+
score = Score(Event(t, {}) for t in (0.0, 0.5, 1.0, 1.5, 2.0))
|
|
138
|
+
sliced = score.segment(offset=0.5, duration=1.0, relativize=False)
|
|
139
|
+
self.assertEqual([e.time for e in sliced], [0.5, 1.0])
|
|
140
|
+
|
|
141
|
+
def test_relativize_preserves_kwargs(self):
|
|
142
|
+
# Only `time` is touched; kwargs (including duration_ticks etc.) stay.
|
|
143
|
+
score = Score([Event(2.0, {"pitch": 60, "duration": 0.25})])
|
|
144
|
+
sliced = score.segment(offset=1.0, duration=2.0)
|
|
145
|
+
self.assertEqual(sliced[0].time, 1.0)
|
|
146
|
+
self.assertEqual(sliced[0].kwargs, {"pitch": 60, "duration": 0.25})
|
|
147
|
+
|
|
148
|
+
def test_returns_score(self):
|
|
149
|
+
score = Score([Event(0.0, {}), Event(0.5, {})])
|
|
150
|
+
self.assertIsInstance(score.segment(offset=0.0, duration=10.0), Score)
|
|
151
|
+
|
|
152
|
+
def test_eps_excludes_exact_end(self):
|
|
153
|
+
score = Score([Event(0.0, {}), Event(1.0, {})])
|
|
154
|
+
# At offset=0 the relativize shift is 0, so the kept event equals the original.
|
|
155
|
+
self.assertEqual(list(score.segment(offset=0.0, duration=1.0)), [score[0]])
|
|
156
|
+
|
|
157
|
+
def test_inclusive_left_boundary(self):
|
|
158
|
+
score = Score([Event(0.0, {}), Event(0.5, {})])
|
|
159
|
+
sliced = score.segment(offset=0.5, duration=1.0)
|
|
160
|
+
self.assertEqual([e.time for e in sliced], [0.0])
|
|
161
|
+
|
|
162
|
+
def test_no_duration_means_unbounded(self):
|
|
163
|
+
# Default still relativizes when only offset is given.
|
|
164
|
+
score = Score(Event(t, {}) for t in (0.0, 1.0, 100.0))
|
|
165
|
+
self.assertEqual([e.time for e in score.segment(offset=0.5)], [0.5, 99.5])
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class TestBasicMetronome(unittest.TestCase):
|
|
169
|
+
def test_basic(self):
|
|
170
|
+
m = BasicMetronome(120)
|
|
171
|
+
self.assertEqual(m.bpm, 120)
|
|
172
|
+
self.assertEqual(m.beat_duration, 0.5)
|
|
173
|
+
self.assertEqual(m.tick_to_seconds(1.0), 0.5)
|
|
174
|
+
self.assertEqual(m.seconds_to_tick(0.5), 1.0)
|
|
175
|
+
|
|
176
|
+
def test_60_bpm(self):
|
|
177
|
+
m = BasicMetronome(60)
|
|
178
|
+
self.assertEqual(m.beat_duration, 1.0)
|
|
179
|
+
self.assertEqual(m.tick_to_seconds(1.0), 1.0)
|
|
180
|
+
self.assertEqual(m.seconds_to_tick(1.0), 1.0)
|
|
181
|
+
|
|
182
|
+
def test_round_trip(self):
|
|
183
|
+
m = BasicMetronome(140)
|
|
184
|
+
for tick in [0.0, 1.0, 2.5, 17.3]:
|
|
185
|
+
self.assertAlmostEqual(m.seconds_to_tick(m.tick_to_seconds(tick)), tick)
|
|
186
|
+
|
|
187
|
+
def test_subclassable(self):
|
|
188
|
+
class Halving(Metronome):
|
|
189
|
+
def tick_to_seconds(self, tick: float) -> float:
|
|
190
|
+
return tick / 2
|
|
191
|
+
|
|
192
|
+
def seconds_to_tick(self, seconds: float) -> float:
|
|
193
|
+
return seconds * 2
|
|
194
|
+
|
|
195
|
+
h = Halving()
|
|
196
|
+
self.assertEqual(h.tick_to_seconds(4.0), 2.0)
|
|
197
|
+
self.assertEqual(h.seconds_to_tick(2.0), 4.0)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
class TestScoreRender(unittest.TestCase):
|
|
201
|
+
def test_uniform_instrument(self):
|
|
202
|
+
# _const_tone is an Instrument: called with **event.kwargs → Audio.
|
|
203
|
+
score = Score([Event(0.0, {"value": 0.5, "duration": 0.01})])
|
|
204
|
+
audio = score.render(_const_tone)
|
|
205
|
+
self.assertEqual(audio.sample_rate, 1000)
|
|
206
|
+
self.assertEqual(audio.num_channels, 1)
|
|
207
|
+
self.assertEqual(audio.num_samples, 10)
|
|
208
|
+
self.assertTrue(np.allclose(audio.samples, 0.5))
|
|
209
|
+
|
|
210
|
+
def test_per_event_dispatch_inside_instrument(self):
|
|
211
|
+
# Per-event dispatch is just a branch inside the instrument — no
|
|
212
|
+
# separate factory concept needed. The deciding kwarg is captured as
|
|
213
|
+
# a named parameter; the rest flow through via **kwargs.
|
|
214
|
+
captured: list[tuple[str, float]] = []
|
|
215
|
+
|
|
216
|
+
def dispatch(value, **kwargs):
|
|
217
|
+
tag = "loud" if value >= 0.15 else "quiet"
|
|
218
|
+
captured.append((tag, value))
|
|
219
|
+
return _const_tone(value=value, **kwargs)
|
|
220
|
+
|
|
221
|
+
score = Score(
|
|
222
|
+
[
|
|
223
|
+
Event(0.0, {"value": 0.1, "duration": 0.01}),
|
|
224
|
+
Event(0.01, {"value": 0.2, "duration": 0.01}),
|
|
225
|
+
]
|
|
226
|
+
)
|
|
227
|
+
score.render(dispatch)
|
|
228
|
+
self.assertEqual(captured, [("quiet", 0.1), ("loud", 0.2)])
|
|
229
|
+
|
|
230
|
+
def test_kwargs_style_instrument_passed_directly(self):
|
|
231
|
+
# A **kwargs-style function is the native instrument form — no
|
|
232
|
+
# wrapping needed; render calls it as instrument(**event.kwargs).
|
|
233
|
+
def kwargs_only(duration, value, **kw):
|
|
234
|
+
return Audio(
|
|
235
|
+
np.full(int(duration * 1000), value, dtype=np.float32),
|
|
236
|
+
sample_rate=1000,
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
score = Score([Event(0.0, {"value": 0.5, "duration": 0.01})])
|
|
240
|
+
audio = score.render(kwargs_only)
|
|
241
|
+
self.assertEqual(audio.num_samples, 10)
|
|
242
|
+
self.assertTrue(np.allclose(audio.samples, 0.5))
|
|
243
|
+
|
|
244
|
+
def test_instrument_missing_kwargs_sink_raises_on_extra_keys(self):
|
|
245
|
+
# An instrument without a **kwargs sink raises TypeError when the
|
|
246
|
+
# event carries keys it doesn't declare (e.g. from_midi's rich kwargs).
|
|
247
|
+
def strict(duration, value):
|
|
248
|
+
return Audio(
|
|
249
|
+
np.full(int(duration * 1000), value, dtype=np.float32),
|
|
250
|
+
sample_rate=1000,
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
score = Score([Event(0.0, {"value": 0.5, "duration": 0.01, "extra": 1})])
|
|
254
|
+
with self.assertRaises(TypeError):
|
|
255
|
+
score.render(strict)
|
|
256
|
+
|
|
257
|
+
def test_single_event_with_offset(self):
|
|
258
|
+
score = Score([Event(0.005, {"value": 0.5, "duration": 0.01})])
|
|
259
|
+
audio = score.render(_const_tone)
|
|
260
|
+
self.assertGreaterEqual(audio.num_samples, 15)
|
|
261
|
+
self.assertTrue(np.all(audio.samples[:5] == 0.0))
|
|
262
|
+
self.assertTrue(np.all(audio.samples[5:15] == 0.5))
|
|
263
|
+
|
|
264
|
+
def test_non_overlapping(self):
|
|
265
|
+
score = Score(
|
|
266
|
+
[
|
|
267
|
+
Event(0.0, {"value": 0.3, "duration": 0.01}),
|
|
268
|
+
Event(0.01, {"value": 0.7, "duration": 0.01}),
|
|
269
|
+
]
|
|
270
|
+
)
|
|
271
|
+
audio = score.render(_const_tone)
|
|
272
|
+
self.assertEqual(audio.num_samples, 20)
|
|
273
|
+
self.assertTrue(np.allclose(audio.samples[:10], 0.3))
|
|
274
|
+
self.assertTrue(np.allclose(audio.samples[10:20], 0.7))
|
|
275
|
+
|
|
276
|
+
def test_overlapping_events_mix_additively(self):
|
|
277
|
+
score = Score(
|
|
278
|
+
[
|
|
279
|
+
Event(0.0, {"value": 0.2, "duration": 0.01}),
|
|
280
|
+
Event(0.0, {"value": 0.3, "duration": 0.01}),
|
|
281
|
+
]
|
|
282
|
+
)
|
|
283
|
+
audio = score.render(_const_tone)
|
|
284
|
+
self.assertEqual(audio.num_samples, 10)
|
|
285
|
+
self.assertTrue(np.allclose(audio.samples, 0.5))
|
|
286
|
+
|
|
287
|
+
def test_metronome_converts_ticks(self):
|
|
288
|
+
# 60_000 BPM → 1 tick = 0.001s = 1 sample @ sr=1000.
|
|
289
|
+
m = BasicMetronome(60_000)
|
|
290
|
+
score = Score([Event(5.0, {"value": 0.5, "duration": 0.01})])
|
|
291
|
+
audio = score.render(_const_tone, metronome=m)
|
|
292
|
+
self.assertTrue(np.all(audio.samples[:5] == 0.0))
|
|
293
|
+
self.assertTrue(np.all(audio.samples[5:15] == 0.5))
|
|
294
|
+
|
|
295
|
+
def test_inconsistent_sample_rates_raises(self):
|
|
296
|
+
score = Score(
|
|
297
|
+
[
|
|
298
|
+
Event(0.0, {"value": 0.5, "duration": 0.01, "sample_rate": 1000}),
|
|
299
|
+
Event(0.01, {"value": 0.5, "duration": 0.01, "sample_rate": 2000}),
|
|
300
|
+
]
|
|
301
|
+
)
|
|
302
|
+
with self.assertRaises(ValueError) as ctx:
|
|
303
|
+
score.render(_const_tone)
|
|
304
|
+
self.assertIn("sample rate", str(ctx.exception).lower())
|
|
305
|
+
|
|
306
|
+
def test_inconsistent_channel_counts_raises(self):
|
|
307
|
+
def dispatch(stereo, **kwargs):
|
|
308
|
+
return _stereo_tone(**kwargs) if stereo else _const_tone(**kwargs)
|
|
309
|
+
|
|
310
|
+
score = Score(
|
|
311
|
+
[
|
|
312
|
+
Event(0.0, {"value": 0.5, "duration": 0.01, "stereo": False}),
|
|
313
|
+
Event(0.01, {"value": 0.5, "duration": 0.01, "stereo": True}),
|
|
314
|
+
]
|
|
315
|
+
)
|
|
316
|
+
with self.assertRaises(ValueError) as ctx:
|
|
317
|
+
score.render(dispatch)
|
|
318
|
+
self.assertIn("channel", str(ctx.exception).lower())
|
|
319
|
+
|
|
320
|
+
def test_missing_sample_rate_raises(self):
|
|
321
|
+
def _no_sr_instrument(**kwargs):
|
|
322
|
+
return Audio(np.zeros(10, dtype=np.float32)) # no sample_rate
|
|
323
|
+
|
|
324
|
+
score = Score([Event(0.0, {})])
|
|
325
|
+
with self.assertRaises(ValueError):
|
|
326
|
+
score.render(_no_sr_instrument)
|
|
327
|
+
|
|
328
|
+
def test_invalid_return_type_raises(self):
|
|
329
|
+
def bad(**kwargs):
|
|
330
|
+
return 42
|
|
331
|
+
|
|
332
|
+
with self.assertRaises(TypeError) as ctx:
|
|
333
|
+
Score([Event(0.0, {})]).render(bad)
|
|
334
|
+
self.assertIn("Audio", str(ctx.exception))
|
|
335
|
+
|
|
336
|
+
def test_empty_score_returns_zero_length_audio(self):
|
|
337
|
+
audio = Score().render(_const_tone)
|
|
338
|
+
self.assertEqual(audio.num_samples, 0)
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
class TestScoreFromMidi(unittest.TestCase):
|
|
342
|
+
"""Tests against the bundled Ravel Bolero MIDI file."""
|
|
343
|
+
|
|
344
|
+
def test_returns_score_and_metronome(self):
|
|
345
|
+
score, metronome = Score.from_midi(_BOLERO_MIDI)
|
|
346
|
+
self.assertIsInstance(score, Score)
|
|
347
|
+
self.assertIsInstance(metronome, MIDIMetronome)
|
|
348
|
+
self.assertGreater(len(score), 0)
|
|
349
|
+
for event in score:
|
|
350
|
+
self.assertIsInstance(event, Event)
|
|
351
|
+
|
|
352
|
+
def test_event_kwargs_have_expected_keys(self):
|
|
353
|
+
score, _ = Score.from_midi(_BOLERO_MIDI)
|
|
354
|
+
expected = {
|
|
355
|
+
"mtype",
|
|
356
|
+
"duration_ticks",
|
|
357
|
+
"duration",
|
|
358
|
+
"pitch",
|
|
359
|
+
"velocity",
|
|
360
|
+
"program",
|
|
361
|
+
"is_drum",
|
|
362
|
+
"channel",
|
|
363
|
+
}
|
|
364
|
+
for event in score[:50]:
|
|
365
|
+
self.assertEqual(set(event.kwargs.keys()), expected)
|
|
366
|
+
self.assertEqual(event.kwargs["mtype"], "note")
|
|
367
|
+
|
|
368
|
+
def test_kwarg_value_ranges(self):
|
|
369
|
+
score, _ = Score.from_midi(_BOLERO_MIDI)
|
|
370
|
+
for event in score:
|
|
371
|
+
kw = event.kwargs
|
|
372
|
+
self.assertEqual(kw["mtype"], "note")
|
|
373
|
+
self.assertGreaterEqual(kw["pitch"], 0)
|
|
374
|
+
self.assertLessEqual(kw["pitch"], 127)
|
|
375
|
+
self.assertGreaterEqual(kw["velocity"], 0)
|
|
376
|
+
self.assertLessEqual(kw["velocity"], 127)
|
|
377
|
+
self.assertGreaterEqual(kw["program"], 0)
|
|
378
|
+
self.assertLessEqual(kw["program"], 127)
|
|
379
|
+
self.assertIsInstance(kw["is_drum"], bool)
|
|
380
|
+
self.assertGreater(kw["duration"], 0.0)
|
|
381
|
+
self.assertGreater(kw["duration_ticks"], 0)
|
|
382
|
+
|
|
383
|
+
def test_as_notes_false_emits_raw_note_on_and_note_off(self):
|
|
384
|
+
score, _ = Score.from_midi(_BOLERO_MIDI, as_notes=False)
|
|
385
|
+
types = {e.kwargs["mtype"] for e in score}
|
|
386
|
+
# Only note_on / note_off in this mode (all_events=False).
|
|
387
|
+
self.assertEqual(types, {"note_on", "note_off"})
|
|
388
|
+
# Each event's kwargs should be the raw mido message attributes
|
|
389
|
+
# (no "duration"/"duration_ticks"/"program"/"is_drum").
|
|
390
|
+
for event in score[:20]:
|
|
391
|
+
self.assertIn("note", event.kwargs)
|
|
392
|
+
self.assertIn("velocity", event.kwargs)
|
|
393
|
+
self.assertIn("channel", event.kwargs)
|
|
394
|
+
self.assertNotIn("duration", event.kwargs)
|
|
395
|
+
self.assertNotIn("duration_ticks", event.kwargs)
|
|
396
|
+
self.assertNotIn("time", event.kwargs) # delta-time stripped
|
|
397
|
+
|
|
398
|
+
def test_as_notes_false_emits_at_least_twice_as_many_events(self):
|
|
399
|
+
# Each paired "note" consumes one note_on + one note_off, so the raw
|
|
400
|
+
# count is at least 2x the paired count (and possibly more if the
|
|
401
|
+
# file contains stray unmatched note_on / note_off messages).
|
|
402
|
+
notes, _ = Score.from_midi(_BOLERO_MIDI, as_notes=True)
|
|
403
|
+
raw, _ = Score.from_midi(_BOLERO_MIDI, as_notes=False)
|
|
404
|
+
self.assertGreaterEqual(len(raw), 2 * len(notes))
|
|
405
|
+
|
|
406
|
+
def test_all_events_true_includes_non_note_events(self):
|
|
407
|
+
score, _ = Score.from_midi(_BOLERO_MIDI, all_events=True)
|
|
408
|
+
types = {e.kwargs["mtype"] for e in score}
|
|
409
|
+
# "note" should still appear (as_notes=True is the default), and
|
|
410
|
+
# standard non-note types should also be present in Bolero.
|
|
411
|
+
self.assertIn("note", types)
|
|
412
|
+
self.assertIn("set_tempo", types)
|
|
413
|
+
self.assertIn("program_change", types)
|
|
414
|
+
|
|
415
|
+
def test_all_events_with_as_notes_false_emits_only_raw_events(self):
|
|
416
|
+
score, _ = Score.from_midi(_BOLERO_MIDI, as_notes=False, all_events=True)
|
|
417
|
+
types = {e.kwargs["mtype"] for e in score}
|
|
418
|
+
# No paired "note" events in this mode.
|
|
419
|
+
self.assertNotIn("note", types)
|
|
420
|
+
# Raw note events and tempo/program changes should still appear.
|
|
421
|
+
self.assertIn("note_on", types)
|
|
422
|
+
self.assertIn("note_off", types)
|
|
423
|
+
self.assertIn("set_tempo", types)
|
|
424
|
+
|
|
425
|
+
def test_events_are_sorted_by_time(self):
|
|
426
|
+
score, _ = Score.from_midi(_BOLERO_MIDI)
|
|
427
|
+
times = [e.time for e in score]
|
|
428
|
+
self.assertEqual(times, sorted(times))
|
|
429
|
+
|
|
430
|
+
def test_first_event_is_a_drum_at_tick_zero(self):
|
|
431
|
+
score, _ = Score.from_midi(_BOLERO_MIDI)
|
|
432
|
+
first = score[0]
|
|
433
|
+
self.assertEqual(first.time, 0)
|
|
434
|
+
self.assertTrue(first.kwargs["is_drum"])
|
|
435
|
+
|
|
436
|
+
def test_accepts_already_parsed_mido_object(self):
|
|
437
|
+
import mido
|
|
438
|
+
|
|
439
|
+
mid = mido.MidiFile(str(_BOLERO_MIDI))
|
|
440
|
+
score_a, _ = Score.from_midi(_BOLERO_MIDI)
|
|
441
|
+
score_b, _ = Score.from_midi(mid)
|
|
442
|
+
self.assertEqual(len(score_a), len(score_b))
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
class TestMIDIMetronome(unittest.TestCase):
|
|
446
|
+
def test_zero_tick_is_zero_seconds(self):
|
|
447
|
+
m = MIDIMetronome(_BOLERO_MIDI)
|
|
448
|
+
self.assertEqual(m.tick_to_seconds(0), 0.0)
|
|
449
|
+
|
|
450
|
+
def test_round_trip_within_tick_resolution(self):
|
|
451
|
+
m = MIDIMetronome(_BOLERO_MIDI)
|
|
452
|
+
for tick in [0, 384, 1000, 4608, 100_000]:
|
|
453
|
+
seconds = m.tick_to_seconds(tick)
|
|
454
|
+
self.assertAlmostEqual(m.seconds_to_tick(seconds), tick, delta=1)
|
|
455
|
+
|
|
456
|
+
def test_known_tick_matches_known_seconds(self):
|
|
457
|
+
# Probed against the bundled file: tempo is ~62 BPM (mspb=967741).
|
|
458
|
+
# 1 quarter note = 384 ticks → 384 ticks ≈ 0.9677 s.
|
|
459
|
+
m = MIDIMetronome(_BOLERO_MIDI)
|
|
460
|
+
self.assertAlmostEqual(m.tick_to_seconds(384), 0.9677, delta=0.001)
|
|
461
|
+
|
|
462
|
+
def test_tick_to_seconds_is_monotonic(self):
|
|
463
|
+
m = MIDIMetronome(_BOLERO_MIDI)
|
|
464
|
+
previous = -1.0
|
|
465
|
+
for tick in range(0, 100_000, 1000):
|
|
466
|
+
seconds = m.tick_to_seconds(tick)
|
|
467
|
+
self.assertGreaterEqual(seconds, previous)
|
|
468
|
+
previous = seconds
|
|
469
|
+
|
|
470
|
+
def test_ticks_per_beat_matches_file(self):
|
|
471
|
+
m = MIDIMetronome(_BOLERO_MIDI)
|
|
472
|
+
self.assertEqual(m.ticks_per_beat, 384)
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
if __name__ == "__main__":
|
|
476
|
+
unittest.main()
|
|
Binary file
|
|
Binary file
|
|
Binary file
|