Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
tools/host_tests/midi.py@0:5c4d7b2438d3, 2016-11-11 (annotated)
- Committer:
- switches
- Date:
- Fri Nov 11 20:59:50 2016 +0000
- Revision:
- 0:5c4d7b2438d3
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | from __future__ import print_function |
switches | 0:5c4d7b2438d3 | 2 | import sys |
switches | 0:5c4d7b2438d3 | 3 | import re |
switches | 0:5c4d7b2438d3 | 4 | import time |
switches | 0:5c4d7b2438d3 | 5 | import mido |
switches | 0:5c4d7b2438d3 | 6 | from mido import Message |
switches | 0:5c4d7b2438d3 | 7 | |
switches | 0:5c4d7b2438d3 | 8 | |
switches | 0:5c4d7b2438d3 | 9 | def test_midi_in(port): |
switches | 0:5c4d7b2438d3 | 10 | expected_messages_count=0 |
switches | 0:5c4d7b2438d3 | 11 | while expected_messages_count < 7: |
switches | 0:5c4d7b2438d3 | 12 | for message in port.iter_pending(): |
switches | 0:5c4d7b2438d3 | 13 | if message.type in ('note_on', 'note_off', 'program_change', 'sysex'): |
switches | 0:5c4d7b2438d3 | 14 | yield message |
switches | 0:5c4d7b2438d3 | 15 | expected_messages_count+=1 |
switches | 0:5c4d7b2438d3 | 16 | time.sleep(0.1) |
switches | 0:5c4d7b2438d3 | 17 | |
switches | 0:5c4d7b2438d3 | 18 | def test_midi_loopback(input_port): |
switches | 0:5c4d7b2438d3 | 19 | expected_messages_count=0 |
switches | 0:5c4d7b2438d3 | 20 | while expected_messages_count < 1: |
switches | 0:5c4d7b2438d3 | 21 | for message in input_port.iter_pending(): |
switches | 0:5c4d7b2438d3 | 22 | print('Test MIDI OUT loopback received {}'.format(message.hex())) |
switches | 0:5c4d7b2438d3 | 23 | expected_messages_count+=1 |
switches | 0:5c4d7b2438d3 | 24 | |
switches | 0:5c4d7b2438d3 | 25 | def test_midi_out_loopback(output_port,input_port): |
switches | 0:5c4d7b2438d3 | 26 | print("Test MIDI OUT loopback") |
switches | 0:5c4d7b2438d3 | 27 | output_port.send(Message('program_change', program=1)) |
switches | 0:5c4d7b2438d3 | 28 | test_midi_loopback(input_port) |
switches | 0:5c4d7b2438d3 | 29 | |
switches | 0:5c4d7b2438d3 | 30 | output_port.send(Message('note_on', note=21)) |
switches | 0:5c4d7b2438d3 | 31 | test_midi_loopback(input_port) |
switches | 0:5c4d7b2438d3 | 32 | |
switches | 0:5c4d7b2438d3 | 33 | output_port.send(Message('note_off', note=21)) |
switches | 0:5c4d7b2438d3 | 34 | test_midi_loopback(input_port) |
switches | 0:5c4d7b2438d3 | 35 | |
switches | 0:5c4d7b2438d3 | 36 | output_port.send(Message('sysex', data=[0x7E,0x7F,0x09,0x01])) |
switches | 0:5c4d7b2438d3 | 37 | test_midi_loopback(input_port) |
switches | 0:5c4d7b2438d3 | 38 | |
switches | 0:5c4d7b2438d3 | 39 | output_port.send(Message('sysex', data=[0x7F,0x7F,0x04,0x01,0x7F,0x7F])) |
switches | 0:5c4d7b2438d3 | 40 | test_midi_loopback(input_port) |
switches | 0:5c4d7b2438d3 | 41 | |
switches | 0:5c4d7b2438d3 | 42 | output_port.send(Message('sysex', data=[0x41,0x10,0x42,0x12,0x40,0x00,0x7F,0x00,0x41])) |
switches | 0:5c4d7b2438d3 | 43 | test_midi_loopback(input_port) |
switches | 0:5c4d7b2438d3 | 44 | |
switches | 0:5c4d7b2438d3 | 45 | output_port.send(Message('sysex', data=[0x41,0x10,0x42,0x12,0x40,0x00,0x04,0x7F,0x3D])) |
switches | 0:5c4d7b2438d3 | 46 | test_midi_loopback(input_port) |
switches | 0:5c4d7b2438d3 | 47 | |
switches | 0:5c4d7b2438d3 | 48 | portname="" |
switches | 0:5c4d7b2438d3 | 49 | |
switches | 0:5c4d7b2438d3 | 50 | while portname=="": |
switches | 0:5c4d7b2438d3 | 51 | print("Wait for MIDI IN plug ...") |
switches | 0:5c4d7b2438d3 | 52 | for name in mido.get_input_names(): |
switches | 0:5c4d7b2438d3 | 53 | matchObj = re.match( r'Mbed', name) |
switches | 0:5c4d7b2438d3 | 54 | |
switches | 0:5c4d7b2438d3 | 55 | if matchObj: |
switches | 0:5c4d7b2438d3 | 56 | portname=name |
switches | 0:5c4d7b2438d3 | 57 | time.sleep( 1 ) |
switches | 0:5c4d7b2438d3 | 58 | |
switches | 0:5c4d7b2438d3 | 59 | try: |
switches | 0:5c4d7b2438d3 | 60 | input_port = mido.open_input(portname) |
switches | 0:5c4d7b2438d3 | 61 | output_port = mido.open_output(portname) |
switches | 0:5c4d7b2438d3 | 62 | |
switches | 0:5c4d7b2438d3 | 63 | print('Using {}'.format(input_port)) |
switches | 0:5c4d7b2438d3 | 64 | |
switches | 0:5c4d7b2438d3 | 65 | print("Test MIDI IN") |
switches | 0:5c4d7b2438d3 | 66 | |
switches | 0:5c4d7b2438d3 | 67 | for message in test_midi_in(input_port): |
switches | 0:5c4d7b2438d3 | 68 | print('Test MIDI IN received {}'.format(message.hex())) |
switches | 0:5c4d7b2438d3 | 69 | |
switches | 0:5c4d7b2438d3 | 70 | test_midi_out_loopback(output_port,input_port) |
switches | 0:5c4d7b2438d3 | 71 | except KeyboardInterrupt: |
switches | 0:5c4d7b2438d3 | 72 | pass |