Making Music with Code: Introduction to Sonic Pi

Published: (February 28, 2026 at 11:11 AM EST)
8 min read
Source: Dev.to

Source: Dev.to

Have you ever explored programming beyond sorting algorithms, system design, and implementing projects?

Most of the time, we associate coding with data structures, debugging errors, or building applications. It feels structured, logical, and sometimes purely technical.

But did you know that you can also create music through programming?

You can generate beats, synths, and melodies using nothing but text. No traditional instruments. No drag‑and‑drop music software. Just code.

What is Sonic Pi

Sonic Pi, developed by Sam Aaron, is a free, open‑source live‑coding environment specifically designed for creating sounds and making music. Built on the Ruby programming language, the software grants access to a massive library of built‑in synths, samples, and effects, all controllable via simple lines of code.

Although originally conceived to teach computer‑science concepts to school children using the Raspberry Pi, the platform makes translating code into music incredibly intuitive. A simple command can trigger a note, while a loop can create a drum pattern. By adjusting timing and parameters, users can build full rhythmic structures using pure logic. Thanks to this powerful audio engine and highly creative workflow, Sonic Pi quickly caught the attention of professional musicians and algorithmic artists alike.

Installation and Setup

Getting started with Sonic Pi is incredibly straightforward. It doesn’t require complex environment setups, dependencies, or command‑line package managers.

Download: Head over to the official website at .
Choose the installer for your operating system. Sonic Pi is fully supported on Windows, macOS, Linux, and Raspberry Pi OS.

Before writing any code, getting familiar with the layout will make your creative process much smoother. The Sonic Pi interface is purposefully designed to be distraction‑free while keeping powerful reference tools right at your fingertips.

SONIC PI Interface

SectionDescription
A. Play ControlsMain audio buttons – Run code, Stop all sounds, Save workspace, Record to an audio file.
B. Editor ControlsQuick buttons to increase or decrease the code‑text size.
C. Info and HelpToggles to open the built‑in Help menu, view app information, or access Preferences.
D. Code EditorMain workspace. It automatically colour‑codes your text (e.g., numbers appear blue) for easy reading.
E. Prefs PanelControl centre for tweaking underlying settings such as volume, stereo/mono output, and log details.
F. Log ViewerReal‑time readout of your track. It prints exactly when each sound triggers, showing what the computer is doing.
G. Help SystemPowerful built‑in manual packed with tutorials and dictionaries for every synth, sample, effect, and command.
H. Scope ViewerAudio visualiser that shows the shape, size, and phase of your sound waves as they play.

Basic Syntax and First Sound

Before you can mix together complex drum loops or layered synthesizers, you need to know how to make a single sound. Mastering the foundational building blocks of the language means learning how to trigger a note, manage the flow of time, and understand how instructions are read from top to bottom. It’s time to transform that blank coding window into a working musical instrument.

The Core Duo: play and sleep

This is the absolute foundation of Sonic Pi. Music is just sound and silence happening over time.

  • play triggers a note (using MIDI note numbers or traditional note names).
  • sleep tells the program how many beats to wait before reading the next line of code.
play 60      # Plays Middle C (MIDI note 60)
sleep 1      # Waits for 1 beat
play :D4     # Plays the note D in the 4th octave
sleep 0.5    # Waits for half a beat

Using Pre‑recorded Sounds: sample

Instead of synthesising a tone, you can trigger built‑in audio files like drum kicks, snares, or ambient noises.

sample :bd_haus   # Plays a house‑music bass drum
sleep 1
sample :sn_dolf   # Plays a snare drum

Shaping the Sound: Parameters (Opts)

You can alter how a play or sample command sounds by adding parameters (called “opts” in Sonic Pi) after the main command. They follow a key: value syntax.

  • amp: Changes the volume (amplitude). 1 is default; 0.5 is half volume.
  • pan: Moves the sound left (-1) or right (1).
  • release: Changes how long the note takes to fade out.
