Jump to content
Search

The new Ubuntu Wiki is live. Legacy content may be unavailable and page links may have changed. Read the announcement on Discourse.

Live code music with Sonic Pi

From Ubuntu Wiki

Sonic Pi is an application for live-coding music.

In this guide, you're going to learn the basics of live coding by sampling retro sounds built in to Ubuntu.

To start, we'll explore built-in sounds with simple tools from the command line. After those awkward first steps, we'll make something that sounds more like music with Sonic Pi.

Prerequisites

To follow this tutorial, it is recommended that you have Ubuntu 24.10[1] or later installed.

Built-in sounds on Ubuntu

Sounds come bundled with Ubuntu Desktop. They can be used to provide feedback to users for notifications and accessibility purposes.

You can explore some of these sounds in the file system:

ls /usr/share/sounds/ # alsa freedesktop gnome ... Yaru
ls /usr/share/sounds/sound-icons # ... glass-water-1.wav guitar-12.wav ...
ls /usr/share/sounds/yaru # ... bell.oga ... warty-startup.oga

"Yaru" is the default theme on Ubuntu Desktop.

One of the interesting sounds in the Yaru/stereo/ folder is warty-startup.oga. This is the original startup sound used in the Warty Warthog release of Ubuntu.

Playing the sounds

If you have an an app for playing audio installed, run the following to hear it:

xdg-open /usr/share/sounds/Yaru/stereo/warty-startup.oga

Alternatively, install SoX, a command-line audio processing tool:

sudo apt install sox

Once installed, you can then use the play command:

play /usr/share/sounds/Yaru/Stereo/warty-startup.oga

The output will display some information about the file and the progress of the playback:

/usr/share/sounds/Yaru/stereo/warty-startup.oga:

 File Size: 574k     Bit Rate: 402k
  Encoding: Vorbis
  Channels: 2 @ 16-bit
Samplerate: 44100Hz
Replaygain: off
  Duration: 00:00:11.41

In:57%  00:00:06.59 [00:00:04.82] Out:291k [-=====|-=====]  Hd:0.9 Clip:0

You may have noticed that the sound-icons directory has some instruments. Could we make a song? We could play a series of sounds in sequence on a loop using bash. Let's use some GNOME alert sounds:

while true; do \
  play /usr/share/sounds/gnome/default/alerts/click.ogg speed 0.8 gain -2; \
  play /usr/share/sounds/gnome/default/alerts/swing.ogg speed 0.75 gain -3; \
  play /usr/share/sounds/gnome/default/alerts/string.ogg speed 0.9 gain -4; \
done

That sounds like... something. But this is no kind of way to make music.

So let's try Sonic Pi, an application that is actually designed for making music with code.

Install Sonic Pi

Sonic Pi is available as an Appimage.

Go to the Sonic Pi website and select the Linux x64 AppImage download.

Once downloaded, make it executable:

chmod +x sonicpi.Appimage

Then run it:

./sonicpi.Appimage

Sampling and remixing the Warty startup sound

In Sonic Pi, we can sample sounds from the file system just like before.

Start with a simple loop:

set :bpm, 90

live_loop :warty_solo do
  with_fx :reverb, room: 0.7 do
    sample "/usr/share/sounds/Yaru/stereo/warty-startup.oga", rate: 2.2, amp: 0.4, start: 0, finish: 0.8
  end
  sleep 2
end

First, we set the bpm. Then we start a loop, apply some FX, and reference the sample path. We also set the pitch, amplitude, and offset.

Evaluate the code with alt+enter.

I don't know about you, but that's already sounding pretty dreamy to me.

Layering the sound

Now let's layer the sample multiple times, modifying it slightly each time. You can tweak the values and listen to what sounds good:

set :bpm, 90

live_loop :warty_solo do
  with_fx :reverb, room: 0.7 do
    sample "/usr/share/sounds/Yaru/stereo/warty-startup.oga", rate: 2.2, amp: 0.4, start: 0, finish: 0.8
  end
  sleep 2
  
  sample "/usr/share/sounds/Yaru/stereo/warty-startup.oga", rate: 1.5, amp: 0.3, start: 0.2, finish: 0.5
  sleep 0.5
  
  with_fx :reverb, room: 0.7 do
    sample "/usr/share/sounds/Yaru/stereo/warty-startup.oga", rate: 6.0, amp: 0.1, start: 0, finish: 1.0
  end
  sleep 1.5
end

Evaluate again with alt+enter to hear the music update live. Sonic Pi also comes with some built-in samples. Let's use them to add some drums to our dreamscape:

set :bpm, 90

live_loop :warty_solo do
  with_fx :reverb, room: 0.7 do
    sample "/usr/share/sounds/Yaru/stereo/warty-startup.oga", rate: 2.2, amp: 0.4, start: 0, finish: 0.8
  end
  sleep 2
  
  sample "/usr/share/sounds/Yaru/stereo/warty-startup.oga", rate: 1.5, amp: 0.3, start: 0.2, finish: 0.5
  sleep 0.5
  
  with_fx :reverb, room: 0.7 do
    sample "/usr/share/sounds/Yaru/stereo/warty-startup.oga", rate: 6.0, amp: 0.1, start: 0, finish: 1.0
  end
  sleep 1.5
end

live_loop :drums do
  sample :bd_haus, amp: 0.3
  sleep 0.5
  sample :bd_haus, amp: 0.25
  sleep 1.0
  sample :sn_dolf, amp: 0.22
  sleep 0.5
end

Next steps

Congratulations, you've live-code some music with retro Ubuntu system sounds.

Maybe you can make your own new startup sound.

To learn more about Sonic Pi, follow the excellent getting started tutorial: sonic-pi.net/tutorial.

References