-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth.html
More file actions
201 lines (175 loc) · 6.47 KB
/
synth.html
File metadata and controls
201 lines (175 loc) · 6.47 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Synesthesia Synthesizer</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
color: #fff;
font-family: Arial, sans-serif;
}
canvas {
display: block;
}
.keys {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
justify-content: center;
align-items: center;
}
.key {
width: 50px;
height: 200px;
background-color: #fff;
border: 2px solid #000;
margin: 0 5px;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
font-weight: bold;
color: #000;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="keys">
<div class="key" data-note="C4">C4</div>
<div class="key" data-note="D4">D4</div>
<div class="key" data-note="E4">E4</div>
<div class="key" data-note="F4">F4</div>
<div class="key" data-note="G4">G4</div>
<div class="key" data-note="A4">A4</div>
<div class="key" data-note="B4">B4</div>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const keys = document.querySelectorAll('.key');
const audioContext = new AudioContext();
const analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
const shapes = [];
keys.forEach(key => {
key.addEventListener('mousedown', () => {
const note = key.dataset.note;
playNote(note);
});
key.addEventListener('mouseup', () => {
audioContext.suspend();
});
});
function playNote(note) {
audioContext.resume();
const frequency = getFrequency(note);
const oscillator = audioContext.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.value = frequency;
oscillator.connect(analyser);
analyser.connect(audioContext.destination);
oscillator.start();
const shape = new Shape(frequency);
shapes.push(shape);
}
function getFrequency(note) {
const noteFrequencies = {
'C4': 261.63,
'D4': 293.66,
'E4': 329.63,
'F4': 349.23,
'G4': 392.00,
'A4': 440.00,
'B4': 493.88
};
return noteFrequencies[note];
}
class Shape {
constructor(frequency) {
this.x = canvas.width / 2;
this.y = canvas.height / 2;
this.size = 10 + frequency / 10;
this.color = `hsl(${frequency}, 100%, 50%)`;
this.speed = 2 + frequency / 100;
this.direction = Math.random() * Math.PI * 2;
this.tracers = [];
this.geometry = this.createSacredGeometry();
}
createSacredGeometry() {
const geometry = new Path2D();
const numPoints = 6;
const radius = this.size;
const angle = (Math.PI * 2) / numPoints;
geometry.moveTo(
this.x + radius * Math.cos(0),
this.y + radius * Math.sin(0)
);
for (let i = 1; i <= numPoints; i++) {
const x = this.x + radius * Math.cos(i * angle);
const y = this.y + radius * Math.sin(i * angle);
geometry.lineTo(x, y);
}
geometry.closePath();
return geometry;
}
update() {
this.x += Math.cos(this.direction) * this.speed;
this.y += Math.sin(this.direction) * this.speed;
this.tracers.push({ x: this.x, y: this.y });
if (this.tracers.length > 20) {
this.tracers.shift();
}
}
draw() {
ctx.fillStyle = this.color;
ctx.fill(this.geometry);
ctx.strokeStyle = this.color;
ctx.lineWidth = 2;
ctx.beginPath();
this.tracers.forEach((tracer, index) => {
if (index === 0) {
ctx.moveTo(tracer.x, tracer.y);
} else {
ctx.lineTo(tracer.x, tracer.y);
}
});
ctx.stroke();
}
}
function animate() {
requestAnimationFrame(animate);
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
analyser.getByteFrequencyData(dataArray);
const bass = dataArray[0];
const mid = dataArray[Math.floor(bufferLength / 2)];
const treble = dataArray[bufferLength - 1];
const gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
gradient.addColorStop(0, `rgb(${bass}, 0, 0)`);
gradient.addColorStop(0.5, `rgb(0, ${mid}, 0)`);
gradient.addColorStop(1, `rgb(0, 0, ${treble})`);
ctx.strokeStyle = gradient;
ctx.lineWidth = 5;
ctx.strokeRect(0, 0, canvas.width, canvas.height);
shapes.forEach((shape) => {
shape.update();
shape.draw();
if (shape.x < 0) shape.x = canvas.width;
if (shape.x > canvas.width) shape.x = 0;
if (shape.y < 0) shape.y = canvas.height;
if (shape.y > canvas.height) shape.y = 0;
});
}
animate();
</script>
</body>
</html>