-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi-blaster.py
529 lines (386 loc) · 12.5 KB
/
midi-blaster.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
522
523
524
525
526
527
528
529
#Author: Benjamin Caccia
#Email: reverbtank@gmail.com
#Website: www.bcacciaaudio.com
#Year: 2015
#MIDI Blaster version 0.0.7
import pygame
import pygame.midi
import time #for MIDI delay time
import os #for clearing the terminal
import sys #platform identification
from colorama import init, Fore #ANSI color rendering support
from Tkinter import * #the entire Tkinter library. This allows us to kill the root window that is spawned by dialogs.
import tkFileDialog #for pop up dialog for file selection
import codecs #for unicode filename handling
def main_menu():
#identifies OS and appropriately clears the terminal
os.system(identify_os())
global inputMIDI
global outputMIDI
print "\nMIDI Blaster 0.0.7"
print "------------------- \n \n"
print "1. Select MIDI Input"
print "2. Select MIDI Output"
print "3. Send MIDI"
print(Fore.GREEN)
print "\nMIDI input: ", inputMIDI + 1
print "MIDI output: ", outputMIDI + 1
print(Fore.RESET)
while True:
try:
inputChoice = int(raw_input("\nEnter the number of your choice: "))
except ValueError:
print("ERROR! Enter a valid menu choice.")
#return to the start of the loop
continue
if inputChoice < 1 or inputChoice > 3:
print("ERROR! Enter a valid menu choice.")
continue
else:
if inputChoice == 1:
inputMIDI = choose_input_device()
break
if inputChoice == 2:
outputMIDI = choose_output_device()
break
if inputChoice == 3:
midi_send_menu()
break
def midi_send_menu():
os.system(identify_os())
print "\nMIDI Blaster 0.0.7"
print "------------------- \n \n"
while True:
print "1. Enter list of MIDI to send"
print "2. Back"
print(Fore.GREEN)
print "\nMIDI input: ", inputMIDI + 1
print "MIDI output: ", outputMIDI + 1
print(Fore.RESET)
try:
inputChoice = int(raw_input("\nEnter the number of your choice: "))
except ValueError:
print("ERROR! Enter a valid menu choice.")
#return to the start of the loop
continue
if inputChoice < 1 or inputChoice > 2:
print("ERROR! Enter a valid menu choice.")
continue
else:
if inputChoice == 1:
midi_send_entry()
break
if inputChoice == 2:
main_menu()
break
def choose_input_device():
os.system(identify_os())
pygame.init()
pygame.midi.init()
deviceNum = pygame.midi.get_count()
defaultDevIn = pygame.midi.get_default_input_id()
defaultDevOut = pygame.midi.get_default_output_id()
print "Number of Devices: [%s]" % deviceNum
print "Default MIDI input [%s]" % defaultDevIn
print "Default MIDI output [%s]" % defaultDevOut
print "\nThese are all available MIDI interfaces"
print "-----------------------------------------------------------"
#Iterate through and print all available MIDI interfaces
for i in range(deviceNum):
interface = pygame.midi.get_device_info(i)
#trim the first garbage part of the device info string
print i + 1, interface[1:]
print "-----------------------------------------------------------"
while True:
try:
inputChoice = int(raw_input("\nEnter the number of the MIDI interface you want to use for input: "))
except ValueError:
print("ERROR! Enter a valid interface number.")
#return to the start of the loop
continue
if inputChoice > deviceNum or inputChoice < 0:
print("ERROR! Enter a valid interface number.")
continue
else:
#exit the loop
break
pygame.midi.quit()
return inputChoice - 1
def choose_output_device():
os.system(identify_os())
pygame.init()
pygame.midi.init()
deviceNum = pygame.midi.get_count()
defaultDevIn = pygame.midi.get_default_input_id()
defaultDevOut = pygame.midi.get_default_output_id()
print "Number of Devices: [%s]" % deviceNum
print "Default MIDI input [%s]" % defaultDevIn
print "Default MIDI output [%s]" % defaultDevOut
print "\nThese are all available MIDI interfaces"
print "-----------------------------------------------------------"
#Iterate through and print all available MIDI interfaces
for i in range(deviceNum):
interface = pygame.midi.get_device_info(i)
#trim the first garbage part of the device info string
print i +1, interface[1:]
print "-----------------------------------------------------------"
while True:
try:
outputChoice = int(raw_input("\nEnter the number of the MIDI interface you want to use for output: "))
except ValueError:
print("ERROR! Enter a valid interface number.")
#return to the start of the loop
continue
if outputChoice > deviceNum or outputChoice < 0:
print("ERROR! Enter a valid interface number.")
continue
else:
#exit the loop
break
pygame.midi.quit()
return outputChoice - 1
def identify_os():
system = sys.platform
if system == "win32":
return "cls"
else:
return "clear"
#checks to make sure that input is between 0 and 127
def midi_range(value):
while value < 0 or value > 127:
value = input("Enter a value between 0 and 127:")
else:
return value
def channel_range(value):
while value < 1 or value > 16:
value = input("Enter a value between 1 and 16:")
else:
return value
#Setup an event loop to listen for MIDI
#Pulled from example code at:
#https://audiodestrukt.wordpress.com/2013/06/23/midi-programming-in-python/
def midi_input():
while True:
if inputMIDI.poll():
print inputMIDI.read(1000)
pygame.time.wait(10)
def midi_send_entry():
os.system(identify_os())
pygame.init()
pygame.midi.init()
global outputMIDI
global sendRepeat
print "\n\nEnter values in HEX (must be preceded with 0x). Enter delay in milliseconds."
print "----------------------------------------"
print "Enter requested values all separated by commas."
print "\nMain menu = m"
print "\nSend = n"
print "\nLoad batch file = i"
print "\nOutput queue to file = o"
print "\nDelete entry = d"
print "\nClear = c"
print "\nReverse queue order = f"
print "\nSet number of times to send contents of queue = r"
#print "\nReplace all status bytes = sr (broken)"
#grants access to globally define variable
global outputList
userMIDI = 0
while userMIDI != "n" or userMIDI != "N":
#prints entire list for user
print(Fore.GREEN)
print "\n\nMIDI items in Queue:", len(outputList)
print "Send entire Queue x:", sendRepeat
print(Fore.RESET)
for i in range(len(outputList)):
print(Fore.YELLOW)
print i + 1, " ", outputList[i]
print(Fore.RESET)
#Example MIDI input values would be "0x90,0x36,0x7F,0"
print "\n\n"
userMIDI = raw_input("Status, Note, Velocity, Send Delay: ")
#userMIDI is split into a list so it can be easily counted without the commas
listTally = []
listTally = userMIDI.split(",")
#checks to make sure that there are 4 values(including commas) being input
if userMIDI == "d" or userMIDI == "D":
userErase = int(raw_input("\nEnter the number of the command to remove: "))
del outputList[userErase - 1]
print userErase, " removed successfully!"
continue
elif userMIDI == "c" or userMIDI == "C":
outputList = []
continue
elif userMIDI == "n" or userMIDI == "N":
break
elif userMIDI == "f" or userMIDI == "F":
flipBucket = []
for i in reversed(outputList):
flipBucket.append(i)
outputList = flipBucket
continue
elif userMIDI == "i" or userMIDI == "I":
midi_batch_send()
continue
elif userMIDI == "o" or userMIDI == "O":
midi_queue_write()
continue
elif userMIDI == "m" or userMIDI == "M":
main_menu()
continue
elif userMIDI == "r" or userMIDI == "R":
sendRepeat = int(raw_input("Enter number of times to send the contents of the queue: "))
continue
#incomplete
elif userMIDI == "sr" or userMIDI == "SR":
newList = []
tempStatusByte = raw_input("\nEnter the new status byte: ")
print outputList[0][0]
for i in range(len(outputList)):
print range(len(outputList))
tempList = list(outputList[0][i])
tempList[0:4] = tempStatusByte
outputList[0][i] = ''.join(tempList)
print outputList
print range(len(outputList))
continue
elif len(listTally) != 4:
while len(listTally) != 4:
userMIDI = raw_input("ERROR! You must enter 4 comma separated values!: ")
listTally = userMIDI.split(",")
else:
#appends midi to list
outputList.append([userMIDI])
else:
#appends midi to list
outputList.append([userMIDI])
midi_send(outputMIDI)
def midi_send(port):
#store this function into a variable so it can easily be deleted and closed at the end
sender = pygame.midi.Output(port)
global outputList
global sendRepeat
counter = 0
pygame.init()
pygame.midi.init()
#try something
try:
while counter < sendRepeat:
#iterate through the list and move each individual list into the tmp variable
for i in range(len(outputList)):
tmp1 = 0
tmp1 = outputList[i]
#splits all list assets into their own variables
status, pitch, velocity, delay = tmp1[0].split(",")
if status[0:2] == "0x":
status = int(status, 16)
pitch = int(pitch, 16)
velocity = int(velocity, 16)
delay = int(delay, 16)
#convert all values to integers so pygame.midi can send them
else:
status = int(status)
pitch = int(pitch)
velocity = int(velocity)
delay = int(delay)
#sleeps for defined millisecond amount. Note that sleep command
#cannot go lower than 10-13ms due to Windows kernel clock rate.
#Unix based OS can get closer to 1ms.
time.sleep(float(delay) / float(1000))
sender.write([[[status, pitch, velocity],0]])
print i + 1, " ", outputList[i]
counter += 1
#catch any thrown exceptions due to bad MIDI data that has been entered by the user.
except:
print "\nIllegal operation, check send queue for incorrect values."
print "Press Enter to return to queue."
stop = raw_input("")
midi_send_entry()
#reset the send repeat counter to 1 so it only sends MIDI data once by default.
sendRepeat = 1
del sender
pygame.midi.quit()
print "\n\n"
print len(outputList), "messages sent."
print "\n1. Send more MIDI"
print "2. Main Menu"
choice = int(raw_input("\nEnter the number of your choice: "))
if choice == 1:
midi_send_entry()
elif choice == 2:
main_menu()
else:
main_menu()
def midi_batch_send():
print "\nSelect the file that you want to load your MIDI commands from:"
root = Tk()
root.withdraw()
inputFile = tkFileDialog.askopenfilename(parent = root)
global outputList
try:
f = open(inputFile)
tempList = []
for line in f:
#strip() takes out all \n characters so they are not displayed in the list
if line == "\n":
continue
#ignores comment lines which are denoted by a #
elif '#' in line:
continue
#it is then appended to the main list
else:
tempList.append(line.strip())
outputList.append(tempList)
tempList = []
print outputList
#close the file object
f.close
#call send function
midi_send_entry()
#catch any exception caused by a corrupt file
except:
print "Invalid or corrupt file."
print "Press Enter to return to queue."
stop = raw_input("")
midi_send_entry()
#half implemented, broken
def midi_queue_write():
global outputList
print "\nEnter the filename and location to save your file:"
root = Tk()
root.withdraw()
outputFile = tkFileDialog.asksaveasfilename(defaultextension = '.txt', parent = root)
#calls codecs.open to deal with unicode filenames
f = codecs.open(outputFile, encoding ='utf-8', mode='w')
try:
#iterates through queue and writes list entry plus a newline. Note that notepad doesn't display \n
for item in outputList:
f.write(str(item[0] + '\n'))
#close open file object
f.close()
midi_send_entry()
except:
print "Error! Returning to send menu."
stop = raw_input("")
midi_send_entry()
def sysex_send(port):
pygame.init()
pygame.midi.init()
bufferSize = raw_input("Enter in the buffer size: ")
# set the number of the device you want to send with here. Set the buffer size here as well.
sender = pygame.midi.Output(port, buffer_size = bufferSize) #Set buffer size here.
# Send the SysEx. In the example below, I'm sending a simple Device Inquiry.
sender.write_sys_ex(0, [0xF0,0x7E,0x0,0x06,0x01,0xF7]) #Put SysEx here. Everything must be preceded with 0X.
# Delete the sender object and then close all running pygame instances. Makes a clean exit, no crash.
del sender
pygame.midi.quit()
# No progress bar. When sending is complete the app will quit
#initializes colorama
init()
#define global variables
inputMIDI = 0
outputMIDI = 0
outputList = []
sendRepeat = 1
#master program loop
while True:
main_menu()