I’m trying to make sonic pi control a light show on my Pi Top pulse. I have the music written in sonic pi and I have the code for the led pattern in python. I’ve found this code that uses osc messages to sync the LEDs to music but I can’t figure out how to edit it to use my music and pattern. I’ve only taught my self a few bits and pieces of coding so any help would be great. Here’s the example code I found.
Hi Ryan
I wrote the article you refer to. Have you tried that first, before going to your code? One potential problem. What version of Raspian or pitop os are you using? Unfortunately the version of sonic pi currently on the latest Raspian buster will NOT be able to send OSC messages. It says it is version 3.1 but is cut down and doesn’t support OSC or midi commands. There are two solutions. Either build version 3.1 or 3,2dev from source or revert to using raspbian scratch with sonic pi 3.0.1 This will not work on a pi4 bit is ok on earlier version of the pi. I have indicated how to do the build for v3 elsewhere on this site.
I’m using a raspberry pi 3 with buster. I’ve got your code up and running on it and it works fine. The sound plays through the pulse and I get a pattern of LEDs in sync with the music. I’m just struggling to understand how I’d edit your code to use my own music and pattern. I’m fairly new to coding and have mostly just made some music in sonic pi and edited python code I’ve found in tutorials.
As programmers we often encounter these situations where a piece of code seems like it will solve a problem, or is closely related to a problem one is trying to solve. My advice is that if it isn’t somewhat obvious how to transition Robin’s example to your needs, then to use it more as a learning guide than a starting point.
As you identified, OSC will provide the foundation for communicating events from your Sonic Pi script to one running on the Pi Top. OSC is a simple, flexible way of sending messages / data back-and-forth and the Sonic Pi docs for it are here: https://sonic-pi.net/tutorial.html#section-12. On the Python side you can use: https://pypi.org/project/python-osc/
I recently added a simple example of receiving OSC events in Python from Sonic Pi: Link sonic pi to Python
For your case, the messages and values to send are whatever makes sense to define for your needs. The example you linked defines 4 custom OSC events: pulse/clear
, pulse/xyrgb
, pulse/setall
, pulse/bright
. The Python script sets up an OSC listener for those events and dispatches to the appropriate function for whichever message comes in. You can reuse this approach, but your events can be called and organized however you’d like. For example you could instead have pitop/clear
, pitop/start_sequence
, my_app/light_row
, whatever/something_else/
, etc.
In the Sonic Pi part of the script you linked, you can see in various places sending out the OSC messages. You’ll edit your Sonic Pi script similarly, to send out the events you have defined at the appropriate places.
Try to get a simple example / modification in your code working first. For example have a live_loop
send out an event and set up your Python script to receive and print out a debug line or flash all the lights or something. Then you should be off and running …
Hi Ryan
I gather you have my software already working, in which case most of the hard work is done.
Basically the python fill will handle all the OSC calls you need sent from Sonic Pi, and it is a question of interleaving suitable OSC calls with statements to produce sounds.
The basic osc calls you can use are
osc “/pulse/clear” which will switch off all the leds
osc “/pulse/setall”,r,g,b where r,g,b are numbers in the range 0.0->1.0 specified the red green and blue components of the colour which will set all the leds to the same specified colour
osc “/pulse/xyrgb”,x,y,r,g,b where x and y are coordinates (each 0->6) of a led and r,g,b are the colour values (0-1.0) assigned to each component of the led colour
osc “/pulse/bright”,b sets the brightness of all the leds where b is a number 0.0->1.0
I suggest you start the python script running as per the instructions in the article.
The start a new Sonic Pi program.
use_osc_logging true #show osc messages in the log
use_osc "localhost",8000
osc"/pulse/bright",1
sleep 0.2
osc "/pulse/setall",1,0,0
sleep 1
osc "/pulse/setall",0,1,0
sleep 1
osc "/pulse/setall",0,0,1
sleep 1
osc "/pulse/setall",1,1,1
sleep 1
osc "/pulse/clear"
That should cycle all the leds round reg green blue white off
You could try adding a sonic pi sound for each e.g.
use_osc_logging true #show osc messages in the log
use_osc "localhost",8000
use_synth :tb303
osc"/pulse/brighth",1
osc "/pulse/setall",1,0,0
play :c3,release: 1,cutoff: 90
sleep 1
osc "/pulse/setall",0,1,0
play :e3,release: 1,cutoff: 100
sleep 1
osc "/pulse/setall",0,0,1
play :g3,release: 1,cutoff: 100
sleep 1
osc "/pulse/setall",1,1,1
play :c4,release: 1,cutoff: 100
sleep 1
osc "/pulse/clear"
You can try addressing single leds using the
osc “/pulse/xyrgb”,x,y,r,g,b command
Ending up with music and leds accompaying is then a process of experimenting.
Try a simple tune then intersperse it with osc calls.
I don’t have access to my pulse unit at prsent, but will try something when I have it again.
Ok thanks so much for your help. I’ll have a go in the next few days and let you know how it goes.