-
Notifications
You must be signed in to change notification settings - Fork 1
/
oompah.py
313 lines (278 loc) · 8.29 KB
/
oompah.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
from __future__ import division
from itertools import izip
from string import Template
import random
import json
# make sure and use of random is controllably deterministic
random.seed(sum(ord(c) for c in "leeds"))
score_template = Template(open('score.template').read())
conductor_template = Template(open('conductor.template').read())
part_template = Template(open('part.template').read())
all_data = json.load(open('renamed_weekly.json'))
scales = [
['aes', 'bes', 'c', 'ees', 'f'],
['aes', 'bes', 'c', 'ees', 'f'],
['bes', 'c', 'ees', 'f', 'g'],
['bes', 'c', 'ees', 'f', 'g'],
['bes', 'c', 'd', 'f', 'g'],
['bes', 'c', 'd', 'f', 'g'],
['a', 'c', 'd', 'f', 'g'],
['a', 'c', 'd', 'f', 'g'],
['a', 'c', 'd', 'e', 'g'],
['a', 'c', 'd', 'e', 'g'],
['a', 'b', 'd', 'e', 'g'],
['a', 'b', 'd', 'e', 'g'],
['a', 'b', 'd', 'e', 'fis'],
['a', 'b', 'd', 'e', 'fis'],
['a', 'b', 'd', 'e', 'g'],
['a', 'b', 'd', 'e', 'g'],
['a', 'c', 'd', 'e', 'g'],
['a', 'c', 'd', 'e', 'g'],
['a', 'c', 'd', 'f', 'g'],
['a', 'c', 'd', 'f', 'g'],
['bes', 'c', 'd', 'f', 'g'],
['bes', 'c', 'd', 'f', 'g'],
['bes', 'c', 'ees', 'f', 'g'],
['bes', 'c', 'ees', 'f', 'g'],
['bes', ],
]
melodies = {}
# ordered set of dynamic markings, increasing in intensity
dynamics = [
'pp', # v.slow
'p', # slow
'mp', # medium
'mf', # fast
'f', # crazy
]
# ordered set of rhythm patterns, increasing in intensity
patterns = [
#v.slow
[
'r1'.split(),
'r2 2'.split(),
'2 r2'.split(),
'2 2'.split(),
'r4 2 r4'.split(),
],
#slow
[
'1'.split(),
'2 2'.split(),
'4 2 4'.split(),
'2 4 4'.split(),
'r2 4 4'.split(),
'2 r2'.split(),
'r2 2'.split(),
'4 r4 2'.split(),
'4 r4 4 r4'.split(),
'r4 4 2'.split(),
],
#medium
[
'4 4 2'.split(),
'4. 8 4. 8'.split(),
'2 8 8 4'.split(),
'4 8 8 2'.split(),
'8 8 4 8 8 4'.split(),
'4 r4 8 8 r4'.split(),
'r4 8 8 8 8 8 8'.split(),
'r4 8 8 r8 8 4'.split(),
'2 2'.split(),
'8 4. 8 4.'.split(),
'8 8 r4 8 8 r4'.split(),
'4. 8 4 r4'.split(),
],
#fast
[
'8. 16 8. 16 4. 8'.split(),
'4. 16 16 8 8 8 8'.split(),
'16 16 8 8 8 16 16 8 16 16 8'.split(),
'8 8 4 8 8 8 8'.split(),
'8 r8 r8 16 16 8 8 4'.split(),
'r8 16 16 8 8 8 8 8 8'.split(),
'8. 16 8. 16 8. 16 8. 16'.split(),
'4. 8 4 r4'.split(),
'16 16 16 16 r4 16 16 16 16 r4'.split(),
'8 8 8 8 8 8 8 8'.split(),
'r4 4 4 4'.split(),
'8 4 16 16 16 16 4 8'.split(),
'r2 8 16 16 4'.split(),
'8 8 16 16 8 r2'.split(),
],
#crazy
[
'4 8. 16 8 4 8'.split(),
'16 16 16 16 8 8 16 16 16 16 8 8'.split(),
'16 16 16 16 4 8 16 16 8 8'.split(),
'16 16 16 16 r4 16 16 16 16 r4'.split(),
'4. 16 16 8 4 8'.split(),
'8 8 16 16 16 16 8 8 16 16 16 16'.split(),
'16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16'.split(),
'8 8 8 8 8 8 8 8'.split(),
'r2 16 16 8 8 16 16'.split(),
'8 16 16 8 16 16 r2'.split(),
'r2 16 16 8 16 16 8'.split(),
'16 8 16 16 8 16 16 8 16 r4'.split(),
'8 4 8 16 16 r8 r4'.split(),
'r4 16 16 16 16 r4 16 16 16 16'.split(),
],
]
parts = {
'soprano_cornet': {
'instrument': 'Soprano Cornet',
'part': 'sop',
},
'solo_cornet': {
'instrument': 'Solo Cornet',
'part': 'cornetone',
},
'flugel_repiano': {
'instrument': 'Flugelhorn / Repiano',
'part': 'flugelhorn',
},
'cornets_2_3': {
'instrument': 'Cornets 2 & 3',
'part': 'cornettwo',
},
'tenor_horns': {
'instrument': 'Tenor Horns',
'part': 'tenorhorn',
},
'baritone': {
'instrument': 'Baritone Horns',
'part': 'baritone',
},
'trombones': {
'instrument': 'Trombones',
'part': 'trombone',
},
'euphonium': {
'instrument': 'Euphonium',
'part': 'euphonium',
},
'EEb_bass': {
'instrument': 'Eb Bass',
'part': 'tubaeflat',
},
'BBb_bass': {
'instrument': 'Bb Bass',
'part': 'tubabflat',
},
'bells': {
'instrument': 'Tubular Bells',
'part': 'tubularbells',
}
}
# intensity bucket thresholds
thresholds = [0] * len(patterns)
def threshold(data):
"""
Returns a numeric indicator or the threshold "position" to use with
patterns and dynamics.
"""
mean = sum(data) // len(data)
# pick intensity based on mean value for this hour based across thresholds
# for all data
for i, threshold in enumerate(thresholds):
if mean < threshold:
break
return i
def pitch(data, scale):
"""Generate notes from the scale driven by the data.
You can change the scale by using send()
"""
index = 0
for d in data:
index = (index + d) % len(scale)
new_scale = yield scale[index]
if new_scale is not None:
scale = new_scale
def rhythm(data, mn, mx):
"""Produce note durations from provided patterns based on mean intensity"""
i = threshold(data)
pattern = patterns[i]
# use the data to chose which pattern, so the process is deterministic
index = 0
for d in data:
index = (index + d) % len(pattern)
for duration in pattern[index]:
yield duration
yield None # indicates a bar has been produced
def voice(data, scale, mn, mx, length=8):
"""Generate a voice from the data, combining pitch and rhythm"""
bars = 0
note_iter = pitch(data, scale)
for duration in rhythm(data, mn, mx):
if duration is None:
bars += 1
yield '|'
if bars == length:
raise StopIteration()
elif duration[0] == 'r':
yield duration
else:
note = next(note_iter)
if '%s' in duration:
# more complex templated duration (e.g. tuplets)
yield duration % note
else:
yield "%s%s" % (note, duration)
everything = []
for place, hours in all_data.items():
for data in hours.values():
everything.extend(data)
everything.sort()
total = len(everything)
MIN = everything[0]
MAX = everything[-1]
bucket_size = len(everything) // len(patterns)
for i, _ in enumerate(patterns):
thresholds[i] = everything[(i + 1) * bucket_size]
print MIN, MAX, thresholds
for place, hours in all_data.items():
melodies[place] = "%% %s\n" % place
print place
scale_counter = 0
for hour, data in sorted(hours.items()):
intensity = threshold(data)
print " %s " % hour,
print "intensity %d " % intensity
scale = scales[scale_counter]
scale_counter += 1
v = list(voice(data, scale, MIN, MAX))
v[0] = v[0] + "\\%s" % dynamics[intensity]
melody = " ".join(v)
melodies[place] += '\\mark \\markup { "%s" }\n%s\n' % (
hour, melody.rstrip('|'))
melodies[place] += 'bes1 \\bar "|."'
# Bell part
chimes = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11]
melodies['bells'] = "%% bells\n"
for count, oclock in enumerate(chimes):
five_bars_rest = 'r1 r1 r1 r1 r1'
bong = "%s'4 " % random.choice(scales[count])
bongs = ''
for i in range(12):
if i < oclock:
bongs += bong
else:
bongs += 'r4 '
foo = bongs.split(' ')[::-1]
bar = ' '.join(foo)
baz = bar.replace('r4 r4 r4 r4', 'r1')
bongs = ' '.join(baz.split(' ')[::-1])
melody = bongs.strip() + five_bars_rest
melodies['bells'] += '\\mark \\markup { "%02d:00" }\n%s\n' % (
count, melody.rstrip('|'))
melodies['bells'] += 'bes1 \\bar "|."'
score = score_template.substitute(melodies)
with open('score.ly', 'wb') as output:
output.write(score)
with open('conductor.ly', 'wb') as output:
output.write(conductor_template.substitute({}))
for filename, data in parts.iteritems():
with open('%s.ly' % filename, 'wb') as output:
part = part_template.substitute(data)
output.write(part)