forked from kohstephen/xylophone-dropzone
/
midi2motors.py
521 lines (443 loc) · 18.3 KB
/
midi2motors.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
from __future__ import print_function
from mingus.containers.instrument import MidiInstrument
from mingus.midi.midi_file_in import MIDI_to_Composition
from mingus.midi.midi_file_out import write_Composition, write_Track,\
write_Bar, write_Note
from mingus.midi import fluidsynth
from mingus.containers.composition import Composition
from mingus.containers.track import Track
from mingus.containers.bar import Bar
from mingus.containers.note_container import NoteContainer
from mingus.containers.note import Note
from mingus.core.intervals import measure
import sys
from math import floor
import warnings
from threading import Thread
import Queue
import serial
'''Function sets the range and type of instrument.'''
def create_instrument(low, high):
xylo = MidiInstrument()
'''
9 = 'Glockenspiel',
11 = 'Vibraphone',
12 = 'Marimba',
13 = 'Xylophone'
'''
xylo.instrument_nr = 11
xylo.set_range((low, high))
xylo.name = 'ballophone'
return xylo
'''Function prompts user for name of MIDI input or output file.'''
def get_name(kind, reserved = None):
if (kind == 'in'):
file = str(raw_input('Please enter the name of the MIDI to be used: '))
elif (kind == 'out'):
# user prohibited from overwriting infile
file = str(raw_input('Please enter the name of the MIDI to be written: '))
while (reserved == file):
file = str(raw_input('You may not overwrite %s. Please enter a \
different name for the output MIDI: '))
return file
'''Function prints output from MIDI_to_Composition.'''
def print_midi(c, bpm):
print(bpm)
print(c, '\n')
'''Function writes rebuilt composition and playback events to location
specified by user and in a user-friendly format.'''
def write_log(c, bpm, filename, pb = None):
# remove mid extension
filename = filename[:-3]
# add txt extension
filename += 'txt'
f = open(filename, 'w')
f.write('BPM: ' + str(bpm) + '\n')
for i in range(len(c)):
f.write('TRACK %d\n' % i)
for j in range(len(c[i])):
f.write('\tBAR %d\n' % j)
for lst in c[i][j]:
f.write('\t\t' + str(lst) + '\n')
if (pb != None):
print('--> Please refer to %s for output to be copied to Arduino \
program <--' % filename)
f.write('\n\nPLAYBACK EVENTS:\n\n')
f.write('%-20s%-20s\n' % ('time (ms):','lsa combo shorthand: '))
for lst in pb:
f.write('%-20s%-20s\n' % (str(lst[0]), str(lst[1])))
f.write('\n\nCOPY THE BELOW OUTPUT TO \'pb\' IN \'stepMotors.c\':\n\n')
f.write('{')
for i in range(len(pb)):
f.write(str(pb[i][0]) + ', \"' + str(pb[i][1]) + '\"')
if (i < len(pb) - 1):
f.write(',\n')
f.write('}')
'''Wrapper function for is_good_track.'''
def select_tracks(c, bpm, xylo):
fluidsynth.init("VintageDreamsWavesv2.sf2")
for i in range(len(c)):
if (is_good_track(c[i], bpm, xylo)):
c.selected_tracks.append(i)
return c.selected_tracks
'''Function prompts user whether or not a track should be included in
what is played by the instrument. Function also acts as a wrapper for
play_track_to_user.'''
def is_good_track(track, bpm, xylo):
play_track_to_user(track, bpm)
# query user to have them indicate whether or not track should be
# included in what gets played by device until they give an
# acceptable response
while (True):
response = str(raw_input('Should this track be included? (\'y\'/\'Y\'' + \
'= \'Yes\', \'n\'/\'N\' = \'No\'): '))
if (response == 'y' or response == 'Y'):
return True
elif (response == 'n' or response == 'N'):
return False
'''Function uses FluidSynth to play a portion of a single track to a user.'''
def play_track_to_user(track, bpm):
# TODO: play track to user w/ FluidSynth, either a small portion
# of it or allow user to exit out of playback
t = Thread(target = start_track, args = (track, bpm,))
t2 = Thread(target=listen_to_user, args=())
t.start()
t2.start()
# if t ends, kill t2
def start_track(track, bpm):
# t = Thread(target=listen_to_user, args=())
# t.start()
print("Playing track... (hit 'a' if you've Already heard enough)")
fluidsynth.play_Track(track, 1, bpm)
def listen_to_user():
while 1:
if (raw_input() == 'a'):
fluidsynth.stop_everything()
'''Function uses FluidSynth to play a portion of the entire composition to a user.'''
def play_comp_to_user(c, bpm):
# TODO: play c to user w/ FluidSynth, either a small portion
# of it or allow user to exit out of playback
pass
# print("Here's what will be played on the ballophone...")
# fluidsynth.play_Composition(c, bpm = bpm)
'''
Function creates a new composition object using an old one. The new one:
1) only contains the tracks the user approved
2) has each track's instrument set to 'xylo'
3) has garbage notes filtered out
4) has every note transposed within range of instrument
'''
def rebuild_composition(old, xylo, cutoff):
lowest = Note.__int__(Note('C', 8))
highest = Note.__int__(Note('C', 0))
new = Composition()
for i in old.selected_tracks:
t = Track()
t.instrument = xylo
for bar in old[i]:
b = Bar()
b.key.name = bar.key.name
b.set_meter(bar.meter)
# note value per beat == the denominator in time signature
dem = b.meter[1]
for lst in bar:
value = lst[1]
if (is_garbage(value, dem, cutoff[old.selected_tracks.index(i)])):
continue
# do not include lists without notes
if (not lst[2]):
continue
nc = NoteContainer()
for note in lst[2]:
if (Note.__int__(note) < lowest): lowest = Note.__int__(note)
if (Note.__int__(note) > highest): highest = Note.__int__(note)
nc + note
b.place_notes(nc, value)
t.add_bar(b)
new.add_track(t)
# can't do the transposing until all notes in all tracks have been
# compared against lowest and highest, which is why it is done here
n1 = Note()
n2 = Note()
low = n1.from_int(lowest)
high = n2.from_int(highest)
# print("lowest and highest notes:", low, high)
if (not xylo.notes_in_range([low, high])):
new = transposer(new, lowest, highest, xylo)
return new
'''Wrapper function for simple_filter.'''
def is_garbage(value, dem, cutoff):
return simple_filter(value, dem, cutoff)
'''Function indicates whether a note's brevity falls below a user-
specified cutoff.'''
def simple_filter(value, dem, cutoff):
# eliminating NoteContainers with really long 'value' (really brief)
return floor(value/dem) > cutoff
'''Function does a better job at indicating whether a note should not
be included in the rebuilt composition.'''
def better_filter():
# TODO: filter out notes in a way that does not require supervision
# e.g. user does not have to specify specific cases for which notes
# should be removed
pass
'''Wrapper function for simple_transpose and better_transpose.'''
def transposer(c, lowest, highest, xylo):
'''
5 cases:
1) lowest < xylo.range[0] and highest > xylo.range[1]
2) lowest < xylo.range[0] and highest < xylo.range[0]
3) lowest > xylo.range[1] and highest > xylo.range[1]
4) lowest < xylo.range[0]
5) highest > xylo.range[1]
(1) definitely requires better_transpose
(2) and (4) require better_transpose if highest + (xylo.range[0] - lowest) > xylo.range[1]
(3) and (5) require better_transpose if lowest - (highest - xylo.range[1]) < xylo.range[0]
'''
xylo_low = Note.__int__(xylo.range[0])
xylo_high = Note.__int__(xylo.range[1])
if (lowest < xylo_low and highest > xylo_high):
c = better_transpose('foo', 'bar')
elif (lowest < xylo_low):
# minimum number of half steps to transpose by such that lowest
# note in the entire composition is within the range of the
# instrument
interval = xylo_low - lowest
if (highest + interval > xylo_high):
c = better_transpose('foo', 'bar')
else:
# whether you can transpose a full octave
full = False
if (highest + interval + 12 <= xylo_high): full = True
c = simple_transpose(c, full, interval, up = True)
elif (highest > xylo_high):
interval = highest - xylo_high
if (lowest - interval < xylo_low):
c = better_transpose('foo', 'bar')
else:
full = False
if (lowest - interval - 12 >= xylo_low): full = True
c = simple_transpose(c, full, interval, up = False)
return c
'''Function transposes every track in the composition by a uniform amount.'''
def simple_transpose(c, full, interval, up):
# determine whether can transpose by a full octave (only want to
# transpose to nearest octave)
# TODO: introduce recursive calls so track transposes itself
# until it is within range, using full octave jumps unless
# necessary to do half steps
if (full):
for i in range(len(c)):
for j in range(len(c[i])):
for k in range(len(c[i][j])):
for l in range(len(c[i][j][k][2])):
if (up):
c[i][j][k][2][l].octave_up()
else:
c[i][j][k][2][l].octave_down()
else:
for i in range(len(c)):
for j in range(len(c[i])):
for k in range(len(c[i][j])):
for l in range(len(c[i][j][k][2])):
n = Note()
if (up):
c[i][j][k][2][l] = n.from_int(Note.__int__(c[i][j][k][2][l]) + interval)
else:
c[i][j][k][2][l] = n.from_int(Note.__int__(c[i][j][k][2][l]) - interval)
'''
else:
for i in range(len(c)):
c[i] = c[i].transpose(interval, up)
'''
return c
'''Function transposes tracks in the composition by different amounts.'''
def better_transpose(foo, bar):
# TODO: transpose a composition that has notes falling outside
# range of instrument regardless of what uniform shift is applied
print('better_transpose required')
sys.exit(0)
'''Function creates a MIDI file of the rebuild composition in the user-
specified location.'''
def write_comp(c, bpm, filename):
write_Composition(filename, c, bpm)
'''Function converts seconds float to milliseconds integer.'''
def convert_to_ms(secs):
return int(round((secs * 1000)))
'''Function creates a compact data structure representing the rebuilt
composition.'''
def create_playback_events(c, bpm, xylo, lsa_ordering):
secs_in_min = 60.0
# seconds per beat
spb = secs_in_min / bpm
# dict of noteContainers containing time-noteContainer pairings
times_dict = {}
for track in c:
cur_time = 0
for bar in track:
# beats per bar
bpb = bar.meter[0]
for lst in bar:
time_into_bar = lst[0] * bpb * spb
key = convert_to_ms(cur_time + time_into_bar)
if (key in times_dict):
times_dict[key] + lst[2]
else:
times_dict[key] = lst[2]
cur_time += bpb * spb
# list of lists containing [time, lsa-combo (int)]
# list(set(noteContainer)) to remove repeated notes
pb = [ [key, notes_to_lsa_bin(list(set(times_dict[key])), xylo, \
lsa_ordering)] for key in times_dict ]
# sort on each sublist's first element (time)
pb = sorted(pb, key=lambda x: x[0], reverse=False)
validate_pb(pb, lsa_ordering, times_dict)
return pb
'''Function creates an integer whose binary form represents the on/off
states of every LSA at a time when notes are being played.'''
def notes_to_lsa_bin(notes, xylo, lsa_ordering):
# convert to int so that sharps of one note and flat of next mean
# the same thing
keys = [ Note.__int__(Note(n)) for n in lsa_ordering ]
vals = range(len(lsa_ordering))
# first lsa's note mapped to 0, second lsa's note mapped to 1, ...
lsa_dict = dict(zip(keys, vals))
bnry = ['0'] * len(lsa_dict)
for note in notes:
# flip LSA to 'on' state for that note
bnry[lsa_dict[Note.__int__(note)]] = '1'
# so that number of leading zeros do not affect place of 'on' LSAs
bnry.reverse()
bnry = ''.join(bnry)
# int whose binary represents the on and off states of all LSAs
# at a given time
combo = int(bnry, 2)
return combo
'''Function checks whether the decoded integer represents the correct
LSAs for every time when notes are played.'''
def validate_pb(pb, lsa_ordering, times_dict):
for lst in pb:
time = lst[0]
lsa_code = lst[1]
original = list(set(times_dict[time]))
decoded = decode_lsas(lsa_code, lsa_ordering)
if (not NoteContainer(original) == NoteContainer(decoded)):
warnings.warn('incorrect LSA encoding at time = %.3f' % (time))
'''Function decodes which combination of notes are being played at a given
time.'''
def decode_lsas(lsa_code, lsa_ordering):
bnry = [d for d in str(bin(lsa_code))[2:]]
notes = []
for i in range(len(bnry)):
if (bnry[i] == '1'):
notes.append(Note(lsa_ordering[(len(bnry) - 1) - i]))
return notes
'''Function for sending playback events to the Arduino.'''
def send_to_Arduino(pbs):
# TODO: implement automated communication with Arduino
pass
'''
# TODO: replace with actual client information
ser = serial.Serial('/dev/tty.usbserial', 9600)
wait_for_response()
# send time followed by lsa-combo for each pb event
for pb in pbs:
ser.write(pb[0])
ser.write(pb[1])
'''
'''Function waits until Arduino declares itself ready.'''
def wait_for_response():
# TODO: implement ability for python program to wait until Arduino
# is ready to receive
pass
def slow_down_composition(c, bpm, step_time):
notes_dict = create_dict(c, bpm)
smallest = step_time
for note in notes_dict:
# TODO: vectorize the below
diff = [j - i for i, j in zip(notes_dict[note][:-1], notes_dict[note][1:])]
if (min(diff) < smallest):
smallest = min(diff)
print(smallest)
if (smallest < step_time):
return (smallest / step_time)
else:
return 1
'''Function creates dictionary of lists of times at which notes are
played in the song. Function has no purpose other than to serve as
an additional way for a user to see a song.'''
def create_dict(c, bpm):
secs_in_min = 60.0
# seconds per beat
spb = secs_in_min / bpm
cur_time = 0
notes_dict = {}
for i in range(len(c.selected_tracks)):
for bar in c[i]:
# beats per bar
bpb = bar.meter[0]
for lst in bar:
time_into_bar = lst[0] * bpb * spb
for note in lst[2]:
note = note.name + '-' + str(int(note.octave))
if note in notes_dict:
notes_dict[note].append(cur_time + time_into_bar)
else:
notes_dict[note] = [cur_time + time_into_bar]
cur_time += bpb * spb
return notes_dict
'''Function prints dictionary of lists of note-time pairings.'''
def print_dict(notes_dict):
for key in notes_dict:
print('%s: [' % key, end = "")
for time in notes_dict[key]:
print('%.2f, ' % time, end = "")
print(']')
def main():
# REPLACE WITH RELEVANT PATHS
path_to_midi = '../../../Xylo/midis/'
path_to_log = '../../../Xylo/logs/'
# F-3 to F-6 would be 37 keys. are we getting rid of 'F-3' or 'F-6'?
low = Note('F-3')
high = Note('F-6')
xylo = create_instrument(low, high)
infile = get_name('in')
c, bpm = MIDI_to_Composition(path_to_midi + infile)
write_log(c, bpm, path_to_log + infile)
# query user to select tracks we actually want from the midi
c.selected_tracks = select_tracks(c, bpm, xylo)
# INSPECT LOG TO SEE WHICH TRACK NUMBERS YOU WANT INCLUDED AND
# PUT THEM INTO THE BELOW LIST
# c.selected_tracks = [1] # for mhall, only the track at index 1 is to be included
# ratio of highest note value to keep in composition to beat unit.
# e.g. if meter is 4/4, then a 'cutoff' of 2 would eliminate any
# notes whose value exceeds 8 (anything briefer than an eighth note)
cutoff = [8] * len(c.selected_tracks)
# composition containing only the selected tracks, the correct notes,
# with everything transposed w/in the range of the instrument
c = rebuild_composition(c, xylo, cutoff)
# seconds it takes for motor to step the necessary amount to drop
# a ball
step_time = 0.1
# multiply BPM by the ratio of smallest gap between
# a note played consecutively and step_time -- only if this number
# is less than 1
# bpm *= slow_down_composition(c, bpm, step_time)
# print(bpm)
play_comp_to_user(c, bpm)
# check that everything sounds ok with selected tracks, removed notes,
# and transposition
outfile = get_name('out', infile)
write_Composition(path_to_midi + outfile, c, bpm)
# TODO: replace with actual ordering once known. ordering by
# distance from device center
lsa_ordering = ['F-3', 'F#-3', 'G-3', 'G#-3', 'A-3', 'A#-3', 'B-3',
'C-4', 'C#-4', 'D-4', 'D#-4', 'E-4', 'F-4', 'F#-4',
'G-4', 'G#-4', 'A-4', 'A#-4', 'B-4', 'C-5', 'C#-5',
'D-5', 'D#-5', 'E-5', 'F-5', 'F#-5', 'G-5', 'G#-5',
'A-5', 'A#-5', 'B-5', 'C-6', 'C#-6', 'D-6', 'D#-6',
'E-6', 'F-6']
# don't need to pass xylo here -- range contained in c[0]
pb = create_playback_events(c, bpm, xylo, lsa_ordering)
send_to_Arduino(pb)
write_log(c, bpm, path_to_log + outfile, pb)
if __name__ == "__main__": main()