midi_handler.py (2006B)
1 #!/usr/bin/env python3 2 # See LICENSE file for copyright and license details. 3 """ 4 MIDI listener daemon for the Caller Station 5 """ 6 7 from subprocess import Popen 8 import mido 9 10 from config import (device_name) 11 12 13 def make_call(): 14 # DISPLAY=:0 xdotool key 1 2 3 Return 15 print('Popping the mechanical turk') 16 Popen(['xdotool', 'key', '1', '2', '3', 'Return'], 17 env={'DISPLAY': ':0'}) 18 19 20 def cancel_call(): 21 print('Popping the mechanical turk') 22 Popen(['xdotool', 'key', 'Escape'], 23 env={'DISPLAY': ':0'}) 24 25 def main(): 26 print('Opening the MIDI input listener') 27 midi_in = mido.open_input(device_name) 28 for msg in midi_in: 29 print('Got MIDI message!') 30 #print('Type:', msg.type) 31 #print('Time:', msg.time) 32 #print('Velocity:', msg.velocity) 33 #print('Note:', msg.note) 34 #print('Channel:', msg.channel) 35 #print('Bytes:', msg.bytes()) 36 #print('Bin:', msg.bin()) 37 #print('Hex:', msg.hex()) 38 print('Dict:', msg.dict()) 39 midi_dict = msg.dict() 40 41 # a control change 42 #mtype = midi_dict.get('type') 43 #mtime = midi_dict.get('time') 44 #mctl = midi_dict.get('control') 45 #mval = midi_dict.get('value') 46 #mchan = midi_dict.get('channel') 47 48 # a note_on 49 mtype = midi_dict.get('type') 50 mtime = midi_dict.get('time') 51 mnote = midi_dict.get('note') 52 mvelo = midi_dict.get('velocity') 53 mchan = midi_dict.get('channel') 54 55 if mtype == 'note_on' and mtime == 0 and mnote == 60 \ 56 and mchan == 0: 57 make_call() 58 print('Got MIDI 60. Making call.') 59 elif mtype == 'note_on' and mtime == 0 and mnote == 63 \ 60 and mchan == 0: 61 cancel_call() 62 print('Got MIDI 63. Cancelling call.') 63 64 65 print('---') 66 67 # Try this out too: 68 #while True: 69 # for msg in midi_in.iter_pending(): 70 # print(msg) 71 72 73 if __name__ == '__main__': 74 main()