-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmtc_listener.py
executable file
·46 lines (36 loc) · 1.1 KB
/
mtc_listener.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
#!/usr/bin/env python3
import click
import mido
import tools
# create a global accumulator for quarter_frames
quarter_frames = [0, 0, 0, 0, 0, 0, 0, 0]
def handle_message(message):
if message.type == 'quarter_frame':
quarter_frames[message.frame_type] = message.frame_value
if message.frame_type == 7:
tc = tools.mtc_decode_quarter_frames(quarter_frames)
print('QF:', tc)
elif message.type == 'sysex':
# check to see if this is a timecode frame
if len(message.data) == 8 and message.data[0:4] == (127, 127, 1, 1):
data = message.data[4:]
tc = tools.mtc_decode(data)
print('FF:', tc)
else:
print(message)
def listen(port_name):
port = mido.open_input(port_name)
# port.callback = print_message
print('Listening to MIDI messages on > {} <'.format(port_name))
while 1:
msg = port.receive(block=True)
handle_message(msg)
@click.command()
@click.option('--port', '-p', help='name of MIDI port to connect to')
def main(port):
if (port is None):
print('Available MIDI ports')
print(mido.get_input_names())
else:
listen(port)
main()