-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmidi_monitor.py
More file actions
315 lines (298 loc) · 11.7 KB
/
midi_monitor.py
File metadata and controls
315 lines (298 loc) · 11.7 KB
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
''' Asynchronous MIDI monitor based on the `log.Log` class
Copyright (c) 2025 Harm Lammers
MIT licence:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import micropython
import builtins
from singleton import singleton
from log import Log
_NOTES = b'C#D#EF#G#A#B' # used for translating MIDI note number to note string
_OUT_LEN_LONG = const(34) # length of long MIDI monitor string
_OUT_LEN_SHORT = const(25) # maximum length of short MIDI monitor string
# String indices of the different elements of a MIDI monitor string
_PRE_0_POS = const(0)
_PRE_0_VAL_POS_0 = const(1)
_PRE_0_VAL_POS_1 = const(2)
_TXT_POS_1_PRE = const(4)
_PRE_1_POS = const(4)
_PRE_1_VAL_POS_0 = const(5)
_PRE_1_VAL_POS_1 = const(6)
_PRE_1_SP_POS = const(7)
_TXT_POS_2_PRE = const(8)
_BYTE_NOTE_POS = const(17)
_BYTE_1_POS_0 = const(18)
_BYTE_1_POS_1 = const(19)
_BYTE_1_POS_2 = const(20)
_BYTE_2_POS_0 = const(22)
_BYTE_2_POS_1 = const(23)
_BYTE_2_POS_2 = const(24)
_WORD_POS_0 = const(20)
_WORD_POS_1 = const(21)
_WORD_POS_2 = const(22)
_WORD_POS_3 = const(23)
_WORD_POS_4 = const(24)
_WORD_POS_SIGN = const(19) # position before sign
_HEX_OPEN_POS = const(26)
_HEX_BYTE_0_POS_0 = const(27)
_HEX_BYTE_0_POS_1 = const(28)
_HEX_BYTE_1_POS_0 = const(29)
_HEX_BYTE_1_POS_1 = const(30)
_HEX_BYTE_2_POS_0 = const(31)
_HEX_BYTE_2_POS_1 = const(32)
_HEX_CLOSE_POS = const(33)
# Constants needed to find the right slices of _TEXT_DEFS
_NUM_SHORT_ROWS = const(7)
_SHORT_STR_LEN = const(17)
_SHORT_ROW_LEN = const(18)
_LONG_STR_LEN = const(21)
_LONG_ROW_LEN = const(22)
# Types of data byte(s) used by MIDI messages (the order of _TYP_* constant enumeration is critical!)
_TYP_NOTE_BYTE = const(0)
_TYP_BYTE_BYTE = const(1)
_TYP_BYTE = const(2)
_TYP_WORD = const(3)
_TYP_WORD_PB = const(4)
# _TYP_NONE = const(5)
# The following bytes object lists for each type of command the monitor base string followed by a byte indicating the type of data byte(s)
# Included types:
# 0x8n Note Off _TYP_NOTE_BYTE
# 0x9n Note On _TYP_NOTE_BYTE
# 0xAn Polyphonic Pressure _TYP_NOTE_BYTE
# 0xBn Control Change _TYP_BYTE_BYTE
# 0xCn Program Change _TYP_BYTE
# 0xDn Channel Pressure _TYP_BYTE
# 0xEn Pitch Bend _TYP_WORD_PB
# 0xF0 SysEx Start _TYP_NONE
# 0xF1 MTC Quarter Frame _TYP_BYTE (not decoded)
# 0xF2 Song Pointer _TYP_WORD
# 0xF3 Song Select _TYP_BYTE
# 0xF6 Tune Request _TYP_NONE
# 0xF7 End of SysEx _TYP_NONE
# 0xF8 Timing Clock _TYP_NONE
# 0xFA Start _TYP_NONE
# 0xFB Continue _TYP_NONE
# 0xFC Stop _TYP_NONE
# 0xFE Active Sensing _TYP_NONE
# 0xFF System Reset _TYP_NONE
_TEXT_DEFS = b''\
b'Note Off \x00'\
b'Note On \x00'\
b'PolyPres \x00'\
b'CtrlChnge \x01'\
b'ProgChnge \x02'\
b'ChanPres \x02'\
b'PitchBend \x04'\
b'SysEx Start \x05'\
b'MTC Qtr Frme \x02'\
b'Song Pos Ptr \x03'\
b'Song Select \x02'\
b'Tune Request \x05'\
b'End of SysEx \x05'\
b'Timing Clock \x05'\
b'Start \x05'\
b'Continue \x05'\
b'Stop \x05'\
b'Active Sensng \x05'\
b'System Reset \x05'\
@singleton
class MidiMonitor:
''' Singleton MIDI monitor which uses the `log.Log` singleton class to log MIDI messages
Args:
long (bool): If `True` include HEX representation of the message; defaults to `True`
'''
def __init__(self, long: bool = True) -> None:
self.long = long
self.buf_len = (buf_len := _OUT_LEN_LONG if long else _OUT_LEN_SHORT)
self._buf = (buf := bytearray(buf_len))
buf[:] = b' ' * buf_len
self._log = Log()
@micropython.viper
def write(self, port_id: int, byte_0: int, byte_1: int, byte_2: int):
''' Generate MIDI monitor string and send to logger; do not use for SysEx data bytes
Args:
port_id (int): Port number
byte_0 (int): First byte of the MIDI message
byte_1 (int): Second byte of the MIDI message
byte_2 (int): Third byte of the MIDI message
'''
DEFS = ptr8(_TEXT_DEFS) # pyright: ignore[reportUndefinedVariable]
buf_obj = self._buf
buf = ptr8(buf_obj) # pyright: ignore[reportUndefinedVariable]
# Port prefix
buf[_PRE_0_POS] = 80 # ‘P’
rem = port_id
quot = rem // 10 if port_id >= 10 else 0
buf[_PRE_0_VAL_POS_0] = 48 + quot
rem -= quot * 10
buf[_PRE_0_VAL_POS_1] = 48 + rem
if byte_0 < 0xF0: # Channel Voice message or Channel Common message: abort (invalid SysEx data)
# Channel prefix
buf[_PRE_1_POS] = 67 # ‘C’
byte = byte_0 & 0x0F
rem = byte
quot = rem // 10 if byte >= 10 else 0
buf[_PRE_1_VAL_POS_0] = 48 + quot
rem -= quot * 10
buf[_PRE_1_VAL_POS_1] = 48 + rem
buf[_PRE_1_SP_POS] = 32 # space
# get definitions
cmd = byte_0 & 0xF0
txt_pos = (cmd >> 4) - 8
if txt_pos < 0: return
txt_pos *= _SHORT_ROW_LEN
txt_len = _SHORT_STR_LEN
type = DEFS[txt_pos + _SHORT_STR_LEN]
pos = _TXT_POS_2_PRE
else: # System Common message or System Real-Time message
# Get definitions
txt_pos = byte_0 & 0x0F
if 4 <= txt_pos <= 5 or txt_pos == 9 or txt_pos == 0xD:
return
if txt_pos > 5:
txt_pos -= 2
if txt_pos > 7: # 9 - 2
txt_pos -= 1
if txt_pos > 0xA: # 0xD - 2 - 1
txt_pos -= 1
txt_pos *= _LONG_ROW_LEN
txt_pos += _NUM_SHORT_ROWS * _SHORT_ROW_LEN
txt_len = _LONG_STR_LEN
type = DEFS[txt_pos + _LONG_STR_LEN]
pos = _TXT_POS_1_PRE
# Text label
for i in range(txt_len):
buf[pos + i] = DEFS[txt_pos + i]
# First data value
if type == _TYP_NOTE_BYTE:
octave = byte_1 // 12
octave -= 1
pos = _BYTE_NOTE_POS + 1 if 0 <= octave <= 9 else _BYTE_NOTE_POS
note_pos = byte_1 % 12
notes = ptr8(_NOTES) # pyright: ignore[reportUndefinedVariable]
if notes[note_pos] == 35: # ‘#’
buf[pos] = notes[note_pos - 1]
pos += 1
buf[pos] = 35 # ‘#’
else:
pos += 1
buf[pos] = notes[note_pos]
pos += 1
if octave < 0:
buf[pos] = 45 # ‘-’
pos += 1
octave = -octave
buf[pos] = 48 + octave
elif type == _TYP_BYTE_BYTE:
rem = byte_1
if byte_1 >= 100:
quot = rem // 100
buf[_BYTE_1_POS_0] = 48 + quot
rem -= quot * 100
if byte_1 >= 10:
quot = rem // 10
buf[_BYTE_1_POS_1] = 48 + quot
rem -= quot * 10
buf[_BYTE_1_POS_2] = 48 + rem
elif type == _TYP_BYTE:
rem = byte_1
if byte_1>= 100:
quot = rem // 100
buf[_BYTE_2_POS_0] = 48 + quot
rem -= quot * 100
if byte_1 >= 10:
quot = rem // 10
buf[_BYTE_2_POS_1] = 48 + quot
rem -= quot * 10
buf[_BYTE_2_POS_2] = 48 + rem
# Second data value
if type <= _TYP_BYTE_BYTE:
rem = byte_2
if byte_2 >= 100:
quot = rem // 100
buf[_BYTE_2_POS_0] = 48 + quot
rem -= quot * 100
if byte_2 >= 10:
quot = rem // 10
buf[_BYTE_2_POS_1] = 48 + quot
rem -= quot * 10
buf[_BYTE_2_POS_2] = 48 + rem
elif type == _TYP_WORD:
word = ((byte_2 << 7) | byte_1)
rem = word
if word >= 10000:
quot = rem // 10000
buf[_WORD_POS_0] = 48 + quot
rem -= quot * 10000
if word >= 1000:
quot = rem // 1000
buf[_WORD_POS_1] = 48 + quot
rem -= quot * 1000
if word >= 100:
quot = rem // 100
buf[_WORD_POS_2] = 48 + quot
rem -= quot * 100
if word >= 10:
quot = rem // 10
buf[_WORD_POS_3] = 48 + quot
rem -= quot * 10
buf[_WORD_POS_4] = 48 + rem
elif type == _TYP_WORD_PB:
word = ((byte_2 << 7) | byte_1) - 0x2000
neg = word < 0
if word < 0: word = -word
rem = word
first_pos = 0
if word >= 1000:
quot = rem // 1000
buf[_WORD_POS_1] = 48 + quot
rem -= quot * 1000
first_pos = 1
if word >= 100:
quot = rem // 100
buf[_WORD_POS_2] = 48 + quot
rem -= quot * 100
if not first_pos: first_pos = 2
if word >= 10:
quot = rem // 10
buf[_WORD_POS_3] = 48 + quot
rem -= quot * 10
if not first_pos: first_pos = 3
buf[_WORD_POS_4] = 48 + rem
if not first_pos: first_pos = 4
if neg:
buf[_WORD_POS_SIGN + first_pos] = 45 # ‘-’
elif word > 0:
buf[_WORD_POS_SIGN + first_pos] = 43 # ‘+’
_log = self._log
if self.long:
# HEX message
buf[_HEX_OPEN_POS] = 0x5B # ‘[’
quot = (byte_0 >> 4) & 0xF
buf[_HEX_BYTE_0_POS_0] = 48 + quot if quot < 10 else 55 + quot
quot = byte_0 & 0xF
buf[_HEX_BYTE_0_POS_1] = 48 + quot if quot < 10 else 55 + quot
quot = (byte_1 >> 4) & 0xF
buf[_HEX_BYTE_1_POS_0] = 48 + quot if quot < 10 else 55 + quot
quot = byte_1 & 0xF
buf[_HEX_BYTE_1_POS_1] = 48 + quot if quot < 10 else 55 + quot
quot = (byte_2 >> 4) & 0xF
buf[_HEX_BYTE_2_POS_0] = 48 + quot if quot < 10 else 55 + quot
quot = byte_2 & 0xF
buf[_HEX_BYTE_2_POS_1] = 48 + quot if quot < 10 else 55 + quot
buf[_HEX_CLOSE_POS] = 0x5D # ‘]’
_log.write(buf_obj)
return
for i in range(_OUT_LEN_SHORT - 1, 0, -1):
if buf[i] != 32: # space
n = i + 1
break
_log.write(buf_obj[builtins.int(0):builtins.int(n)])