-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
371 lines (317 loc) · 10.4 KB
/
index.html
File metadata and controls
371 lines (317 loc) · 10.4 KB
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
<html>
<head>
<title>Living Visualizer</title>
<link href='https://fonts.googleapis.com/css?family=Orbitron:900' rel='stylesheet' type='text/css'>
<style>
body {
background: #000000;
font-family: tahoma, verdana, sans serif;
margin: 0 0 0 0;
}
h1
{
font-family: 'Orbitron' , sans-serif;
text-align: center;
color: #ffffff;
}
div{
text-align:center;
color: #ffffff;
}
controls{
margin-left:10px;
margin-top:10px;
}
canvas{
background: black;
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
}
</style>
</head>
<body>
<h1>Living Visualizer</h1>
<canvas></canvas>
<div id="controls">
<audio controls loop></audio>
<br/>
<label>Track:
<select id="trackSelect" >
<option value="media/17Years.mp3">17 Years</option>
<option value="media/XP_Sounds.mp3">XP Song</option>
<option value="media/Skrillex.mp3">Skrillex</option>
<option value="media/OneWingedAngel.mp3">One Winged Angel</option>
<option value="media/Lamprey.mp3">Lamprey</option>
<option value="media/New Adventure Theme.mp3">New Adventure Theme</option>
<option value="media/Peanuts Theme.mp3">Peanuts Theme</option>
<option value="media/The Picard Song.mp3">The Picard Song</option>
</select>
</label>
<br/>
<span>
<label for="lineCheck">Draw Lines</label>
<input type="checkbox" id="lineCheck" checked = "checked" >
</span>
<select id ="lineSelect">
<option value ="straight">Straight Lines</option>
<option value ="quadratic" selected>Quadratic Lines</option>
<option value ="cubic">Cubic Lines</option>
<option value = "random">Random</option>
</select>
<br/>
<span>
<label for="circleCheck">Draw Cells</label>
<input type="checkbox" id="circleCheck" checked >
</span>
<span>
<label for="filterCheck">Pixel Filter</label>
<input type="checkbox" id="filterCheck">
</span>
<br/>
Audio Effect:
<select id ="audioSelect">
<option value ="none">No Audio Effect</option>
<option value ="reverb">Reverb Effect</option>
<option value ="filter">Lowpass Filter Effect</option>
</select>
<br/>
Waveform Generation
<input type="checkbox" id="waveformGen">
<div>
<label for="lineWidthSlider">Line Width</label>
<input id="lineWidthSlider" type ="range" min ="0.1" max="5.0" step ="0.1" value ="1.0"/>
</div>
<div>
<label for="radiusSlider">Cell Size</label>
<input id="radiusSlider" type ="range" min ="0.75" max="5.0" step ="0.1" value ="1.0"/>
</div>
<div>
<label for="reverbSlider">Reverb Strength</label>
<input id="reverbSlider" type ="range" min ="0.01" max="1.0" step ="0.05" value =".2"/>
</div>
<div>
<label for="audioFilterSlider">Filter Frequency</label>
<input id="audioFilterSlider" type ="range" min ="100" max="1000" step ="020" value ="440"/>
</div>
<p id="status">???</p>
</div>
</body>
<script src='setup.js'></script>
<script src='automota.js'></script>
<script>
(function(){
"use strict";
Draw.init()
window.onload = init;
var colony;
var NUM_SAMPLES = 256;
var SOUND_6 = 'media/XP_Sounds.mp3', SOUND_4 = 'media/New Adventure Theme.mp3', SOUND_2 = 'media/Peanuts Theme.mp3';
var SOUND_3 = 'media/The Picard Song.mp3', SOUND_5 = 'media/Skrillex.mp3', SOUND_1 = 'media/17Years.mp3';
var SOUND_7 = 'media/OneWingedAngel.mp3', SOUND_8 = 'media/Lamprey.mp3';
var audioElement, analyserNode;
var lineStyle = "quadratic", lineWidth = 1, radiusScale = 1;
var drawLines = true, drawCircles = true, pixelFilter = false, waveFormData = false;
var audioEffect = "none";
var filter, filterFrequency = 440, reverbScale = .3;
var audioCtx, analyserNode, sourceNode, convolver;
function init() {
// Initialize audio systems
audioElement = document.querySelector('audio');
analyserNode = createWebAudioContextWithAnalyserNode(audioElement);
setupUI();
playStream(audioElement,SOUND_1);
colony = Colony.create(Draw.canvas.width, Draw.canvas.height, NUM_SAMPLES / 2, NUM_SAMPLES / 2);
//colony.randomizeState();
update();
}
function createWebAudioContextWithAnalyserNode(audioElement) {
// create new AudioContext
// The || is because WebAudio has not been standardized across browsers yet
// http://webaudio.github.io/web-audio-api/#the-audiocontext-interface
audioCtx = new (window.AudioContext || window.webkitAudioContext);
// create an analyser node
analyserNode = audioCtx.createAnalyser();
// fft stands for Fast Fourier Transform
analyserNode.fftSize = NUM_SAMPLES;
// this is where we hook up the <audio> element to the analyserNode
sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(analyserNode);
//impulse responses from http://noisehack.com/custom-audio-effects-javascript-web-audio-api/
convolver = audioCtx.createConvolver();
var noiseBuffer =audioCtx.createBuffer(2, reverbScale * audioCtx.sampleRate, audioCtx.sampleRate);
var left = noiseBuffer.getChannelData(0);
var right = noiseBuffer.getChannelData(1);
for (var i = 0; i < noiseBuffer.length; i++) {
left[i] = Math.random() * 3 - 1;
right[i] = Math.random() * 3 - 1;
}
convolver.buffer = noiseBuffer;
//http://www.html5rocks.com/en/tutorials/webaudio/intro/
filter = audioCtx.createBiquadFilter();
filter.type='lowpass';
filter.frequency.value = filterFrequency;
switch(audioEffect) {
case "none":
analyserNode.connect(audioCtx.destination);
break;
case "reverb":
analyserNode.connect(convolver);
convolver.connect(audioCtx.destination);
break;
case "filter":
analyserNode.connect(filter);
filter.connect(audioCtx.destination);
break;
}
return analyserNode;
}
function setupUI() {
document.querySelector("#trackSelect").onchange = function(e) {
playStream(audioElement,e.target.value);
colony.clearState();
};
document.querySelector("#lineSelect").onchange = function(e) {
lineStyle= e.target.value;
};
document.querySelector("#lineWidthSlider").oninput = function(e) {
lineWidth = e.target.value;
};
document.querySelector("#radiusSlider").oninput = function(e) {
radiusScale = e.target.value;
};
document.querySelector("#lineCheck").onchange = function(e) {
drawLines = e.target.checked;
};
document.querySelector("#circleCheck").onchange = function(e) {
drawCircles = e.target.checked;
};
document.querySelector("#waveformGen").onchange = function(e) {
waveFormData = e.target.checked;
};
document.querySelector("#audioFilterSlider").oninput = function(e) {
filterFrequency = e.target.value;
filter = audioCtx.createBiquadFilter();
filter.type='lowpass';
filter.frequency.value=filterFrequency;
if(audioEffect=="filter") {
convolver.disconnect();
analyserNode.disconnect();
analyserNode.connect(filter);
filter.connect(audioCtx.destination);
}
};
document.querySelector("#radiusSlider").oninput = function(e) {
radiusScale = e.target.value;
};
document.querySelector("#filterCheck").onchange = function(e) {
pixelFilter = e.target.checked;
};
document.querySelector("#audioSelect").onchange = function(e) {
audioEffect= e.target.value;
switch(audioEffect)
{
case "none":
convolver.disconnect();
filter.disconnect();
analyserNode.disconnect();
analyserNode.connect(audioCtx.destination);
break;
case "reverb":
filter.disconnect();
analyserNode.disconnect();
analyserNode.connect(convolver);
convolver.connect(audioCtx.destination);
break;
case "filter":
convolver.disconnect();
analyserNode.disconnect();
analyserNode.connect(filter);
filter.connect(audioCtx.destination);
break;
}
};
document.querySelector("#reverbSlider").oninput = function(e) {
reverbScale = e.target.value;
var noiseBuffer =audioCtx.createBuffer(2, reverbScale * audioCtx.sampleRate, audioCtx.sampleRate);
var left = noiseBuffer.getChannelData(0);
var right = noiseBuffer.getChannelData(1);
for (var i = 0; i < noiseBuffer.length; i++) {
left[i] = Math.random() * 2 - 1;
right[i] = Math.random() * 2 - 1;
}
convolver.buffer = noiseBuffer;
if(audioEffect=="reverb") {
analyserNode.disconnect();
analyserNode.connect(convolver);
convolver.connect(audioCtx.destination);
}
};
}
function playStream(audioElement,path) {
audioElement.src = path;
audioElement.play();
audioElement.volume = 0.2;
document.querySelector('#status').innerHTML = "Now playing: " + path;
}
function update() {
var delay = 25;
setTimeout(function() {
requestAnimationFrame(update);
Draw.ctx.fillStyle = "rgba(0,0,0,.1)";
Draw.ctx.fillRect(0,0,Draw.canvas.width ,Draw.canvas.height );
colony.draw(Draw.ctx, lineWidth, lineStyle, radiusScale, drawLines, drawCircles);
colony.update();
// create a new array of 8-bit integers (0-255)
var data = new Uint8Array(NUM_SAMPLES);
// populate the array with the frequency data
// notice these arrays can be passed "by reference"
if(!waveFormData) {
analyserNode.getByteFrequencyData(data);
} else {
analyserNode.getByteTimeDomainData(data);
}
// loop through the data and draw!
for(var i=0; i<NUM_SAMPLES / 4; i++) {
Draw.ctx.fillStyle = 'rgba(0,255,0,0.6)';
if(data[i] > 100) {
colony.automota[i][Math.round(colony.ySize / 2)].nextState = Math.round(data[i] / 25);
}
}
var extraCount = 0;
for(var i=NUM_SAMPLES / 2 - 1; i > NUM_SAMPLES / 4; i--) {
Draw.ctx.fillStyle = 'rgba(0,255,0,0.6)';
if(data[extraCount] > 100) {
colony.automota[i][Math.round(colony.ySize / 2)].nextState = Math.round(data[extraCount] / 25);
}
extraCount++;
}
if(pixelFilter) {
manipulatePixels();
}
}, delay);
}
function manipulatePixels() {
var imageData = Draw.ctx.getImageData(0, 0, Draw.canvas.width, Draw.canvas.height);
var data = imageData.data;
var length = data.length;
var width = imageData.width;
for(var i = 0; i < length; i += 4) {
if(data[i] != 0 || data[1 + 1] != 0) {
data[i] = Math.round(Math.random() * data[i] * 2 );
data[i + 1] = 0;
data[i + 2] = Math.round(Math.random() * data[i + 2] * 2 );
}
}
Draw.ctx.putImageData(imageData, 0, 0);
}
// HELPER
function makeColor(red, green, blue, alpha) {
var color='rgba('+red+','+green+','+blue+', '+alpha+')';
return color;
}
}());
</script>
</html>