Using Patterns to generate Melodies


I’ve been using basic patterns for a while (Pwhite, Prand, Pxshuf, Pstutter, Pseq, Pseg, Pwrand probably being the most common objects) and I generally either end up composing very specific melodies eg.

\degree, Pseq([0, 2, 3, 5, 7], inf)

or something random which ends up sounding textural to my ears eg.

\degree, Pxrand([0,2,3,5,7], inf),

// or some kind of interesting rhythmic pattern 
\dur, Pseq([1, 1, 1, 2], inf)
\degree, Pseq([0, 3, 5], inf),

I think for my aural perception of what counts as a ‘melody,’ there needs to be a degree of repetition and variation that my current understanding of patterns in SC just doesn’t allow. Ideally I’d like to give some kind of information to an algorithm eg. a pitch set which then returns results that can be repeated or varied a few times. I’d also be interested in hearing any other strategies that people here are using to generate melodies in improvisation / algorithmic composition that creatively get around the ‘total randomness’ vs ‘totally determined’ binary.



You might be interested to check out my bachelor’s thesis:

Section 4 goes into how I approach algorithmic composition with traditional harmony, including a strategy for a melody generator. The idea is to start with a random arpeggiator on chord tones and add non-chord tone “gestures.” Enter in a large library of gestures and you have a pretty nice-sounding melody generator. It makes a good baseline where you can add on motivic development and rhythm.

1 Like

One technique I’ve been playing around with is using the Signal class to create values which can be sequenced, parameterized and manipulated in a lot of different ways. Here’s a simple example.

		\fold, Pwhite(1.0, 5.0).stutter(32),
		\sig, Pfunc({|evt|
			var fold = evt[\fold];
			(Signal.chebyFill(32, [0, 1, 0, 1, 0]) * fold).fold(-1, 1).linlin(-1, 1, 0, 7).round
		\index, Pseq( (0..31), inf ),
		\degree, Pbinop(\at, Pkey(\sig), Pkey(\index)),
		\octave, Pbjorklund(3, 8).linlin(0, 1, 5, 6),
		\stretch, 0.25
1 Like


currently I’m not generating melodic structures with SC but miSCellaneous_lib contains some classes that can be used for that purpose.

(1) PSrecur for recursive operations can be used to do developing variation (in wide interpretation of Brahms and Schoenberg): in this example it’s only applied to melodies, but of course this can be extended to groups of motifs, chords, rhythms etc.

a = (60..71);
b = (75..86);

// randomly exchange one pitch of array
p = PSrecur({ |x|
	x[0][(0..2).choose] = (a.asSet - x[0].asSet).choose; 
	// random start
}, 1, start: [a.scramble[0..2]]);

q = PSrecur({ |x|
	x[0][(0..4).choose] = (b.asSet - x[0].asSet).choose; 
}, 1, start: [b.scramble[0..4]]);

x = Pbind(
	\dur, 0.2,
	\midinote, Pseq([p.trace.flatten, q.trace.flatten], inf)


x = Pbind(
	\dur, 0.2,
	\midinote, Pseq([p.trace.flatten, (p + 12).trace.flatten], inf)


(2) The method ‘enum’ can be used to find melodies of same shape, which can be used for variations, an idea of Fabrice Mogini). See Ex.2 of the enum help file.

(3) Idev suite – from its tutorial file: “DIdev / PIdef / PLIdev search for numbers with integer distance from a source signal / pattern up to a given deviation. Repetitions within a lookback span are avoided, DIdev / PIdef / PLIdev randomly choose from possible solutions. Intended for search within integer grids (pitches, indices etc.), however applications with non-integer sources are possible, see examples.
The main musical idea of these classes is, that it’s often unwanted to have certain characteristics repeated within a perceptional time window. This might apply to melodic lines as well as to rhythmic patterns or sequences of timbre and can be continued in the domain of microsound. The principle can easily be understood with pitches, therefore most examples are of this kind.”

A rewrite of some old programs which I used for instrumental pieces back then, e.g. this one for accordion and string trio. The described principles were used with a mode on changing roots.

The algos were applied in an amotivic way, but this can be done differently with Idev classes (Idev could e.g. run over indices refering to motifs).


I feel like any one of these three replies would be enough to keep me busy for weeks! Thanks all!

@dkmayer I actually (finally) installed miSCellaneous_lib last night and started having a look. On some level I still have a compulsion to learn how to do things without Quarks/Extensions but I’m not sure if this is naivety or similar. In any case I’m keen to check everything out!

This is a great read. I’d also be interested in hearing recordings of performances with FORAY if there’s anything you’re comfortable sharing.