Raspberry Pi For Dummies, 4th Edition
Book image
Explore Book Buy On Amazon
You can use your Raspberry Pi to compose your own tunes or Sonic Pi can also use samples, which are snippets of music that you can manipulate, such as by changing their speed or adding effects to them. Sonic Pi includes a wide range of samples, and you can see a list of them by showing the Help pane and then using the Sample button in the bottom left. They're especially useful for adding drum loops to your music.

Here’s a favorite:

sample :loop_industrial

Put that into a new buffer and click Run to hear it. You can speed it up or slow it down by changing its rate. Here’s how you make it play at half its normal speed:

sample :loop_industrial, rate: 0.5

You can repeat that sample to make a continuous rhythm. Similar to composing your own music, you can use the sleep command to put a pause between each repetition. Samples can be different lengths, however, which can make it difficult to work out how long to sleep. Luckily, Sonic Pi provides a feature in the language to stretch a sample over a certain number of beats, which solves this problem. You can use it like this:

loop do

sample :loop_industrial, beat_stretch: 2

sleep 2

end

Now the sample is stretched over 2 beats, and there is a 2-beat pause between each repetition. As a result, you can hear a continuous rhythm. As you saw when experimenting with live loops, you can change the tempo with the use_bpm command.

About This Article

This article is from the book:

About the book authors:

Sean McManus is an expert technology and business author. His previous books include Mission Python, Coder Academy, and Cool Scratch Projects in Easy Steps. Mike Cook is a lifelong electronics buff, a former lecturer in physics at Manchester Metropolitan University, and the author of more than 300 articles on computing and electronics. You'll often find him monitoring technology forums under the moniker Grumpy Mike.

This article can be found in the category: