-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.js
398 lines (363 loc) · 11.6 KB
/
compile.js
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Instrument = (function () {
function Instrument(t) {
_classCallCheck(this, Instrument);
this.tau = 2 * Math.PI;
this.t = t;
}
_createClass(Instrument, [{
key: "sin",
value: function sin(x) {
return Math.sin(this.tau * this.t * x);
}
}, {
key: "square",
value: function square(x) {
return this.sin(x) > 0 ? 1 : -1;
}
}, {
key: "saw",
value: function saw(x) {
return 1 - 2 * (this.t % (1 / x)) * x;
}
}, {
key: "drums",
value: function drums() {
return this.t % (1 / 2) < 1 / 16 ? 2 * Math.random() - 1 : 0;
}
}, {
key: "bass",
value: function bass(x) {
return this.t % 1 < 1 / 4 ? Math.sin(this.tau * this.t * x) : 0;
}
}, {
key: "lowDrums",
value: function lowDrums(x) {
return this.t % (1 / 2) < 1 / 16 ? 10 * Math.cos(this.tau * this.t * x) : 0;
}
}, {
key: "clarinet",
value: function clarinet(x) {
return this.sin(x) + 0.75 * this.sin(x * 3) + 0.5 * this.sin(x * 5) + 0.14 * this.sin(x * 7) + 0.5 * this.sin(x * 9) + 0.12 * this.sin(11 * x) + 0.17 * this.sin(x * 13);
}
}, {
key: "chord",
value: function chord(x) {
return this.sin(x) + this.sin(x * (5 / 4)) + this.sin(x * (3 / 2));
}
}, {
key: "violin",
value: function violin(x) {
return (this.sin(x) + 1 / 2 * this.sin(x * 2) + 1 / 3 * this.sin(x * 3) + 1 / 4 * this.sin(x * 4) + 1 / 5 * this.sin(x * 5) + 1 / 6 * this.sin(x * 6)) * this.sin(5) * (1 - Math.pow(Math.E, -this.t * 3));
}
}, {
key: "pluck",
value: function pluck(freq, a, n, s) {
var scalar = Math.max(0, 0.95 - this.t % a * n / (this.t % a * n + 1));
var sum = 0;
for (var i = 0; i < s; i++) {
sum += Math.sin(this.tau * (this.t % a) * (freq + i * freq));
}
return scalar * sum / 3;
}
}, {
key: "uku",
value: function uku(freq, a, n, s) {
var scalar = Math.max(0, 0.95 - this.t % a * n / (this.t % a * n + 0.5));
var sum = 0;
for (var i = 0; i < s; i++) {
sum += Math.sin(this.tau * (this.t % a) * (freq + i * freq));
}
return scalar * sum / 3;
}
}]);
return Instrument;
})();
function freq(note) {
return Math.pow(2, (note.key - 49) / 12) * 440 / note.octave;
};
var Note = function Note(key, dur) {
_classCallCheck(this, Note);
var noteMap = { C: 4, D: 6, E: 8, F: 9, G: 11, A: 13, B: 15 };
var octaveMap = {
1: 1,
2: 1 / 2,
3: 1 / 4,
4: 1 / 8,
5: 1 / 16,
6: 1 / 32,
7: 1 / 64,
8: 1 / 128
};
var note = {
key: noteMap[key[0]],
octave: octaveMap[key[1]]
};
this.freq = freq(note);
this.duration = dur;
};
/*class Buffer {
constructor(ctx) {
this.ctx = ctx;
this.bufferList = [];
this.bufferSources = [];
}
createAllBufferSources() {
for (var i = 0; i < this.bufferList.length; i++) {
this.ctx.decodeAudioData(this.bufferList[i], function(buffer)
{
var source = createBufferSource();
bufferData = buffer;
source.buffer = bufferData;
source.loop = true;
this.bufferSources.push(source);
}, this.onDecodeError);
}
}
createBuffer(cb, dur) {
var arrayBuffer = new Float32Array(44100*dur);
for(var i = 0; i < 44100*dur; i++) {
arrayBuffer[i] = cb(this.frequencyAdjust, i/44100);
}
this.bufferList.push(arrayBuffer);
}
getBuffers() {
return this.bufferList;
}
updateBuffer(cb, dur) {
var arrayBuffer = [];
for(var i = 0; i < 44100*dur; i++) {
arrayBuffer[i] = cb(this.frequencyAdjust, i/44100);
}
this.bufferList.push(arrayBuffer);
}
}*/
var Waver = (function () {
function Waver(ctx, dur, cb) {
_classCallCheck(this, Waver);
this.frequencyAdjust = 2;
this.ctx = ctx;
this.dur = dur;
this.cb = cb;
this.init();
}
_createClass(Waver, [{
key: "init",
value: function init() {
var channels = 2;
var frameCount = this.ctx.sampleRate * this.dur;
this.sourceBuffer = this.ctx.createBuffer(channels, frameCount, this.ctx.sampleRate);
for (var channel = 0; channel < channels; channel++) {
var nowBuffering = this.sourceBuffer.getChannelData(channel);
for (var i = 0; i < this.sourceBuffer.length; i++) {
nowBuffering[i] = this.cb(i / ctx.sampleRate);
}
}
}
}]);
return Waver;
})();
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function sum(terms) {
return terms.reduce(function (a, b) {
return a + b;
});
}
var MusicMatrix = (function () {
function MusicMatrix() {
_classCallCheck(this, MusicMatrix);
this._previous_note = null;
this._markov = new MarkovBuilder(["a", "a#", "b", "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#"]);
this._timings = new MarkovBuilder([1, 2, 3, 4, 5, 6, 7, 8]);
}
_createClass(MusicMatrix, [{
key: "add",
value: function add(to_note) {
if (this._previous_note === null) {
this._previous_note = to_note;
return true;
}
var from_note = this._previous_note;
this._markov.add(from_note[0], to_note[0]);
this._timings.add(from_note[1], to_note[1]);
this._previous_note = to_note;
}
}, {
key: "next_note",
value: function next_note(from_note) {
return [this._markov.next_value(from_note[0]), this._timings.next_value(from_note[1])];
}
}]);
return MusicMatrix;
})();
var MarkovBuilder = (function () {
function MarkovBuilder(value_list) {
_classCallCheck(this, MarkovBuilder);
this._values_added = 0;
this._reverse_value_lookup = value_list;
this._value_lookup = {};
for (var i = 0; i < value_list.length; i++) {
this._value_lookup[value_list[i]] = i;
}
this._matrix = [];
for (var i = 0; i < value_list.length; i++) {
this._matrix[i] = [];
for (var j = 0; j < value_list.length; j++) {
this._matrix[i][j] = 0;
}
}
}
_createClass(MarkovBuilder, [{
key: "add",
value: function add(from_value, to_value) {
//Add a path from a note to another note. Re-adding a path between notes will increase the associated weight.
var value = this._value_lookup;
this._matrix[value[from_value]][value[to_value]] += 1;
this._values_added = this._values_added + 1;
}
}, {
key: "next_value",
value: function next_value(from_value) {
var value = this._value_lookup[from_value];
var value_counts = this._matrix[value];
var value_index = this.randomly_choose(value_counts);
if (value_index < 0) {
throw "Non-existent value selected.";
} else {
return this._reverse_value_lookup[value_index];
}
}
}, {
key: "randomly_choose",
value: function randomly_choose(choice_counts) {
//Given an array of counts, returns the index that was randomly chosen
var counted_sum = 0;
var count_sum = sum(choice_counts);
var selected_count = Math.floor(Math.random() * (count_sum - 1) + 1);
for (var i = 0; i < choice_counts.length; i++) {
counted_sum += choice_counts[i];
if (counted_sum >= selected_count) {
return i;
}
}
throw "Impossible value selection made. BAD!";
}
}]);
return MarkovBuilder;
})();
var musicLearner = new MusicMatrix();
musicLearner.add(["d", 4]);
musicLearner.add(["e", 3]);
musicLearner.add(["e", 3]);
musicLearner.add(["d", 4]);
musicLearner.add(["e", 3]);
musicLearner.add(["f", 4]);
musicLearner.add(["c", 4]);
musicLearner.add(["c", 4]);
musicLearner.add(["c", 4]);
musicLearner.add(["g", 4]);
musicLearner.add(["g", 4]);
musicLearner.add(["g", 4]);
musicLearner.add(["e", 2]);
musicLearner.add(["e", 4]);
musicLearner.add(["e", 3]);
musicLearner.add(["e", 4]);
musicLearner.add(["e", 3]);
musicLearner.add(["e", 4]);
musicLearner.add(["e", 3]);
musicLearner.add(["e", 4]);
musicLearner.add(["e", 3]);
musicLearner.add(["e", 4]);
musicLearner.add(["c", 4]);
musicLearner.add(["c", 4]);
musicLearner.add(["c", 4]);
musicLearner.add(["g", 3]);
musicLearner.add(["f", 4]);
musicLearner.add(["e", 3]);
musicLearner.add(["d", 4]);
"use strict";
var ctx = new (window.AudioContext || window.webkitAudioContext)();
var instruments = [];
function instrumentUpdate(ins) {
instruments.push(ins);
if (play) {
stopTwo(play[0]);
play = startTwo(ctx, instruments);
}
}
function instrumentRemove(ins) {
instruments = instruments.splice(instruments.indexOf(ins), 1);
if (play) {
stopTwo(play[0]);
play = startTwo(ctx, instruments);
}
}
var play;
var inputElements = document.getElementsByClassName("instrument");
for (var i = 0; i < inputElements.length; i++) {
if (inputElements[i].checked) {
instrumentUpdate(inputElements[i].value);
}
inputElements[i].onclick = function () {
if (this.checked) {
instrumentUpdate(this.value);
} else {
instrumentRemove(this.value);
}
};
}
play = startTwo(ctx, instruments);
function startTwo(ctx, instruments) {
var random_score = [];
current_note = ["c", 4];
for (var i = 0; i < 50; i++) {
var current_note = musicLearner.next_note(current_note);
var note = new Note(current_note[0].toUpperCase() + current_note[1].toString());
random_score.push(note);
}
var node = new Waver(ctx, 32, function (t) {
var i = new Instrument(t);
var sum = 0;
for (var j = 0; j < instruments.length; j++) {
switch (instruments[j]) {
case "chord":
sum += i.chord(random_score[Math.floor(t % random_score.length)].freq);
break;
case "clarinet":
sum += i.clarinet(random_score[Math.floor(t % random_score.length)].freq);
break;
case "violin":
sum += i.violin(random_score[Math.floor(t % random_score.length)].freq * 2);
break;
case "pluck":
sum += i.pluck(random_score[Math.floor(t % random_score.length)].freq * 3 / 2, 0.5, 10, 10);
break;
case "drum":
sum += i.drums() + (i.sin(3) + i.sin(4));
break;
case "bass":
sum += i.bass(random_score[Math.floor(t % random_score.length)].freq % 200);
break;
case "lowDrum":
sum += i.lowDrums(100) + (i.sin(2) + i.square(3));
break;
}
}
return sum;
});
var song = ctx.createBufferSource();
var synthDelay = ctx.createDelay(5);
song.loop = true;
song.buffer = node.sourceBuffer;
song.start();
song.connect(ctx.destination);
return [song, node];
};
function stopTwo(song) {
song.stop();
song.disconnect();
}