forked from tchandler/AuDOMio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
317 lines (251 loc) · 9.07 KB
/
script.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
(function(tab) {
var tree = {
name: "tagName",
children: []
};
var flat = [];
var nodeCount = 0;
var freqs = [];
var freqsToPlay = [];
var notes = 'ABCDEFG';
function findNearestFrequency(find) {
var low = 0,
high = freqs.length - 1,
i, comparison;
while (low <= high) {
i = Math.floor((low + high) / 2);
if (freqs[i] < find) {
low = i + 1;
continue;
};
if (freqs[i] > find) {
high = i - 1;
continue;
};
return freqs[i];
}
if (Math.abs(freqs[high] - find) > Math.abs(freqs[low] - find)) {
return freqs[low];
}
return freqs[i];
};
function generateNameValue(name) {
var val = 0;
if (name === "BR" || name === "A") return val;
for (var i = 0; i < name.length; i++) {
val += name[i].charCodeAt();
}
val = findNearestFrequency(val);
freqsToPlay.push(val);
return val;
}
function randomColor() {
var rint = Math.round(0xffffff * Math.random());
return ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');
}
function buildTagTree(rootElement) {
var treeElement = {};
treeElement.name = rootElement.tagName;
treeElement.nameValue = generateNameValue(treeElement.name);
treeElement.content = rootElement.innerHTML;
treeElement.addBorder = function() {
rootElement.style.backgroundColor = randomColor();
rootElement.style.border = "1px solid" + randomColor();
};
treeElement.removeBorder = function() {
rootElement.style.backgroundColor = "";
rootElement.style.border = "";
};
treeElement.children = [];
for (var i = 0; i < rootElement.childNodes.length; i++) {
var childNode = rootElement.childNodes[i];
if (childNode.nodeType === 1) {
treeElement.children.push(buildTagTree(childNode));
nodeCount++;
}
}
flat.push(treeElement);
return treeElement;
}
function makeFrequencies() {
var title = document.title;
var root = "C";
var key = "major";
for (var l = 0; l < title.length; l++) {
var idx = notes.indexOf(title[l].toUpperCase());
if (idx != -1) {
root = notes.charAt(idx);
break;
}
}
if (document.documentURI.indexOf("https") !== -1) {
key = "harmonic minor";
}
if (document.documentURI.indexOf(".com") === -1) {
key = "natural minor";
}
if (document.documentURI.indexOf(".org") !== -1) {
key = "minor pentatonic";
}
if (document.documentURI.indexOf(".co.") !== -1) {
key = "major pentatonic";
}
for (var o = 1; o <= 8; o++) {
var n = Note.fromLatin(root + o);
var scale = n.scale(key);
for (var i = 0; i < scale.length; i++) {
freqs.push(scale[i].frequency());
}
}
console.log(freqs);
}
function nthItems(arr, start, step) {
var retArray = [];
for (var i = start; i < arr.length; i += step) {
if (i > arr.length) continue;
retArray.push(arr[i]);
}
return retArray;
}
function forEachNode(callback) {
for (var i = 0; i < flat.length; i++) {
callback(flat[i]);
}
}
makeFrequencies();
console.log(buildTagTree(document.body));
var tempo = 0;
var Synth = function(audiolet, nodeInfo) {
var frequency = nodeInfo.nameValue;
var children = nodeInfo.children.length || 1;
AudioletGroup.apply(this, [audiolet, 0, 1]);
if (nodeInfo.children.length === 0) {
this.sine = new Sine(this.audiolet, frequency);
} else {
this.sine = new Saw(this.audiolet, frequency);
}
this.gain = new Gain(this.audiolet);
this.modulator = new Triangle(this.audiolet, frequency / 2);
this.modulatorMulAdd = new MulAdd(this.audiolet, frequency * children, frequency);
var release = tempo / (8 - nodeInfo.children.length % 8);
this.envelope = new PercussiveEnvelope(this.audiolet, 1, 0.1, release, function() {
nodeInfo.removeBorder();
this.audiolet.scheduler.addRelative(0, this.remove.bind(this));
}.bind(this));
this.envMulAdd = new MulAdd(this.audiolet, 0.5, 0);
this.filter = new LowPassFilter(this.audiolet, flat.length);
this.reverb = new Reverb(this.audiolet, 0.33, 0.5, 0.5);
this.upMixer = new UpMixer(audiolet, 2);
this.modulator.connect(this.modulatorMulAdd);
this.modulatorMulAdd.connect(this.sine);
this.envelope.connect(this.envMulAdd);
this.envMulAdd.connect(this.gain, 0, 1);
this.sine.connect(this.reverb);
this.reverb.connect(this.gain);
this.gain.connect(this.upMixer);
this.upMixer.connect(this.outputs[0]);
};
var Kick = function(audiolet) {
AudioletGroup.call(this, audiolet, 0, 1);
// Main sine oscillator
this.sine = new Sine(audiolet, 80);
// Pitch Envelope - from 81 to 1 hz in 0.3 seconds
this.pitchEnv = new PercussiveEnvelope(audiolet, 1, 0.001, 0.3);
this.pitchEnvMulAdd = new MulAdd(audiolet, 80, 1);
// Gain Envelope
this.gainEnv = new PercussiveEnvelope(audiolet, 1, 0.001, 0.3, function() {
// Remove the group ASAP when env is complete
this.audiolet.scheduler.addRelative(0, this.remove.bind(this));
}.bind(this));
this.gainEnvMulAdd = new MulAdd(audiolet, 0.7);
this.gain = new Gain(audiolet);
this.upMixer = new UpMixer(audiolet, 2);
// Connect oscillator
this.sine.connect(this.gain);
// Connect pitch envelope
this.pitchEnv.connect(this.pitchEnvMulAdd);
this.pitchEnvMulAdd.connect(this.sine);
// Connect gain envelope
this.gainEnv.connect(this.gainEnvMulAdd);
this.gainEnvMulAdd.connect(this.gain, 0, 1);
this.gain.connect(this.upMixer);
this.upMixer.connect(this.outputs[0]);
};
extend(Kick, AudioletGroup);
var Hat = function(audiolet) {
AudioletGroup.call(this, audiolet, 0, 1);
// Main sine oscillator
this.sine = new WhiteNoise(this.audiolet);
// Gain Envelope
this.gainEnv = new PercussiveEnvelope(audiolet, 1, 0.01, 0.05, function() {
// Remove the group ASAP when env is complete
this.audiolet.scheduler.addRelative(0, this.remove.bind(this));
}.bind(this));
this.gainEnvMulAdd = new MulAdd(audiolet, 0.7);
this.gain = new Gain(audiolet);
this.filter = new BandPassFilter(audiolet, 3000);
this.upMixer = new UpMixer(audiolet, 2);
// Connect oscillator
this.sine.connect(this.filter);
this.filter.connect(this.gain);
// Connect gain envelope
this.gainEnv.connect(this.gainEnvMulAdd);
this.gainEnvMulAdd.connect(this.gain, 0, 1);
this.gain.connect(this.upMixer);
this.upMixer.connect(this.outputs[0]);
};
extend(Hat, AudioletGroup);
extend(Synth, AudioletGroup);
var AudioletApp = function() {
this.audiolet = new Audiolet();
var that = this;
var scheduler = this.audiolet.scheduler;
var audiolet = this.audiolet;
tempo = flat.length / 3000;
tempo = 1.5 - tempo * 1.5;
if (tempo > 2) tempo = 2;
if (tempo < 0.75) tempo = 0.75;
console.log(flat.length);
var freqPattern, freqPattern2, freqPattern3;
var chunkSize = Math.floor(flat.length / 3);
freqPattern = new PSequence(flat.slice(0, chunkSize));
freqPattern2 = new PSequence(flat.slice(chunkSize + 1, chunkSize * 2));
freqPattern3 = new PSequence(flat.slice(chunkSize * 2 + 1));
var beats = [
new PSequence([tempo, tempo, tempo, tempo / 2, tempo / 2], Infinity),
new PSequence([tempo / 3, tempo / 3, tempo / 3, tempo * 3, tempo * 2, tempo], Infinity),
new PSequence([tempo * 2], Infinity), new PSequence([tempo / 2], Infinity),
new PSequence([tempo / 2, tempo / 2, tempo, tempo / 2, tempo / 2, tempo], Infinity),
new PSequence([tempo], Infinity), new PSequence([tempo, tempo, tempo, tempo / 2], Infinity),
new PSequence([tempo / 3], Infinity),
new PSequence([tempo * 3, tempo / 3, tempo / 3, tempo / 3], Infinity),
new PSequence([tempo], Infinity)
];
var kickBeat = beats[document.title.length % 10];
var hatBeat = beats[document.documentURI.length % 10];
scheduler.play([freqPattern], tempo, function(info) {
info.addBorder();
var synth = new Synth(audiolet, info);
synth.connect(audiolet.output);
}.bind(that));
scheduler.play([freqPattern2], tempo / 2, function(info) {
info.addBorder();
var synth = new Synth(audiolet, info);
synth.connect(audiolet.output);
}.bind(that));
scheduler.play([freqPattern3], tempo * 2, function(info) {
info.addBorder();
var synth = new Synth(audiolet, info);
synth.connect(audiolet.output);
}.bind(that));
scheduler.play([], kickBeat, function() {
var kick = new Kick(audiolet);
kick.connect(audiolet.output);
}.bind(that));
scheduler.play([], hatBeat, function() {
var hat = new Hat(audiolet);
hat.connect(audiolet.output);
}.bind(that));
};
window.audioletApp = new AudioletApp();
})();