New sounds

Up until now, we've been using the default sound in Sonic Pi, called beep, but there are many other sounds available.

Open up a couple of empty lines before the loop, type in use_synth, and then press the spacebar. Synth is short for synthesizer, which is a program or machine that generates sound. You will see a long list of sound names appear, with beep at the top. Choose one of the sounds by double-clicking on it or typing enough of its name so that it is the only option. Then press Enter. If you run your code now, you will hear the same notes, but the sounds will be different. Try a few different sounds to see which ones you like.

The use_synth function routine changes the sound of all notes until the next use_synth routine is used. It is possible to change the sound of notes as many times as you like within a song. We can even play a trick similar to the trick that we used when playing a random note. Try adding this line of code inside your loop:

  use_synth choose([:pretty_bell, :dsaw, :hollow])

Again, feel free to add or remove any elements from this list. You should now hear random notes being played in random styles.

We can also change the length of time for which each note plays. There are three main parts to this:

  • attack: This is the number of seconds taken to increase the volume to the maximum
  • sustain: This is the number of seconds spent at maximum volume
  • release: This is the number of seconds taken to decrease the volume to zero

The sum of all three of these times will give the total duration of our sound. The default attack and sustain values are zero; release is the most important value in most cases.

To use these features, add them at the end of the play command. You can use all three of them or none, or any number in between. Here's an example:

play 60, attack: 0.5, sustain: 0.1, release: 1

See how each option is separated with a comma and the option's name and its value are separated by a colon.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.5.57