Skip to content

Commit f5b2788

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

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
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: 30 additions & 12 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
#########################################
@@ -424,14 +429,27 @@ def MidiSerialCallback():
424429
# MAIN LOOP
425430
#########################################
426431

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)
432+
if RTMIDI:
433+
midi_in = [rtmidi.MidiIn()]
434+
previous = []
435+
while True:
436+
for num_port, port in enumerate(midi_in[0].get_ports()):
437+
if port not in previous and 'Midi Through' not in port:
438+
midi_in.append(rtmidi.MidiIn())
439+
midi_in[-1].set_callback(MidiCallback)
440+
midi_in[-1].open_port(num_port)
441+
print('Opened MIDI: ' + str(port))
442+
previous = midi_in[0].get_ports()
443+
time.sleep(2)
444+
else:
445+
midi_in = [rtmidi.MidiIn(b'in')]
446+
previous = []
447+
while True:
448+
for port in midi_in[0].ports:
449+
if port not in previous and b'Midi Through' not in port:
450+
midi_in.append(rtmidi.MidiIn(b'in'))
451+
midi_in[-1].callback = MidiCallback
452+
midi_in[-1].open_port(port)
453+
print('Opened MIDI: ' + str(port))
454+
previous = midi_in[0].ports
455+
time.sleep(2)

0 commit comments

Comments
 (0)