Run-code not working in headless mode

I would like to use Sonic Pi as a backend/synth engine for my own GUI. I have been looking into using the OSC run-code message.

I first try was to use SonicPipe. I first started Sonic-Pi (non-headless) and looked up the ports and security token in spider.log. Fixing some bugs in SonicPipe I was able to execute code via Sonic Pi, so I am hearing the sounds when running the following Python script:

from sonic_pipe import SonicPipe
import time
sonic_path = “/opt/sonic-pi/app/server/ruby/bin/daemon.rb”

pi = SonicPipe(use_daemon=False,daemon_rb_location=sonic_path)
pi.pipe(“play :c, amp: 1”)
pi.pipe(“play :c, amp: 1”)

This means that run-command is correctly received by Sonic Pi.

Next step was to run the same code but now in headless mode (use_daemon=True) which uses the daemon.rb script:
pi = SonicPipe(use_daemon=Truedaemon_rb_location=sonic_path)

I see that Sonic Pi is started, and spider.log shows which ports to use. Adding some debugging info to SonicPipe also shows that it is sending the run-command OSC message to the correct port and with the correct token. However no sound can be heard.

Any idea what could be the problem?

Is there maybe a log file which shouws which OSC messages were received?

Apologies, I have no direct experience of using Python to drive Sonic Pi. However, I can say that using daemon.rb is the best way of working towards using Sonic Pi headless and that if you don’t use Sonic Pi’s language for timing, you are missing out on pretty much all of the hard work I’ve put into the timing system. The timing will drift and not be accurate, work poorly across multiple threads (not be in sync) and not benefit from the Ableton Link metronome.

Python’s time.sleep(0.5) is not at all the same as Sonic Pi’s sleep 0.5 even though they look very similar.

Thanks for your answer. In this case I am using python purely to send run-code commands. Especially because as you mention, I want to make use of Sonic Pi’s timing capabilities. Using sleep was just to test…

The current problem I am having is:

The run-command is correctly processed by sonic pi if I send it to a normally started running sonic pi instance which includes a gui.

The run-command is not processed somehow if I send it to a sonic pi which was started headless using daemon.rb. But maybe something else is wrong. Is there a debug flag or something with which I could see if osc messages are received and maybe I can see if for example the token is incorrect?

There’s no debug flag as such but if you modify the following functions to return true, you’ll see a lot more debug messages in the logs - including incoming OSC messages :slight_smile:

1 Like

In the end I needed to wait longer (about 6 seconds on an RPI4) before Sonic Pi is ready to process the run-command OSC mesaages.

1 Like