14
14
15
15
16
16
class MIDIExample (MIDIInterface ):
17
+ # Very simple example event handler functions, showing how to receive note
18
+ # and control change messages sent from the host to the device.
19
+ #
20
+ # If you need to send MIDI data to the host, then it's fine to instantiate
21
+ # MIDIInterface class directly.
22
+
23
+ def on_open (self ):
24
+ super ().on_open ()
25
+ print ("Device opened by host" )
26
+
17
27
def on_note_on (self , channel , pitch , vel ):
18
28
print (f"RX Note On channel { channel } pitch { pitch } velocity { vel } " )
19
29
@@ -35,17 +45,26 @@ def on_control_change(self, channel, controller, value):
35
45
36
46
print ("Starting MIDI loop..." )
37
47
48
+ # TX constants
49
+ CHANNEL = 0
50
+ PITCH = 60
51
+ CONTROLLER = 64
52
+
38
53
control_val = 0
39
- channel = 0
40
54
41
55
while m .is_open ():
42
56
time .sleep (1 )
43
- m .note_on (channel , 60 )
57
+ print (f"TX Note On channel { CHANNEL } pitch { PITCH } " )
58
+ m .note_on (CHANNEL , PITCH ) # Velocity is an optional third argument
44
59
time .sleep (0.5 )
45
- m .note_off (channel , 60 )
60
+ print (f"TX Note Off channel { CHANNEL } pitch { PITCH } " )
61
+ m .note_off (CHANNEL , PITCH )
46
62
time .sleep (1 )
47
- m .control_change (channel , 64 , control_val )
63
+ print (f"TX Control channel { CHANNEL } controller { CONTROLLER } value { control_val } " )
64
+ m .control_change (CHANNEL , CONTROLLER , control_val )
48
65
control_val += 1
49
66
if control_val == 0x7F :
50
67
control_val = 0
51
68
time .sleep (1 )
69
+
70
+ print ("USB host has reset device, example done." )
0 commit comments