-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundscene.html
386 lines (345 loc) · 11.3 KB
/
soundscene.html
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
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="soundscene.css"/>
<script type="text/javascript" src="js/pitch.js"></script>
<script src="https://rawgithub.com/mrdoob/three.js/master/build/three.js"></script>
<script src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
ColorMode = {
MONOCHROME: 0,
TWOTONE: 1,
THREETONE: 2,
GRADIENT: 3,
RANDOM: 4
};
ShapeMode = {
CIRCLE: 0,
SQUARE: 1,
SKEW: 2
};
GridMode = {
RIGID: 0,
CONCENTRIC: 1,
RANDOM: 2,
SPIRAL: 3,
REVERSESPIRAL: 4,
BUTTERFLY:5,
ASTROID:6,
EPICYCLOID:7,
REVERSEEPIC:8,
HYPOTROCHOID: 9,
};
var audioContext;
var audioSource;
var analyser;
var freqDomain;
var timeDomain;
var canvas;
var ctxt;
var GRID_WIDTH = 32;
var GRID_HEIGHT = 32;
var SAMPLE_MAX = 256;
var WIDTH = window.innerWidth * 0.8;
var HEIGHT =window.innerHeight * 0.8;
var lastAmps;
var frame = 0;
var colormode = ColorMode.THREETONE;
var shapemode = ShapeMode.CIRCLE;
var gridmode = GridMode.SPIRAL;
var interp = 0;
function init() {
canvas = document.getElementById('canvas');
ctxt = canvas.getContext('2d');
canvas.width = WIDTH;
canvas.height = HEIGHT;
console.log('width = ' + WIDTH + 'height = ' + HEIGHT);
$('#canvas').hide();
$('#wavedistortion').hide();
try {
// Fix up for prefixing
window.AudioContext = window.AudioContext
||window.webkitAudioContext;
audioContext = new AudioContext();
}
catch(e) {
alert('Web Audio API is not supported in this browser');
}
ctxt.strokeRect(0, 0, canvas.width, canvas.height);
}
// load sound from input mp3
function loadSound(file) {
var reader = new FileReader();
reader.onload = function(e) {
var buff = e.target.result;
playSound(buff);
}
reader.readAsArrayBuffer(file);
}
function analyzeSound() {
requestAnimationFrame(analyzeSound);
// console.log('analyzing...');
if (frame % 10 != 0 && colormode != ColorMode.MONOTONE) {
frame ++;
return;
}
var timeDomain = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteTimeDomainData(timeDomain);
var max = 0;
/* for (var i = 0; i < timeDomain.length; i++) {
if (timeDomain[i] > max) {
max = timeDomain[i];
}
}
console.log('max = ' + max);*/
var freqDomain = new Float32Array(analyser.frequencyBinCount);
analyser.getFloatFrequencyData(freqDomain);
ctxt.clearRect(0,0, WIDTH, HEIGHT);
for (var freq = 0; freq < freqDomain.length; freq++) {
var x, y, r;
var amp = freqDomain[freq];
switch(gridmode) {
case GridMode.RIGID:
x = Math.floor(freq % GRID_WIDTH) * WIDTH/GRID_WIDTH + 10;
y = Math.floor(freq / GRID_WIDTH) * HEIGHT/GRID_HEIGHT + 10;
r = Math.abs(amp/SAMPLE_MAX) * 30;
break;
case GridMode.CONCENTRIC:
x = WIDTH/2;
y = HEIGHT/2;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * WIDTH/4;
break;
case GridMode.RANDOM:
x = Math.random() * WIDTH;
y = Math.random() * HEIGHT;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * 50;
break;
case GridMode.SPIRAL:
x = 0.2 * freq * Math.cos(freq) + WIDTH/2;
y = 0.2 * freq * Math.sin(freq) + HEIGHT/2;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * 50;
break;
case GridMode.REVERSESPIRAL:
var rev = 1024 - freq;
x = 0.3 * rev * Math.cos(rev) + WIDTH/2;
y = 0.3 * rev * Math.sin(rev) + HEIGHT/2;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * 50;
break;
case GridMode.BUTTERFLY:
var cosf = Math.cos(freq);
var sinf12 = Math.sin(freq/12);
var ecosf = Math.pow(Math.E, cosf);
var mult = (ecosf - 2 * Math.cos(4 * freq) - Math.pow(sinf12, 5));
x = 70 * Math.sin(freq) * mult + WIDTH/2;
y = 50 * Math.cos(freq) * mult + HEIGHT/2;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * 50;
break;
case GridMode.ASTROID:
x = 300 *(freq/1024)* Math.pow(Math.cos(freq), 3) + WIDTH/2 ;
y = 240 *(freq/1024)* Math.pow(Math.sin(freq), 3) + HEIGHT/2;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * 50;
break;
case GridMode.EPICYCLOID:
var a = freq/1024 * 200;
var rev = 1024 - freq;
var b = a/7;
var revx = (a + b) * Math.cos(rev) - b * Math.cos((a / b + 1)*rev);
var revy = (a + b) * Math.sin(rev) - b * Math.sin((a / b + 1)*rev);
x = (1- interp) * (a + b) * Math.cos(freq) - b * Math.cos((a / b + 1)*freq) +
interp * revx + WIDTH/2;
y = ((1- interp) * a + b) * Math.sin(freq) - b * Math.sin((a / b + 1)*freq) +
interp * revy + HEIGHT/2;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * 50;
break;
case GridMode.REVERSEEPIC:
var rev = 1024 - freq;
var a = rev/1024 * 200;
var b = a/7;
var c = 100;
x = (a + b) * Math.cos(rev) - b * Math.cos((a / b + 1)*rev) + WIDTH/2;
y = (a + b) * Math.sin(rev) - b * Math.sin((a / b + 1)*rev) + HEIGHT/2;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * 50;
break;
case GridMode.HYPOTROCHOID:
var a = freq/1024 * 200;
var b = a/7;
var c = 150;
x = (a - b) * Math.cos(freq) - c * Math.cos((a / b - 1)*freq) + WIDTH/2;
y = (a - b) * Math.sin(freq) - c * Math.sin((a / b - 1)*freq) + HEIGHT/2;
r = Math.abs(Math.pow(amp/SAMPLE_MAX, 2)) * 50;
break;
default:
x = Math.floor(freq % GRID_WIDTH) * WIDTH/GRID_WIDTH + 10;
y = Math.floor(freq / GRID_WIDTH) * HEIGHT/GRID_HEIGHT + 10;
r = Math.abs(amp/SAMPLE_MAX) * 30;
}
var lastamp = lastAmps[freq];
var delta = (lastamp - amp);
switch (colormode) {
case ColorMode.MONOCHROME:
ctxt.fillStyle = 'rgba(0, 255, 0,' + r/30 * 0.9 + ')';
break;
case ColorMode.TWOTONE:
if (delta > 1) {
ctxt.fillStyle = 'rgba(0 ,0, 255,' + r/30 * 0.9 + ')';
}
else {
ctxt.fillStyle = 'rgba(0, 255, 0,' + r/30 * 0.9 + ')';
}
break;
case ColorMode.THREETONE:
if (delta > 1) {
ctxt.fillStyle = 'rgba(0 ,0, 255,' + r/30 * 0.9 + ')';
}
else if (delta < -1) {
ctxt.fillStyle = 'rgba(255, 255, 0,' + r/30 * 0.9 + ')';
}
else {
ctxt.fillStyle = 'rgba(0, 255, 0,' + r/30 * 0.9 + ')';
}
break;
case ColorMode.GRADIENT:
ctxt.fillStyle = 'rgba(' + + ' , ' + + ' , ' + + ' , ' + r/30 * 0.9 + ')';
break;
case ColorMode.RANDOM:
var red = Math.random() * 255;
var green= Math.random() * 255;
var blue = Math.random() * 255;
var a = r/30 * 0.9;
// ctxt.fillStyle = 'rgba(' + red + ',' + green + ',' + blue + ', 1)';
ctxt.fillStyle = 'rgba('+ red + ','+ green + ',' + blue + ',' + r/30 * 0.9 + ')';
break;
default:
ctxt.fillStyle = 'rgba(0, 255, 0,' + r/30 * 0.9 + ')';
}
switch (shapemode) {
case ShapeMode.CIRCLE:
ctxt.beginPath();
ctxt.arc(x, y, r, 0, Math.PI * 2);
ctxt.fill();
break;
case ShapeMode.SQUARE:
ctxt.beginPath();
ctxt.rotate(frame % 2 * Math.PI);
ctxt.rect(x - r, y-r, r * 2, r * 2);
ctxt.fill();
break;
case ShapeMode.SKEW:
ctxt.beginPath();
ctxt.arc(x, y, r, 0, Math.PI * 2);
ctxt.fill();
break;
default:
ctxt.beginPath();
ctxt.arc(x, y, r, 0, Math.PI * 2);
ctxt.fill();
}
}
lastAmps = freqDomain;
frame++;
}
// play sound from ArrayBuffer
function playSound(buffer) {
// disconnect if there is already a song uploaded
if (audioSource) {
audioSource.stop();
audioSource.disconnect(0);
}
audioSource = audioContext.createBufferSource();
try {
audioSource.buffer = audioContext.createBuffer(buffer,false);
} catch (e) {
console.log("error reading sound file");
return;
}
$('h1').hide();
$('#dragDiv').hide();
$('#canvas').show();
$('#wavedistortion').show();
$('#controls').hide();
console.log('colormode = ' + colormode);
console.log('gridmode = ' + gridmode);
// set up the AudioAnalserNode
analyser = audioContext.createAnalyser();
audioSource.connect(analyser);
lastAmps = new Float32Array(analyser.frequencyBinCount);
analyser.connect(audioContext.destination);
audioSource.connect(audioContext.destination);
audioSource.start(0);
analyzeSound();
}
function allowDrop(ev) {
// console.log('registered drag');
ev.preventDefault();
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.files[0];
if (data) {
loadSound(data);
}
}
$(document).ready(function() {
init();
$(".colormode").change(function () {
colormode = parseInt(this.value);
console.log('colormode = ' + colormode);
});
$('.gridmode').change(function () {
gridmode = parseInt(this.value);
console.log('gridmode = ' + gridmode);
});
$('.shapemode').change(function () {
shapemode = parseInt(this.value);
console.log('shapemode = ' + shapemode);
});
$('#wavedistortion').change(function () {
interp = $(this).val();
console.log(this);
console.log('interp updated to ' + interp);
});
});
</script>
</head>
<body>
<h1>Sound Scene</h1>
<div id="dragDiv" ondragover="allowDrop(event)" ondrop="drop(event)"><span> drag sound here </span></div>
<input type ='range' id='wavedistortion' name = "distortion" min="0" max ="1" step = "0.01" value="0">DISTORT</input>
<canvas id="canvas"></canvas>
<div id='controls'>
<p>//TEMPORARY CONTROLS</p>
<p>Shape Mode: </p>
<input type='radio' class='shapemode' name='shapemode' value='0'> CIRCLE </input>
<br></br>
<input type='radio' class='shapemode' name='shapemode' value='1'> SQUARE </input>
<br></br>
<p>Color Mode: </p>
<input type='radio' class='colormode' name='colormode' value='0'> MONOCHROME </input>
<br></br>
<input type='radio' class='colormode' name='colormode' value='1'> TWOTONE </input>
<br></br>
<input type='radio' class='colormode' name='colormode' value='2'> THREETONE </input>
<p>Grid Mode: </p>
<input type='radio' class='gridmode' name='gridmode' value='0'> RIGID </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='1'> CONCENTRIC </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='2'> RANDOM </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='3'> SPIRAL </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='4'> REVERSE SPIRAL </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='5'> BUTTERFLY </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='6'> ASTROID </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='7'> EPICYCLOID </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='8'> REVERSE EPICYCLOID </input>
<br></br>
<input type='radio' class='gridmode' name='gridmode' value='9'> HYPOTROCHOID </input>
<br></br>
</div>
</body>
</html>