Tutorial

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Generating Beats - An infinite algorithmic drum machine Reload page to restart example
This example generates a 4 beat drum pattern, plays it, then generates another and so on. There are a bunch a generative music tricks in this example which will be explained below, as well it shows how SoundCipher's callback can be used to make the music updating process quite dynamic (every few beats) and also callbacks are used to syncronise drawing with significant drum beats. The program sets up an infinite loop to control the stopping you could add conditional statements before the play command.



The first line imports the SoundChiper library.

The second line sets up a SoundCipher score object to hold the drum pattern.

The third line creates a variable array, r, to hold values for the drawn shape.

The setup() method halts Processing's draw thread. Which allows us to control drawing with SoundCipher's scheduling using redraw(). The method then sets some score constants for tempo and registering the callback listener. Finally it passes control to the makeMusic() method.

The makeMusic() method does all the algorithmic work of constructing a 4 beat drum pattern. It fills the score with notes at appropriate times, as well it adds some callbacks that coincide with some kick and snare drums hits. The pattern generation balances regularity and variety by have some set kick and snare beats on specified beat subdivisions. The for-loop serves to create a 16 step sequence of 1/16th notes = 4 beats. The regular kick and snare events are calculated using modulo (% in Java) maths to add them at regular locations within each 8 or 16 steps. On all other steps the kick and snare may also be placed according to a random probability. Changing this probability will increase or decrease the number of additional kick and snare hits in the pattern. The hi hat part uses the same probability method to choose between closed and open hi hat notes. Additional variety is provided to the hi hats by randomly varying the dynamic parameter of hi hat notes. Finally, a callback at the end of the score is added which is used to trigger the recalculation and playback of the next drum pattern, then the score is started.

Callbacks are parsed by the handleCallbacks() method. Because there are a number of different callback IDs being used a switch-statement is employed to deal with callback IDs on a case by case basis. ID 0 indicates the end of a pattern and triggers the start of a new one; ID 1 indicates a regular kick drum hit and triggers a redraw() after specifying some shape size parameters using constrained randomness; ID 2 indicates a regular snare drum hit and triggers the drawing of a different shape.

The draw() method is called by redraw() and clears the graphics window before drawing a rectangle with the current parameters held in the float array called r.

The stop() method halts playback when the program is stopped. This is necessary especially to halt playback in Applets.