Sonic Pi Script Utilities

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