Control Sonic-Pi (server) via Python on Windows

What I want to achieve: A python script that lets me start Sonic-Pi and send full Sonic-Pi-code to it to be run. (No manual interaction with Sonic-Pi itself, and not just single notes or instruments, full code)

1. Question: I currently need some Sonic-Pi code to run that can then interpret the code I am sending via Python:

Is it necessary to have this code in Sonic-Pi first (and actively needing to click run first) so that the Code from Python can be interpreted? I can’t figure out a way so that it is played without prior code running in Sonic Pi.

2. Question: How can I start the Sonic-Pi server and use osc, so that I do not need to interact with Sonic-Pi manually. I found the sonic-pi-tool und GitHub:

I could rewrite it to work within Python and not just the CLI. Emlyn already says in the Readme that starting the Server does not work yet on Windows. I thought that I merely need to change the paths in the code, but this seems not to be sufficient to get it to work. (image bottom right)

I would greatly appreciate if someone could help me out with my two question and/or give me a few tips on how I may achieve my goal. Thanks! :slight_smile:


sounds like you want to skip the whole GUI and just start the internal daemon.rb script directly. This is essentially what the GUI does in the background.

This is the script you want to be starting:

You should be able to find it embedded in the Sonic Pi app you have downloaded. Follow the instructions in the comments in the top of this file to see how to interact with it. You essentially want to read the output it produces to figure out which ports have been selected and to get the security token. You can then send OSC messages with the code you want to evaluate along with the security token to produce sound.

There are two important things to note about this approach:

  1. You also need to send regular heartbeat OSC messages to a different port (also obtained from the standard out) in order to keep the daemon alive. Sonic Pi uses a bunch of different programs which the daemon script manages for you. In order to stop zombie processes the daemon will also ensure these sub-programs are stopped and is pretty aggressive about it :slight_smile:

  2. Be very careful who you let send Ruby code to your daemon script. The reason why the port number is randomised and a security token is necessary is that the program that sends Ruby code essentially has full command line access to your machine if they know what they’re doing. So you’re essentially opening up a huge security hole to anyone that knows the port and the security token.

Good luck!