How to Make Music with Code Using Sonic Pi
Learn how to create music through coding with Sonic Pi, a fun programming tool perfect for families to explore together.
- Getting Started with Sonic Pi. First, download Sonic Pi for free from sonic-pi.net - it works on Windows, Mac, and Raspberry Pi computers. Once installed, you'll see a simple interface with a large text area where you'll write your code, and some buttons at the top. The most important button is the big 'Run' button that will play your music. Start by typing 'play 60' in the text area and clicking Run - you'll hear a single musical note. That number 60 represents middle C on a piano, and you can try different numbers to hear different pitches.
- Creating Your First Simple Song. To make actual music, you'll want to play multiple notes in sequence. Type each 'play' command on a new line, like 'play 60', then 'play 64', then 'play 67'. When you run this, the notes will play so quickly they sound like a chord. To make them play one after another, add 'sleep 1' between each note - this tells the computer to wait one beat before playing the next note. You can change the sleep number to make notes longer or shorter. Try different note numbers between 50 and 80 to create your own melodies.
- Adding Rhythm and Drums. Sonic Pi has built-in drum sounds that make your music more exciting. Instead of 'play', use 'sample' followed by a drum name in quotes, like 'sample :bd_haus' for a bass drum or 'sample :drum_cymbal_closed' for a cymbal. You can layer drums with your melody by putting drum commands between your melody notes. Try creating a simple pattern like bass drum, sleep, cymbal, sleep, and repeat this pattern while your melody plays above it.
- Using Loops to Repeat Patterns. Instead of typing the same notes over and over, Sonic Pi lets you create loops. Wrap your music in a loop by typing 'live_loop :my_song do' before your notes and 'end' after them. Everything between 'do' and 'end' will repeat forever until you stop it. You can create multiple loops with different names - maybe one called ':melody' for your tune and another called ':drums' for your rhythm section. Each loop will play at the same time, creating layered music.
- Changing Sounds and Effects. Make your music more interesting by changing the instrument sounds. Add 'use_synth :piano' before your play commands to sound like a piano, or try ':saw', ':beep', or ':tb303' for electronic sounds. You can also add effects by wrapping your notes in effect commands like 'with_fx :reverb do' and 'end' to make your music sound like it's in a big hall. Other fun effects include ':echo', ':distortion', and ':wobble'. Experiment with different combinations to find sounds your family enjoys.
- Saving and Sharing Your Music. Sonic Pi automatically saves your code, but you can also save specific projects by clicking the 'Save' button and giving your song a name. To share your music with others, you can record it by clicking the 'Rec' button before playing your song, then 'Rec' again to stop recording. This creates an audio file you can play on any device. You can also share your code with other Sonic Pi users so they can run your program and hear exactly what you created.