-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymph.html
223 lines (195 loc) · 8.3 KB
/
symph.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dreamy Interactive Spectrograph Music Visualizer</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: black;
touch-action: none;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
}
canvas {
display: block;
}
#error-message {
position: absolute;
top: 20px;
left: 20px;
color: #ff0000;
background: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 5px;
z-index: 10;
}
</style>
</head>
<body>
<div id="error-message" style="display: none;"></div>
<canvas id="glCanvas"></canvas>
<script>
const canvas = document.getElementById("glCanvas");
const gl = canvas.getContext("webgl");
const ctx2d = canvas.getContext("2d");
if (!gl) {
alert("Unable to initialize WebGL. Your browser may not support it.");
throw new Error("WebGL not supported");
}
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
const vertexShaderSource = `
attribute vec4 a_position;
void main() {
gl_Position = a_position;
}
`;
const fragmentShaderSource = `
precision highp float;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_audioData;
uniform vec3 u_colorOffset;
uniform vec2 u_touch;
vec3 dreamyGradient(vec2 uv, float timeOffset) {
vec3 color = vec3(0.4 + 0.4 * sin(uv.x * 10.0 + u_audioData * 6.0 + timeOffset),
0.4 + 0.4 * cos(uv.y * 10.0 + u_audioData * 6.0 + timeOffset),
0.5 + 0.4 * sin((uv.x + uv.y) * 8.0 + u_audioData * 6.0 + timeOffset));
return color + u_colorOffset * 0.7;
}
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution.xy;
uv -= 0.5;
uv *= 2.0;
float dist = distance(uv, u_touch);
float ripple = sin(dist * 15.0 - u_time * 3.0) * 0.15;
float bloom = 0.3 / (dist * dist + 0.25);
uv += ripple;
vec3 color = dreamyGradient(uv, u_time * 0.6) * u_audioData;
color += vec3(0.4 + 0.4 * sin(u_time * 0.4 + u_audioData * 2.0),
0.4 + 0.4 * cos(u_time * 0.5 + u_audioData * 3.0),
0.5 + 0.4 * sin(u_time * 0.6 + u_audioData * 1.5));
color += bloom * 0.6;
gl_FragColor = vec4(color, 1.0);
}
`;
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.error(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
throw new Error("Program failed to link");
}
gl.useProgram(program);
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
const positions = [
-1.0, -1.0,
1.0, -1.0,
-1.0, 1.0,
-1.0, 1.0,
1.0, -1.0,
1.0, 1.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
const positionAttributeLocation = gl.getAttribLocation(program, "a_position");
gl.enableVertexAttribArray(positionAttributeLocation);
gl.vertexAttribPointer(positionAttributeLocation, 2, gl.FLOAT, false, 0, 0);
const resolutionUniformLocation = gl.getUniformLocation(program, "u_resolution");
const timeUniformLocation = gl.getUniformLocation(program, "u_time");
const audioDataUniformLocation = gl.getUniformLocation(program, "u_audioData");
const colorOffsetUniformLocation = gl.getUniformLocation(program, "u_colorOffset");
const touchUniformLocation = gl.getUniformLocation(program, "u_touch");
gl.uniform2f(resolutionUniformLocation, canvas.width, canvas.height);
let time = 0.0;
let audioData = 0.0;
let colorOffset = [0.0, 0.0, 0.0];
let touchPoint = [0.0, 0.0];
let analyser;
async function setupAudio() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const audioContext = new AudioContext();
const source = audioContext.createMediaStreamSource(stream);
analyser = audioContext.createAnalyser();
analyser.fftSize = 256;
source.connect(analyser);
getAudioData();
} catch (err) {
showError('Unable to access the microphone. Please allow microphone permissions and reload the page.');
console.error("Error capturing audio: ", err);
}
}
function getAudioData() {
requestAnimationFrame(getAudioData);
if (analyser) {
const dataArray = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(dataArray);
const average = dataArray.reduce((sum, value) => sum + value, 0) / dataArray.length;
audioData = average / 128.0;
updateSpectrograph(dataArray);
}
}
function updateSpectrograph(dataArray) {
ctx2d.clearRect(0, 0, canvas.width, canvas.height);
const gradient = ctx2d.createLinearGradient(0, 0, canvas.width, canvas.height);
gradient.addColorStop(0, '#ff6ec7');
gradient.addColorStop(0.5, '#8e44ad');
gradient.addColorStop(1, '#3498db');
ctx2d.fillStyle = gradient;
const barWidth = (canvas.width / dataArray.length) * 1.5;
let barHeight;
let x = 0;
for (let i = 0; i < dataArray.length; i++) {
barHeight = dataArray[i] / 2;
ctx2d.fillRect(x, canvas.height - barHeight, barWidth, barHeight);
x += barWidth + 1;
}
}
setupAudio();
function render() {
time += 0.02;
gl.uniform1f(timeUniformLocation, time);
gl.uniform1f(audioDataUniformLocation, audioData);
gl.uniform3fv(colorOffsetUniformLocation, colorOffset);
gl.uniform2fv(touchUniformLocation, touchPoint);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 6);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
// Handle touch input
canvas.addEventListener('touchmove', (event) => {
const touch = event.touches[0];
const x = (touch.clientX / canvas.width) * 2.0 - 1.0;
const y = -(touch.clientY / canvas.height) * 2.0 + 1.0;
touchPoint = [x, y];
});
</script>
</body>
</html>