Sonic Pi and Docker - the story so far

This is the first in what I hope is a series of posts explaining some of the directions and projects that I’ve tried with Sonic Pi in the past. It’s an attempt both to share my experiences (and mistakes) and to inspire those who want to pick up the baton if any of these ideas interest them. I have quite a backlog of ideas from my three years or so working with SP (ideas are cheap!).

A while ago I went crazy and tried to get Sonic Pi working inside a Docker container. The logic was that I wanted to work toward creating an online Sonic Pi bot that could “render” any code to an audio file. Docker made sense because it allows it to be sandboxed on a server and it would also “solve” the issue of complex server setups, especially where audio configuration was concerned.

Initially I had a big hairy Dockerfile which installs all the packages, configures SSH and audio and then builds SuperCollider and Sonic Pi from source e.g. https://github.com/xavriley/sonic-pi-docker/blob/master/Dockerfile#L5-L35 I got this working in virtualbox with audio, although this will probably fail now as you’d need to compile the Sonic Pi release from the time which was around 2 years ago.

A little while later I tried using the Debian package and it was much simpler! That work is in this branch here https://github.com/xavriley/sonic-pi-docker/tree/server_rendering What this branch also allows is for Sonic Pi code to be “rendered” to a wav file.

Another development since I tried that (around 9 months ago) is that Heroku (who I work for) now support Docker containers meaning that I have a place to run this if I want to https://devcenter.heroku.com/articles/container-registry-and-runtime

All the pieces are there, I just need to do the hard work of turning this into a useful service. I’m thinking something that will look for gists https://gist.github.com tagged with #sonicpi or whatever, fetch these and render an audio file (say 1 minute by default) and then upload it to a service like https://clyp.it/ (which has gone premium since I last looked…)

Any comments or queries are welcome. Would this be a useful thing?

5 Likes

Hi Xavier

I don’t know if you remember but I met you a couple years back in Horsham - Tubular Bells etc.

Anyway I think what you are proposing would be great for Code Clubs if you could ‘render’ the Code from Trinket etc.

One of the problems with the Sonic Pi projects in Code Club is that it can’t be run in the browser like most the other projects can - event the Sense Hat now has an emulator.

If the kids could use Trinket and then ‘export to WAV’ etc I think you could be onto a winner.

I think its a great idea to quickly browse through online available sonic pi scripts and play them there and then. The online service may become a hub for compositions.

Also there can be some sort of a magnet link to send code in the gist from browser to the locally running sonic pi instance so that people may not need to render it on a remote server.

Lovely idea, although I’d be wary of executing code from a link as it would be a powerful attack vector without sufficient sand boxing :slight_smile:

Big announcement - I have Sonic Pi running on Docker on Heroku and rendering audio files!

See the README here for instructions https://github.com/xavriley/sonic-pi-docker/tree/server_rendering

This is still very early days so it’s just a proof of concept rather than a finished product at the moment. It’s running here:

sonic-pi-gallery.herokuapp.com

This site will take code, wrap in in record_start and record_stop statements and upload the resulting audio to S3 for further listening. There’s no database or anything to store submissions but building a website around this is the easy bit :slight_smile:

Feels good to get this working after what was probably months of research on and off. It also opens the doors to other things like streaming audio and twitter bots. There’s a “todo” list for ideas on the README so I welcome contributions to that.

If anyone wants to use their webdev skills to help me polish this up and turn it into a community resource that would also be very welcome.

4 Likes

Nice work. Had it playing scales quite happily.

1 Like

Oh, neat! This might be exactly what I’m looking for - I’m looking to play around with machine learning and Sonic Pi, and to that I figure I need to supply code and receive a sound file.

Do you know if this can render sound at faster than 1:1 speed?

1 Like

It can’t render faster than 1:1 at the moment but you can run multiple instances of it fairly easily. That would give you some parallel processing at least.

SuperCollider (the underlying synth engine for SP) does allow for some non-realtime processing but it wouldn’t be compatible with the way that Sonic Pi sends timed messages.

I’ve been chipping away at this

https://sonic-pi-gallery.herokuapp.com/

It’s now a bit more stable and shows the code along with the rendered file. You still have to refresh the page manually for the results to show (takes around 30 seconds) but it should be basically usable. If anyone wants to use it to share examples etc. feel free to do so.

2 Likes

Is there a duration time limit on the code uploaded? It rendered the first 34 seconds of a longer piece I tried.

Yes there’s a limit of 30 seconds at the moment which is arbitrary. I don’t think I can detect when a piece stops naturally yet so I have to impose some kind of time limit for now. Should be possible to fix that in future though.