Skip to main content

Quick Play with SonicPi (updated)



From Nursery Rhymes to Algorithmic Art: A Maker’s Journey with Sonic Pi

For many in the STEM community, the "A" in STEAM (Science, Technology, Engineering, Arts, and Mathematics) can feel like a high fence. We are comfortable with logic gates, sensor arrays, and Python loops, but "music theory" often feels like a foreign language. I will be the first to admit: I have zero musical ability. I don’t play an instrument, and sheet music looks like an encrypted text to me.

However, I do understand code. And that is why Sonic Pi is such a revelation for the maker and educator community. It turns music into a physical computing project where the "wires" are lines of Ruby-based code and the "output" is an immersive soundscape



The Entry Point: Deconstructing the "Hot Cross Buns"

Every maker starts with "Hello World." In the world of music education, that equivalent is often "Hot Cross Buns." It’s a simple, three-note descending melody. To get my head around the software, I treated the melody like a data set.


Instead of worrying about staves and clefs, I used MIDI numbers—a system where each note is assigned a value (e.g., Middle C is 60). Using resources like the UNSW note-to-frequency charts, I mapped the recorder notes to their digital counterparts.


use_synth :tech_saws
2.times do
  play 71
  sleep 0.5
  play 69
  sleep 0.5
  play 67
  sleep 0.5
end
4.times do
  play 67
  sleep 0.25
end
4.times do
  play 69
  sleep 0.25
end
play 71
sleep 0.5
play 69
sleep 0.5
play 67

The "Aha!" Moment: Code as an Abstraction of Theory

The real shift occurred when I moved away from manual MIDI numbers and started using the chord function and with_fx blocks. This was my "Aha!" moment: Music theory is just another form of abstraction.

In programming, we use functions to hide complexity. In Sonic Pi, the chord(:b4, :minor7) function does exactly that. It takes a complex musical concept—the specific frequency ratios that make a chord sound "sad" or "aggressive"—and packages it into a single, readable command. For a non-musician, this is empowering. Suddenly, I wasn't just playing notes; I was manipulating the emotional texture of the sound through logic.

use_synth :prophet
2.times do
  play 71
  play 62
  play 66
  sleep 0.5
  play 69
  play 60
  play 64
  sleep 0.5
  play 67
  play 70
  play 62
  sleep 0.5
end
4.times do
  play 67
  play 70
  play 62
  sleep 0.25
end
4.times do
  play 69
  play 60
  play 64
  sleep 0.25
end
play 71
play 62
play 66
sleep 0.5
play 69
play 60
play 64
sleep 0.5
play 67
play 70
play 62
sleep 0.5


Adding sound effects is great fun - same code as above with a slightly techno sound to it

use_synth :prophet
with_fx :ixi_techno do
  2.times do
    play 71
    play 62
    play 66
    sleep 0.5
    play 69
    play 60
    play 64
    sleep 0.5
    play 67
    play 70
    play 62
    sleep 0.5
  end
  4.times do
    play 67
    play 70
    play 62
    sleep 0.25
  end
  4.times do
    play 69
    play 60
    play 64
    sleep 0.25
  end
  play 71
  play 62
  play 66
  sleep 0.5
  play 69
  play 60
  play 64
  sleep 0.5
  play 67
  play 70
  play 62
  sleep 0.5
end

or alternatively

use_synth :prophet
with_fx :ixi_techno do
  2.times do
    play chord(:b4, :minor7)
    sleep 0.5
    play chord(:a4, :minor7)
    sleep 0.5
    play chord(:g4, :minor7)
    sleep 0.5
  end
  4.times do
    play chord(:g4, :minor7)
    sleep 0.25
  end
  4.times do
    play chord(:a4, :minor7)
    sleep 0.25
  end
  play chord(:b4, :minor7)
  sleep 0.5
  play chord(:a4, :minor7)
  sleep 0.5
  play chord(:g4, :minor7)
  sleep 0.5
end

Remixing the Traditional: The "Alien" Dimension

One of the most exciting aspects for educators is the ability to "remix" the familiar. When you apply the :whammy effect to a nursery rhyme, you are doing more than just making a funny noise; you are exploring Digital Signal Processing (DSP).

Adding the :whammy effect or the :prophet synth transforms a 19th-century folk song into something that sounds like it was composed for a sci-fi film. For a student, this lowers the stakes. It’s no longer about "playing the song correctly"—it’s about experimenting with the extra dimension of sound design. If "Hot Cross Buns" sounds like an alien transmission, the student hasn't "failed" the music lesson; they’ve succeeded in a sound engineering challenge.

Why This Matters for STEM Educators

If you are an educator, Sonic Pi offers a unique bridge between disciplines:

  1. Iteration and Debugging: If the timing of a sleep command is off, the "logic" of the song breaks. Students learn to debug their rhythms.

  2. Mathematics of Harmony: Chords are essentially ratios. Exploring major vs. minor chords in code is a practical application of intervals and sequences.

  3. Synthesizer Physics: Choosing between :saw, :sine, or :square waves introduces the physics of sound waves and harmonics without needing a textbook.

Beyond the PC

While I ran these experiments on a PC for ease of use, the beauty of Sonic Pi is its portability. For makers, the ultimate goal is often to get the code off the screen and into the world. Imagine a Raspberry Pi-powered robot that changes its "theme music" based on its proximity to an object using an ultrasonic sensor. The code snippets above aren't just for speakers; they are triggers for a physical environment.

