Advanced Topics 1
Sonic Pi comes with approximately 164 samples that you can freely use and play around with, but it also fully supports using external samples. For example, maybe you would like to record something (like your own voice or guitar) and have it in Sonic Pi to use in your song.
First, you’ll have to have some audio samples in WAV format placed in a folder on your hard drive. In the following examples, we’re using a free sample pack by Mehackit. It is called Solenoid Samples 1 and you can download it from here. It contains 14 one-shot and looping samples that were recorded from a kinetic sound installation we created in a workshop in early 2016.
Once you have downloaded the pack and extracted the files to a folder, you’ll have to check the full path to that folder. For example, if you extracted the files to a folder called ‘Samples’ on your Desktop the path is most likely following:
Just remember to use your own username instead of ‘sam’. The sample pack contains following files: hit_1.wav
to hit_7.wav
(percussive hits) and loop_1.wav
to loop_7.wav
(looping beats are recommended to be played with the sampler opt beat_stretch
).
Now you can play them directly with the sample
command by using the right file path:
If the path was set correctly for the sample
command you should now hear the sound loop_1.wav
. Just remember to use your own file path instead of the one shown in these examples! That is pretty straightforward way to access and play samples. However, you would probably like to write the folder path only once in the code and play the samples by only referring to their filenames. You can declare a variable for the file path and use that in conjunction with the sample
command. After sample
you can enter the variable name containing the file path and then the name of the sample. We’ll declare a variable for the sample folder file path called solenoids in the example below. When you run it, the sample loop_4.wav
should start playing and looping:
Now you’re able to use external samples and samples from Sonic Pi’s own library in your productions. Try playing the example below that uses four different instances of live_loop
to play external and Sonic Pi’s own samples. Please note that in the live_loop :solenoid2
we’re using a variable samplename
to randomly select one of the samples from hit_1.wav
to hit_7.wav
.