Use of Erlang in Sonic Pi

@samaaron so I wanted to understand a bit better the role of Erlang in the SP ecosystem. There’s not a lot of Erlang there, just osc.erl and pi_server.erl. I uncommented the log messages in the latter, recompiled and tried a few note experiments in the editor. It looks to me like Erlang is handling the o2m messages, sending OSC messages via UDP to the (serverised) osmid o2m process. So am guessing Erlang is in some way handling the timing of MIDI messages. But it doesn’t seem to be involved in sending messages to SCD, at least I couldn’t see any log messages. Could you elaborate a bit on Erlang’s role ? Am interested in why it might be used for timing in one part of the system but maybe not another. Thank you -

Hi there,

Sonic Pi works by scheduling all events ahead of time.

Prior to v3, the only events that needed to be scheduled were audio events. This was made possible because SuperCollider, the synth engine that Sonic Pi uses, is able to receive timestamped messages ahead of time, and schedule them internally to execute at exactly the right time. This is made possible via OSC bundles. For a (probably way too detailed treaty of this take a look at this paper: https://www.cs.kent.ac.uk/people/staff/dao7/publ/farm14-aaron.pdf)

With the release of v3, Sonic Pi gained the ability to talk OSC and MIDI. It’s clearly important that these OSC and MIDI events do not happen with the code executes in Sonic Pi as this would be before the audio is heard. This is because both the audio and the MIDI/OSC events will be created ahead of time, whilst the audio event will then be accurately scheduled to happen a little later and the MIDI/OSC events will happen immediately.

It was therefore essential to schedule MIDI/OSC events in a similar fashion to the audio events. Unfortunately SuperCollider’s scheduler only manages its own internal events, so it was clear that we had to build our own scheduler which worked in a similar fashion.

Unfortunately an implementation in Ruby would be subject to potential timing issues as Ruby has a global garbage collector which can execute at arbitrary times. I therefore wanted to use a language built for reliably low latency and high concurrency (lots of messages coming in independently wanting to be scheduled independently). Erlang is a perfect fit for this.

Luckily we had Joe Armstrong (one of the co-creators of Erlang) contribute a first iteration of a scheduler which is what you see with osc.erl and pi_server.erl. This scheduler is able to take any OSC message and delay it for a specific amount of time before forwarding it on to a different host/port with ~1ms accuracy (more could be achieved at the cost of CPU).

The next step was to implement all external non-audio comms as OSC messages. Clearly this is trivial for OSC messages, but for MIDI we implemented an OSC -> MIDI bridge (thanks to the good work of Luis Lloret) which would convert OSC messages directly to MIDI as quickly as possible (this is implemented in efficient C++).

To bring it all together, the audio events sent to SuperCollider are as before, they just go directly to Supercollider from the Sonic Pi server. All other events are first encoded as OSC, sent to Erlang which schedules it to trigger in time with SuperCollider then we have tools to convert OSC to MIDI.

I can totally imagine other similar tools converting OSC to other useful formats which will also allow us to take advantage of the Erlang scheduler in the future.

Does this help? :slight_smile:

4 Likes

Yeah this is great, thanks for spending the time to document. I have a good deal of Erlang experience (and some Ruby) so this makes a lot of sense.

1 Like

Is it possible to run my own Erlang program using what’s included in Sonic Pi? I don’t know much about Erlang, but I found this tutorial (http://howistart.org/posts/erlang/1/) a while ago, which looked pretty fun. I wasn’t sure about installing Erlang just to try out the tutorial, but if it’s possible to do it with just what’s in Sonic Pi, that would be pretty neat.

Sure you could bundle the SP Erlang files in your own Erlang server and compile it, but to what end ? To do anything meaningful you’d have to include all the m2o and o2m stuff as well to be able to send MIDI messages. Erlang is great but has relatively steep learning curve. What exactly are you trying to achieve ? There is probably a simpler way.

Well, there’s nothing specific that I’m trying to do, I just thought that maybe in the same way that I can learn Ruby a little bit at a time by typing something in and pressing command-R, maybe there’s some way to type in Erlang in Sonic Pi, and press command-R, and have it run it. The tutorial was kind of funny, and I like things with funny learning material, but if it involves compiling and configuring things, then that’s kind of a downer.

I would stick with Ruby initially, much much simpler