-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsmm.py
454 lines (357 loc) · 10.8 KB
/
dsmm.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
#!/usr/bin/env python3
import rtmidi, sys, threading, time
import tkinter as tk
#midiin = rtmidi.RtMidiIn()
#ports = range(midiin.getPortCount())
notes=[]
#def dsmmText(delay, text):
# dsmmMain.update_idletasks()
# dsmmMain.after(delay,dsmmMain.pictureString.set(text))
logo="""
### ### # # # #
# # # ## ## ## ##
# # ## # # # # # #
# # # # # # #
### ### # # # #
"""
mainHelpText = """
Main
=====
This software shows hashes
on place each Matrix.
The picture has a
resolution 16x8
because of resolution
the single MIDI channel
to 128 notes.
Table below illustrates,
how this software runs.
+-----+-----------+
| | Midi note |
| Row | number |
+-----+-----------+
| 0 | 0-15 |
| 1 | 16-31 |
| 2 | 32-47 |
| 3 | 48-63 |
| 4 | 64-79 |
| 5 | 80-95 |
| 6 | 96-111 |
| 7 | 112-127 |
+-----------------+
"""
aboutText = """
\n\n
Damn Simple MIDI Matrix \n
by Ar2Di2 \n
\n\n
If do You have any suggestions,\n
please write me on
artur.wrona91 (at) gmail (dot) com
\n\n
"""
def initPort():
messageText=""
dsmmMain.text(10, " ")
for i in ports:
device = rtmidi.RtMidiIn()
messageText+='OPENING '
messageText+=dev.getPortName(i)
messageText+="\n"
dsmmMain.text(10, messageText)
collector = Collector(device, i)
collector.start()
collectors.append(collector)
messageText="\n"
dsmmMain.text(1000, " ")
global running
running=True
def quitPorts():
for c in collectors:
c.quit = True
def scanning():
if running:
#print(ports)
dsmmMain.text(0, drawMatrix(notes))
dsmmMain.after(0, scanning)
def destroyPort():
for c in collectors:
c.quit = True
def print_message(midi):
if midi.isNoteOn():
noteNumber=midi.getNoteNumber()
notes.append(noteNumber)
return noteNumber
elif midi.isNoteOff():
#print('OFF:', midi.getMidiNoteName(midi.getNoteNumber()))
notes.clear()
#print("OFF")
return 0
elif midi.isController():
pass
def tuiDrawMatrix(fps):
time.sleep(1/fps)
print("\033c")
print(drawMatrix(notes))
return 0
def drawMatrix(notes):
#label.after(250)
allNotes=range(0,128)
count=1
charString=""
#charString+=makeHorizontalFrame(18)
for i in allNotes:
noteIn=(i in notes)
## Input Hash or Space if MIDI input
if (noteIn==False):
char=" "
elif (noteIn==True):
char="#"
#### rows divisions
if ((count%16==0) and (count!=0)):
#print(char, end="#\n")
charString+=char+"\n"
elif (count%16==1):
charString+=char
else:
charString+=char
count+=1
return charString
####### GUI
class dsmmApp(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.master.title('DSMM')
self.bind('<Escape>', self.escapeHandle)
self.bind('m', self.chooseMidiHandle)
self.bind('<F1>', self.helpHandle)
self.pictureString=tk.StringVar()
self.grid()
self.createWidgets()
self.intro(10)
self.text(1000, "")
self.dsmmStart()
def intro(self, delay):
string=logo
string+="Damn Simple MIDI Matrix"
string+="\n\n"
string+="by Ar2Di2"
self.text(delay,string)
def escapeHandle(self, event=None):
self.close()
def chooseMidiHandle(self, event=None):
self.chooseMidi()
def helpHandle(self, event=None):
self.help()
def dsmmStart(self):
self.after(500, initPort)
self.after(1000, scanning)
def text(self, delay, text):
self.update_idletasks()
self.after(delay, self.pictureString.set(text))
def close(self):
self.update_idletasks()
global running
running=False
self.text(100, "Bye, bye!!!")
self.after(1000, self.master.destroy)
def chooseMidi(self):
self.update_idletasks()
global running
running=False
self.text(100, "Choose MIDI Devices")
dsmmChooseMidi=dsmmMidi()
dsmmChooseMidi.focus_set()
dsmmChooseMidi.mainloop()
def help(self):
self.update_idletasks()
global running
running=False
#self.text(1000, "Help")
dsmmHelpWindow=dsmmHelp()
dsmmHelpWindow.focus_set()
dsmmHelpWindow.mainloop()
def createWidgets(self):
self.matrixFrame = tk.Label(
self,
textvariable=self.pictureString,
bg='black',
fg='white',
width=30,
height=20,
anchor="center",
font ="Mono 20"
)
self.quitButton = tk.Button(
self,
text='Quit (Esc)',
command=self.close
)
self.midiButton = tk.Button(
self,
text='MIDI (M)',
command=self.chooseMidi
)
self.helpButton = tk.Button(
self,
text='Help (F1)',
command=self.help
)
self.matrixFrame.grid(row=0, columnspan=3)
self.quitButton.grid(row=1, column=0)
self.midiButton.grid(row=1, column=1)
self.helpButton.grid(row=1, column=2)
class dsmmMidi(tk.Toplevel):
def __init__(self, parent=None):
tk.Toplevel.__init__(self, parent)
self.title('MIDI')
self.geometry('+0+0')
self.bind('<Escape>', self.escapeHandle)
self.bind('<Return>', self.enterHandle)
self.grid()
self.createWidgets()
def listPorts(self):
for each_item in allPorts:
self.port = dev.getPortName(each_item)
self.midiDevices.insert(tk.END, self.port)
#for each_item in range(len(ports)):
# self.midiDevices.insert(tk.END, ports[each_item])
def escapeHandle(self, event=None):
self.cancel()
def enterHandle(self, event=None):
self.applyAndExit()
def apply(self):
global ports
ports=[]
self.selected=self.midiDevices.curselection()
stringText=""
for i in self.selected:
ports.append(i)
ports=self.selected
def cancel(self):
self.destroy()
quitPorts()
dsmmMain.dsmmStart()
def applyAndExit(self):
self.apply()
self.cancel()
#self.after(200, self.cancel())
def text(self, delay, text):
self.update_idletasks()
self.after(delay, self.pictureString.set(text))
def createWidgets(self):
self.midiDevices = tk.Listbox(
self,
selectmode = "multiple",
exportselection = 0
)
self.applyAndExitButton = tk.Button(
self,
text="OK" ,
command=self.applyAndExit
)
self.applyButton = tk.Button(
self,
text="Apply" ,
command=self.apply
)
self.cancelButton = tk.Button(
self,
text="Cancel",
command=self.cancel
)
self.listPorts()
self.midiDevices.grid(row=0, column=0, columnspan=3)
self.applyButton.grid(row=1, column=1)
self.applyAndExitButton.grid(row=1, column=0)
self.cancelButton.grid(row=1, column=2)
class dsmmHelp(tk.Toplevel):
def __init__(self, parent=None):
tk.Toplevel.__init__(self, parent)
self.geometry('+0+0')
self.title("Help")
self.pictureString=tk.StringVar()
self.bind('<Escape>', self.escapeHandle)
self.bind('<F1>', self.mainHelpHandle)
self.bind('<F2>', self.aboutHandle)
self.grid()
self.createWidgets()
def text(self, delay, text):
self.update_idletasks()
self.after(delay, self.pictureString.set(text))
def escapeHandle(self, event=None):
self.cancel()
def aboutHandle(self, event=None):
self.about()
def mainHelpHandle(self, event=None):
self.mainHelp()
def about(self):
self.text(500, aboutText)
def mainHelp(self):
self.text(500, mainHelpText)
def cancel(self):
self.destroy()
dsmmMain.dsmmStart()
def createWidgets(self):
self.helpText = tk.Label(
self,
textvariable=self.pictureString,
bg='black',
fg='white',
width=30,
height=30,
wraplength=300,
anchor="center",
font ="Mono 12"
)
self.mainHelpButton = tk.Button(
self,
text="Main (<F1>)" ,
command=self.mainHelp
)
self.aboutButton = tk.Button(
self,
text="About (<F2>)" ,
command=self.about
)
self.cancelButton = tk.Button(
self,
text="Cancel (Esc)",
command=self.cancel
)
self.helpText.grid(row=0, column=0, columnspan=3)
self.mainHelpButton.grid(row=1, column=0)
self.aboutButton.grid(row=1, column=1)
self.cancelButton.grid(row=1, column=2)
self.mainHelp()
####### End GUI
class Collector(threading.Thread):
def __init__(self, device, port):
threading.Thread.__init__(self)
self.setDaemon(True)
self.port = port
self.portName = device.getPortName(port)
self.device = device
self.quit = False
def run(self):
self.device.openPort(self.port)
self.device.ignoreTypes(True, False, True)
while True:
if self.quit:
return
msg = self.device.getMessage(250)
#print(msg)
if msg:
print_message(msg)
#for i in notes:
# print(i, end=" ")
#print("")
dev = rtmidi.RtMidiIn()
collectors = []
allPorts=range(dev.getPortCount())
#ports = [0]
ports = allPorts
if __name__ == '__main__':
dsmmMain = dsmmApp()
dsmmMain.focus_set()
dsmmMain.mainloop()