-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
369 lines (219 loc) · 7.88 KB
/
test.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
360
361
362
363
364
365
366
367
368
369
"""
A set of components and tools to test yap-midi components.
"""
import asyncio
import struct
from ymidi.decoder import ModularDecoder, MetaDecoder
from ymidi.events.voice import NoteEvent, NoteOff, NoteOn
from ymidi.events.system.system_exc import SystemExclusive
from ymidi.events.builtin import StopPattern, StartTrack
from ymidi.events.meta import MetaText
from ymidi.handlers.maps import GLOBAL
from ymidi.handlers.track import time_profile
from ymidi.io import container
from ymidi.io.file import MIDIFile
from ymidi.containers import Track, Pattern
from ymidi.io.base import RouteIO
from ymidi.io.container import ContainerIO
from ymidi.misc import ytime
from ymidi.protocol import BlockingFileProtocol
from ymidi.seq import YMSequencer
def midi_stream():
# Test MIDI stream decoding:
"""
This example byte stream is super weird,
and is actually not valid MIDI data!
Still, it is good to be able to handle stream interrupts,
incase some synths implement this feature.
"""
stream = [0x90,60,0xF8,0xF0,1,2,0xF8,3,4,5,0xF7,64,60,0x80,30,0xF8,0x90,55,55,30,64,78,23,0xF8]
#stream = bytes(stream)
print(stream)
print(type(stream))
# Time, Time, SystemExclusive(1,2,3,4,5), On(60,64), Time, On(55,55), Off(30,30), On(60,64), On(78,23), Time
decoder = ModularDecoder()
decoder.load_default()
final = []
#print(decoder.collection)
# Decode the data:
for bts in stream:
print("Decoding value: {}".format(bts))
print(type(bts))
value = decoder.seq_decode(bts)
print("Response: {}".format(value))
if value is not None:
final.append(value)
if isinstance(value, NoteEvent):
print("Pitch: {} ; Velocity: {}".format(value.pitch, value.velocity))
if isinstance(value, SystemExclusive):
print("SystemExc values: {}".format(value.data))
print(final)
def variable_decode():
# Test variable length decoding:
stream = [0xF0, 1, 2, 3, 4, 5, 0xF7]
decoder = ModularDecoder()
decoder.load_default()
final = []
for bts in stream:
print("Decoding value: {}".format(bts))
value = decoder.seq_decode(bts)
print("Response: {}".format(value))
if value is not None:
final.append(value)
if isinstance(value, SystemExclusive):
print("Data: {}".format(value.data))
def varlen_decoding():
"""
Tests if we can decode variable length integers.
"""
meta = MetaDecoder()
# 59:
byts = meta.write_varlen(0x0fffffff)
print("Varlen: {}".format(byts))
final = meta.read_varlen(byts)
print("Final: {}".format(final))
async def midi_file():
"""
Tests the MIDI file functionality.
We load a MIDI file and inspect it's content.
Test MIDI File Size: 2079 Bytes
"""
# Create a MIDI File:
file = MIDIFile('purcell_queen.mid')
# Start the MIDI File:
await file.start()
start = await file.get()
# Print some stats:
print("Number of Tracks: {}".format(start.num_tracks))
print("File format: {}".format(start.format))
print("File length: {}".format(start.length))
print("Divisions: {}".format(start.divisions))
print("Builtin length: {}".format(len(start)))
print(file.collection.qsize())
while file.has_events():
# Get the event:
print("Getting event ...")
event = await file.get()
print(event)
if isinstance(event, StartTrack):
print("Start of track: {}".format(event.length))
if event is StopPattern:
print("End of MIDI file!")
break
if isinstance(event, MetaText):
print("Text event, contents: {}".format(event.text))
print(bytes(event))
print("No more events!")
async def single_track():
"""
Tests the single track functionality of yap-midi Track objects.
"""
track = Track()
# Add time_profile for performance testing:
track.out_hands[GLOBAL].append(time_profile)
on = NoteOn(1, 1)
track.append(on)
off = NoteOff(1,1)
off.delta = 100
track.append(off)
on = NoteOn(2,2)
on.delta = 200
track.append(on)
text = MetaText.fromstring("This is a test!")
text.delta = 400
track.append(text)
off = NoteOff(4,4)
off.delta = 300
# This time, insert the object in between the on and text events:
track.insert(3, off)
# Print for fun:
print(track)
# Iterate and get some info:
for event in track:
print("Event: {}".format(event))
print("Delta: {}".format(event.delta))
print("Delta time: {}".format(event.delta_time))
print("Tick number: {}".format(event.tick))
print("Total time: {}".format(event.time))
# Play the events:
print("--------- Start Playback: ---------")
track.start_playback()
av = 0
while track.index < len(track):
res = await track.time_get()
print("Got event: {}".format(res))
print("Exit delta: {}".format(res.exit_delta))
print("Actual time: {}".format(res.time))
print("Time diff: {}".format(res.exit_delta - res.time))
print("Time diff(seconds): {}".format((res.exit_delta - res.time) / 1000000))
av += (res.exit_delta - res.time)
print("--------- Stop Playback: ---------")
av = av / len(track)
print("Average time diff: {}".format(av))
print("Average time diff (Seconds): {}".format(av / 1000000))
async def test_route():
# Tests the IO routing capabilities of yap-midi
loop = asyncio.get_running_loop()
print(loop.is_running)
pattern = ContainerIO()
file = MIDIFile(path='majicalljarr.mid')
# Set the protocol:
file.proto = BlockingFileProtocol('majicalljarr.mid')
route = RouteIO()
# Connect the modules together:
print("loading file...")
route.load_input(file)
print("Done loading file...")
print("Loading pattern...")
route.load_output(pattern)
print("Done loading pattern....")
# Wait until we are done:
print("Waiting ...")
#await route.tasks[0]
#await asyncio.sleep(2)
await file.wait()
print("Done waiting!")
# Output contents:
for num, track in enumerate(pattern.container):
print("------ Track: {} ------".format(num))
print(track)
print("------ End of Track: {} -------".format(num))
print(track[-1].time)
print(track[-1].time / 1000)
print(track[-1].tick)
print(track[-1].track)
input()
# Play the track!
start = ytime()
pattern.container.start_playback()
print("Start playback!")
while pattern.container.playing:
# Get the event and output it:
event = await pattern.container.time_get()
print("Got event playing: {}".format(event))
elapsed = ytime() - start
print("Elapsed: {}".format(elapsed))
print("Elapsed (Seconds): {}".format(elapsed / 1000000))
async def test_seq():
"""
The big shabang - Test out the sequencer class!
"""
# Sequencer object to test:
seq = YMSequencer()
# Create a FileIO class for input:
file = MIDIFile(path='majicalljarr.mid')
# Register the callbacks:
@seq.callback(NoteOn.statusmsg)
async def handler_on(hand, event):
print("Got note on: {}".format(event))
@seq.callback(NoteOff.statusmsg)
async def handler_off(hand, event):
print("Got note off: {}".format(event))
# Create a ContainerIO class for output:
pattern = ContainerIO()
# Attach both IO modules:
seq.input.load_module(file)
seq.output.load_module(pattern)
# We should wait here or something ...
#midi_stream()
asyncio.run(test_route(), debug=True)