Discover Sonic Pi: Install and Code Music on Raspberry Pi

Sonic Pi is a fun project for your Raspberry Pi that lets you create music with lines of code. However, the initial setup and understanding of how to write your first lines can be a bit intimidating. That’s why I’ve created this beginner’s guide for you!

A package for Sonic Pi is available on the official website and can be installed with a few command lines. From there, a special programming language generates notes from the coding interface.

This article will act as a comprehensive guide for Sonic Pi on the Raspberry Pi, covering how to install and get started with the software’s functionality. Before we get started, let’s learn more about Sonic Pi and its Raspberry Pi installation requirements.

If you’re looking to quickly progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge where you learn one new thing every day until you become a Raspberry Pi expert. The first third of the book teaches you the basics, but the following chapters include projects you can try on your own.

Want the best experience? Become a premium member for ad-free browsing, access exclusive content, and ask questions in our private forums. Your membership helps support the site!

Sonic Pi – A Gentle Introduction

Sonic Pi, created by Dr. Sam Aaron, is a free and open-source software which allows users to create music through a simple coding interface. It provides a new way for people to apply their musical skills to produce something great.

There are various features that the software provides to make it stand out in comparison to other similar software. For instance, the “Live Coding” functionality allows you to change your music code in real-time and the music will change accordingly. That is just great!

Now that we know what Sonic Pi is, let’s start by installing it on our Raspberry Pi.

Installing Sonic Pi on Raspberry Pi

This section will help you install Sonic Pi on your Raspberry Pi. Let’s start with the hardware and software requirements you will need to follow this tutorial.

Hardware and Software Requirements

You will need to have the following to use this tutorial:

  • Regarding hardware, I suggest using a Raspberry Pi 3 with 1 GB RAM at minimum. I recommend having a Raspberry Pi 4 with at least 2 GB RAM.
  • You also need an 8 GB SD Card (preferably Class 10 or higher).
  • The Pi should ideally be running the latest version of Raspberry Pi OS Bookworm for the software as this tutorial has been tested for that OS version.

You will need access to the Raspberry Pi Desktop (terminal access will not suffice).

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

One option is to set up VNC on your Pi so you can access the interface remotely. Here is a tutorial for this: Getting Started With VNC on Raspberry Pi (Bookworm update). But do note that if you use VNC for this tutorial, you will need a pair of wired headphones connected to your Pi to get audio access (VNC does not have that in the free version).

So maybe the best setup for this project is to use an HDMI-supported Monitor with a keyboard and a mouse to control your Pi, as I did.

Installing Sonic Pi on Raspberry Pi OS

Installing Sonic Pi on the Pi is easy. Make sure you are booted into Raspberry Pi OS and follow the simple 3-step guide given below:

  • First, head on to Sonic Pi’s website homepage. Scrolling down a bit will show you the different platforms the software supports. Click on the one that says Raspberry Pi OS.


  • Clicking on it will take you to the Raspberry Pi installation section of the website. There, you will see the option to download the 64-bit and 32-bit versions of the software. So, choose which one you would need based on your OS version. I am using the 64-bit version.


    The 32-bit version download link is available just below the section shown in the figure above.
  • This will start the download for the latest version of Sonic Pi installer (v4.5.1 at the time of writing this article). It may take some time depending on your internet speed.

    Once the download is complete, type in the following commands in a new terminal window to complete the installation:

    cd <your Pi's download directory path>
    sudo apt update
    sudo apt install ./sonic-pi_4.5.0_1_bookworm.arm64.deb


    Note that the file name in the last command may change if you are installing the 32-bit version or a newer/older version of the software. So, modify the command accordingly.

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.

Once all the above steps are done, you have successfully installed Sonic Pi on your Raspberry Pi. Let’s create some sweet music now!

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

Getting started with Sonic Pi

This section is the heart of this article. We will be covering the basics of this software and we will also be learning a lot about the features of Sonic Pi. So, let’s start with the absolute basics.

Before starting with that, let’s quickly see how to start Sonic Pi. Just open the Raspberry Pi’s Start menu, and you will find the application under the Programming section.

Clicking on it will start the application. The application home screen will look a bit overwhelming if you are seeing it for the first time, so don’t panic (this was what I told myself after seeing the home screen for the first time). Let’s explore the interface a bit (Image from official tutorial).

For a detailed explanation of each labelled section, refer to this tutorial chapter created by Sonic Pi itself. Once you are familiar with the GUI, let’s take our very first step in coding music: playing a simple sound.

Playing your First Sound

This is the very foundation of any musical composition: the sound. Let’s learn how to write a simple code that allows you to play the simplest possible sound: “a beep”.

The first code is simple; in fact, it’s just a simple line:
play 65

Once you have written this in the code editor, press the “run” button to execute this code. As expected, it will play a simple beep sound.

Congratulations on creating your first musical note! Now, here is what’s going on; the values after the word “play” represent MIDI note numbers. Here is a table that will help you understand the relation better.

