on
sπ00: Sonic Pi Setup for Hackers
This is the starting point in a series of posts in which I document my process of getting to known Sonic Pi while also learning and relearning a lot about making music. I try to write one post per month, aggregating some information and posting only things that work or where I understand why it doesn’t work.
This is the 0-th part of the series and is supposed to explain my setup and enable you to reproduce it. The main point is that I want to be able to control Sonic Pi from Vim. Then I can use all the features of Vim to fine tune my work flow.
What is Sonic Pi?
Sonic Pi is a software for algorithmically creating electronic live music, originally written to run on a Raspberry Pi, hence the name.
It defines a language derived from ruby, that can be used to create and control sounds. There are two was to make a sound in Sonic Pi: Samples and synthesizers. For example playing a C with the default synthesizer is very easy:
play :C
You can select a different synthesizer with use_synth
to create different sounds.
All the usual features of general purpose programming languages are part of your tool box:
Loops, functions, data structures like lists and much more.
Here is an example where the choose
method of lists is used to pseudo randomly select notes from a list.
use_synth :saw
loop do
use_random_seed 42
16.times do
play [:E2, :G2, :Bf2, :B2].choose, sustain: 0.2, release: 0.1, amp: 0.4
sleep 0.2
end
end
This will play a pseudo random melody, consisting of only the notes E, G, B♭ and B that repeats every 16 notes.
Listen how it sounds here:
Do you want to create you own? Read on.
Getting Sonic Pi
To start you need to install Sonic Pi on whatever system you want to produce your music. If you actually use a Raspberry Pi you might already have it, since it is pre-installed on Raspbian.
Sonic Pi was written with a Raspberry Pi in mind, but it will also run on your laptop if you can get a version compiled for your operating system. For my part I use Archlinux where I can install from the official repositories
$ sudo pacman -S sonic-pi
It should be as easy for many Linux distros. For different systems have a look at the installation instructions.
Sonic Pi in hacker mode: Starting a headless server
The UI of Sonic Pi is nice, but we want to get our hacker credibility up, we shouldn’t be content with the intended interface. Luckily it is already implemented in a client/server model, we just need a way to start the server without the default client and a different client.
There are tools to do this, I use sonic-pi-tool (mainly because it is written in Rust, which makes our hackiness go way up). Here we have to work a bit harder to install it, but not much. First you need Cargo, the Rust package manager, which should be available for all major operating systems. Then you can run
$ cargo install -f --git https://github.com/lpil/sonic-pi-tool/
This will run for a while since it compiles the tool and it’s dependencies from source.
As an alternative you could use
sonic-pi-cli
,
but I arbitrarily decided to go with sonic-pi-tool
.
Integrating in Vim
Now we’re getting closer to a actually talking with the server.
In principle we could build this ourselves using only the sonic-pi-tool eval
command
(you can consider this as an exercise for the reader).
But there is also a plugin, which is pretty convenient,
called sonicpi.vim.
You can install it using your favorite plugin manager, for example I use minpac:
" init.vim
call minpac#add('dermusikman/sonicpi.vim')
The plugin defines two key bindings:
<leader>r
: send current buffer to server<leader>S
: stop all running loops
which is basically all you need.
(If you don’t know what <leader>
means, it means \
for you.)
Workflow
Let’s make some music. When I want to play with Sonic Pi I just open a terminal and type
$ sonic-pi-tool start-server
and put it somewhere in the background or on another workspace. Then I open two separate terminals, one where I type
$ sonic-pi-tool logs
and one where I open a .rb
file with vim.
$ vim techno.rb
That’s it.
Start typing some live loops and use <leader>r
whenever you want to update them.
If you want, you can stop reading here and concentrate on the music. The rest of this post are relatively specific additions I made.
Extended auto completion with deoplete
sonicpi.vim
provides completions for build in samples and synths.
But I usually use a lot of custom samples and thought it would be nice to also have completions for them.
Additionally the completions that sonicpi.vim
provides it provides are not read by
deoplete, the completion engine that I use.
So I wrote my own Vim plugin: deoplete-sonic-pi. I believe writing Vim plugins is the ultimate form of procrastination, so I tried to keep it short. It’s just one python file, that add the feature I wanted via the deoplete API.
You can install it like any other plugin.
" init.vim
call minpac#add('frcl/deoplete-sonic-pi')