Now let’s look at how we can spice up our samples even more!
You may remember from earlier that we were able to add options to our notes to affect the way they sound. We can do the same thing with samples but here we refer to them as parameters. They work exactly the same way as before, so let’s take a look at our old friends amp and pan. You call these in the exact same way we did earlier as you can see in the following:
sample :loop_amen, pan: -1, amp: 2
This code will play the amen loop, panned to the left side, and amplified by 2. These parameters can be added to any sample you can imagine to create a whole new sound!
What if the sample you want to use is too short? What if its too long? Sonic Pi provides a way to deal with that in the form of another parameter called rate. Rate is used in a similar way to pan and amp and can be seen in use below:
sample :loop_amen, rate: 2
This code will play your sample twice as fast and make the pitch go up. What do you think would happen if you gave the parameter a value of -2? Run the following code to find out:
sample :loop_amen, rate: -2
This code will then reverse the sample and play it at twice the speed Using a negative value will reverse the sample and play it at the given rate!
sample :loop_amen, rate: 1 will play at normal speed sample :loop_amen, rate: 2 will play at twice the speed sample :loop_amen, rate: 0.5 will play at half the speed sample :loop_amen, rate: -1 will play in reverse
The last thing to note is that using rate will cause your sample’s duration, or length, to be different. So if you are using sample_duration to space out your sounds you need to take that into account. The easiest way to do that is to use the formula
(1 / rate) * sample_duration
to find your sample’s new duration. The following code is an example of that:
sample :loop_amen, rate: 0.5 sleep (1 / 0.5) * (sample_duration :loop_amen) sample :loop_amen, rate: 2
It is very important that you remember to include the parenthesis, otherwise, the code will not run!
Just like with notes, samples can have their ADSR envelopes set as well! The only difference is that you can’t use an envelope to lengthen a sample. The sample either stops at the end of the envelope or at the end of the sample, whichever comes first.
The first major use of these envelopes could be to fade in or fade out. To fade in you simply change the attack value. To fade out you need to change the release value. There is no need to change the sustain value unless you want to explicitly state how long it stays at full volume. An example of fading in and out follows:
sample :loop_amen, attack: 0.5, release: 0.5
You can hear from that code that our sample now fades in and then fades out. You can use one or both of these values to achieve different effects. All of these parameters can help to completely change the sound of a sample!
We have already discussed one way to edit and chop up samples but Sonic Pi provides us with a second more powerful way to chop them up as well. We can actually declare a start and endpoint for our samples to just play parts of them.
The start can simply be declared by using the start parameter as you can see below:
sample :loop_amen, start: 0.5
This will cause the sample to start at the 0.5 second mark and play until the end.
You can also do the opposite and clip a sample so it ends before it’s supposed to, like this:
sample :loop_amen, finish: 1
Want to only play the middle of a sample? You can see an example of this below:
sample :loop_amen, start: 0.5, finish: 1
This will cause the sample to only play the section, or segment, between 0.5 and 1 seconds into it. This allows us to create new samples out of old samples.
Note: If your finish value is bigger than your start value, the segment will play in reverse.
Be aware that all of this can be combined with ADSR envelopes and other parameters to create really interesting effects!