Learn creative coding writing simple programs

100. Sound and random rhythms

We are going to use the modulo operation to create rhythms. The idea was explained in episodes 72 and 73. At that time we used graphics to show rhythms, but now we know how to play sounds, so lets try make real rhythms! The program we create is something like a step sequencer. There are 64 steps that repeate over and over. In 4 of those steps we play a drum sound.

After creating a fixed rhythm, we modify the program to sometimes change the step in which sounds are played. We create an unfrequent event. When this event happens, we randomly choose one of the four sounds. The chosen sound will then be played at a new random step.

First we attempt giving a totally new random step (a number between 0 and 63). But that's not the best option when create rhythms. Instead of choosing a completely random number, it's better to have less options. We choose one of these numbers instead: 0, 8, 16, 24, 32, 40, 48, 56. We achieve this by writing 8 * int(random(8)). The result is more like a real rhythm.

This episode is a bit longer than usual, maybe a bit dense too, but since it's the last one this year, you will have enough time to digest it :)

Ubuntu users: by default you might be limited to playing just one audio sample. I explained how I fixed this issue at https://forum.processing.org/topic/why-can-t-i-play-more-than-one-audiosample

Thanks to http://www.freesound.org/people/stomachache/ and http://www.freesound.org/people/junggle/ for the audio samples.

Tags: sound, rhythm, sample, audio

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```