-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frex.js
353 lines (296 loc) · 10.8 KB
/
frex.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
function modulo(n,d) {
return ((n % d) + d) % d;
}
var audioContext;
var lookahead;
var schedulerInterval = 10; // ms
var kybdActive = true;
var keys, rate, sequence, tuning, waveform;
var baseFreq;
var analyser;
var oscilloscope;
var sequencer;
var step=0;
var stepTime;
var lastRate = 0;
var stopped = true;
var highlightedStep;
var pressedKeys = new Set();
var voices = {};
const fields = {k:'keymap',
p:'partials',
s:'scale',
r:'rate',
q:'sequence',
n:'baseNote',
f:'baseFreq'};
Module.onRuntimeInitialized = function() {
document.getElementById('keymap').oninput = changedKeymap;
document.getElementById('partials').oninput = changedPartials;
document.getElementById('scale').oninput = changedTuningString;
document.getElementById('baseNote').oninput = changedTuningString;
document.getElementById('baseFreq').oninput = changedTuningString;
document.getElementById('sequence').oninput = changedSequence;
document.getElementById('rate').oninput = changedRate;
for (el of document.getElementsByClassName('input')) {
el.setAttribute("contenteditable","plaintext-only");
el.setAttribute("spellcheck", "false");
el.style.color = "#0f0";
el.style.background = "#00ff000f";
el.onblur = () => {
window.history.replaceState({}, "", getLink())
kybdActive = true;
};
el.onfocus = () => { kybdActive = false };
}
for (el of document.getElementsByClassName('single-line')) {
el.addEventListener('keydown', (e) => {if (e.keyCode == 13) e.preventDefault();});
el.addEventListener('paste', e => {
let paste = (e.clipboardData || window.clipboardData).getData('text');
document.execCommand("insertText", false, paste.replace(/[\r?\n]+/g,""));
e.preventDefault();
});
}
for (const [key,value] of new URLSearchParams(document.location.search.replace(/^\?|\/$/g,"")))
if (key in fields) document.getElementById(fields[key]).innerHTML = decodeURIComponent(value);
for (numbered of document.getElementsByClassName("numbered")) initNumbers(numbered);
audioContext = new (window.AudioContext || window.webkitAudioContext)();
document.getElementById("start").onclick = start;
start();
};
function getStepAtTime(time) {
return stopped ? 0 : modulo(step + Math.trunc(lastRate * (time - stepTime)), sequence.length);
}
function start() {
// Check if autoplay is not allowed
if (audioContext.state=='suspended') audioContext.resume();
if (audioContext.state=='running') {
analyser = audioContext.createAnalyser();
analyser.fftSize = 32768;
analyser.connect(audioContext.destination);
if (analyser.getFloatTimeDomainData)
oscilloscope = new Float32Array(analyser.fftSize);
else
oscilloscope = new Uint8Array(analyser.fftSize);
lookahead = 2*schedulerInterval/1000;
for (el of document.getElementsByClassName("input")) {
}
document.getElementById("start").style.display = "none";
document.addEventListener('keydown', onkeydown);
document.addEventListener('keyup', onkeyup);
// if(navigator.requestMIDIAccess) {
// navigator.requestMIDIAccess({sysex: false}).then((midiAccess) => {
// midiAccess.onstatechange = () => {populateIO(midiAccess)};
// populateIO(midiAccess);
// });
// }
changedTuningString();
changedKeymap();
changedPartials();
changedSequence();
changedRate();
requestAnimationFrame(draw);
}
}
function htmlToString(html) {
return html.replace("<br>","\n").replace(/\n$/,"");
}
function draw() {
let time = audioContext.currentTime;
let currentStep = getStepAtTime(time);
if (highlightedStep != currentStep) {
if (highlightedStep) highlightedStep.style.color = "";
highlightedStep = document.querySelector('#sequence').parentNode.firstChild.childNodes[currentStep];
if (highlightedStep) highlightedStep.style.color = "white";
}
let transform;
if (analyser.getFloatTimeDomainData) {
analyser.getFloatTimeDomainData(oscilloscope);
transform = x => x;
} else {
analyser.getByteTimeDomainData(oscilloscope);
transform = x => (x-128)/128;
}
if (oscilloscope.length && baseFreq) {
let el = document.getElementById("oscilloscope");
let str = "";
let sr = audioContext.sampleRate;
let period = Math.min(oscilloscope.length/2, sr/baseFreq);
let phase = modulo(time*sr, period);
for (let i=0; i<2*period; i++)
str += (i===0? "M " : "L ") + (i-phase)/period + " " +
transform(oscilloscope[oscilloscope.length-1-i]) + " ";
el.setAttribute('d', str);
}
window.requestAnimationFrame(draw);
}
function stepSequence() {
let time = audioContext.currentTime;
let nextStep, nextStepTime;
lastRate = rate;
while (true) {
if (stopped) { nextStep = step; nextStepTime = time; stopped = false; }
else {
nextStep = modulo(step + (rate<0 ? -1 : 1), sequence.length);
nextStepTime = stepTime + Math.abs(1/rate);
}
if (nextStepTime > time + lookahead) break;
else { stepTime = nextStepTime; step = nextStep; }
for (key in voices)
if (!sequence[step].includes(key))
stopVoice(key, stepTime, 'seq');
for (let i=0; i<sequence[step].length; i++) {
startVoice(sequence[step][i], stepTime, 'seq');
}
}
}
function startVoice(key, time, source) {
if (key in keys) {
let freq = tuning.noteToFreq(keys[key]);
if (freq!=0 && !isNaN(freq)) {
if (key in voices) voices[key].holds.add(source);
else {
let absFreq = Math.abs(freq);
let osc = audioContext.createOscillator();
let gain = audioContext.createGain();
osc.setPeriodicWave(waveform);
osc.frequency.value = freq;
gain.gain.value = 0;
gain.gain.setTargetAtTime(10/absFreq, time, 0.5/absFreq);
osc.connect(gain);
gain.connect(analyser);
osc.start(time);
voices[key] = {osc: osc,
gain: gain,
holds: new Set([source]),
startTime: time};
}
}
}
}
function stopVoice(key, time, source) {
if (key in voices) {
voices[key].holds.delete(source);
if (voices[key].holds.size==0) {
let fade = 0.5/voices[key].osc.frequency.value;
voices[key].gain.gain.setTargetAtTime(0, time, fade);
voices[key].osc.stop(time + 10*fade);
delete voices[key];
}
}
}
function onkeydown(e) {
if (kybdActive && !(e.repeat || e.ctrlKey || e.metaKey))
startVoice(e.key, audioContext.currentTime, 'kybd');
}
function onkeyup(e) {
if (kybdActive && !(e.ctrlKey || e.metaKey))
stopVoice(e.key, audioContext.currentTime, 'kybd');
}
function changedRate() {
rate = parseExpr(document.getElementById('rate').innerHTML);
if (rate==0) {
for (key in voices) stopVoice(key, audioContext.currentTime, 'seq');
sequencer = clearInterval(sequencer);
step = 0;
stopped = true;
}
else if (!sequencer) sequencer = setInterval(stepSequence, schedulerInterval);
}
function changedSequence() {
let elm = document.getElementById('sequence');
sequence = htmlToString(elm.innerHTML).split(/\r?\n/);
step = modulo(step, sequence.length);
if (sequencer) {
for (key in voices)
if (!sequence[step].includes(key))
stopVoice(key, audioContext.currentTime, 'seq');
for (const key of sequence[step])
if (!(key in voices))
startVoice(key, audioContext.currentTime, 'seq');
}
}
function changedKeymap() {
let elm = document.getElementById('keymap');
let lines = htmlToString(keymap.innerHTML).split(/\r?\n/);
keys = {};
for (let i=0; i<lines.length; i++)
for (let j=0; j<lines[i].length; j++)
keys[lines[i][j]] = i+1;
}
function changedTuningString() {
let elm = document.getElementById("scale");
let scaleString = htmlToString(elm.innerHTML);
let baseNoteString = document.getElementById("baseNote").innerHTML;
let baseFreqString = document.getElementById("baseFreq").innerHTML;
baseFreq = Math.abs(parseExpr(baseFreqString));
tuning = new Tuning(baseNoteString, baseFreqString, scaleString);
let time = audioContext.currentTime;
for (key in voices) {
let period = 1/voices[key].osc.frequency.value;
let phase = modulo(time-voices[key].startTime, period) / period;
let newFreq = tuning.noteToFreq(keys[key]);
if (newFreq!==0 && !isNaN(newFreq)) {
let absFreq = Math.abs(newFreq);
voices[key].osc.frequency.setValueAtTime(newFreq, time);
voices[key].gain.gain.setTargetAtTime(10/absFreq,time,0.25/absFreq);
voices[key].startTime = time - phase/newFreq;
}
}
}
function changedPartials() {
let elm = document.getElementById("partials");
let partials = htmlToString(elm.innerHTML).split(/\r?\n/).map(parseExpr).map(Math.round);
let spectrum = new Float32Array(Math.min(4096, 1 + Math.max(...partials.map(Math.abs))));
for (partial of partials) if (partial!=0 && Math.abs(partial)<4096) spectrum[Math.abs(partial)] = 1/partial;
waveform = audioContext.createPeriodicWave(new Float32Array(spectrum.length), spectrum, {disableNormalization: true});
let time = audioContext.currentTime;
for (voice of Object.values(voices)) {
let period = 1/voice.osc.frequency.value;
let startTime = time - modulo(time-voice.startTime, period) + period;
voice.osc.stop(startTime);
let newOsc = audioContext.createOscillator();
newOsc.setPeriodicWave(waveform);
newOsc.frequency.value = voice.osc.frequency.value;
newOsc.connect(voice.gain);
newOsc.start(startTime);
voice.osc = newOsc;
}
}
function getLink() {
let str = "";
for (const [key,value] of Object.entries(fields)) {
str += "&" + key + "=" + encodeURIComponent(document.getElementById(value).innerHTML);
}
return str.replace(/^\&/,"?");
}
function initNumbers(numbered, title) {
let container = document.createElement("div");
let numbers = document.createElement("pre");
numbered.parentNode.insertBefore(container, numbered);
container.appendChild(numbers);
container.appendChild(numbered);
if (title) {
let title = document.createElement("pre");
}
container.style.position = "relative";
numbers.style.position = "absolute";
numbers.style.textAlign = "right";
numbers.style.top = "0";
numbers.style.marginLeft = "1em";
let style = window.getComputedStyle(numbered);
numbers.style.padding = style.getPropertyValue('padding');
updateNumbers(numbered);
numbered.addEventListener("input", () => {updateNumbers(numbered)});
}
function updateNumbers(numbered) {
let numbers = numbered.parentNode.firstChild;
numbers.innerHTML = '<div class="l1">1</div>';
let line = 2;
while (numbers.clientHeight < numbered.clientHeight)
{
numbers.insertAdjacentHTML("beforeend", '<div class="l'+line+'">'+line+"</div>");
line++;
}
numbers.style.left = (-numbers.clientWidth).toString()+"px";
}