I’ve been having a lot of discussions around using MIDI on a nerves device and wanted to start a thread to discuss some of it in the open so that it is easier to help others in the same discussion.
I have midi 1.0 working in nerves with Circuits.UART and some diy hardware. I need to publish a public repo with the UART Framing (will be sharing soon).
Some of the more interesting discussions and my current interest are around working with USB midi.
I’m just starting to look at “official” MIDI on Linux because like you, I’ve just always rolled my own UART implementation and avoided USB altogether.
That being said, without having looked at midiex (which looks pretty nice on via my desktop+Elixir), I wonder if there are missing pieces in your Nerves system that it’s looking for…
It appears ALSA and MIDI depend on kernel modules that aren’t part of a basic Nerves system, notably snd_usbmidi_lib.
Can you get to the iex shell on your Nerves system and type:
cmd "lsmod"
…and see if that module is loaded?
Also, try running “dmesg” to see if your USB MIDI module is detected when it’s plugged in.
Oh, and another question… what USB/MIDI interface are you using?
Since I’ve yet to build a Nerves system from scratch, I do wonder where the options to include this ALSA stuff lives. The basic audio stuff is loaded on the Nerves fleet distro on the boxes I’ve set up for Lars…
Just pulling this code now… it looks like it wraps RtMidi, which in turn (under Linux at least) links with ALSA and/or JACK libraries.
For simple and non-portable stuff, I think this is possible with much less code using ALSA directly, but I bet it’ll still depend on a Linux build with ALSA MIDI stuff included.
ChatGPT gave me a very simple snip of code to enumerate the visible MIDI ports which I built and ran on my Raspbian RPi3 device to show my Arturia Beatstep pads:
pi@co2:~ $ ./listmidi
MIDI Device: Midi Through
Port: Midi Through Port-0
MIDI Device: Arturia BeatStep
Port: Arturia BeatStep MIDI 1
Code courtesy ChatGPT (build via gcc listmidi.c -o listmidi -lasound … you might need to sudo apt install libasound2-dev to build) :
#include <stdio.h>
#include <alsa/asoundlib.h>
int main() {
int status;
snd_seq_t *seq_handle;
snd_seq_client_info_t *cinfo;
snd_seq_port_info_t *pinfo;
// Open ALSA sequencer
status = snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_DUPLEX, 0);
if (status < 0) {
printf("Error opening ALSA sequencer.\n");
return 1;
}
// Allocate space for client and port info
snd_seq_client_info_alloca(&cinfo);
snd_seq_port_info_alloca(&pinfo);
// Iterate over all clients (devices)
snd_seq_client_info_set_client(cinfo, -1);
while (snd_seq_query_next_client(seq_handle, cinfo) >= 0) {
int client = snd_seq_client_info_get_client(cinfo);
// Iterate over all ports for the client
snd_seq_port_info_set_client(pinfo, client);
snd_seq_port_info_set_port(pinfo, -1);
while (snd_seq_query_next_port(seq_handle, pinfo) >= 0) {
if (snd_seq_port_info_get_capability(pinfo) & SND_SEQ_PORT_CAP_SUBS_WRITE) {
printf("MIDI Device: %s\n", snd_seq_client_info_get_name(cinfo));
printf("Port: %s\n", snd_seq_port_info_get_name(pinfo));
}
}
}
snd_seq_close(seq_handle);
return 0;
}
On my unmodified “Lars fleet RPi3”, this binary isn’t able to find the symbol snd_seq_open so I suspect buildroot/??? left pieces of ALSA out.
One of these days I should actually build a system from scratch instead of hoping someone else will do it for me,
I’m questioning my sanity regarding USB/MIDI devices on Linux.
However I have evidence (old Python scripts) that indicate at one time my Arturia BeatStep USB/MIDI device showed up as both a serial device (under a /dev/ttyUSB# scheme) and under /dev/midi#.
The world seems to be changing under my feet and I have no knowledge of udev and friends, so could it be possible that the /dev/ttyUSB# device could still be available with some fiddling of configuration?
I’ll wander off and do my own research, but thought I’d mention it here, since dealing with these devices as UART drags a lot less baggage than ALSA and / or libusb (which I’ve already spent way more time on than I thought I would, LOL).
Interestingly enough my problem with getting the rustler deps to load was related to this thread:
@octetta It did end up being an issue related to buildroot but also an operator error. (I’ll push up some code this afternoon after work to show the specifics)
@fhunleth when I included the extra alsa packages and blew my system away and rebuilt it I was able to get the midiex dependency to work on the firmware. I’ll run some more tests and confirm it actually works later but this is pretty cool.
Would you want some of these extra deps included in the official system? I’m going to publish my fork for audio/midi but could also just contribute back if you think it would be useful.
@thejohncotton I really like that more of ALSA stuff is possible… and I’ll probably pursue something in the vein of an Erlang port driver for MIDI stuff. I have a half-baked POC port driver for audio sampling and playback that I hope to share soon (targeting the Nerves meetup in November).
I think I’m on the right track, but not able to get any midi devices to show up yet. I’ve got several to try most are class compliant usb devices so shouldn’t need drivers.