Multi-channel midi-capable audio/midi player on the Raspberry Pi platform using Python 3.
By D. Cooper Dalrymple, 2021 - 2023
Octopy is intended to be used to play audio tracks synchronized with midi tracks and video output when triggered by midi for the purpose of live music performances. This way, midi-controlled devices (drum machines, keyboards, etc) can be sequenced with a backing track and controlled by a simple and fitting midi device all within a small package, no laptops and fancy DAWs required.
This software could also be configured to play midi files into a midi sound device such as the MT32 or simply play back audio files for other purposes triggered from a device in another location. It is not required that both audio, midi, & video files are provided for each track.
- Raspberry Pi 3 or greater - Raspberry Pi 1 experiences stuttering in midi and audio output (Model 3 A+ used for testing)
- Linux-based x86-64/ARM computer - This software has not been optimized for Windows or Mac
- HiFiBerry Audio Hats - Tested with the DAC+ Standard
- Serial-based MIDI Hats - ie: OSA MIDI Board (custom board used for testing)
- Linux-based OS - ideally Raspberry Pi OS Lite
- Python 3.x with pyalsaaudio, python-rtmidi, pyserial, wave, and mido modules
- ALSA
- JACK Audio Connection Kit
- PyGame
- Video Player: pyvidplayer (ARM/x86), OMXPlayer (ARM), MPV (ARM/x86), FFmpeg (x86), or hello video (ARM)
Installing Octopy on Raspbian can be completed easily using the provided install script which can be run remotely. Use the following command on a clean install of Raspbian OS Lite to configure Octopy and all necessary dependencies. This script can also be used to update a pre-existing install.
wget -O - https://raw.githubusercontent.com/dcooperdalrymple/octopy/master/install.sh | bash
After installation is completed, you will need to perform some additional configuration steps to ensure that Octopy is fit for your system.
- Edit the configuration settings within
/root/octopy.ini
as needed. - Run
sudo python3 ~/octopy/octo.py --verbose
to test your system before deployment and scan available alsa devices. - If you're using a separate hardware audio driver such as the audio hats provided by HiFiBerry, make sure to follow the instructions provided by the manufacturer to make them ALSA-compatible.
- Use
sudo raspi-config
to adjust various parameters of your Raspberry Pi device:- If using GPIO serial for MIDI input/output, you may need to enable "Serial Port" under "Interface Options". Do not enable login shell over serial.
- If you would like to start octopy after boot automatically, enable auto-login to console.
- If you would like to make your boot media read-only to avoid potential memory corruption when powering off, enable "Overlay File System". Only do this after configuration is completed and you've ensured that every aspect of your system is running as desired.
On Ubuntu/Debian/Raspbian systems, ensure that all dependencies are met by typing sudo apt-get install libasound2-dev libjack-dev
and pip3 install pyalsaaudio python-rtmidi pyserial wave mido pygame
in the terminal. At least one of the 3 aforementioned video playback handlers must also be installed if you plan on using video files.
This is the preferred video playback method (especially for headless operation) because it renders directly to the pygame output buffer. The typical installation process is as follows (from within the octopy
directory):
sudo apt-get install libmediainfo-dev libavcodec-dev libavfilter-dev libavdevice-dev libavformat-dev libavutil-dev libswscale-dev libswresample-dev libpostproc-dev ffmpeg
git submodules init
git submodules update
pip3 install -r pyvidplayer/requirements.txt
Notes:
- If you have trouble installing the recommended python package versions, try directly installing the packages listed in
pyvidplayer/requirements.txt
without the specified version number. - It may also be necessary to create an empty
__init__.py
file within thepyvidplayer
directory to allow the module to be loaded.
This video playback utility will only work on Raspbian devices. Follow the instructions at the popcornmix/omxplayer repository for installation. This utility supports the following formats: avi, mov, mkv, mp4, m4v
.
MPV is supported on both Raspbian and x86 systems (Linux, Windows, & Mac). Follow the instructions on the MPV website for installation. On Linux and Raspbian systems, it should be as simple as running: sudo apt-get install mpv
.
FFmpeg supports a wider range of video playback formats, but is typically only compatible with x86-based systems. If using a ubuntu/debian-based system, install FFmpeg with this command: sudo apt-get install ffmpeg
.
Raspberry Pi hello video playback utility will only work on Raspbian devices. Follow the instructions at the adafruit/pi_hello_video repository for installation. This utility only supports the h264
format.
A configuration file can be used for configuring the default settings for the program. All of the default settings are set by config.ini
in the octopy
directory. Copy this file to ~/octopy.ini
to override its values.
If you'd like to automatically mount and search an external storage device such as a USB drive, install the pip packages and run octo.py with sudo. Then use the name of the device (such as "sda1") with the -m argument or configure Media -> Storage in the user config file. Only files in the top-level directory will be scanned when building the database.
Type python3 octo.py
in the top level directory to run the program. Type python3 octo.py --help
for command information. The command arguments will override any settings defined by the configuration files.
All discovered sound/midi files are listed in alphabetical order when you first run the program with the --verbose
flag set. When a midi note is received on the selected channel (or on all channels by default) that corresponds with number value of the song, it will begin playing along the wave file and midi data depending on what is available (both, only midi, or only wave). If any files were playing previously, they are immediately stopped before the new file begins playing. If the a midi note is received with the note value of 0, it stops all currently playing streams.
This setup may not be ideal for use with a keyboard, but a properly configured midi pad device (such as the Akai LPD8) where each midi note is configured for the desired song and one of the pads is reserved as a stop pad should do the job. Organizing your files to begin with a number signifying the desired MIDI note (with a leading-zero ideally) will help with keeping everything in the proper order.
This software supports both USB midi devices or GPIO midi over the Raspberry Pi's serial interface. See this page for information about configuring the serial port on your Pi.
Any ALSA-enabled playback interface can be used. See the listed "Available Audio Devices" when you run the program with verbose enabled. A DAC hat such as the HiFiBerry DAC can also be used for audio playback (see for details).