File: midi_chords.py

package info (click to toggle)
python-jack-client 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 356 kB
  • sloc: python: 2,123; makefile: 6
file content (40 lines) | stat: -rwxr-xr-x 1,118 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3

"""JACK client that creates minor triads from single MIDI notes.

All MIDI events are passed through.
Two additional events are created for each NoteOn and NoteOff event.

"""
import jack
import struct

# First 4 bits of status byte:
NOTEON = 0x9
NOTEOFF = 0x8

INTERVALS = 3, 7  # minor triad

client = jack.Client('MIDI-Chord-Generator')
inport = client.midi_inports.register('input')
outport = client.midi_outports.register('output')


@client.set_process_callback
def process(frames):
    outport.clear_buffer()
    for offset, indata in inport.incoming_midi_events():
        # Note: This may raise an exception:
        outport.write_midi_event(offset, indata)  # pass through
        if len(indata) == 3:
            status, pitch, vel = struct.unpack('3B', indata)
            if status >> 4 in (NOTEON, NOTEOFF):
                for i in INTERVALS:
                    # Note: This may raise an exception:
                    outport.write_midi_event(offset, (status, pitch + i, vel))

with client:
    print('#' * 80)
    print('press Return to quit')
    print('#' * 80)
    input()