play 60, amp: 0.5, pan: -1   # Plays quietly in the left ear
sample :bd_boom, amp: 2      # Plays a bass drum twice as loud

Changing Instruments: use_synth

Sonic Pi defaults to a basic sine‑wave “beep.” You can change the instrument for the whole composition, or just for specific notes.

use_synth :tb303   # Classic acid bass sound
play :E2
sleep 1

use_synth :prophet # Rich, sweeping synthesiser
play :E4

Loops and Live‑Coding Functions

Linear Lists of Play and Sleep Commands Get Tedious Quickly

Music is inherently repetitive, which makes it perfect for coding loops. While you can use standard Ruby times loops, Sonic Pi introduces a special, magical structure called the live_loop.

The live_loop is the beating heart of Sonic Pi. It allows you to change the code while the music is playing without stopping the track.

Basic Repetition: The times Block

If you want a specific sequence to play a set number of times before moving on to the next part of your code, use the times block. Everything nestled between the do and end statements will repeat.

4.times do
  sample :bd_haus
  sleep 0.5
end
play :C4   # This plays only after the 4 drum beats finish

The Heartbeat of Live Coding: live_loop

While standard loops are useful, the true power of Sonic Pi lies in the live_loop. A live_loop repeats forever, but crucially, it runs independently. This means you can have multiple live_loops running at the exact same time (e.g., one playing drums while another plays a bassline).

Even better, you can edit the code inside a live_loop while the music is playing, and it will automatically update on its next cycle.

Every live_loop needs a unique name, written with a colon (e.g., :drums).

live_loop :drums do
  sample :bd_tek
  sleep 1
end

live_loop :hihat do
  sample :elec_tick
  sleep 0.25
end

Packaging Code with Functions: define

If you have a specific melody, chord progression, or drum fill that you want to use multiple times throughout your song, you can package it into a function. Think of a function as teaching Sonic Pi a brand‑new, custom command. You do this using define.

define :my_melody do
  play :C4
  sleep 0.5
  play :E4
  sleep 0.5
  play :G4
  sleep 1
end

Bringing It Together: Calling Functions Inside Loops

Functions become incredibly powerful when you drop them inside your loops. This keeps your main performance space clean and allows you to trigger complex sequences with a single word.

Define your custom musical phrase

define :drum_fill do
  4.times do
    sample :sn_dub, rate: 1.5
    sleep 0.25
  end
end

Call the function inside your repeating loop

live_loop :main_track do
  4.times do
    sample :bd_haus
    sleep 1
  end
  drum_fill   # Triggers your custom 4‑beat snare fill here!
end

Welcome to the Music Industry

Congratulations! You have just learned the basic syntax of Sonic Pi!

You now have the syntax, the structure, and the tools—so it’s time to make some noise. By combining the simple commands and loops you have explored, you possess everything you need to start producing real, multi‑layered electronic tracks.

Now it’s your turn to create your own music and experiment. The blank screen in front of you is a limitless recording studio. Try:

  • Swapping a kick drum for a crash cymbal
  • Speeding up the tempo
  • Typing random notes just to hear how they sound

In live coding, there are no mistakes; a simple typo might just become your favorite new bassline.

Ready to take your tracks to the next level? Enhance your skills and dive deeper into Sonic Pi here:

Conclusion

Sonic Pi proves that programming does not have to be rigid or strictly mathematical; it can be incredibly expressive and deeply musical. By lowering the barrier to entry, this Ruby‑based environment empowers anyone to translate pure logic into rhythm and melody. Whether you are using it to grasp the fundamentals of computer science or to perform algorithmic beats live on stage, the software bridges the gap between code and art. The power to create is literally at your fingertips—all you have to do is type play.

0 views
Back to Blog

Related posts

Read more »

Google Gemini Writing Challenge

What I Built - Where Gemini fit in - Used Gemini’s multimodal capabilities to let users upload screenshots of notes, diagrams, or code snippets. - Gemini gener...