Here, we’re going to write our very first Sonic Pi program. This will cover the basics of coding with Sonic Pi, but if you haven’t used it before you can check out the introductory guide here.
The most basic code you can write with Sonic Pi is play followed by a number. The number represents the musical pitch, lower numbers make lower pitched beep and higher numbers make higher pitched beep sound.
play 75
The notes are based on the notes on a piano (it uses MIDI keyboard numbers). So, if you type play 60, you are telling the program to play the 60th note on the piano.
You can use the sleep option (followed by the duration) to add a break between notes. For example:
play 60
sleep 0.5
play 62
sleep 0.5
play 64
sleep 0.5
play 60
sleep 0.5
This combination will play everyone’s favourite tune, Frère Jacques.
But you don’t have to use the MIDI numbers if you know the notes. You can use notes in Sonic-Pi too.
play :C
sleep 0.5
play :D
sleep 0.5
play :E
sleep 0.5
play :C
sleep 0.5
If we want to play it through more than once , we wrap it in a loop specifying how many times to run the program using do
and end
.
2.times do
play 60
sleep 0.5
play 62
sleep 0.5
play 64
sleep 0.5
play 60
sleep 0.5
end
If we want to keep repeating a particular piece of music, use a forever loop:
loop do
play 60
sleep 0.5
end
You can specify the synth to use to change the sound.
use_synth :fm
2.times do
play 60
sleep 0.5
play 67
sleep 0.5
end
The synth options (like other code in Sonic Pi) will autocomplete so you can try out different ones at random or you can refer to the list of available synths from the help panel at the bottom of the screen.
And you can add samples. Select samples on the left hand side of the help window to browse through the samples and how they can be used.
2.times do
sample :loop_amen
sleep 1.753
end
Let’s add a backbeat (because everything is better with a backbeat).
To play more than one thing at a time, you can wrap your code together using threads.
# Frère Jacques again - this is a comment in
# the code, they always start with a # symbol
thread do
use_synth :beep
loop do
sample :drum_cymbal_closed
play 60
sleep 0.5
play 62
sleep 0.5
play 64
sleep 0.5
play 60
sleep 0.5
end
end
And what about if you are still creating and you want to change things as you go? You can use live_loops to create music on the fly. Change the sample and click Run again to hear the new version instantly.
You need to give your live loop a name, something descriptive so you can keep track of what it is. Ours is called breakbeat_backbeat.
live_loop :breakbeat_backbeat do
sample :loop_breakbeat
sleep 2
end
You can use lots of live_loops at a time to play different samples, beats and notes simultaneously.
There are lots of options and additional functionality to discover with Sonic Pi but this is enough to get you started coding your very own music.
Subscribe to this blog via RSS.
Question 1
Ingredients 21
Recipes 26
Getting started with JavaScript Blocks Editor (PXT) on Micro:Bit
Posted on 10 May 2017Arduino (2) Microcontrollers (2) Makeymakey (6) Scratch (8) Coding (6) Software (3) Sketches (1) Ide (1) Raspberrypi (11) Linux (2) 3dprinting (1) Manufacturing (1) Vr (1) Virtual_reality (1) Google (1) Circuits (3) Littlebits (4) Kits (2) Keyboard (2) Inputs (1) Cli (1) Command-line (1) Magazine (1) Make (2) Events (4) Makerfaire (1) Wires (2) Equipment (2) Breadboard (1) Electronics (3) Audio (1) Soundplant (2) Planning (2) Funding (1) Ozobot (1) Creative-computing (1) Music (3) Sonicpi (3) Cloudbit (2) Thimble (1) Images (1) Html (2) Webmakers (2) Internet (1) Ifttt (1) Sensors (2) Css (1) Learntocode (4) Paint (1) Conductivity (2) Picoboard (2) Activities (1) Workshops (3) Families (1) Microbit (4) Javascript (2) Paper (1) Electricity (1) Display (1) Browser (1) Remote (1) Headless (1) Signage (2) Digital signage (2) Privacy (1) Security (1) Configuration (1) Tools (1) Troubleshooting (1) Bugs (1) Problem-based learning (1) Hex (1) Programs (1) Onboarding (1)