Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:02dd72d1d465 1 from __future__ import print_function
borlanic 0:02dd72d1d465 2 import sys
borlanic 0:02dd72d1d465 3 import re
borlanic 0:02dd72d1d465 4 import time
borlanic 0:02dd72d1d465 5 import mido
borlanic 0:02dd72d1d465 6 from mido import Message
borlanic 0:02dd72d1d465 7
borlanic 0:02dd72d1d465 8
borlanic 0:02dd72d1d465 9 def test_midi_in(port):
borlanic 0:02dd72d1d465 10 expected_messages_count=0
borlanic 0:02dd72d1d465 11 while expected_messages_count < 7:
borlanic 0:02dd72d1d465 12 for message in port.iter_pending():
borlanic 0:02dd72d1d465 13 if message.type in ('note_on', 'note_off', 'program_change', 'sysex'):
borlanic 0:02dd72d1d465 14 yield message
borlanic 0:02dd72d1d465 15 expected_messages_count+=1
borlanic 0:02dd72d1d465 16 time.sleep(0.1)
borlanic 0:02dd72d1d465 17
borlanic 0:02dd72d1d465 18 def test_midi_loopback(input_port):
borlanic 0:02dd72d1d465 19 expected_messages_count=0
borlanic 0:02dd72d1d465 20 while expected_messages_count < 1:
borlanic 0:02dd72d1d465 21 for message in input_port.iter_pending():
borlanic 0:02dd72d1d465 22 print('Test MIDI OUT loopback received {}'.format(message.hex()))
borlanic 0:02dd72d1d465 23 expected_messages_count+=1
borlanic 0:02dd72d1d465 24
borlanic 0:02dd72d1d465 25 def test_midi_out_loopback(output_port,input_port):
borlanic 0:02dd72d1d465 26 print("Test MIDI OUT loopback")
borlanic 0:02dd72d1d465 27 output_port.send(Message('program_change', program=1))
borlanic 0:02dd72d1d465 28 test_midi_loopback(input_port)
borlanic 0:02dd72d1d465 29
borlanic 0:02dd72d1d465 30 output_port.send(Message('note_on', note=21))
borlanic 0:02dd72d1d465 31 test_midi_loopback(input_port)
borlanic 0:02dd72d1d465 32
borlanic 0:02dd72d1d465 33 output_port.send(Message('note_off', note=21))
borlanic 0:02dd72d1d465 34 test_midi_loopback(input_port)
borlanic 0:02dd72d1d465 35
borlanic 0:02dd72d1d465 36 output_port.send(Message('sysex', data=[0x7E,0x7F,0x09,0x01]))
borlanic 0:02dd72d1d465 37 test_midi_loopback(input_port)
borlanic 0:02dd72d1d465 38
borlanic 0:02dd72d1d465 39 output_port.send(Message('sysex', data=[0x7F,0x7F,0x04,0x01,0x7F,0x7F]))
borlanic 0:02dd72d1d465 40 test_midi_loopback(input_port)
borlanic 0:02dd72d1d465 41
borlanic 0:02dd72d1d465 42 output_port.send(Message('sysex', data=[0x41,0x10,0x42,0x12,0x40,0x00,0x7F,0x00,0x41]))
borlanic 0:02dd72d1d465 43 test_midi_loopback(input_port)
borlanic 0:02dd72d1d465 44
borlanic 0:02dd72d1d465 45 output_port.send(Message('sysex', data=[0x41,0x10,0x42,0x12,0x40,0x00,0x04,0x7F,0x3D]))
borlanic 0:02dd72d1d465 46 test_midi_loopback(input_port)
borlanic 0:02dd72d1d465 47
borlanic 0:02dd72d1d465 48 portname=""
borlanic 0:02dd72d1d465 49
borlanic 0:02dd72d1d465 50 while portname=="":
borlanic 0:02dd72d1d465 51 print("Wait for MIDI IN plug ...")
borlanic 0:02dd72d1d465 52 for name in mido.get_input_names():
borlanic 0:02dd72d1d465 53 matchObj = re.match( r'Mbed', name)
borlanic 0:02dd72d1d465 54
borlanic 0:02dd72d1d465 55 if matchObj:
borlanic 0:02dd72d1d465 56 portname=name
borlanic 0:02dd72d1d465 57 time.sleep( 1 )
borlanic 0:02dd72d1d465 58
borlanic 0:02dd72d1d465 59 try:
borlanic 0:02dd72d1d465 60 input_port = mido.open_input(portname)
borlanic 0:02dd72d1d465 61 output_port = mido.open_output(portname)
borlanic 0:02dd72d1d465 62
borlanic 0:02dd72d1d465 63 print('Using {}'.format(input_port))
borlanic 0:02dd72d1d465 64
borlanic 0:02dd72d1d465 65 print("Test MIDI IN")
borlanic 0:02dd72d1d465 66
borlanic 0:02dd72d1d465 67 for message in test_midi_in(input_port):
borlanic 0:02dd72d1d465 68 print('Test MIDI IN received {}'.format(message.hex()))
borlanic 0:02dd72d1d465 69
borlanic 0:02dd72d1d465 70 test_midi_out_loopback(output_port,input_port)
borlanic 0:02dd72d1d465 71 except KeyboardInterrupt:
borlanic 0:02dd72d1d465 72 pass