Link sonic pi to Python

Hello !
I’m working on a university project which is to create a python project during our english classes. We oriented this project as a music video-game (like Voez) and I recently discoverer Sonic Pi then learned to use it.
The thing is that the video game is coded in Python 3 and the music on Sonic Pi. Is there a way to link both (I’ve done randomly created musics) ?

Thank you !

Hi @Gydhia, welcome!

“link both” could probably mean several things, but I’ll assume you mean having Sonic Pi play sounds in response to game events. A couple possibilities:

  • A Python interface for Sonic Pi I have not used this and cannot vouch for the quality. You might be able to port your to Python code using this and have it all run from your game
  • OSC / MIDI communication - You can have your Python program send messages to Sonic Pi via one of the communication protocols. I’d choose OSC because it is more general purpose. Then you could send OSC messages from your game, receive them in Sonic Pi, and map them to audio actions like starting a loop, playing a melody / sample, etc.

Personally, I’d probably avoid the Python interface even though I’m a longtime professional Python coder. You might run into weird bugs or edge case issues and spend all your time fighting those. But if you try it and it works I’d be glad to hear about your experience! Using OSC, you should be able to get up and running with only a few lines of code on each side.

1 Like

I agree with perpetual_monday that OSC is the way to go. I have done several projects to link python based projects to sonic pi using OSC messaging.
Take a look at https://rbnrpi.wordpress.com/project-list/a-musical-12-tone-alarm-for-the-raspberry-pi-part-1/12-input-touch-keyboard-for-sonic-pi/

and at https://rbnrpi.wordpress.com/2017/07/31/control-sonic-pi-3-with-a-ps3-wireless-controller/

In both cases, inputs dreived from external devices connected to a python program are used to send OSC messages to Sonic Pi where they are used to produce sounds.

Thank a lot !
I come back to you because I checked the OSC and tried to make it work with my python program. The thing is that I’m not used in the projects that require a “link” between 2 differents langages. perpetual_monday, you were right about what I wanted !
Basically, the game will begin with 80BPM and will increase depending on the actions. What I want is to send the BPM of my python game to my sonic pi music to adjust both of them (or from music pi to python, doesn’t matter). But even by checking your projects I’m kinda lost about what to do to link the 2 programs :frowning:
They would be on the same folder, what do I need to launch them in the same time and make them speak between each other ?

Thank you for your help !

@Gydhia

Here is a simple example with a Python client sending OSC messages to Sonic Pi. First, the Python client:

import random
import time

from pythonosc import udp_client

client = udp_client.SimpleUDPClient('127.0.0.1', 4559)

while True:
    client.send_message("/bpm", random.randint(40, 200))
    time.sleep(5)

The Python code establishes a client connection to the Sonic Pi OSC server which runs on port 4559 on my localhost (127.0.0.1). OSC is a network protocol, so it doesn’t matter where the program runs from; only a UDP connection is needed, and the python-osc library provides that. Next, I enter an infinite loop which will send OSC messages called bpm every 5 seconds. The bpm label was arbitrary - you can name your messages whatever you want, but that name / key is what you’ll use to gather data. The bpm message is sending along a single randomly chosen number between 40 - 200 which I’ll use for the BPM.

The Python program can be run at this point and it will start sending UDP messages to port 4559. It doesn’t matter at this point if Sonic Pi is running, just that the messages won’t get received by anything.

Here is an example Sonic Pi code that will alter the BPM of a metronome as it receives messages:

# Initial BPM
set :bpm, 120

live_loop :receiver do
  bpm_data = sync "/osc/bpm"
  set :bpm, bpm_data[0]
end

live_loop :metronome do
  with_bpm get(:bpm) do
    sample :elec_ping if [1,0,1,0,1,0,1,0].ring.tick == 1
    sleep 0.5
  end
end

Make sure your Sonic Pi is set to be an OSC server in settings, run the above, and you should see / hear the metronome changing tempo every 5 seconds.

what do I need to launch them in the same time and make them speak between each other ?

It sounds like you probably want a ‘one-click’ solution where a user could launch the Python script and Sonic Pi with a single action? There are different ways you could do this - for example via a shell/bash script, or you could launch Sonic Pi from within your Python program. However, you’d likely need a buffer or few to execute after startup and I’m not sure the best way to accomplish that programmatically, but others will probably have some ideas. Not that you want to add another tool (from another language) into your setup, but there is a project that might suit you well for starting the Sonic Pi server and sending initial code to execute: https://github.com/lpil/sonic-pi-tool