-
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
import bisect
import math
import struct
import sys
# Number of times to sample each second
bitrate = 44100
intro = [
(1,3,3),
(1,3,7),
(1,3,10),
(6,4,2),
(1,3,1),
(1,3,5),
(1,3,8),
(3,4,0),
(1,2,11),
(1,3,3),
(1,3,6),
(3,3,10),
(1,2,8),
(1,3,0),
(1,3,3),
(8,3,7),
]
melody = [
(1,3,3),
(1,3,7),
(1,3,10),
(1,4,2),
(1,4,3),
(1,4,7),
(2,4,8),
(1,3,1),
(1,3,5),
(1,3,8),
(1,4,0),
(1,4,1),
(1,4,5),
(2,4,8),
(1,3,3),
(1,3,7),
(1,3,10),
(1,4,2),
(1,4,3),
(1,4,10),
(2,4,3),
(1,3,1),
(1,3,5),
(1,3,8),
(1,4,0),
(1,4,10),
(1,4,8),
(2,4,10),
(1,3,3),
(1,3,7),
(1,3,10),
(1,4,2),
(1,4,3),
(1,4,7),
(2,4,8),
(1,3,1),
(1,3,5),
(1,3,8),
(1,4,0),
(1,4,1),
(1,4,5),
(2,4,1),
(1,3,3),
(1,3,7),
(1,3,10),
(1,4,2),
(1,4,3),
(1,4,10),
(1,4,8),
(1,4,7),
(1,3,1),
(1,3,5),
(1,3,8),
(1,4,0),
(1,4,10),
(1,4,8),
(2,4,10),
]
bass = [
(1,1,3),
(1,1,10),
(1,1,1),
(1,1,8),
(1,1,3),
(1,2,3),
(1,1,1),
(1,1,10),
]
melody2 = [
(1,0,0),
(1,5,10),
(1,5,8),
(1,5,7),
(1,5,8),
(3,5,7,2),
(1,5,3),
(1,4,10),
(6,5,1,2),
(1/2,5,0,2),
(1/2,5,1,2),
(3,5,3,2),
(1/2,5,10,2),
(7/2,5,3,2),
(8,0,0),
(1,0,0),
(1,5,3),
(1,5,10),
(1,5,10),
(4/3,5,10),
(4/3,5,8),
(4/3,5,7),
(1,0,0),
(1,5,1),
(1,5,8),
(1,5,8),
(4/3,5,8),
(4/3,5,8),
(4/3,5,10),
(8,0,0),
(1,0,0),
(5,5,3,2),
(2,5,10,2),
]
melody3 = [
(1,0,0),
(1,5,10),
(1/2,5,8,2/3),
(1/2,5,7,2/3),
(1/4,5,8,1/2),
(1/4,5,7,1/2),
(1/4,5,8,1/2),
(1/4,5,7,1/2),
(1,5,8),
(3,5,7,2),
(1,5,3),
(1,4,10),
(1,5,1),
(5,5,7,2),
(1/2,5,7),
(1/2,5,10),
(1/4,5,7),
(1/4,5,10),
(1/4,5,7),
(1/4,5,10),
(1,6,3),
(2,5,3,2),
(1/2,6,3),
(5/2,5,3,2),
(1/2,5,10),
(1/2,5,8),
(1/2,5,7),
(1/2,5,8),
(1/2,5,7),
(1/2,5,3),
(1/2,4,10),
(1/2,5,1),
(1/2,5,0),
(1/2,4,10),
(1/2,4,8),
(1/2,4,10),
(1/2,5,3),
(1/2,5,7),
(1/2,5,3),
(1/2,5,10),
(4/3,5,7),
(4/3,6,3),
(4/3,6,3),
(4/3,6,2),
(4/3,5,10),
(4/3,5,7),
(3,5,5),
(2,5,7),
(2,5,8),
(1,6,1),
(1,5,3),
(1,5,5),
(2,5,7),
(1,5,3),
(1,5,8),
(2,5,10),
(3/2,6,0),
(3/2,6,1),
(5,6,3,2),
]
outro = [
(1,3,3),
(1,3,7),
(1,3,10),
(1,4,2),
(1,4,3),
(1,4,7),
(2,4,8),
(1,3,1),
(1,3,5),
(1,3,8),
(1,4,0),
(1,4,1),
(1,4,5),
(2,4,8),
(1,2,11),
(1,3,3),
(1,3,6),
(1,3,10),
(1.5,3,11),
(1.5,4,3),
(3,4,8),
(1.5,2,8),
(1.5,3,0),
(2,3,3),
(16,3,7,2),
]
def process(notes, start, speed=1, gain=1):
"""
Adds a list of notes to the music list
"""
t = start
for note in notes:
vol = 1
if len(note) == 4:
vol = note[3]
start = min(t, t + note[0] / speed)
end = max(t, t + note[0] / speed)
music.append((start, end, note[1], note[2], vol * gain))
t = end
# Process all lists of notes
music = []
process(intro, 0, 4)
process(melody, 8, 4)
process(melody, 24, 4)
process(bass, 24, gain=1.5)
process(bass, 32, gain=1.5)
process(melody, 40, 4)
process(melody2, 40, 4)
process(bass, 40, gain=1.5)
process(bass, 48, gain=1.5)
process(melody, 56, 4)
process(melody3, 56, 4)
process(bass, 56, gain=1.5)
process(bass, 64, gain=1.5)
process(outro, 72, 4)
music.sort()
def freq(octave, step):
"""
Returns the frequency of a note
"""
return 55 * 2 ** (octave + step / 12 - 1)
def tone(f, t):
"""
Returns the intensity of a tone of frequency f sampled at time t
"""
# https://dsp.stackexchange.com/questions/46598/mathematical-equation-for-the-sound-wave-that-a-piano-makes
# https://youtu.be/ogFAHvYatWs?t=254
# return int(2**13*(1+square(t, 440*2**(math.floor(5*t)/12))))
# Y = sum([math.sin(2 * i * math.pi * t * f) * math.exp(-0.0004 * 2 * math.pi * t * f) / 2**i for i in range(1, 4)])
# Y += Y * Y * Y
# Y *= 1 + 16 * t * math.exp(-6 * t)
w = 2 * math.pi * f
Y = 0.6 * math.sin(w * t) * math.exp(-0.001 * w * t)
Y += 0.2 * math.sin(2 * w * t) * math.exp(-0.001 * w * t)
Y += 0.05 * math.sin(3 * w * t) * math.exp(-0.001 * w * t)
Y += Y * Y * Y
Y *= 1 + 16 * t * math.exp(-6 * t)
return Y
def at(t):
"""
Returns the total intensity of music sampled at time t
"""
i = bisect.bisect(music, (t, 2**31))
# This is actually pretty efficient ngl
# Because people usually don't have that many overlapping notes
ret = 0
for j in range(max(i - 30, 0), i):
m = music[j]
# if m[0] + m[1] > t:
ret += m[4] * tone(freq(m[2], m[3]), t - m[0])
return int(2**28 * ret)
# Print out music encoded in s16 to standard output
for i in range(0 * bitrate, 84 * bitrate):
sys.stdout.buffer.write(struct.pack("i", at(i / bitrate)))