Script for launching PD patch with MIDI without aconnect

For various reasons I’d like to launch a PD patch on my raspi with the Pisound without pressing the button - I’d like to have it run on startup. I’m using a headless Pi running the latest “lite” raspbian and the latest PD version.

My patch includes MIDI input. I’m currently able to get this working by launching the patch in the background, and then using aconnect to connect the MIDI input (either from the Pisound DIN connectors or USB) to my PD patch.

At the moment I am putting the following line at the bottom of /etc/profile to get it to launch at startup:
puredata -stderr -nogui -alsa -audioadddev pisound -alsamidi -channels 2 -r 48000 -mididev 1 mypatch.pd $@ &

that launches the patch fine, but then I have to type "connect 20:0 128:0 to get the MIDI working. I’d much prefer this to happen automatically. I imagine there’s a good way to script this but I don’t really know what it is, any tips?

ah ha, I have found a solution, though it feels terrible…

after my launch command, if I do this it works:
sleep 3 aconnect 'Teensy MIDI':0 'Pure Data':0
but this feels like a bad solution, since I guess it’s just waiting for long enough that hopefully the patch will be launched and the MIDI ports in Pure Data will be available.

Hey! We see the same difficulty of setting up MIDI software connections over and over again, so we’ve come up with a tiny program running in the background which we haven’t announced yet, but will do that soon :slight_smile:

What it does is it sits in the background, waiting for MIDI software / hardware ports to appear in the system, then it automatically connects the very first Input / Output pair of software and hardware ports together.

Anyway, I think your scenario is perfect to give it a try, you can find the code here: https://github.com/BlokasLabs/amidiauto

To install (you may have to first install libasound2-dev via apt-get):

git clone https://github.com/BlokasLabs/amidiauto
cd amidiauto
make
sudo make install

Then to get it started automatically, add to the script you use to start PD, above puredata line:

amidiauto &

(the & makes it run in the background)

oh cool thanks! I’ll give that a try.

I seem to remember that when I first got the Pisound this wasn’t an issue, I never had to use aconnect. Is it something that got changed in a Raspberry Pi update or something? It seems like the forums are full of people finding this confusing these days.

If you launched via the button or the app, then we do the connections in a similar fashion to your solution. :slight_smile:

hmm maybe I’m clueless about how compiling things works, but when I do the make command after cloning from GitHub I get an error:
g+±4.9 -c -O3 amidiauto.cpp -o amidiauto.o
make: g+±4.9: Command not found
Makefile:33: recipe for target ‘amidiauto.o’ failed
make: *** [amidiauto.o] Error 127

Please edit the Makefile, and change CXX=g++-4.9 to CXX=g++.

Or do sudo apt install g++-4.9 - we’re using 4.9 version of GCC to support the Raspbian’s older Wheezy version.

I edited the makefile as you said and it compiled.

And, more excitingly, this solution works for me so far! I put amidiauto & before my launch script and the patch connected to my USB MIDI controller automatically. It’s great!

The only downside appears to be that for the first minute or so the MIDI connect comes in and out, I guess as all the various connections are made? I’m not really sure, I may have been imagining it. I’ll let you know how it goes. Thanks!

I’m not sure I understood what you mean by this, do you mean the data flow gets interrupted?

Yeah, it will even autoconnect MIDI controllers that were just plugged on the fly :slight_smile: So there should be no more need to rerun any scripts or restart software for this purpose.

yes that’s what I meant, though I recognise that isn’t very clear! I’ll test it some more later this week and let you know if it continues. it certainly wasn’t a big deal anyway.

I would love to see a how to video on how to set this up if anyone has a link to it. Ta

Is this going to be fixed? It think it is ridiculous that one has to go to such lengths to get the pisound to work as advertised.
It is also ridiculous that this problem is mentioned nowhere in the pisound documentation. I wasted hours trying to figure out what I was doing wrong, when it was in fact a failure of Blokas’ software. Blokas claims that pisound works “out of the box” with Pure Data, which is only half true.

