/
study.ix.py
357 lines (264 loc) · 11.7 KB
/
study.ix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from pippi import dsp, tune
from hcj import fx, keys, snds, drums, Sampler
import ctl
dloop2 = dsp.read('samples/jess/loop2.wav').data
dloop1 = dsp.read('samples/jess/loop1.wav').data
dloop1 = dsp.fill(dloop1, dsp.flen(dloop2))
kicksoft = dsp.read('samples/jess/kickshuffle.wav').data
kickhard = dsp.read('samples/jess/kickcym.wav').data
rimshot = dsp.read('samples/jess/rimshot.wav').data
rimshot = dsp.amp(rimshot, 4)
snare = dsp.read('samples/jess/snare.wav').data
snare = dsp.amp(snare, 3)
snare2 = snds.load('hits/hisnarespa.wav')
snare2 = dsp.amp(snare2, 0.25)
clap = snds.load('hits/tapeclap.wav')
clap = dsp.amp(clap, 0.25)
flam = dsp.read('samples/jess/snareflam.wav').data
flam = dsp.amp(flam, 3)
smash = dsp.read('samples/jess/smash.wav').data
skitter = dsp.read('samples/jess/skitter.wav').data
paper = snds.load('hits/papersnap.wav')
sock = snds.load('hits/detroitkick1.wav')
hat = snds.load('hits/keyshihat.wav')
section_choices = {
'intro': (['intro'] * 4) + (['stasis'] * 1),
'buildup': ['verse'] * 2 + ['buildup'],
'stasis': ['buildup'] + (['stasis'] * 2),
'verse': ['verse'] * 2 + ['chorus'],
'chorus': ['chorus'] * 2 + ['bridge', 'intro', 'float', 'verse'],
'bridge': (['bridge'] * 10) + ['chorus', 'stasis', 'float', 'intro'],
'float': ['afterfloat'],
'afterfloat': ['chorus', 'verse', 'bridge'],
}
section_defs = {
'intro': ['prog0', 'hats'],
'buildup': ['prog1', 'choppy', 'hats', 'rims', 'kicks', 'smashes'],
'stasis': ['prog1', 'choppy', 'hats', 'kicks'],
'verse': ['prog1', 'snares', 'choppy', 'swells', 'hats', 'rims', 'kicks', 'bloops'],
'chorus': ['prog2', 'rushes', 'rims', 'smashes', 'arps', 'bloops'],
'bridge': ['prog3', 'snares', 'rushes', 'choppy', 'hats', 'rims', 'kicks', 'smashes', 'arps', 'bloops'],
'float': ['prog2', 'arps', 'bloops', 'swells', 'hats'],
'afterfloat': ['prog2', 'arps', 'bloops', 'swells', 'hats', 'rims', 'choppy'],
'ending': ['prog0', 'swells'],
}
def makeSwells(cname, numswells, length, key='e', octave=1):
swells = []
freqs = tune.chord(cname, key, octave)
root = tune.ntf(key, octave)
# Naive pitch wrapping
for i, freq in enumerate(freqs):
if freq > root * 2.5:
freqs[i] = freq * 0.5
for _ in range(numswells):
slength = dsp.randint((length / numswells) * 0.85, length / numswells)
swell = [ keys.pulsar(freq, slength, drift=dsp.rand(0.001, 0.01), speed=dsp.rand(0.1, 0.5), amp=dsp.rand(0.2, 0.5)) for freq in freqs ]
swell = dsp.mix(swell)
swell = dsp.env(swell, 'hann')
swell = dsp.taper(swell, 30)
swells += [ swell ]
swells = [ dsp.fill(swell, length / numswells, silence=True) for swell in swells ]
return ''.join(swells)
def makeArps(length, beat, cname):
freqs = tune.chord(cname, key, dsp.randint(1,4))
tone = Sampler(snds.load('tones/nick.wav'), direction='fw-bw-loop', tails=False)
freqs = dsp.randshuffle(freqs)
beat = int((beat * 2) / dsp.randchoose([0.5, 1, 2, 3, 1.5]))
numbeats = length / beat
out = ''
for i in range(numbeats):
out += tone.play(freqs[i % len(freqs)], beat)
out = dsp.fill(out, length)
out = fx.penv(out)
return out
def makeRhodes(length, beat, freqs, maxbend=0.05):
backup = Sampler(snds.load('tones/nycrhodes01.wav'), tune.ntf('c'), direction='fw-bw-loop', tails=False)
chord = [ keys.rhodes(length, freq, dsp.rand(0.4, 0.7)) for freq in freqs ]
chord = dsp.randshuffle(chord)
chord = [ dsp.mix([ dsp.env(fx.penv(backup.play(freq * 2**dsp.randint(0,2), length, dsp.rand(0.4, 0.6))), 'line'), c ]) for freq, c in zip(freqs, chord) ]
pause = 0
for i, c in enumerate(chord):
pause = pause + (dsp.randint(1, 4) * beat)
c = dsp.pan(c, dsp.rand())
c = fx.bend(c, [ dsp.rand() for _ in range(dsp.randint(5, 10)) ], dsp.rand(0, maxbend))
chord[i] = dsp.pad(dsp.fill(c, length - pause), pause, 0)
return dsp.mix(chord)
def makeHat(length, i):
h = dsp.env(dsp.fill(hat, dsp.flen(hat) / 3), 'phasor')
h = dsp.amp(h, 0.3)
h = dsp.mix([ h, dsp.pad(dsp.transpose(h, dsp.rand(1.51, 2.03)), dsp.randint(1, 44), 0) ])
return dsp.fill(h, length, silence=True)
def makeSnare(length, i):
s = dsp.mix([ dsp.randchoose([snare, snare2, clap]) for _ in range(3) ])
s = dsp.fill(s, dsp.randint(dsp.flen(s)/2, dsp.flen(s)))
s = dsp.env(s, 'phasor')
s = dsp.amp(s, dsp.rand(0.9,1))
return dsp.fill(s, length, silence=True)
def makeRimshot(length, i):
return dsp.fill(rimshot, length, silence=True)
def makeKick(length, i):
k = dsp.randchoose([ kickhard, kicksoft ])
k = dsp.mix([ k, dsp.env(sock, 'phasor') ])
if dsp.rand() > 0.4:
k = dsp.env(k, 'phasor')
return dsp.fill(k, length, silence=True)
def makeBloop(length, i, bfreqs):
bfreq = dsp.randchoose(bfreqs)
if dsp.rand() > 0.5:
blength = length if length <= dsp.mstf(80) else dsp.mstf(dsp.rand(40, 80))
else:
blength = length
wf = dsp.breakpoint([0] + [ dsp.rand(-1,1) for _ in range(dsp.randint(8, 20)) ] + [0], 512)
bloop = dsp.mix([ keys.pulsar(bf, blength, wf=wf, amp=dsp.rand(0.1, 0.5)) for bf in [ bfreq, bfreq / 2, bfreq * 2 ] ])
bloop = dsp.env(bloop, 'phasor')
bloop = dsp.fill(bloop, length, silence=True)
return bloop
def makePaper(out):
tail = dsp.cut(paper, dsp.mstf(60), dsp.flen(out) - dsp.mstf(60))
tail = [ dsp.transpose(tail, dsp.rand(0.95, 1.05)) for _ in range(dsp.randint(3,6)) ]
tail = [ fx.penv(t) for t in tail ]
out = dsp.mix(tail + [ out ])
return out
def forceChange(section, count, numsections, ending_length):
last_section = section
section = nextSection(section, count, numsections, ending_length)
if last_section == section and section != 'ending':
section = forceChange(section, count, numsections, ending_length)
return section
def nextSection(section, count, numsections, ending_length=4):
if count >= numsections - ending_length:
return 'ending'
else:
return dsp.randchoose(section_choices[section])
def canPlay(inst, section):
return inst in section_defs[section]
out = ''
elapsed = 0
count = 1
section = 'intro'
reps = 0
numsections = dsp.randint(150, 200)
ending_length = dsp.randint(1, 3)
key = dsp.randchoose('abcdefg')
for section_index in range(numsections):
last_section = section
if reps > 2:
section = forceChange(section, count, numsections, ending_length)
if last_section == section:
reps += 1
else:
reps = 0
print 'Rendering section %s... %s' % (count, section)
layers = []
numbeats = 16
# Look ma, harmonic motion!
progression = ('i i v IV IV6 ' * 4) + 'v9 v9 ii7 vi9 IV69 ' + ('i i v IV IV6 ' * 2) + 'v9 v9 ii7 ii7 vi9 IV69 IV69'
progression = progression.split(' ')
if canPlay('prog0', section):
progression = 'i II7 IV6'.split(' ')
if canPlay('prog1', section):
progression = 'i i v IV IV6'.split(' ')
if canPlay('prog2', section):
progression = 'v9 v9 ii7 vi9 IV69'.split(' ')
numbeats = 16 + (4 * dsp.randint(2, 4))
if canPlay('prog3', section):
progression = 'II7 II7 V69'.split(' ')
cname = progression[ count % len(progression) ]
if section == 'float':
numbeats = 16 * dsp.randint(2, 20)
if section == 'ending':
numbeats = dsp.randint(16, 40)
bpm = 84
beat = dsp.bpm2frames(bpm) / 4
bar_length = beat * numbeats
bar = dsp.pad('', 0, bar_length)
if canPlay('choppy', section):
dl = ''.join([ dsp.mix([ dsp.randchoose([ dloop1, dloop2 ]), dsp.randchoose([ kickhard, kicksoft ]) ]) for _ in range(3) ])
dl = dsp.split(dl, dsp.flen(dl) / 16)
dl = dsp.randshuffle(dl)
dl = [ dsp.env(dd, 'phasor') for dd in dl ]
for i, b in enumerate(dl):
if dsp.rand() > 0.75:
dl[i] = dsp.pad('', 0, dsp.flen(b))
elif dsp.rand() > 0.75:
dl[i] = dsp.split(b, dsp.flen(b) / 2)[0] * 2
dl[i] = dsp.amp(dl[i], dsp.rand(0.9, 2))
dl[i] = dsp.fill(dl[i], beat, silence=True)
dl = ''.join(dl)
layers += [ dl ]
if canPlay('swells', section):
octave = 1 if section == 'float' else dsp.randint(2, 3)
sdivs = [ dsp.randint(2, 8) for _ in range(dsp.randint(2, 4)) ]
swells = dsp.mix([ makeSwells(cname, nswell, bar_length, key, octave) for nswell in sdivs ])
if section not in ('float', 'ending'):
swells = fx.penv(swells)
swells = dsp.env(swells, 'random')
layers += [ swells ]
if canPlay('rushes', section):
rushes = [ drums.roll(dsp.randchoose([paper, smash, snare, rimshot, kickhard]), bar_length, dsp.randint(1, 3)) for _ in range(3) ]
layers += [ dsp.amp(dsp.mix(rushes), 0.5)]
if canPlay('snares', section):
snarep = '....x...'
if elapsed > dsp.stf(90) and dsp.rand() > 0.75:
snarep = '....xx..'
if elapsed > dsp.stf(120) and dsp.rand() > 0.75:
snarep = '..x..x..x'
pattern = ctl.parseBeat(snarep)
snares = ctl.makeBeat(pattern, [ beat for _ in range(numbeats) ], makeSnare)
snares = dsp.amp(snares, dsp.rand(2,4))
snares = dsp.fill(snares, bar_length)
layers += [ snares ]
if canPlay('hats', section):
hatp = 'x.x.'
pattern = ctl.parseBeat(hatp)
hats = ctl.makeBeat(pattern, [ beat for _ in range(numbeats) ], makeHat)
if section == 'float':
hats = fx.penv(hats)
layers += [ hats ]
if canPlay('rims', section):
rimshotp = '....x...'
pattern = ctl.parseBeat(rimshotp)
rimshots = ctl.makeBeat(pattern, [ beat for _ in range(numbeats) ], makeRimshot)
if dsp.rand() > 0.75:
rimshots = dsp.amp(drums.roll(rimshot, dsp.flen(bar), dsp.randint(2, 6)), dsp.rand(0.4, 1))
layers += [ rimshots ]
if canPlay('kicks', section):
kickp = 'x.......'
pattern = ctl.parseBeat(kickp)
kicks = ctl.makeBeat(pattern, [ beat for _ in range(numbeats) ], makeKick)
kicks = dsp.amp(kicks, dsp.rand(2,4))
layers += [ kicks ]
if canPlay('smashes', section):
sm = dsp.fill(smash, dsp.flen(bar))
sm = dsp.mix([ dsp.alias(sm), fx.penv(sm) ])
sm = dsp.amp(sm, dsp.rand(0.5, 2))
sk = dsp.fill(skitter, dsp.flen(bar))
sk = fx.penv(sk)
layers += [ sm, sk ]
if count % 4 == 0:
layers += [ dsp.pad(flam, dsp.flen(bar) - dsp.flen(flam), 0) ]
if canPlay('bloops', section):
bfreqs = tune.chord(cname, key, 2)
bloopp = 'x.x.--------' if dsp.rand() > 0.5 else 'xxx.--------'
bloopp = bloopp if dsp.rand() > 0.65 else 'xxx.----'
pattern = ctl.parseBeat(bloopp)
bloops = ctl.makeBeat(pattern, [ beat / 2 for _ in range(16) ], makeBloop, bfreqs)
layers += [ bloops ]
if canPlay('arps', section):
arpses = dsp.mix([ makeArps(dsp.flen(bar), beat, cname) for _ in range(dsp.randint(2,4)) ])
layers += [ arpses ]
if section not in ('intro', 'ending'):
rfreqs = tune.chord(cname, key, 2)
maxbend = 0.005 if dsp.rand() > 0.3 else 0.05
rhodeses = makeRhodes(dsp.flen(bar), beat, rfreqs, maxbend)
layers += [ rhodeses ]
papers = dsp.fill(dsp.amp(makePaper(paper), dsp.rand(2, 4)), dsp.flen(bar), silence=True)
if section == 'intro' and dsp.rand() > 0.5:
papers = fx.bend(papers, [ dsp.rand() for _ in range(dsp.randint(5, 10)) ], dsp.rand(0, 0.5))
layers += [ papers ]
bar = dsp.mix(layers)
out += bar
elapsed += dsp.flen(bar)
count += 1
section = nextSection(section, count, numsections, ending_length)
dsp.write(out, '03-the_green_green_horse')