Skip to content

Commit 81c5a77

Browse files
committed
Merge pull request spatialaudio#2 from spatialaudio/midi
Support for MIDI ports See spatialaudio#2.
2 parents 483687b + ac86738 commit 81c5a77

3 files changed

Lines changed: 435 additions & 99 deletions

File tree

‎examples/midi_chords.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python3
2+
3+
"""JACK client that creates minor triads from single MIDI notes.
4+
5+
All MIDI events are passed through.
6+
Two additional events are created for each NoteOn and NoteOff event.
7+
8+
"""
9+
import jack
10+
import struct
11+
12+
# First 4 bits of status byte:
13+
NOTEON = 0x9
14+
NOTEOFF = 0x8
15+
16+
INTERVALS = 3, 7 # minor triad
17+
18+
client = jack.Client("MIDI-Chord-Generator")
19+
inport = client.midi_inports.register("input")
20+
outport = client.midi_outports.register("output")
21+
22+
23+
def callback(frames, userdata):
24+
outport.clear_buffer()
25+
for offset, indata in inport.incoming_midi_events():
26+
# Note: This may raise an exception:
27+
outport.write_midi_event(offset, indata) # pass through
28+
if len(indata) == 3:
29+
status, pitch, vel = struct.unpack('3B', indata)
30+
if status >> 4 in (NOTEON, NOTEOFF):
31+
for i in INTERVALS:
32+
# Note: This may raise an exception:
33+
outport.write_midi_event(offset, (status, pitch + i, vel))
34+
return jack.CALL_AGAIN
35+
36+
client.set_process_callback(callback)
37+
38+
with client:
39+
print("#" * 80)
40+
print("press Return to quit")
41+
print("#" * 80)
42+
input()

‎examples/midi_monitor.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
3+
"""JACK client that prints all received MIDI events."""
4+
5+
import jack
6+
import binascii
7+
8+
client = jack.Client("MIDI-Monitor")
9+
port = client.midi_inports.register("input")
10+
11+
12+
def callback(frames, userdata):
13+
for offset, data in port.incoming_midi_events():
14+
# TODO: use ringbuffer
15+
print("{0}: 0x{1}".format(client.last_frame_time + offset,
16+
binascii.hexlify(data).decode()))
17+
return jack.CALL_AGAIN
18+
19+
client.set_process_callback(callback)
20+
21+
with client:
22+
print("#" * 80)
23+
print("press Return to quit")
24+
print("#" * 80)
25+
input()

0 commit comments

Comments
 (0)