-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
535 lines (482 loc) · 18 KB
/
index.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<html>
<!--
Arranged by Kouzerumatsu 9/21/2021
The code mostly stolen from references
- https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode
- https://developer.mozilla.org/en-US/docs/Web/API/AudioContext
- https://stackoverflow.com/questions/6921792/how-to-draw-circle-in-html-page
- https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes
- https://www.javascripttutorial.net/javascript-dom/javascript-change-event/
- https://www.w3schools.com/tags/att_input_type_radio.asp
- https://www.a1k0n.net/2015/11/09/javascript-ft2-player.html
- some stackexchanges and google.
-->
<body>
<table><tr><td>
<table><tr><td><h1>Waveplot view: </h1></td><td>
<table><tr><td>
<form id="wave-shape">
<div id="error-audio"></div>
<label class="radio-inline">
<input type="radio" name="optshape" class="shapeRadio">Sine
</label>
<label class="radio-inline">
<input type="radio" name="optshape" class="shapeRadio">Triangle
</label>
<label class="radio-inline">
<input type="radio" name="optshape" class="shapeRadio" checked>Pulse
</label>
<label class="radio-inline">
<input type="radio" name="optshape" class="shapeRadio">Noise
</label>
</form>
</td><td><table><tr><td><span id="prate">Pulse rate: </span><span id="dtyvalue"></span></td></tr><tr><td>
<input type="range" min="0" max="64" value="32" class="slider" id="dtyslider">
</td><td>
<label>
<input type="checkbox" id="lockpos" checked>Lock position
</label>
</td></tr></table>
</td></tr></table>
</td></tr></table>
</td><td>
<table><tr><td><h1>Spectrograph view: </h1></td>
<td><button id="playButton" onclick="initializeAudio('playButton')">Start sound</button></td></tr>
</td></tr></table>
</td></tr><tr><td>
<div class="wavevisContainer">
<canvas id="waveViszr" height="256px" width="768px"></canvas>
</div>
</td><td>
<div class="wavevisContainer">
<canvas id="spectrog" height="256px" width="512px"></canvas>
</div>
</td></tr>
</table>
<div class="sliderContainer">
Oscillations: <span id="oscvalue"></span> samples per pulse
<input type="range" min="1" max="512" value="256" class="slider" id="oscslider">
<b>qty</b> <i>(supersamples decimated): </i><span id="qtyvalue"></span>
<input type="range" min="1" max="32" value="16" class="slider" id="qtyslider">
</div>
<table>
<tr>
<td>
<h1>Low-pass parameters:</h1>
</td>
<td>
<form id="lowpass-setting">
<label class="radio-inline">
<input type="radio" name="optshape" class="lowpassRadio"> Zero-order (Rectangular)
</label>
<label class="radio-inline">
<input type="radio" name="optshape" class="lowpassRadio"> First-order (Exponential)
</label>
<label class="radio-inline">
<input type="radio" name="optshape" class="lowpassRadio" checked> Second-order (Elastic)
</label>
</form>
</td>
</tr>
</table>
<div class="sliderContainer">
<b>rnd</b> <i>(acceleration strength): </i><span id="rndvalue"></span>
<input type="range" min="1" max="256" value="8" class="slider" id="rndslider">
<b>dec</b> <i>(velocity attenuation): </i><span id="decvalue"></span>
<input type="range" min="1" max="256" value="8" class="slider" id="decslider">
</div>
<table>
<tr>
<td>
<h1>High-pass parameters:</h1>
</td>
<td>
<form id="lowpass-setting">
<label class="radio-inline">
<input type="radio" name="optshape" class="highpassRadio" checked> Off
</label>
<label class="radio-inline">
<input type="radio" name="optshape" class="highpassRadio"> First-order (Exponential)
</label>
<label class="radio-inline">
<input type="radio" name="optshape" class="highpassRadio"> Second-order (Elastic)
</label>
</form>
</td>
</tr>
</table>
<div class="sliderContainer">
<b>rnd</b> <i>(acceleration strength): </i><span id="rndvalue2"></span>
<input type="range" min="1" max="256" value="8" class="slider" id="rndslider2">
<b>dec</b> <i>(velocity attenuation): </i><span id="decvalue2"></span>
<input type="range" min="1" max="256" value="8" class="slider" id="decslider2">
</div>
</body>
<script>
var oscSl = document.getElementById("oscslider"), oscVal = document.getElementById("oscvalue");
var qtySl = document.getElementById("qtyslider"), qtyVal = document.getElementById("qtyvalue");
var rndSl = document.getElementById("rndslider"), rndVal = document.getElementById("rndvalue");
var decSl = document.getElementById("decslider"), decVal = document.getElementById("decvalue");
var rndSl2= document.getElementById("rndslider2"),rndVal2= document.getElementById("rndvalue2");
var decSl2= document.getElementById("decslider2"),decVal2= document.getElementById("decvalue2");
var wavVs = document.getElementById("waveViszr"), spectr = document.getElementById("spectrog");
var dtySl = document.getElementById("dtyslider"), dtyVal = document.getElementById("dtyvalue");
var rdShp = document.querySelectorAll(".shapeRadio");
var rdLwp = document.querySelectorAll(".lowpassRadio");
var rdHwp = document.querySelectorAll(".highpassRadio");
var lockP = document.getElementById("lockpos");
wavVs.ss = {"pos":0,"val":0,"vel":0,"val2":0,"vel2":0}
function updateValues(){
var params = getLowpassParams()
oscVal.innerHTML = params.osc;
qtyVal.innerHTML = params.qty;
rndVal.innerHTML = params.rnd;
decVal.innerHTML = params.dec;
rndVal2.innerHTML = params.rnd2;
decVal2.innerHTML = params.dec2;
lastQTY = qtySl.value;
lastRND = rndSl.value;
lastRND2= rndSl2.value;
lastDTY = dtySl.value;
dtyVal.innerHTML =
getSelect(rdShp)<3?
(dtySl.value/64).toFixed(3):"";
}
function drawWavePlot(){
requestAnimationFrame(drawWavePlot)
var _ = wavVs.getContext("2d")
var W = wavVs.width;
var H = wavVs.height;
var sY = H/2*0.5
_.fillStyle = "#222222"
_.fillRect(0,0,W,H)
_.fillStyle = "#555555"
_.fillRect(W/2,0,2,H)
_.fillRect(0,H/2,W,2)
var pos = 0;
if (wavVs.ss) pos = wavVs.ss.pos
var params = getLowpassParams()
var shape = params.shape;
var osc = params.osc;
var qty = params.osc;
var rnd = params.osc;
var dec = params.osc;
var step = 1/osc/6;
var shape = getSelect(rdShp);
var E = oscillations(512,wavVs.ss);
var Z = FFT(new Float32Array(E),new Float32Array(E.length))
var fR = Z[0], fI = Z[1];
var zR = new Float32Array(fR.length*4);
var zI = new Float32Array(fI.length*4);
zR.set(fR.slice(0,256),0);
zR.set(fR.slice(256),2048-256);
zI.set(fI.slice(0,256),0);
zI.set(fI.slice(256),2048-256);
var ZZ = FFT(zR,zI,-1)
F = ZZ[0].map(_=>_/512).reverse()
_.strokeStyle = "#AAAAAA"
_.beginPath()
var o = 0;
if(lockP.checked)
o = (2+(2-pos-step*W)%2)%2;
_.moveTo(0,H/2+shapes[shape]((pos+o)%2)*sY);
pos=(pos+step)%2;
for(var x=0,i=1;x<W;i++){
_.lineTo(x+=0.5,shapes[shape]((pos+o)%2)*sY+H/2)
pos=(pos+step)%2;
}
_.stroke()
_.strokeStyle = "#FFFF00"
_.beginPath()
var o = 0;
if(lockP.checked)
o = Math.round(0.25*F.length-pos*osc*4-1)
_.moveTo(0,H/2+F[o]*sY)
for(var x=0,i=o+1;x<W;i++){
_.lineTo(x+=3/4,F[i]*sY+H/2)
}
_.stroke()
_.strokeStyle = "#00FFFF"
var o = 0
if(lockP.checked)
o = Math.round(0.25*E.length-pos*osc)
_.moveTo(0,H/2+E[o]*sY)
for(var x=0,i=1;x<W;i++){
_.beginPath()
_.arc(x+=3, E[i+o]*sY+H/2, 2, 0, Math.PI * 2, false);
_.stroke()
}
if(audioctx) drawSpectrograph()
}
function drawSpectrograph() {
var canvas = spectr;
var canvasCtx = spectr.getContext("2d");
fftNode.getFloatFrequencyData(fftNode.dataArray);
canvasCtx.fillStyle = "#000000";
canvasCtx.fillRect(0, 0, canvas.width, canvas.height);
canvasCtx.fillStyle = "#FF0000";
var sliceWidth = canvas.width * 1 / fftNode.bufferLength;
var x = 0;
for (var i = 0; i < fftNode.bufferLength; i++) {
var v = (60+fftNode.dataArray[i])/64;
var y = v * canvas.height;
canvasCtx.fillRect(x,canvas.height-y,sliceWidth,y)
x += sliceWidth;
}
}
function FFT(R,I,s=1){
//The original code is ported directly from:
//https://youtu.be/h7apO7q16V0
var n = R.length;
if (n == 1) return [R,I]
var Wr = Math.cos(2*Math.PI/n)
var Wi = Math.sin(2*Math.PI/n)
var PEr=new Float32Array(n/2)
var PEi=new Float32Array(n/2)
var POr=new Float32Array(n/2)
var POi=new Float32Array(n/2)
for(var z=0;z<n;z++){
if(z&1){
POr[z>>1]=R[z]
POi[z>>1]=I[z]
}else{
PEr[z>>1]=R[z]
PEi[z>>1]=I[z]
}
}
var ye = FFT(PEr,PEi)
var yo = FFT(POr,POi)
var yer = ye[0], yei = ye[1];
var yor = yo[0], yoi = yo[1];
var yr = new Float32Array(n)
var yi = new Float32Array(n)
for(var z=0,m=n/2,p=1,q=0,r=0;z<m;z++){
var a=yer[z],b=yei[z];
var c=yor[z],d=yoi[z];
yr[z ]=a+p*c-q*d;
yi[z ]=b+p*d+q*c;
yr[z+m]=a-p*c+q*d;
yi[z+m]=b-p*d-q*c;
r=p*Wr-q*Wi;
q=p*Wi+q*Wr;
p=r;
}
return [yr,yi];
}
var shapes = [
_=>Math.sin(Math.PI*(_+8*(lastDTY/64)**2*Math.sin(Math.PI*_))),
_=>2*(_/2<lastDTY/64?_/2/lastDTY*64:1-(_/2-lastDTY/64)/(1-lastDTY/64))-1,
_=>2*((_/2)%1<lastDTY/64)-1,
_=>2*Math.random()-1,
]
function getLowpassParams(){
var k = getSelect(rdLwp)
var l = getSelect(rdHwp)
return {
"osc": oscSl.value/8,
"qty": qtySl.value,
"rnd": k<1?1:
k<2?rndSl.value:
rndSl.value**2,
"dec": k<2?1:
decSl.value,
"rnd2": l<1?Infinity:
l<2?rndSl2.value:
rndSl2.value**2,
"dec2": l<2?1:
decSl2.value,
"shape": getSelect(rdShp)
}
}
function getSelect(target){
for(var k=0;k<target.length;k++){
if(target[k].checked) return k
}
}
function oscillations(length,
sampleState={"pos":0,"val":0,"vel":0,"val2":0,"vel2":0}){
var params = getLowpassParams()
var shape = params.shape;
var osc = params.osc;
var qty = params.qty;
var rnd = params.rnd;
var dec = params.dec;
var rnd2= params.rnd2;
var dec2= params.dec2;
var pos = sampleState.pos; // SAMPLE POSITION
var val = sampleState.val; // BLEP VALUE
var vel = sampleState.vel; // BLEP VELOCITY
var val2 = sampleState.val2; // DC OFFSET CANCELLATION
var vel2 = sampleState.vel2; // DC OFFSET CANCELLATION
val2 = rnd2==Infinity?0:val2;
var data = new Float32Array(length);
var step = 1/osc;
for(var i = 0; i < data.length; i++) {
for(var j = 0; j < qty; j++){
var sample = shapes[shape](pos)
pos = ( pos + step / qty ) % 2;
vel += sample - val - vel / dec;
val += vel / rnd;
vel2+= val - val2 - vel2 / dec2;
val2+= vel2 / rnd2;
}
data[i] = val - val2;
}
sampleState.pos=pos;
sampleState.val=val;
sampleState.vel=vel;
sampleState.val2=val2;
sampleState.vel2=vel2;
return data;
}
var lastQTY = qtySl.value;
var lastDTY = dtySl.value;
var lastRND = rndSl.value**2;
var lastRND2= rndSl2.value**2;
oscSl.oninput = updateValues
qtySl.oninput = updateValues
rndSl.oninput = updateValues
decSl.oninput = updateValues
rndSl2.oninput = updateValues
decSl2.oninput = updateValues
dtySl.oninput = updateValues
function changeShape(){
var pRate = document.getElementById("prate")
switch(getSelect(rdShp)){
case 0:
dtySl.disabled = false;
pRate.innerHTML = "Modulate rate: "; break;
case 1:
dtySl.disabled = false;
pRate.innerHTML = "Slope rate: "; break;
case 2:
dtySl.disabled = false;
pRate.innerHTML = "Pulse rate: "; break;
case 3:
dtySl.disabled = true;
pRate.innerHTML = "White noise "; break;
}
updateValues()
}
function changeLowpass(){
switch(getSelect(rdLwp)){
case 0:
rndSl.disabled = true;
decSl.disabled = true;
break;
case 1:
rndSl.disabled = false;
decSl.disabled = true;
break;
case 2:
rndSl.disabled = false;
decSl.disabled = false;
break;
}
updateValues()
}
function changeHighpass(){
switch(getSelect(rdHwp)){
case 0:
rndSl2.disabled = true;
decSl2.disabled = true;
break;
case 1:
rndSl2.disabled = false;
decSl2.disabled = true;
break;
case 2:
rndSl2.disabled = false;
decSl2.disabled = false;
break;
}
updateValues()
}
for(var k=0; k<rdShp.length; k++)
rdShp[k].onchange = changeShape
for(var k=0; k<rdLwp.length; k++)
rdLwp[k].onchange = changeLowpass
for(var k=0; k<rdHwp.length; k++)
rdHwp[k].onchange = changeHighpass
var audioctx, gainNode, jsNode, fftNode;
var sampleStates = {"pos":0,"vel":0,"val":0,"val2":0,"vel2":0};
function initializeAudio(target){
if (!audioctx) {
var AudioContext = window.AudioContext || window.webkitAudioContext;
audioctx = new AudioContext();
}
if(audioctx){
if(!jsNode){
gainNode = audioctx.createGain();
gainNode.gain.value = 0.1; // master volume
jsNode = audioctx.createScriptProcessor(2048, 0, 1);
jsNode.onaudioprocess = function(e) {
var buflen = e.outputBuffer.length;
var data = e.outputBuffer.getChannelData(0);
var E = oscillations(data.length,sampleStates)
for(var i=0;i<data.length;i++)
data[i] = E[i];
};
jsNode.connect(gainNode);
gainNode.connect(audioctx.destination);
fftNode = audioctx.createAnalyser();
fftNode.fftSize = 1024;
fftNode.bufferLength = fftNode.frequencyBinCount;
fftNode.dataArray = new Float32Array(fftNode.bufferLength);
fftNode.getFloatFrequencyData(fftNode.dataArray);
jsNode.connect(fftNode);
document.getElementById(target).innerHTML="Stop sound";
}else{
jsNode.disconnect();
gainNode.disconnect();
fftNode.disconnect();
jsNode = null;
document.getElementById(target).innerHTML="Start sound";
}
}
}
requestAnimationFrame(drawWavePlot)
updateValues()
</script>
<style>
.slidecontainer {
width: 100%;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 15px;
height: 15px;
background: #04AA6D;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 15px;
height: 15px;
background: #04AA6D;
cursor: pointer;
}
.slider:disabled::-webkit-slider-thumb {
background: #777777 !important;
}
.slider:disabled::-moz-range-thumb {
background: #777777 !important;
}
form, h1 {
margin:auto;
}
</style>
</html>