# Sonic Pi Script Utilities

**URL:** <https://in-thread.sonic-pi.net/t/sonic-pi-script-utilities/5666>\
**Category:** Support, Help & Resources\
**Created:** [June 4, 2021, 5:35am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-script-utilities/5666 "2021-06-04T05:35:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![din](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@din](https://in-thread.sonic-pi.net/u/din)\
**Post date:** [June 4, 2021, 5:35am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-script-utilities/5666/1 "2021-06-04T05:35:02Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [June 4, 2021, 6:32am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-script-utilities/5666/2 "2021-06-04T06:32:03Z")

</div>

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’ 🙂).

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](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:

> [@Idea for drums management](https://in-thread.sonic-pi.net/t/idea-for-drums-management/4874):
>
> Hello, I’m new here and I want to share with you this implementation I made to handle drums inside a Sonic Pi track, in order to understand if this can be a good solution slight_smile I liked the idea I found around the web (mostly [here](https://www.youtube.com/watch?v=RaJZho7p2Y4) and [here](https://gist.github.com/darinwilson/a3e5909db339838a67fe)) to map the beats inside an array, to emulate somehow the common grids used by real drum machines and simplify the creation of beats. I also wanted to have something that gives the possibility to manage in easy way the evolution of the beats during …

> [@Rhythm notation for tuplets with amplitude](https://in-thread.sonic-pi.net/t/rhythm-notation-for-tuplets-with-amplitude/5368):
>
> Hi everyone! blush I’m starting a generative EDM project, and I was looking for the simplest way to represent complex rhythms. This is what I found: My starting point was something I observed in @Nechoj’s work: x---x---x---x---, where each x represents a hit. I wanted to represent amplitude information, so I replaced the xs with digits 0-9: 8---6---8---6---. (A similar approach can be found in @mlange’s work — see the discussion.) My “aha” moment was when I realised that simply by factoring…

> [@General Purpose Drumkit](https://in-thread.sonic-pi.net/t/general-purpose-drumkit/4687):
>
> Greatly inspired yesterday by @alexesc’s ace method for drum patterns, and in my quest to use SPi as a general purpose live and recording instrument I wrote a general purpose drumkit script. I’ve put this in 6/4, not to annoy you but coz that’s the track I’m working on just now. Four on the floor or boots and cats are great for sure, but c’mon there’s so many other great rhythms in the world smile and not just random ones ha ha This is so much easier than working out all the individual sle…

* * *

> 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](https://github.com/search?q=sonic+pi)

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

---

<div class="post-metadata">

**Author:** ![din](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@din](https://in-thread.sonic-pi.net/u/din)\
**Post date:** [June 4, 2021, 4:21pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-script-utilities/5666/3 "2021-06-04T16:21:44Z")

</div>

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.

🙂
