-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
345 lines (263 loc) · 7.87 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
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<title>spirolab</title>
<link rel="stylesheet" href="fork.css">
<link rel="stylesheet" href="kbd.css">
<link rel="stylesheet" href="style.css">
<style>
.fork-me {
background-color: rgb(0, 255, 149);
}
body { margin:0; padding:0; } /* to remove the top and left whitespace */
html, body { width:100%; height:100%; } /* just to be sure these are full screen*/
canvas {
position: fixed;
display: block; /* To remove the scrollbars */
z-index: -10;
}
body {
color:white;
background-color: black;
}
h1 {
margin: 0;
}
#controls {
z-index: 3;
cursor: default;
}
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #424242;
text-align: center;
padding: 8px;
}
</style>
</head>
<body>
<canvas id="oscilloscope"></canvas>
<div class="fork-me-wrapper">
<div class="fork-me">
<a class="fork-me-link" href="https://github.com/nolanhergert/spirolab">
<span class="fork-me-text">Fork Me On GitHub</span>
</a>
</div>
</div>
<div id="controls">
<h1>spirolab!</h1>
<table id="keyboard_shortcuts">
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<tr>
<td><kbd id="Shift">Shift </kbd></td>
<td>Scrub Horizontally</td>
</tr>
<tr>
<td><kbd id="Control">Ctrl</kbd></td>
<td>Scrub Vertically</td>
</tr>
<tr>
<td><kbd id="Control">Ctrl</kbd> + <kbd id="Shift">Shift </kbd></td>
<td>Scrub Diagonally!</td>
</tr>
<tr>
<td><kbd id=" ">Spacebar</kbd></td>
<td id="mouse_control_state">Toggle mouse control</td>
</tr>
</table>
<p></p>
<button class="button-46" role="button" onclick="startAudio();" id="start_button">Start!</button>
<p id="freq1"></p>
<p id="freq2"></p>
<p id="freq3"></p>
</div>
<script>
// create web audio api context
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const canvas = document.getElementById('oscilloscope');
const context = canvas.getContext('2d');
context.globalCompositeOperation = 'destination-over'
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
class Oscillator {
constructor() {
this.oscillator = audioCtx.createOscillator();
this.oscillator.type = 'sine';
this.oscillator.frequency.setValueAtTime(400, audioCtx.currentTime); // value in hertz
this.gainNode = audioCtx.createGain();
this.gainNode.gain.value = 0.3;
this.analyzer = audioCtx.createAnalyser();
this.oscillator.connect(this.gainNode);
this.gainNode.connect(this.analyzer);
this.analyzer.connect(audioCtx.destination);
}
}
let osc1 = new Oscillator();
let osc2 = new Oscillator();
let osc3 = new Oscillator();
function startAudio() {
// Hide Start button
document.getElementById('start_button').style.display = 'none';
osc1.oscillator.start();
osc2.oscillator.start();
osc3.oscillator.start();
// Start the draw events too
//updateWaveform();
drawOscilloscope();
}
listenToMouse = true;
scrubHorizontal = false;
scrubVertical = false;
diagonalSlope = 1;
function mouseMove(e) {
if (!listenToMouse) return;
if (scrubHorizontal == true && scrubVertical == true) {
// Maintain constant slope from origin, save off value for precision
if (diagonalSlope == 1) {
diagonalSlope = e.clientY / e.clientX;
}
osc2.oscillator.frequency.value = e.clientX / 2;
osc3.oscillator.frequency.value = e.clientX * diagonalSlope / 2;
return;
}
// Reset diagonal slope in any other scenario
diagonalSlope = 1;
if (scrubHorizontal == true) {
osc2.oscillator.frequency.value = e.clientX/2;
} else if (scrubVertical == true) {
osc3.oscillator.frequency.value = e.clientY/2;
} else {
osc2.oscillator.frequency.value = e.clientX/2;
osc3.oscillator.frequency.value = e.clientY/2;
}
}
function mapWaveformToXY(waveformT, waveformTMinus1, gain) {
let x,y,dx,offset, x_draw, y_draw = 0;
// If all we have from the oscillator is waveform[i] = x = sin(t), then
// to get y = cos(t) we need to do some math!
// y = cos(t)
// y = sin(pi/2 + t)
// x = sin(t)
// asin(x) = t. Substitute t in y equation.
// y = sin(pi/2 + asin(x))
// This works, except the range of asin(x) is limited. The offset (pi/2) needs to be flipped
// when the slope of x is negative.
dx = (waveformT - waveformTMinus1)/gain;
if (dx >= 0) {
offset = -Math.PI/2;
} else {
offset = Math.PI/2;
}
x = waveformT / gain;
y = Math.sin(offset + Math.asin(x));
return [x,y];
}
window.addEventListener('mousemove', mouseMove);
window.addEventListener("keydown", function (event) {
//console.log(event.key);
//document.getElementById(event.key).className = 'kbd-down';
switch (event.key) {
case " ":
// Set global
listenToMouse = !listenToMouse;
break;
case "Control":
scrubVertical = true;
break;
case "Shift":
scrubHorizontal = true;
break;
}
});
// Handle keyboard events
window.addEventListener("keyup", function (event) {
if (event == undefined) {
return;
}
//console.log(event.key);
//document.getElementById(event.key).className = 'kbd-up';
switch (event.key) {
case "Control":
scrubVertical = false;
break;
case "Shift":
scrubHorizontal = false;
break;
}
/*
switch (event.key) {
case "ArrowDown":
console.
// code for "down arrow" key press.
break;
case "ArrowUp":
// code for "up arrow" key press.
break;
case "ArrowLeft":
// code for "left arrow" key press.
break;
case "ArrowRight":
// code for "right arrow" key press.
break;
default:
return; // Quit when this doesn't handle the key event.
}
*/
// Cancel the default action to avoid it being handled twice
//event.preventDefault();
});
// the last option dispatches the event to the listener first,
// then dispatches event to window
function drawOscilloscope() {
requestAnimationFrame(drawOscilloscope);
context.clearRect(0, 0, canvas.width, canvas.height);
const centerX = osc2.oscillator.frequency.value*2;
const centerY = osc3.oscillator.frequency.value*2;
const radius = 5;
// Draw current frequency position
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'purple';
context.fill();
context.lineWidth = 5;
context.strokeStyle = '#003300';
context.stroke();
context.beginPath();
let waveform1 = new Float32Array(osc1.analyzer.frequencyBinCount);
let waveform2 = new Float32Array(osc2.analyzer.frequencyBinCount);
let waveform3 = new Float32Array(osc3.analyzer.frequencyBinCount);
osc1.analyzer.getFloatTimeDomainData(waveform1);
osc2.analyzer.getFloatTimeDomainData(waveform2);
osc3.analyzer.getFloatTimeDomainData(waveform3);
for(let i = 1; i < waveform1.length; i++) {
const [x1,y1] = mapWaveformToXY(waveform1[i], waveform1[i-1], osc1.gainNode.gain.value);
const [x2,y2] = mapWaveformToXY(waveform2[i], waveform2[i-1], osc2.gainNode.gain.value);
const [x3,y3] = mapWaveformToXY(waveform3[i], waveform3[i-1], osc3.gainNode.gain.value);
// Combine the oscillator plots to get interesting waveforms
const x = x1+x2+x3;
const y = y1+y2+y3;
// Scale x and y to graph them
const x_draw = 400 + x * Math.min(canvas.width, canvas.height) / 8;
const y_draw = 400 + y * Math.min(canvas.width, canvas.height) / 8;
if(i == 0) {
context.moveTo(x_draw, y_draw);
} else {
context.lineTo(x_draw, y_draw);
}
}
context.strokeStyle = 'rgb(0, 255, 149)';
context.lineWidth = 4;
context.stroke();
document.getElementById("freq1").innerHTML = "Freq 1 Hz: " + osc1.oscillator.frequency.value;
document.getElementById("freq2").innerHTML = "Freq 2 Hz: " + osc2.oscillator.frequency.value;
document.getElementById("freq3").innerHTML = "Freq 3 Hz: " + osc3.oscillator.frequency.value;
}
</script>
</body>
</html>