-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
507 lines (443 loc) · 14.6 KB
/
main.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
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
// Start off by initializing a new context.
context = new (AudioContext || webkitAudioContext)();
/*
* ===========
* oscillators
* ===========
*/
/*===============
=== Control ===
===============*/
/*
* ======
* Slider
* ======
*/
// cutoff slider
var cutoff_slider = document.getElementById("cutoff_slider");
cutoff_slider.oninput = function() {
filter.frequency.value = 440 * Math.pow(2, (this.value - 80) /12) ;
}
// resonance slider
var resonance_slider = document.getElementById("resonance_slider");
resonance_slider.oninput = function() {
filter.Q.value = Math.pow(2.7, this.value/9);
}
// freq slider
var freq_slider = document.getElementById("freq_slider");
freq_slider.oninput = function() {
osc.setFreqFromRange(this.value);
}
// sub slider
var sub_slider = document.getElementById("sub_slider");
sub_slider.oninput = function() {
osc.setAmpSub(this.value/100);
}
// amp slider
var amp_slider = document.getElementById("amp_slider");
amp_slider.oninput = function() {
osc.setAmpMain(this.value/100);
}
// lfo freq slider
var lfo_freq_slider = document.getElementById("lfo_freq_slider");
lfo_freq_slider.oninput = function() {
lfo.setFreqFromRange(this.value);
}
// lfo amp slider
var lfo_amp_slider = document.getElementById("lfo_amp_slider");
lfo_amp_slider.oninput = function() {
lfo.setAmpFromRange(this.value);
}
// lfo freq slider
var lfo2_freq_slider = document.getElementById("lfo2_freq_slider");
lfo2_freq_slider.oninput = function() {
lfo2.setFreqFromRange(this.value);
}
// lfo amp slider
var lfo2_amp_slider = document.getElementById("lfo2_amp_slider");
lfo2_amp_slider.oninput = function() {
lfo2.setAmpFromRange(this.value);
}
// Selects
var lfo_type_select = document.getElementById("lfo_type_select");
lfo_type_select.onchange = function() {
lfo.setType(this.value);
}
var lfo2_type_select = document.getElementById("lfo2_type_select");
lfo2_type_select.onchange = function() {
lfo2.setType(this.value);
}
var osc_main_type_select = document.getElementById("osc_main_type_select");
osc_main_type_select.onchange = function() {
osc.setTypeMain(this.value);
}
var osc_main_sub_select = document.getElementById("osc_sub_type_select");
osc_main_sub_select.onchange = function() {
osc.setTypeSub(this.value);
}
var filter_type_select = document.getElementById("filter_type_select");
filter_type_select.onchange = function() {
filter.type = (this.value);
}
var drawSpectrum = false;
var wave_draw_select = document.getElementById("wave_draw_select");
wave_draw_select.onchange = function() {
drawSpectrum = this.value;
updateWave();
}
/*=============
=== Buttons ===
===============*/
var muted = false;
var mute_button = document.getElementById("mute_button");
mute_button.onclick = function() {
if(muted){
muted = false;
osc.unmute();
}
else{
muted = true;
osc.mute();
}
}
class OscSub {
constructor(freq, type, type2){
this.gain = 1;
this.amp_mix = context.createGain();
this.amp_main = context.createGain();
this.amp_main.connect(this.amp_mix);
this.amp_sub = context.createGain();
this.amp_sub.connect(this.amp_mix);
this.amp_sub.gain.value = 0;
this.osc_main = context.createOscillator();
this.osc_main.type = type;
this.osc_main.frequency.value = freq;
this.osc_main.connect(this.amp_main);
this.osc_sub = context.createOscillator();
this.osc_sub.type = type2;
this.osc_sub.frequency.value = freq / 2;
this.osc_sub.connect(this.amp_sub);
this.start_note = 69;
}
setFreq (freq) {
this.osc_main.frequency.value = freq;
this.osc_sub.frequency.value = freq / 2;
}
setFreqFromRange (value) {
this.setFreq( 440 * Math.pow(2, (value - this.start_note) /12) );
}
setWave (wave) {
this.osc_main.setPeriodicWave(wave);
this.osc_sub.setPeriodicWave(wave);
}
setTypeMain(type){
this.osc_main.type = type;
}
setTypeSub(type){
this.osc_sub.type = type;
}
setAmpMain(amp){
this.amp_main.gain.value = amp;
}
setAmpSub(amp){
this.amp_sub.gain.value = amp;
}
setAmpMix(amp){
this.amp_mix.gain.value = amp;
}
mute(){
this.gain = this.amp_mix.gain.value;
this.amp_mix.gain.value = 0;
}
unmute(){
this.amp_mix.gain.value = this.gain;
}
start(){
this.osc_main.start();
this.osc_sub.start();
}
connect(dest){
this.amp_mix.connect(dest);
}
}
class OscSimple {
constructor(freq, amp, type){
this.amp = context.createGain();
this.amp.gain.value = amp;
this.osc = context.createOscillator();
this.osc.type = type;
this.osc.frequency.value = freq;
this.osc.connect(this.amp);
this.start_note = 150;
}
setFreq (freq) {
this.osc.frequency.value = freq;
}
setFreqFromRange (value) {
this.setFreq(440 * Math.pow(2, (value - this.start_note) /12) );
}
setPeriodicWave(wave){
this.osc.setPeriodicWave(wave);
}
setType(type){
this.osc.type = type;
}
setAmp(amp){
this.amp.gain.value = amp;
}
setAmpFromRange(value){
this.amp.gain.value = Math.pow(2.7, value/9);
}
mute(){
this.gain = this.amp.gain.value;
this.amp.gain.value = 0;
}
unmute(){
this.amp.gain.value = this.gain;
}
start(){
this.osc.start();
}
connect(dest){
this.amp.connect(dest);
}
}
var osc = new OscSub (1, osc_main_type_select.value, osc_main_sub_select.value);
osc.setFreqFromRange(freq_slider.value);
osc.setAmpMix(amp_slider.value/100);
var lfo = new OscSimple (1, 0, lfo_type_select.value);
lfo.setFreqFromRange(lfo_freq_slider.value);
lfo.setAmpFromRange(lfo_amp_slider.value);
var lfo2 = new OscSimple (1, 0, lfo2_type_select.value);
lfo.setFreqFromRange(lfo2_freq_slider.value);
lfo.setAmpFromRange(lfo2_amp_slider.value);
/*
* ==========
* the filter
* ==========
*/
var filter = context.createBiquadFilter();
// Create and specify parameters for the low-pass filter.
filter.type = 'lowpass'; // Low-pass filter. See BiquadFilterNode docs
filter.frequency.value = 440 * Math.pow(2, (cutoff_slider.value- 80) /12);
filter.Q.value = 0;
/*===============
=== Wave Form ===
===============*/
// get the canvas and paint it white
var waveform = document.getElementById("waveform");
var waveformCtx = waveform.getContext("2d");
waveform.width = waveform.getClientRects()[0].width;
waveform.height = waveform.getClientRects()[0].height;
waveformCtx.fillStyle = 'rgb(250, 250, 250)';
waveformCtx.fillRect(0, 0, waveform.width, waveform.height);
function drawWave () {
waveformCtx.fillStyle = 'rgb(250, 250, 250)';
waveformCtx.fillRect(0, 0, waveform.width, waveform.height);
waveformCtx.fillStyle = 'rgb(0, 0, 0)';
for (var i = 0; i < waveformDrawBuffer.length; i++){
var rectStartX = i * stepSize;
var rectStartY = waveform.height - waveformDrawBuffer[i];
var rectWidth = stepSize;
var rectHeight = waveformDrawBuffer[i];
waveformCtx.fillRect(rectStartX, rectStartY, rectWidth, rectHeight);
}
}
// allocate Buffers depending on the width of the window and the width of a frequency
var stepSize = 10;
var waveformBufferReal = new Float32Array(waveform.width/stepSize);
var waveformBufferImg = new Float32Array(waveform.width/stepSize);
var waveformDrawBuffer = new Float32Array(waveform.width/stepSize);
waveformDrawBuffer
for (var i = 0; i < waveformDrawBuffer.length; i++){
waveformDrawBuffer[i] = Math.sin(i * 2 * Math.PI / (waveform.width/stepSize)) * waveform.height /2 + waveform.height/2;
}
function computeDft(inreal, inimag) {
var n = inreal.length;
var outreal = new Array(n);
var outimag = new Array(n);
for (var k = 0; k < n; k++) { // For each output element
var sumreal = 0;
var sumimag = 0;
for (var t = 0; t < n; t++) { // For each input element
var angle = 2 * Math.PI * t * k / n;
sumreal += inreal[t] * Math.cos(angle) + inimag[t] * Math.sin(angle);
sumimag += -inreal[t] * Math.sin(angle) + inimag[t] * Math.cos(angle);
}
outreal[k] = sumreal;
outimag[k] = sumimag;
}
return [outreal, outimag];
}
function createWave(){
for (let i = 0; i < waveformDrawBuffer.length; i++) {
waveformBufferReal[i] = waveformDrawBuffer[i] /waveform.height;
}
if(drawSpectrum == "frequency")
osc.setWave(context.createPeriodicWave(waveformBufferReal, waveformBufferImg));
else{
var dft = computeDft(waveformBufferReal, waveformBufferImg);
osc.setWave(context.createPeriodicWave(dft[0], dft[1]));
}
}
waveform.addEventListener('mousemove', (e) => {
context.resume();
if(e.buttons == 1){ // if mouse is clicked
waveformCtx.fillStyle = 'rgb(0, 0, 0)';
var index = parseInt(e.offsetX / stepSize);
waveformDrawBuffer[index] = waveform.height - e.offsetY;
updateWave();
}
});
function updateWave(){
createWave ();
drawWave ();
context.resume();
}
/*==================
===== Analyser =====
==================*/
var ana_gramm = context.createAnalyser();
var spectrogramm = document.getElementById("spectrogramm");
var spectrogrammCtx = spectrogramm.getContext("2d");
var ana_spec = context.createAnalyser();
var spectrum = document.getElementById("spectrum");
var spectrumCtx = spectrum.getContext("2d");
spectrum.width = spectrum.getClientRects()[0].width;
spectrum.height = spectrum.getClientRects()[0].height;
spectrogramm.width = spectrogramm.getClientRects()[0].width;
spectrogramm.height = spectrogramm.getClientRects()[0].height;
function visualizeSpectrogramm() {
WIDTH = spectrogramm.width;
HEIGHT = spectrogramm.height;
ana_gramm.fftSize = 256;
var bufferLength = ana_gramm.fftSize;
var dataArray = new Uint8Array(bufferLength);
var drawGramm = function() {
// not sure if necessary
drawVisual = requestAnimationFrame(drawGramm);
// get the FFT data of the current frame
ana_gramm.getByteFrequencyData(dataArray);
// get image data
var imgData = spectrogrammCtx.getImageData(0, 0, WIDTH, HEIGHT);
var newImgData = spectrogrammCtx.createImageData(WIDTH, HEIGHT);
// shift image data
for (let i = 0; i < HEIGHT; i++) {
for (let j = 0; j < WIDTH - 1; j++) {
for (let col = 0; col < 4; col++) {
newImgData.data[i * (WIDTH*4) + j*4 + col] = imgData.data[(i) * (WIDTH*4) + (j+1)*4 + col];
}
}
}
for (let i = 0; i < HEIGHT; i++) {
data = dataArray[i];
newImgData.data[i * (WIDTH*4) + (WIDTH-1)*4 + 0] = data;
newImgData.data[i * (WIDTH*4) + (WIDTH-1)*4 + 1] = data;
newImgData.data[i * (WIDTH*4) + (WIDTH-1)*4 + 2] = data;
newImgData.data[i * (WIDTH*4) + (WIDTH-1)*4 + 3] = 255;
}
// append the current FFTbuffer
// for (let i = 0; i < HEIGHT; i++) {
// // avg values from fft data
// var avgNum = parseInt(bufferLength/2 / HEIGHT);
// var sum = 0;
// // for (let avgIndex = 0; avgIndex < avgNum; avgIndex++) {
// // sum += dataArray[i * avgNum + avgIndex];
// // }
// // sum /= avgNum;
// sum = dataArray[i]
// // write the RGBA value
// min_data = 0;
// max_temp = 255;
// half_temp = 128;
// max_data = 1;
// // get element and normalize it
// // sum = (sum - min_data) / (max_data - min_data);
// // sum *= max_temp;
// // sum = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
// // sanity
// if (sum < 0 || sum > max_temp){
// console.log("EERRROORR");
// }
// // map to rgb temp code
// if (sum <= half_temp){
// // no red
// red = -2*sum + max_temp;
// // calc blue color
// blue = sum * 2;
// // calc green color
// green = -2*sum + max_temp;
// }
// else {
// // no blue
// blue = max_temp;
// // calc red color
// green = (sum - half_temp) * 2;
// // calc green color
// red = 0;
// }
// newImgData.data[i * (WIDTH*4) + (WIDTH-1)*4 + 0] = red;
// newImgData.data[i * (WIDTH*4) + (WIDTH-1)*4 + 1] = green;
// newImgData.data[i * (WIDTH*4) + (WIDTH-1)*4 + 2] = blue;
// newImgData.data[i * (WIDTH*4) + (WIDTH-1)*4 + 3] = 255;
// }
// draw image data
spectrogrammCtx.putImageData(newImgData, 0, 0);
}
drawGramm();
}
function visualizeWaveform() {
WIDTH = spectrum.width;
HEIGHT = spectrum.height;
ana_spec.fftSize = 2048;
var bufferLength = ana_spec.fftSize;
var dataArray = new Uint8Array(bufferLength);
// reset the
spectrumCtx.clearRect(0, 0, WIDTH, HEIGHT);
var draw = function() {
// not sure if necessary
drawVisual = requestAnimationFrame(draw);
// get current data frame
ana_spec.getByteTimeDomainData(dataArray);
// draw background
spectrumCtx.fillStyle = 'rgb(200, 200, 200)';
spectrumCtx.fillRect(0, 0, WIDTH, HEIGHT);
// set line parameters
spectrumCtx.lineWidth = 2;
spectrumCtx.strokeStyle = 'rgb(0, 0, 0)';
var sliceWidth = WIDTH * 1.0 / bufferLength;
var x = 0;
// draw line
spectrumCtx.beginPath();
for(var i = 0; i < bufferLength; i++) {
var v = dataArray[i] / 128.0;
var y = v * HEIGHT/2;
if(i === 0) {
spectrumCtx.moveTo(x, y);
} else {
spectrumCtx.lineTo(x, y);
}
x += sliceWidth;
}
spectrumCtx.lineTo(spectrum.width, spectrum.height/2);
spectrumCtx.stroke();
};
draw();
}
/*===============
=== data flow ===
===============*/
updateWave();
lfo.connect(filter.frequency);
lfo2.connect(osc.osc_main.frequency);
lfo2.connect(osc.osc_sub.frequency);
osc.connect(filter);
filter.connect(ana_spec);
ana_spec.connect(ana_gramm);
ana_gramm.connect(context.destination);
osc.start();
lfo.start();
lfo2.start();
visualizeWaveform();
visualizeSpectrogramm();
updateWave();