Hey, could you please describe the issue you are facing, it’s a pretty broad topic with many expectations one may have and different possible solutions.

Keep in mind that MIDI on Linux works differently than on other major OSes, unfortunately this is not in our control, we have to abide by Linux standards in order to be able to interoperate with existing MIDI software. That said, what we can do is we can try to improve the situation, and that’s what we did with the amidiauto service which is integrated by default in Patchbox OS image. Please see this documentation page: amidiauto - Patchbox OS Documentation

1 Like

Hi, thanks for your quick response. Sorry for the tone of my post, but I’ve been very frustrated by this problem.

I’m trying to use the DIN midi in on the pisound to communicate with PD. I am using a Pi 3B+ with Rasbian stretch.

I installed amidiauto per the instructions on https://community.blokas.io/t/script-for-launching-pd-patch-with-midi-without-aconnect/1010/3?u=amazingrolo

I have also installed the pisound software and updated it with pisound-config (it was already up to date.)

When I type amidiauto in the terminal window (with or without the &) I get an error:

pi@raspberrypi:~ $ Reading ‘/etc/amidiauto.conf’ failed! (-2)
Using default ‘allow all’ rule.
Allowing ‘’ -> '

I look in /etc and there is no amidiauto.conf. Could this be the problem?

Then I launch PD (0.49.0 Plain vanilla, no externals) using the app menu in the Pi desktop. Under the PD preferences I am able to change the audio device to pisound, but when I try to set the midi device, there is only “none”. When I open the “test audio and midi” patch from the Media menu, the audio in and out work, but not midi. The red light next to the midi in jack on the pisound flashes, as well as a red led in the upper corner left of the word “pisound” when I play my midi device.

Please let me know if I can give you more information about the problem. Any help you can give would be greatly appreciated!

Thanks,

Karl

Hey, I discovered the problem. I hadn’t selected ALSA midi in PD. I tried it before I installed amidiauto, with no results, so I assumed it didn’t matter.
It would be really nice if there was somewhere in the main Pisound documentation that explains how to get this to work. I wasted many hours tracking down the problem.

Thanks!

Karl

Yes, ALSA MIDI backend is the best choice for MIDI on Linux. It all works based on virtual ports that must be interconnected together, according to your needs. This design has an additional step that users must be aware of, but provides for a lot of flexibility. Other OSes typically have audio/MIDI software list the MIDI devices, so you can pick the ones you want to use, which is straightforward.

As for making the connections, aconnect is the command line utility to use, and we’ve built amidiauto to automatically connect things for the most common use case of hardware interacting with software MIDI ports, and it also allows customization of that logic in the /etc/amidiauto.conf file for specific use cases.

Btw, once installed, amidiauto automatically registers a systemd service to run in the background, so these commands have to be used:

  • sudo systemctl status amidiauto - check whether it’s running.
  • sudo systemctl enable amidiauto - enable amidiauto to start automatically on boot.
  • sudo systemctl disable amidiauto - disable autostart on boot.
  • sudo systemctl start amidiauto - start amidiauto.
  • sudo systemctl stop amidiauto - stop amidiauto. If autostart is enabled, it will start again on next boot automatically.

We’d recommend using the Patchbox OS image for use with PD and other audio software - it already has the audio programs preconfigured to work smoothly, and if you update the patchbox config utility, it allows configuring a PD patch to be automatically started on system boot via the module submenu.

Also PD can be started via The Button and Pisound App, both of these ways of launching PD provide the right config to the PD process and takes care of MIDI connections.

1 Like

I updated patchbox-config but could not see any option to start a Pd patch at boot. There is no module submenu

Try running sudo apt install -y patchbox-cli to update the patchbox config tool.

1 Like

Ah, now I have the modules section. I thought apt-get update, upgrade, dist-upgrade would bring me up to date.

I went to /usr/local/puredata-patches and made mkdir testpatch, touch testpatch/startup.pd
then ran the patchbox-config again but could not see my patch in the puredata module list.
How can I run my own patch?

Use ‘main.pd’ for the entry point.

1 Like