Generative Music Patterns
by Jean-Michel Rolland

Generative Music Patterns is a project for the web residencies program by Akademie Schloss Solitude, Stuttgart & ZKM | Center for Art and Media, Karlsruhe on the topic FRACTAL HORSES, curated by Claudia Matť.
Generative music always obeys patterns defined by the coder. Under these patterns, lines of code use mathematical or random algorithms to make the music sound somehow melodious. During the month of July 2017, I will create as many different patterns as possible, using mathematical formulas to develop algorithms that will make the music sound different. In all cases, the machine will be the composer, but it will have to create music within the limits I have established. Visualizations will evolve constantly with the patterns created. The work in progress will be updated as often as possible on this website with my creative process explained and will be available as a free app for Android on Google Play.

#1  #2  #3  #4  #5 



Generative Music Patterns #1
Click to play and stop


Iím launching an experiment in which I will study different patterns to create generative music with the simplest features possible. Iím working with Processing, free software that enables artists to create generative art with a simplified Java language. The code will be adapted so it can be embedded into a web page.

I first create 60 mp3 piano samples that go from C0 (32.70 Hertz) to B4 (987.77 Hertz) that will be preloaded by the browser to be ready for playback when theyíre called, thanks to this line, repeated 60 times:
<audio id="1" src="gmpfiles/note1.mp3" preload="auto"></audio>
<audio id="2" src="gmpfiles/note2.mp3" preload="auto"></audio> etc.
Then I use a JavaScript to create ten audio channels to enable the browser to play the notes smoothly.
Note that Google Chrome, Safari and Mozilla Firefox work well, but Internet Explorer has difficulties playing the sounds.

Then comes the Processing code itself. The first thing to do is to declare the variables, then give a few first orders gathered in the void setup().
These orders will be executed only once when the page loads. For example, they set the size and the background color of the animation.

Then comes the part that will be executed 60 times per second, the group of orders gathered in the void draw().

In this very first work, the notes will be chosen completely randomly. Hereís the line of code that determines which note will be played every 500 milliseconds:
note = int(random(1, 61));

I also made the visualization as simple as possible; a white line that starts from the position of the previous note and ends at the position of the current note:
line(prevPointx, prevPointy, pointx, pointy);

And last, I included another group of orders, void mouseReleased(), to enable the user to start and stop the animation with a mouse click.





-------------------------------------------------



Generative Music Patterns #2
Click to play and stop


In this second version, I decided to use a sin() function to determine which notes to play, in a circular motion.
The sin() function returns the sine of a number. As the sine will always be a number between -1 and 1, I scale it between 1 and 60 (the number of possible notes) with the map() function:
note = int(map(sin(note*TWO_PI/60), -1, 1, 1, 60));
To fully understand how sine works, please check this page: http://bit.ly/sine_cosine
If a G0 (eighth note) or a D#4 (52nd note) is played, we enter a two-note loop because:
int(map(sin(8*TWO_PI/60), -1, 1, 1, 60)) = 52 and int(map(sin(52*TWO_PI/60), -1, 1, 1, 60)) = 8.
If an F2 is played (30th note), we enter a one-note loop because:
int(map(sin(30*TWO_PI/60), -1, 1, 1, 60)) = 30!
But most of the time, we enter a looping pattern of ten notes: G#1, F4, C1, A#4, D2, D3, F0, A#3, C0 and G#2.



-------------------------------------------------



Generative Music Patterns #3
Click to play and stop


In this third version, which is very similar to V2, I exchange the sin() function for a cos() function. The notes are chosen depending on the cosine of the previous note :
note = int(map(cos(note*TWO_PI/60), -1, 1, 1, 60));
I donít like the result, because too many times, int(map(cos(note*TWO_PI/60), -1, 1, 1, 60)) = note, which leads to repeat the same note forever... So Iím deciding to change the algorithm this way :
note = int(map(cos(note), -1, 1, 1, 60));
Itís a simplification because I no longer scale the previous note to the circle, but the result is better because it leads to three new patterns, each one consisting of four notes : D#3, A4, A2 and E0; A3, E1, A1 and C0; F#0, D#4, C2 and A#4.
When notes 45 (G#3) or 57 (G#4) are triggered, we enter both times in a one note-loop because int(map(cos(45), -1, 1, 1, 60)) = 45 and int(map(cos(57), -1, 1, 1, 60)) = 57.



-------------------------------------------------



Generative Music Patterns #4


I now want to use a Perlin noise to generate the notes. This algorithm was created to generate complex visuals but I use it here for sound design. It looks random, but it isnít.
On Processing website, we can read : ďPerlin noise is a random sequence generator producing a more natural, harmonic succession of numbers than that of the standard random() function. It was developed by Ken Perlin in the 1980s and has been used in graphical applications to generate procedural textures, shapes, terrains, and other seemingly organic forms.Ē
But I now have to explain why there isnít any animation here but a video, screen capture of my computer. To embed Processing sketches on a website, I use Processing.js, which is powerful and quite easy to handle, but it alas manages the Perlin noise quite badly.
Hereís the new line of code that determines which note is to be played :
note = round(noise(prevNote)*60);
The function noise() returns values between 0 and 1, so I have to multiply them by 60 to cover my 60 notes. I create a new variable that I call prevNote to be able to compare the new note to the previous one :
if (note==prevNote) noiseSeed(int(random(10000)));
That means that if thereís a note repetition, it wonít occur more than once because I change the parameters of the Perlin noise with the function noiseSeed(). I also use this function each time the drawing goes back to the starting point at the left to generate a new pattern.



-------------------------------------------------



Generative Music Patterns #5
Click to play and stop - drag your mouse to change pattern


In this fifth episode, I work with the sin() function again (see episode two for more explanations on this function), but this time, I add some interactivity.
The line of code was previously :
note = int(map(sin(note*TWO_PI/60), -1, 1, 1, 60));
and now becomes :
int divider = int(1+mouseX/10.0);
note = int(map(sin(note*TWO_PI/divider), -1, 1, 1, 60));

In fact, I create a new variable that I call divider. This variable changes according to the horizontal position of your mouse and is displayed at the window's left bottom corner. It enables you to change the position of the notes on the imaginary circle, which calculates their sine. Thus, each time you move your mouse, youíre changing the algorithm and melody patterns. This actually enables you to create your own patterns.



-------------------------------------------------