-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
377 lines (337 loc) · 13.3 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
<!DOCTYPE html>
<html>
<head>
<title>Musical Oscillator</title>
<style>
body {
background-color: black;
font-family: monospace;
font-size: 16px;
color: green;
margin: 0;
padding: 2em;
color:#000;
}
canvas {
background-color: black;
}
button {
background-color: green;
color: black;
border: none;
padding: 10px;
margin: 5px;
border-radius: 5px;
font-size: 16px;
font-weight: bold;
}
button.active {
background-color: black;
color: green;
}
#visualizer {
display: block;
margin: 15px auto;
border:1px solid green;
border-radius: 150px;
}
#buttons {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
#buttons img {
width: 40px;
margin-right: 10px;
}
header { width:800px; margin:0 auto; text-align:center; color:green }
.matrix-background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('https://i.imgur.com/DuV7Iyf.gif');
background-repeat: repeat;
animation: matrix 20s linear infinite;
z-index: -1;
}
footer { text-align: center; padding-top:2em; color:green; }
footer, footer a, footer a:visited { color:green; }
@keyframes matrix {
0% {
background-position: 0px 0px;
}
100% {
background-position: 0px -4000px;
}
}
#piano-keys {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
width: 800px;
margin: 15px auto;
}
#piano-keys .piano-key {
width: 100px;
height: 200px;
background-color: green;
border: 2px solid black;
margin: 5px;
border-radius: 5px;
text-align: center;
color: #000;
}
#piano-keys .piano-key:hover { background-color: darkgreen; }
#piano-keys .piano-key.black {
width: 20px;
height: 100px;
background-color: black;
margin: 5px;
border-radius: 5px;
border:1px solid black
}
#piano-keys .piano-key.black:nth-child(5n + 1) {
margin-left: 0;
}
#piano-keys .piano-key.black:nth-child(5n + 5) {
margin-right: 0;
}
#piano-keys .piano-key.black:nth-child(5n + 2),
#piano-keys .piano-key.black:nth-child(5n + 3) {
margin-left: -10px;
}
#piano-keys .piano-key.black:nth-child(5n + 4),
#piano-keys .piano-key.black:nth-child(5n + 5) {
margin-left: -20px;
}
.in-key { width:100px; height:10px; background:#000; }
</style>
</head>
<body>
<header>
<h1>Musical Oscillator</h1>
</header>
<canvas id="visualizer" width="800" height="500"></canvas>
<div id="buttons"></div>
<div id="piano-keys"></div>
<footer>
<p>Click to start ^</p>
Source: <a href="https://github.com/rochal/oscillator">https://github.com/rochal/oscillator</a></footer>
<script>
const canvas = document.getElementById('visualizer');
const canvasContext = canvas.getContext('2d');
canvasContext.strokeStyle = 'green';
let started = false;
let audioContext, analyser, oscillator, gainNode, bufferLength, dataArray;
const width = 800, height = 500;
function Initialise(type){
audioContext = new AudioContext();
analyser = audioContext.createAnalyser();
oscillator = audioContext.createOscillator();
gainNode = audioContext.createGain();
bufferLength = analyser.frequencyBinCount;
dataArray = new Uint8Array(bufferLength);
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
//oscillator.connect(canvasContext.destination);
oscillator.type = type;
oscillator.frequency.value = 800;
gainNode.gain.value = 1;
gainNode.gain.setValueAtTime(1, audioContext.currentTime);
}
document.getElementById('piano-keys').addEventListener('click', () => {
if (!started){
Initialise("sine");
audioContext.resume().then(() => {
oscillator.start();
oscillator.frequency.value = 100;
draw();
started = true;
});
}
});
document.getElementById('visualizer').addEventListener('click', () => {
if (!started){
Initialise("sine");
audioContext.resume().then(() => {
oscillator.start();
oscillator.frequency.value = 100;
draw();
started = true;
});
}
});
document.addEventListener('mouseleave', () => {
if (started) {
gainNode.gain.setValueAtTime(0, audioContext.currentTime);
}
});
document.addEventListener('mouseenter', () => {
if (started) {
gainNode.gain.setValueAtTime(1, audioContext.currentTime);
}
});
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden' && started) {
oscillator.stop();
audioContext.close();
started = false;
}
});
const oscillatorFunctions = {
'sine': (oscillator, canvas, i, audioContext) => {
return canvas.height / 2 + oscillator.frequency.value / 1000 * canvas.height / 2 * Math.sin(2 * Math.PI * oscillator.frequency.value * i / audioContext.sampleRate);
},
'sawtooth': (oscillator, canvas, i, audioContext) => {
return canvas.height / 2 + oscillator.frequency.value / 1000 * canvas.height / 2 * (2 * ((i * audioContext.sampleRate / oscillator.frequency.value) % audioContext.sampleRate) / audioContext.sampleRate - 1);
},
'square': (oscillator, canvas, i, audioContext) => {
return canvas.height / 2 + oscillator.frequency.value / 1000 * canvas.height / 2 * (Math.sign(Math.sin(2 * Math.PI * oscillator.frequency.value * i / audioContext.sampleRate) - 0.5) + 1) / 2;
},
'triangle': (oscillator, canvas, i, audioContext) => {
return canvas.height / 2 + oscillator.frequency.value / 1000 * canvas.height / 2 * (1 - 4 * Math.abs(Math.round((oscillator.frequency.value * i / audioContext.sampleRate) - 0.25) - (oscillator.frequency.value * i / audioContext.sampleRate) + 0.25));
},
'default': (oscillator, canvas, i, audioContext) => {
return canvas.height / 2;
}
};
canvas.addEventListener('mousemove', handleInput);
canvas.addEventListener('touchmove', handleInput);
function handleInput(event) {
event.preventDefault();
let mouseX, mouseY;
if (event.type === 'touchmove') {
mouseX = event.touches[0].clientX;
mouseY = event.touches[0].clientY;
} else {
mouseX = event.clientX;
mouseY = event.clientY;
}
const frequency = (mouseX / window.innerWidth) * 1000 + 100;
const volume = mouseY / window.innerHeight;
if (started) {
oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
gainNode.gain.setValueAtTime(volume, audioContext.currentTime);
}
}
// draw oscilator wave
function draw() {
requestAnimationFrame(draw);
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
canvasContext.beginPath();
canvasContext.moveTo(0, canvas.height / 2);
for (let i = 0; i < canvas.width; i++) {
const x = i;
const oscillatorType = oscillator.type || 'default';
const oscillatorFunction = oscillatorFunctions[oscillatorType];
const y = oscillatorFunction(oscillator, canvas, i, audioContext);
canvasContext.lineTo(x, y);
}
// draw stroke on the canvas
canvasContext.stroke();
}
// create array of oscillator types
const oscillatorTypes = ['sine', 'square', 'sawtooth', 'triangle'];
// create buttons for each oscillator type
const buttonContainer = document.querySelector('#buttons');
oscillatorTypes.forEach((type, index) => {
const button = document.createElement('button');
button.textContent = type;
button.addEventListener('click', () => {
setOscillatorType(type);
updateButtonStyles(button);
});
// Add the "active" class to the first button
if (index === 0) {
button.classList.add("active");
}
buttonContainer.appendChild(button);
});
let currentFrequency = 800; // set initial frequency value
let currentAmplitude = 0; // set initial amplitude value
function setOscillatorType(type) {
// store current frequency and amplitude values
currentFrequency = oscillator.frequency.value;
currentAmplitude = gainNode.gain.value;
oscillator.type = type;
// set back the current frequency and amplitude values
oscillator.frequency.setValueAtTime(currentFrequency, audioContext.currentTime);
gainNode.gain.setValueAtTime(currentAmplitude, audioContext.currentTime);
console.log("Set oscillator type", oscillator.type);
}
// function to update button styles
function updateButtonStyles(selectedButton) {
const buttons = document.querySelectorAll('button');
buttons.forEach(button => {
if (button === selectedButton) {
button.classList.add('active');
} else {
button.classList.remove('active');
}
});
}
// construct html for piano keys
const pianoKeys = document.querySelector('#piano-keys');
const pianoKeysArray = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
const pianoKeysOctaves = [4];
const pianoKeysWhite = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
const pianoKeysBlack = ['C#', 'D#', 'F#', 'G#', 'A#'];
const pianoKeysBlackOffset = [1, 3, 6, 8, 10];
const pianoKeysWhiteOffset = [0, 2, 4, 5, 7, 9, 11];
function createPianoKey(key, octave, offset, isBlack) {
const pianoKey = document.createElement('div');
pianoKey.classList.add('piano-key');
if (isBlack) {
pianoKey.classList.add('black');
} else {
pianoKey.classList.add('white');
}
const frequency = getFrequency(key, octave);
pianoKey.setAttribute('data-frequency', frequency);
pianoKey.setAttribute('data-key', key);
pianoKey.setAttribute('data-octave', octave);
pianoKey.setAttribute('data-offset', offset);
pianoKey.setAttribute('data-is-black', isBlack);
pianoKey.addEventListener('mousemove', e => {
const rect = pianoKey.getBoundingClientRect();
const height = rect.height;
const mouseY = e.clientY - rect.top;
const ratio = mouseY / height;
pianoKey.firstChild.style.height = `${ratio * 100}%`;
oscillator.frequency.value = pianoKey.dataset.frequency * ratio * -2;
});
pianoKey.addEventListener('touchmove', e => {
const rect = pianoKey.getBoundingClientRect();
const height = rect.height;
const touchY = e.touches[0].clientY - rect.top;
const ratio = touchY / height;
pianoKey.firstChild.style.height = `${ratio * 100}%`;
oscillator.frequency.value = pianoKey.dataset.frequency * ratio * -2;
});
return pianoKey;
}
// loop through octaves
pianoKeysOctaves.forEach(octave => {
// loop through white keys
pianoKeysWhite.forEach((key, index) => {
const offset = pianoKeysWhiteOffset[index];
const isBlack = false;
const pianoKey = createPianoKey(key, octave, offset, isBlack);
const insideKey = document.createElement('div');
insideKey.classList.add('in-key');
pianoKey.appendChild(insideKey)
pianoKeys.appendChild(pianoKey);
});
});
function getFrequency(key, octave) {
const keyOffset = pianoKeysArray.indexOf(key);
const noteNumber = (octave - 4) * 12 + keyOffset;
return 440 * Math.pow(2, noteNumber / 12);
}
</script>
</body>
</html>