Skip to content

Commit 4ea2106

Browse files
author
dosas
committed
Add support for python-rtmidi
while keeping backwards compatibility to rtmidi-python
1 parent 59006e2 commit 4ea2106

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Website: [www.samplerbox.org](https://www.samplerbox.org)
1212

1313
# Install
1414

15-
SamplerBox works with the RaspberryPi's built-in soundcard, but it is recommended to use a USB DAC (PCM2704 USB DAC for less than 10€ on eBay is fine) for better sound quality.
15+
SamplerBox works with the RaspberryPi's built-in soundcard, but it is recommended to use a USB DAC (PCM2704 USB DAC for less than 10€ on eBay is fine) for better sound quality.
1616

1717
You can use a ready-to-use ISO image from the [Releases](https://github.com/josephernest/SamplerBox/releases) page or do a manual install:
1818

@@ -22,28 +22,38 @@ You can use a ready-to-use ISO image from the [Releases](https://github.com/jose
2222

2323
~~~
2424
sudo apt update
25-
sudo apt -y install git python3-pip python3-smbus python3-numpy libportaudio2
25+
sudo apt -y install git python3-pip python3-smbus python3-numpy libportaudio2 libasound2-dev
2626
sudo apt -y install raspberrypi-kernel # quite long to install, do it only if necessary, it solves a "no sound before 25 second on boot" problem
27-
sudo pip3 install cython rtmidi-python cffi sounddevice pyserial
27+
sudo pip3 install cython cffi sounddevice pyserial
2828
~~~
29-
30-
2. Download SamplerBox and build it with:
29+
30+
For python < 3.9
31+
~~~
32+
sudo pip3 install rtmidi-python
33+
~~~
34+
35+
For python >= 3.9
36+
~~~
37+
sudo pip3 install python-rtmidi
38+
~~~
39+
40+
1. Download SamplerBox and build it with:
3141
3242
~~~
3343
git clone https://github.com/josephernest/SamplerBox.git
3444
cd SamplerBox
3545
sudo python3 setup.py build_ext --inplace
3646
~~~
3747
38-
3. Reboot the Pi, and run the soft with:
39-
48+
1. Reboot the Pi, and run the soft with:
49+
4050
~~~
4151
sudo python3 samplerbox.py
4252
~~~
4353
4454
Play some notes on the connected MIDI keyboard, you'll hear some sound!
4555
46-
4. *(Optional)* Modify `config.py` if you want to change root directory for sample-sets, default soundcard, etc.
56+
1. *(Optional)* Modify `config.py` if you want to change root directory for sample-sets, default soundcard, etc.
4757
4858
4959
# How to use it

samplerbox.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
import threading
2424
from chunk import Chunk
2525
import struct
26-
import rtmidi_python as rtmidi
26+
try:
27+
import rtmidi_python as rtmidi
28+
RTMIDI = False
29+
except ModuleNotFoundError:
30+
import rtmidi
31+
RTMIDI = True
2732
import samplerbox_audio
2833

2934
#########################################
@@ -165,7 +170,18 @@ def AudioCallback(outdata, frame_count, time_info, status):
165170
b *= globalvolume
166171
outdata[:] = b.reshape(outdata.shape)
167172

168-
def MidiCallback(message, time_stamp):
173+
174+
def midi_callback(message, data=None):
175+
message, deltatime = message
176+
177+
return midi_callback_common(message, None)
178+
179+
180+
def MidiCallback(message, timestamp):
181+
return midi_callback_common(message, timestamp)
182+
183+
184+
def midi_callback_common(message, timestamp):
169185
global playingnotes, sustain, sustainplayingnotes
170186
global preset
171187
messagetype = message[0] >> 4
@@ -424,14 +440,27 @@ def MidiSerialCallback():
424440
# MAIN LOOP
425441
#########################################
426442

427-
midi_in = [rtmidi.MidiIn(b'in')]
428-
previous = []
429-
while True:
430-
for port in midi_in[0].ports:
431-
if port not in previous and b'Midi Through' not in port:
432-
midi_in.append(rtmidi.MidiIn(b'in'))
433-
midi_in[-1].callback = MidiCallback
434-
midi_in[-1].open_port(port)
435-
print('Opened MIDI: ' + str(port))
436-
previous = midi_in[0].ports
437-
time.sleep(2)
443+
if RTMIDI:
444+
midi_in = [rtmidi.MidiIn()]
445+
previous = []
446+
while True:
447+
for num_port, port in enumerate(midi_in[0].get_ports()):
448+
if port not in previous and 'Midi Through' not in port:
449+
midi_in.append(rtmidi.MidiIn())
450+
midi_in[-1].set_callback(midi_callback)
451+
midi_in[-1].open_port(num_port)
452+
print('Opened MIDI: ' + str(port))
453+
previous = midi_in[0].get_ports()
454+
time.sleep(2)
455+
else:
456+
midi_in = [rtmidi.MidiIn(b'in')]
457+
previous = []
458+
while True:
459+
for port in midi_in[0].ports:
460+
if port not in previous and b'Midi Through' not in port:
461+
midi_in.append(rtmidi.MidiIn(b'in'))
462+
midi_in[-1].callback = MidiCallback
463+
midi_in[-1].open_port(port)
464+
print('Opened MIDI: ' + str(port))
465+
previous = midi_in[0].ports
466+
time.sleep(2)

0 commit comments

Comments
 (0)