Sonic Pi Script Utilities

Hi there,

while leaning sonic pi, I’ve realized you can import files through require. Meaning you can create functions which is huge to me.

I stumbled upon this really cool live coding done in vim! https://youtu.be/GPan4gRSwZs

I was wondering if anyone knows what this live_coding/utils.rb file is?

And how that pattern thing works? Does anyone have any or know of any sonic pi script repos that are useful to play around with? Searching on the internet sends me in circles.

Thanks

Hey there @din, welcome!

I’ve realized you can import files through require. Meaning you can create functions which is huge to me.

A couple of clarifications around this:
Yes, it is potentially possible to use require. However, you need to keep in mind that Sonic Pi is not Ruby. (It is only based on-top of Ruby. We did not design it to be totally compatible - and the only ‘officially supported’ commands are those specific to the Sonic Pi DSL.
Anything you attempt beyond that may or may not work. (There are some fairly basic plain Ruby commands and operators which do work for the moment, such as commands that work with Arrays and other types of Enumerables - but compatibility with any parts of Ruby may change at any time in the future. As has been mentioned here on the forum before: ‘Let the buyer beware’ :slight_smile:).

Re ‘you can create functions’ - not sure exactly if you’re talking about the same thing, but it is already very possible to create functions in Sonic Pi. (See section 5.5 of the in-built tutorial in the app, also at https://sonic-pi.net/tutorial#section-5-5).


I was wondering if anyone knows what this live_coding/utils.rb file is?

Sonic Pi allows you to load and run external files containing Ruby/Sonic Pi code. Here, Greg has manually told Sonic Pi to evaluate (run) some code he placed in his own util file. The way he calls eval and File.read is outdated however - there are already built in Sonic Pi functions for loading and running files. (See the eval_file, run_code and run_file function references in the built in ‘Lang’ section of the app’s Help panel).


And how that pattern thing works?

That is just a custom method for notating and playing rhythms that Greg has used.
In terms of how it works - that’s a bit hard to say exactly without seeing the drums function (which I’m guessing is defined in utils.rb). The basic idea though is that the :jungle live_loop defines a pattern of drums and rests, and then loops over the pattern to either play a drum (when the pattern value is a number) and sleep, or just sleep (when the pattern value is -).
There are a bunch of posts here on the forum about various ways people have explored encoding and playing rhythms - here’s a couple of posts to start with if you’re interested:


Does anyone have any or know of any sonic pi script repos

There may be repos of people’s utility files and/or Sonic Pi music compositions spread around the internet, but there are no central listings of such things AFAIK. Here’s a few of my suggestions:

  • It’s quite possible that many folks on the forum here share links to their code in their posts, so might be worth searching for keywords here like github (just not in the ‘Support, Help & Resources’ category, since you’ll get drowned in posts about GitHub issue tickets)
  • GitHub is a fairly common place for people to store code, so it’s well worth searching there directly - try https://github.com/search?q=sonic+pi

Hope that all helps! It’s quite possible others might have further suggestions too.

1 Like

Oh wow, this was a huge clarification.

Wish there was more documentation on how sonic pi works (out side of the tutorials) focused on the programming aspect of what you can do with it.

For example I’ve never touched Ruby until playing with Sonic Pi and only due to your reply do I now realize that it’s not really “Ruby”. But it be so cool configure sonic pi with custom functions that I can just call. Regardless, the more I learn about it the more I appreciate that it exist.

There is so much I’ve yet to try with Sonic Pi like connecting it to midi, or adding different synths etc…

Thank you for talking the time to write all of that. I’ll lurk here and try and get a feel for what others are doing.

:slightly_smiling_face:

1 Like