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