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