Partition a musical duration into rhythmic phrases.
- Determine the pool of durations to use in your rhythmic phrase:
128th to 128: .03125 .0625 .125 .25 .5 1 2 4 8 16 32 64 128
# durations = [ 2**x for x in range(-5, 8) ]
Triplets: .167 .333 .667 1.333 2.667
# durations = [ 2**x/3 for x in range(-1, 4) ]
Dotted: .375 .75 1.5 3 6
# durations = [ 2**x+2**x/2 for x in range(-2, 3) ]
Double dotted: .4375 .875 1.75 3.5 7
# durations = [ 2**x+2**x/2+2**x/4 for x in range(-2, 3) ]
where 1
is a quarter-note, 0.5
is an eighth, 2
is a half-note, etc.
- Import the class:
from random_rhythms import Rhythm
- Instantiate a random-rhythm object:
params = { # these are the defaults:
measure_size: 4, # in quarter-notes (or fractions thereof)
durations: [ 1/4, 1/2, 1/3, 1, 3/2, 2 ], # 1 = quarter-note
weights: [ 1, 1, 1, 1, 1, 1 ],
groups: {}, # number of notes, keyed by a duration
smallest: 1/128 # lower threshold for adding to the phrase
}
r = Rhythm(**params)
- Get a motif:
motif = r.motif()
print(motif)
from music21 import *
import random
from random_rhythms import Rhythm
r = Rhythm(measure_size=5 groups={1/3: 3, 1/2: 2})
motifs = [ r.motif() for _ in range(4) ]
sc = scale.WholeToneScale('C4')
s = stream.Stream()
s.append(meter.TimeSignature('5/4'))
for m in motifs:
for d in m:
p = random.choice(sc.pitches)
n = note.Note(p)
n.duration = duration.Duration(d)
s.append(n)
s.show() # or 'midi'