Final Thoughts for the "Unmusical" Maker

To my fellow makers: do not let the "Music" label stop you. You don't need to know how to play a piano to appreciate the logic of a sequence or the beauty of a well-placed live_loop. Sonic Pi is a playground for logic, and your "lack of ability" is actually an advantage—it means you’ll approach the sounds with the curiosity of a hacker rather than the rigidity of a traditionalist.

Go ahead, download it, grab a classic melody, and see if you can make it sound like an alien invasion. It’s the most fun you can have with a semicolon.

Remixing the Traditional: The "Alien" Dimension

One of the most exciting aspects for educators is the ability to "remix" the familiar. When you apply the :whammy effect to a nursery rhyme, you are doing more than just making a funny noise; you are exploring Digital Signal Processing (DSP).

Adding the :whammy effect or the :prophet synth transforms a 19th-century folk song into something that sounds like it was composed for a sci-fi film. For a student, this lowers the stakes. It’s no longer about "playing the song correctly"—it’s about experimenting with the extra dimension of sound design. If "Hot Cross Buns" sounds like an alien transmission, the student hasn't "failed" the music lesson; they’ve succeeded in a sound engineering challenge.

Why This Matters for STEM Educators

If you are an educator, Sonic Pi offers a unique bridge between disciplines:

  1. Iteration and Debugging: If the timing of a sleep command is off, the "logic" of the song breaks. Students learn to debug their rhythms.

  2. Mathematics of Harmony: Chords are essentially ratios. Exploring major vs. minor chords in code is a practical application of intervals and sequences.

  3. Synthesizer Physics: Choosing between :saw, :sine, or :square waves introduces the physics of sound waves and harmonics without needing a textbook.

Beyond the PC

While I ran these experiments on a PC for ease of use, the beauty of Sonic Pi is its portability. For makers, the ultimate goal is often to get the code off the screen and into the world. Imagine a Raspberry Pi-powered robot that changes its "theme music" based on its proximity to an object using an ultrasonic sensor. The code snippets above aren't just for speakers; they are triggers for a physical environment.

Final Thoughts for the "Unmusical" Maker

Do not let the "Music" label stop you. You don't need to know how to play a piano to appreciate the logic of a sequence or the beauty of a well-placed live_loop. Sonic Pi is a playground for logic, and your "lack of ability" is actually an advantage—it means you’ll approach the sounds with the curiosity of a hacker rather than the rigidity of a traditionalist.

Go ahead, download it, grab a classic melody, and see if you can make it sound like an alien invasion.


All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Comments

Popular posts from this blog

Programming Anki's Vector robot

With the sad news that Anki is shutting down ( https://www.vox.com/2019/4/29/18522966/anki-robot-cozmo-staff-layoffs-robotics-toys-boris-sofman ) I thought it was time I start playing with the SDK for the Vector robot. In this short post, I providing a quick overview of getting going with this with a simple program based on the tutorials Anki provide. Installation I am using a Mac (more details are available here  https://developer.anki.com/vector/docs/install-macos.html ) but there are instructions for Windows and Linux. - You need the Vector to have been set-up previous on a tablet, and an account set-up on the Anki Cloud. - Install Homebrew - available here  https://brew.sh/ - Using Homebrew to install Python3  brew install python3 -Now install the SDK  python3 - m pip install -- user anki_vector - Lst but not least configure the set-up  python3 - m anki_vector . configure - that is it. The configuration tool tells you where the serial...

Speech Recognition in Scratch 3 - turning Hello into Bonjour!

The Raspberry Pi Foundation recently released a programming activity Alien Language , with support Dale from Machine Learning for Kids , that is a brilliant use of Scratch 3 - Speech Recognition to control a sprite in an alien language. Do the activity, and it is very much worth doing, and it will make sense! I  would also recommend going to the  machinelearningforkids.co.uk   site anyway it is full of exciting things to do (for example loads of activities  https://machinelearningforkids.co.uk/#!/worksheets  ) . Scratch 3 has lots of extensions that are accessible through the Extension button in the Scratch 3 editor (see below) which add new fun new blocks to play with. The critical thing for this post is  Machine Learning for Kids  have created a Scratch 3 template with their own extensions for Scratch 3 within it  https://machinelearningforkids.co.uk/scratch3/ . One of which is a Speech to Text extension (see below). You must use this one ...

Robot Software

In the previous blog posts for this 'series' "It is a good time...."  Post 1  looked at the hardware unpinning some of this positive rise in robots; Post 2  looked at social robots; Post 3  looked at a collection of small robots; Post 4 looked at further examples of small robots Robots, such as the forthcoming Buddy and JIBO, will be based some established open sourceand other technologies. Jibo will be based around various technologies including Electron and JavaScript (for more details see:  http://blog.jibo.com/2015/07/29/jibo-making-development-readily-accessible-to-all-developers/ ). Buddy is expected to be developed around tools for Unity3d, Arduino and OpenCV, and support Python, C++, C#, Java and JavaScript (for more details see http://www.roboticstrends.com/article/customize_your_buddy_companion_robot_with_this_software_development_kit ).  This post contin ues with some of the software being used with the smaller robots.  A number ...