﻿ patterns « Coding, Sounds and Colors | A blog about algorithmic experiments in music and visual art. Sort of.

## The Molecular Music Box in SuperCollider

posted by on 2014.08.19, under Supercollider
19:

Via Reaktor tutorials I came across this video. I have already talked about generative systems that create rich patterns (see here, and here), and how simple rules can give rise to emergent complexity.
Watch the video to know what the simple rules are in this case (and to see why it is called “molecular” ), or look at the following SuperCollider code, which, I must say, took me a bit more than I thought

MIDIClient.init;

~mOut = MIDIOut.new(3);

(
var seed = 48;
var degrees, deg;
var length = Pseq([4, 3], inf).asStream;
var dur = length.next();
var bars = 25;
var quant = 16;
var notes = [];
var loop = [];
var pos = [];
var next = 0;

degrees = [];

//Building the MIDI values for the white keys

9.do({|i|
degrees = degrees++([0, 2, 4, 5, 7, 9, 11] + (12*i));
});

//Starting notes from the seed

deg = Pseq(degrees, inf, degrees.indexOf(seed)).asStream;

(bars * quant).do({|i|
var note;

if((i%quant == 0) && (notes != []),
{
notes = [];
});

if((i%quant == next) && (pos.includes(next) == false),{
next = (next + dur)%quant;
});

if ( (i%quant == next) && (pos.includes(next) == true),{
dur = length.next();
next = (next + dur)%quant;
});

});

loop.do({|patt, i|
patt.postln;
patterns = patterns++([i * 4, Pbind(*[\type,\midi,\chan,0,
\midiout,~mOut,
[\midinote, \dur]: Pseq(patt, inf),
\legato: 1,
\amp: rrand(0.1,0.5)])]);
});

Ptpar(patterns, 1).trace.play;
)

Notice that you can very easily change any of the rules (duration length, scale used, etc.) with a few keyboard strokes: the power of a text based programming language!
I have sent the output of this to the Grand Piano instrument in Ableton Live 9.
Here is the result for 4C3