Skip to Content

How can we loop music or sound with code?

Think About

  • Why is it useful to repeat musical patterns?
  • How can you repeat musical patterns with code?

Get Started

Choose whether you want to program your music to loop with Scratch or Sonic Pi and then give it a try!

Loop Music With Scratch

  1. In the following Scratch project example, there are three different sprites.
  2. Each sprite will create random music when you click on them.
    1. BUT,  the microphone sprite uses a repeat to make a random beatbox.
  3. Try adding in different “Control” blocks (yellow blocks) and “Sounds” blocks (purple blocks) to create something different.

Click here for the Scratch Project Example.

This is what control blocks look like:


This is what sound blocks look like:


Wait, what do the blocks do?

If you’re not sure what a block does, click on the question mark on the top of the screen and then click on a block for an explanation of what the block does.

Can you meet these challenges?

  • Can you use loops to make a song that never ends?
  • Can you make a repeating song that doesn’t get boring?
  • Can you use loops to make a beat with drums, melody, and harmony?
  • Can you make a bunch of loops that sound almost the same but have little differences?

Loop Music With Sonic Pi

  1. Try out tutorial 5.2  in Sonic Pi to learn how to loop blocks of code to make music.
  2. Once you’ve tried out this tutorial, try looping blocks of code to create a repeating melody, harmony, or beat.

Need an example to repeat?

  • Use one of the examples in the “Help” section of Sonic Pi
  • OR
  • Follow these steps to code Hot Cross Buns
  • OR
  • Remix an example of music coded by Jared O’Leary

Share What You Learned

  • Post sample code from Sonic Pi
  • Describe what is looping
  • Explain what you learned about looping music or sound in Scratch or Sonic Pi.