forked from omnikrll/convex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convex.js
415 lines (369 loc) · 10.8 KB
/
convex.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
// context defined on window load
var context;
//do all buffers in one list.
var buffers = [];
//sample select dropdowns
var carrierMenu = document.getElementById('carrierMenu');
var modulatorMenu = document.getElementById('modulatorMenu');
//samples to be loaded
var samples = [
{
'name': 'Boy Voice',
'path': '../convex/sounds/bruno.wav'
},
{
'name': 'Fripp Guitar',
'path': '../convex/sounds/frippguitar.wav'
},
{
'name': 'Teapot',
'path': '../convex/sounds/teapot.wav'
},
{
'name': 'Cello',
'path': '../convex/sounds/cello.wav'
},
{
'name': 'Tape Choir 1',
'path': '../convex/sounds/tape-choir.wav'
},
{
'name': 'Tape Choir 2',
'path': '../convex/sounds/tape-choir-2.wav'
},
{
'name': 'Cheapo Synth',
'path': '../convex/sounds/cheapo-synth.wav'
},
{
'name': 'Processed Image Wave',
'path': '../convex/sounds/andrews-img.wav'
},
{
'name': 'Tape Flute',
'path': '../convex/sounds/tape-flute.wav'
},
{
'name': 'Tape Female Voice',
'path': '../convex/sounds/tape-femvoice.wav'
},
{
'name': 'Brain Massage',
'path': '../convex/sounds/brain-massage.wav'
},
{
'name': 'Dot Matrix Printer',
'path': '../convex/sounds/printer.wav'
},
{
'name': 'Transporter Beam',
'path': '../convex/sounds/transporter.wav'
},
{
'name': 'Tricorder Glitch',
'path': '../convex/sounds/denybeep.wav'
},
{
'name': 'Van der Pol (Driven)',
'path': '../convex/sounds/vanderpoldriven.wav'
},
{
'name': 'Van der Pol (Sweep)',
'path': '../convex/sounds/vanderpolsweep.wav'
},
{
'name': 'Dishwasher',
'path': '../convex/sounds/dishwasher.wav'
},
{
'name': 'Circuit Bent Noise 1',
'path': '../convex/sounds/circuitbend-1.wav'
},
{
'name': 'Circuit Bent Noise 2',
'path': '../convex/sounds/circuitbend-2.wav'
},
{
'name': 'Circuit Bent Noise 3',
'path': '../convex/sounds/circuitbend-3.wav'
},
{
'name': 'Circuit Bent Noise 4',
'path': '../convex/sounds/circuitbend-4.wav'
},
{
'name': 'Ray Gun 1',
'path': '../convex/sounds/raygun1.wav'
},
{
'name': 'Ray Gun 2',
'path': '../convex/sounds/raygun2.wav'
},
{
'name': 'Ray Gun 3',
'path': '../convex/sounds/raygun3.wav'
},
{
'name': 'Ray Gun 4',
'path': '../convex/sounds/raygun4.wav'
}
];
// define uncasted matrix node names:
var carrier; // bufferSource for sample playback
var carrierGain; // gain stage for bufferSource
var carrierIndex; // pointer to source buffer
var modIndex; // pointer to convolver buffer
var convolver; // convolver node
var filter; // filter node
var compressor; // compressor node
var aaFilter; // dedicated lowpass sitting at nyquist frequency (not a true anti-aliasing filter)
var masterVolume; // gain stage at final output
var semitoneRatio = Math.pow(2, 1/12); // multiplier for chromatic scaling
var root = 69; // root key is A440 by default
var rampMin = 0.00001; // ensure fine scaling for ramping functions
var rampMax = 1 - rampMin;
//==== INIT ====//
window.addEventListener('load', init, false);
function init() {
try {
window.AudioContext = window.AudioContext||window.webkitAudioContext;
context = new AudioContext();
// alert('Please wait for ' + samples.length + ' .wav samples to load.');
setupEnv(samples, function() {
if (buffers.length == samples.length) {
document.getElementById("loading").remove();
createMatrix(buffers);
}
});
} catch(e) {
alert("Whoops! Something's wrong-- " + e.name + ": " + e.message + "\nAre you viewing this page in Chrome or Safari? Is your browser up to date?");
}
}
//==== SETUP ENVIRONMENT ====//
function setupEnv(samples, callback) {
var last_sample = samples.length;
for (i = 0; i < last_sample; i++) {
loadSounds(samples[i], function(buffer, option) {
var clone = option.cloneNode(true);
buffers.push(buffer);
carrierMenu.add(option, null);
modulatorMenu.add(clone, null);
if (i === last_sample) {
if (typeof callback === 'function') {
return callback();
}
}
});
}
}
//==== LOAD SOUNDS ====/
function loadSounds(sampleInfo, callback) {
var option = document.createElement('option');
option.text = sampleInfo.name;
var request = new XMLHttpRequest();
request.open('GET', sampleInfo.path, true);
request.responseType = 'arraybuffer';
request.onload = function() {
context.decodeAudioData(request.response, function(buffer) {
if (typeof callback === 'function') {
return callback(buffer, option);
}
});
}
request.error = function(e) {
console.log(e);
}
request.send();
}
//==== CREATE MATRIX ====//
function createMatrix(buffers) {
console.log('you are now entering the matrix...');
// bind keyboard event handlers
var keys = document.getElementsByClassName('key');
var numKeys = keys.length;
for (var i = 0; i < numKeys; i++) {
keys[i].addEventListener('mousedown', playSound, false);
keys[i].addEventListener('mouseup', stopSound, false);
}
carrierIndex = 0;
modIndex = 0;
// cast nodes
carrier = context.createBufferSource();
carrierGain = context.createGain();
convolver = context.createConvolver();
filter = context.createBiquadFilter();
aaFilter = context.createBiquadFilter();
compressor = context.createDynamicsCompressor();
masterVolume = context.createGain();
// set up carrier node
carrier.buffer = buffers[carrierIndex];
carrier.loop = true;
carrier.isPlaying = false;
carrier.connect(aaFilter);
aaFilter.type = 'lowpass';
aaFilter.frequency.value = 22050;
aaFilter.connect(carrierGain);
// set up convolver node
convolver.buffer = buffers[modIndex];
convolver.connect(filter);
// connect sourceGain to filter
carrierGain.gain.value = rampMax;
carrierGain.connect(filter);
// set up filter
filter.type = 'lowpass';
filter.frequency.value = 22050;
filter.connect(compressor);
// compressor => masterVolume => output
compressor.connect(masterVolume);
masterVolume.connect(context.destination);
}
//==== EVENT HANDLERS ====//
// set pointer to playback buffer - buffer itself changed in midiToPitch()
function toggleCarrier(element) {
carrierIndex = element.selectedIndex;
}
// set convolver sample
function toggleConvolver(element) {
var i = element.selectedIndex - 1;
carrierGain.disconnect(0);
if (element.selectedIndex > 0) {
convolver.buffer = buffers[i];
carrierGain.connect(convolver);
} else {
carrierGain.connect(filter);
}
}
// change filter frequency
function filterFrequency(element) {
// Clamp the frequency between the minimum value (40 Hz) and half of the sampling rate.
var minValue = 40;
var maxValue = context.sampleRate / 2;
// Logarithm (base 2) to compute how many octaves fall in the range.
var numberOfOctaves = Math.log(maxValue / minValue) / Math.LN2;
// Compute a multiplier from 0 to 1 based on an exponential scale.
var multiplier = Math.pow(2, numberOfOctaves * (element.value - 1.0));
// Get back to the frequency value between min and max.
filter.frequency.value = maxValue * multiplier;
}
// change filter Q
function filterQ(element) {
filter.Q.value = element.value * 30;
}
// change filter type
function filterType(element) {
filter.type = element.value;
}
// master volume fader
function masterVolumeLvl(element) {
var fraction = parseInt(element.value) / parseInt(element.max);
// Let's use an x*x curve (x-squared) since simple linear (x) does not sound as good.
masterVolume.gain.value = fraction * fraction;
};
function playSound(note) {
if (carrier.isPlaying) stopSound();
carrier.buffer = buffers[carrierIndex];
if (typeof note == 'number') {
carrier.playbackRate.value = midiToPitch(note);
} else {
carrier.playbackRate.value = noteToMidi(note[0], note[1]);
}
carrier.start(0);
carrier.isPlaying = true;
}
function fadeIn(note, time) {
time = (time === undefined) ? 0 : parseInt(document.getElementById("fadeInLevel").value);
var level = masterVolume.gain.value = 0;
playSound(note);
var timer;
// var level = masterVolume.gain.value;
var change = 1 / (time/10);
if (level == 1) return;
timer = setInterval(function() {
if (level >= 1) {
clearInterval(timer);
return;
}
level += change;
if (level >= 1) level = 1;
masterVolume.gain.value = Math.round(level * 10) / 10;
});
}
function fadeOut(time) {
time = (time === undefined) ? 0 : parseInt(document.getElementById("fadeOutLevel").value);
var timer;
var level = masterVolume.gain.value;
var change = level / (time/10);
if (level == 0) return;
timer = setInterval(function() {
if (level <= 0) {
clearInterval(timer);
stopSound();
return;
}
level -= change;
if (level <= 0) level = 0;
masterVolume.gain.value = Math.round(level * 10) / 10;
});
}
function stopSound() {
carrier.isPlaying = false;
carrier.stop(0);
carrier = context.createBufferSource();
carrier.loop = true;
carrier.connect(carrierGain);
}
// set carrier buffer, compute correct pitch, and initiate playback
function midiToPitch(note) {
var semitone = parseInt(note) - root;
return Math.pow(semitoneRatio, semitone);
// ^ for an oscillator, multiply by 440 to do midi-to-frequency
}
// int: scale degree (0 - 11), int: octave (-1 - 9)
function noteToMidi(note, octave) {
midiToPitch((note + 60) + ((octave-4) * 12));
}
var pressedKey = -1;
var octave = 0;
window.addEventListener('keydown', function(e) {
asciiHandler(e);
}, true);
var noteKeys = [65, 87, 83, 69, 68, 70, 84, 71, 89, 72, 85, 74, 75];
var sysKeys = [16, 17, 18, 91] // exclude keys pressed when holding shift, ctrl, opt, or command keys
var prevCode;
function asciiHandler(e) {
document.getElementById('carrierMenu').blur();
document.getElementById('modulatorMenu').blur();
document.getElementById('filterTypePicker').blur();
var code = e.keyCode || e.which;
var oldKey = document.getElementsByClassName('pressed');
console.log(code, prevCode);
if (noteKeys.indexOf(code) != -1 && sysKeys.indexOf(prevCode) == -1) {
if (oldKey.length) {
oldKey[0].classList.remove('pressed');
}
thisKey = document.getElementById('ascii_' + code);
if (pressedKey != code) {
pressedKey = code;
var note = 69 + noteKeys.indexOf(code) + octave;
fadeIn(note, 800)
thisKey.classList.add('pressed');
} else if (pressedKey == code) {
pressedKey = -1;
fadeOut(800);
}
} else if (code == 188 || code == 190) {
var inc = (code == 188) ? -12 : 12;
var dir = (inc == -12) ? 'down' : 'up';
octave += inc;
document.getElementById('octave-val').innerHTML = octave/12 + 4;
} else if (code == 32) {
if (pressedKey != -1) {
if (oldKey.length) {
oldKey[0].classList.remove('pressed');
}
pressedKey = -1;
fadeOut(800);
}
}
prevCode = code;
}