CDEFGAB
60626465676971
Music Notes mapped to MIDI Notes

You can choose a value between 60-120 and the intensity of the sound will change accordingly (lower values ⇒ lower pitched sounds).

Loops: Continuously Playing a Tune

Next, what if you created a particular beat pattern you would like to play, say 8 times. It wouldn’t be ideal to write the same thing 8 times. Hence, we will see how we can create a simple loop that plays the same sound over and over.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

The following code lets you do that:

3.times do
# your music code
   play 65
   sleep 1
end

This is how to write a simple loop in Sonic Pi. Let’s understand the above code block:

  • The number 3 indicates the number of times you would like to repeat the tune you have coded.
  • After “do”, do not forget to add tab spacing for the music code. The sleep command allows you to add delays to your music (in terms of the number of seconds).
  • Finally, the end command is used to show the system that the code ends here.


Aside from this, if you want to play a tune forever, you can replace the “3.times” with the loop command. Next, let’s see the ways you can modify a tune.

Parameters for your Sound!

Now that we know how to play a sound, let’s tweak it according to our taste! There are several parameters you can use to modify your tunes. Let’s explore some important ones in this section.

Amplitude

The first thing you can change is the amplitude (Loudness) of your sound. You can do this by adding the following command after play, separated by a comma as shown below. Let’s double the amplitude.

play 65, amp:2

This will double the loudness of the sound you are playing. Let’s see the difference.

Panning

Panning a sound (in stereo) allows you to select the sounds you would like to play from each speaker (left or right). The table below will help you map the values with the speaker.

Left CenterRight
-101
Value to Speaker Mapping for Panning


Also, you can choose any value in this range to control the exact sound position you would like in stereo. Here is the code example for this:

play 65, pan:-1
sleep 1
play 65, pan:1

Let’s see how this sounds actually (For this, I would suggest connecting headphones to your Pi to hear a clear distinction between the 2 sounds). You will see that the pink graph in the scope represents the left speaker sound and the blue graph shows the right speaker sounds.

Switching Instruments (Synths)

Although beeps are fun to listen to, you will probably be bored of them like me after you work with them for an hour or so. Let’s learn how to change these sounds in Sonic Pi.

Basically, they are called synths, which essentially mean a different instrument (each synth has unique properties). You can choose from over 20 synths Sonic Pi offers (look at them from the Synths section in the Help window).

Let’s try the prophet synth. It’s easy to change the default synth (which is beep) using a single line of code:
use_synth:prophet
play 65, amp:2


This is how it sounds in Sonic Pi:

Exploring Different Sounds

You now know how to play a single sound multiple times. But, think about it, can you create music using a single sound? Of course not! So, let’s see how to add multiple sounds and play them together.

As shown in the previous section, you can add delays between them using the sleep command and even change how it sounds with parameters to compose a melody. We will start with the basics: playing a combination of 3 sounds. We will use different synths too.

use_synth :tb303
play 38, amp:2
sleep 1
use_synth :dsaw
play 50, amp:2
sleep 1
use_synth :prophet
play 57, amp:2
sleep 1

This will play 3 different sounds with a delay of one second between each sound.

Introducing Samples: Creating some Music!

Sonic Pi offers a range of pre-recorded samples you can choose from. As before, explore the samples available from the help section.

Let’s use samples to create a simple melody in Sonic Pi. It’s great progress: starting from a simple beep to creating a real tune. The neat thing is, it’s easy!

use_synth :mod_fm
sample :ambi_lunar_land
play 60
sleep 0.5
play 65
sleep 0.5

I first played a great sample followed by a closing sound using one of the synths (mod_fm). You can play with the code anyway you want to create some great music. Do share the tunes in the comments below!

Playing Multiple Tunes

Until now, we have explored the premise where we play multiple tunes one after the other by adding delays between them. However, music often has multiple tracks playing at the same time (one in the background, like drum rolls and the song on top of that).

Guess what, this can be done easily in Sonic Pi as well! The code specified between in_thread do and end will be played at the same time. Here is a simple example:

# The Melody
in_thread do
  3.times do
    sample :loop_amen
    sleep 1.753
  end
end

# Bacground Track
in_thread do
  2.times do
    play 75
    sleep 1.753
    play 74
    sleep 0.25
  end
end

The first in_thread block acts as the main melody and you can add the background music (called backtrack) using a second in_thread block as shown above. Playing this will run both blocks simultaneously.


This marks the end of this section and the tutorial.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Conclusion and Next Steps

Well! This was a fun tutorial, right? Guess what, the fun has just begun. This was a basic tutorial designed to get you started with Sonic Pi. It provides more customizations and features for the users, like:

To conclude, you are just getting started! I provided you with the basics you need to create some great music. Stay tuned and create!

Whenever you’re ready, here are other ways I can help you:

The RaspberryTips Community: If you want to hang out with me and other Raspberry Pi fans, you can join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.

Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.

The Raspberry Pi Bootcamp: Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts