File size: 16,851 Bytes
60beac4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56b2b6e
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Synesthetic Audio Visualizer</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vanta@latest/dist/vanta.globe.min.js"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.js"></script>
    <style>
        #audioCanvas {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
        }
        .file-upload:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
        }
        .synesthesia-mode:hover {
            transform: scale(1.05);
            transition: all 0.3s ease;
        }
    </style>
</head>
<body class="bg-gray-900 text-white min-h-screen flex flex-col">
    <div id="vanta-bg" class="fixed inset-0 opacity-20"></div>
    
    <header class="py-6 px-4 sm:px-6 lg:px-8 text-center">
        <h1 class="text-4xl md:text-5xl font-bold mb-2 bg-clip-text text-transparent bg-gradient-to-r from-purple-400 via-pink-500 to-red-500">
            Synesthetic Audio Visualizer
        </h1>
        <p class="text-lg text-gray-300 max-w-2xl mx-auto">
            Experience music through the lens of synesthesia. Scientifically grounded audio-visual mappings.
        </p>
    </header>

    <main class="flex-grow container mx-auto px-4 py-8 flex flex-col items-center">
        <div class="w-full max-w-2xl bg-gray-800 bg-opacity-70 backdrop-blur-lg rounded-xl shadow-2xl p-6 mb-8">
            <div class="flex flex-col sm:flex-row gap-4 mb-6">
                <label class="file-upload flex-1 cursor-pointer">
                    <input type="file" id="audioUpload" accept=".mp3,.wav" class="hidden">
                    <div class="flex items-center justify-center bg-gray-700 hover:bg-gray-600 rounded-lg p-4 transition-all duration-300">
                        <i data-feather="upload" class="mr-2"></i>
                        <span>Choose Audio File</span>
                    </div>
                </label>
                <button id="playBtn" class="bg-purple-600 hover:bg-purple-700 px-6 py-3 rounded-lg font-medium flex items-center justify-center transition-colors">
                    <i data-feather="play" class="mr-2"></i>
                    Play
                </button>
            </div>

            <div class="mb-6">
                <div class="flex justify-between text-sm text-gray-400 mb-1">
                    <span id="currentTime">0:00</span>
                    <span id="totalTime">0:00</span>
                </div>
                <input type="range" id="progressBar" class="w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer" value="0">
            </div>

            <div class="grid grid-cols-2 sm:grid-cols-3 gap-3">
                <button class="synesthesia-mode bg-gradient-to-br from-blue-500 to-indigo-600 px-4 py-3 rounded-lg font-medium transition-all" data-mode="chromesthesia">
                    Chromesthesia
                </button>
                <button class="synesthesia-mode bg-gradient-to-br from-green-500 to-teal-600 px-4 py-3 rounded-lg font-medium transition-all" data-mode="tactile">
                    Auditory-Tactile
                </button>
                <button class="synesthesia-mode bg-gradient-to-br from-red-500 to-orange-600 px-4 py-3 rounded-lg font-medium transition-all" data-mode="spatial">
                    Spatial Harmony
                </button>
            </div>
        </div>

        <div id="visualizerContainer" class="relative w-full max-w-4xl aspect-square rounded-xl overflow-hidden shadow-2xl">
            <div id="audioCanvas"></div>
            <div class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-30" id="uploadPrompt">
                <div class="text-center p-6">
                    <i data-feather="music" class="w-12 h-12 mx-auto mb-4 text-gray-300"></i>
                    <h3 class="text-xl font-medium mb-2">Upload an audio file</h3>
                    <p class="text-gray-300">Supported formats: MP3, WAV</p>
                </div>
            </div>
        </div>
    </main>

    <footer class="py-6 px-4 text-center text-gray-400 text-sm">
        <p>Based on neurological research into synesthesia and cross-modal perception</p>
    </footer>

    <script>
        feather.replace();
        
        // Initialize Vanta.js background
        VANTA.GLOBE({
            el: "#vanta-bg",
            mouseControls: true,
            touchControls: true,
            gyroControls: false,
            minHeight: 200.00,
            minWidth: 200.00,
            scale: 1.00,
            scaleMobile: 1.00,
            color: 0x8a4fff,
            backgroundColor: 0x09090b,
            size: 0.8
        });

        // Audio visualization setup
        let audioContext, analyser, dataArray, source;
        let isPlaying = false;
        let currentMode = 'chromesthesia';
        let audioElement, gainNode;

        document.getElementById('audioUpload').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                document.getElementById('uploadPrompt').style.display = 'none';
                
                if (audioElement) {
                    audioElement.pause();
                    audioElement = null;
                }
                
                audioElement = new Audio(URL.createObjectURL(file));
                document.getElementById('playBtn').innerHTML = '<i data-feather="play" class="mr-2"></i>Play';
                feather.replace();
                
                // Set up audio context on first interaction
                if (!audioContext) {
                    audioContext = new (window.AudioContext || window.webkitAudioContext)();
                    analyser = audioContext.createAnalyser();
                    analyser.fftSize = 256;
                    gainNode = audioContext.createGain();
                    
                    source = audioContext.createMediaElementSource(audioElement);
                    source.connect(gainNode);
                    gainNode.connect(analyser);
                    analyser.connect(audioContext.destination);
                    
                    dataArray = new Uint8Array(analyser.frequencyBinCount);
                }
                
                // Set up duration display
                audioElement.addEventListener('loadedmetadata', function() {
                    const duration = audioElement.duration;
                    const minutes = Math.floor(duration / 60);
                    const seconds = Math.floor(duration % 60).toString().padStart(2, '0');
                    document.getElementById('totalTime').textContent = `${minutes}:${seconds}`;
                });
                
                // Update progress bar
                audioElement.addEventListener('timeupdate', function() {
                    const currentTime = audioElement.currentTime;
                    const duration = audioElement.duration || 1;
                    const minutes = Math.floor(currentTime / 60);
                    const seconds = Math.floor(currentTime % 60).toString().padStart(2, '0');
                    document.getElementById('currentTime').textContent = `${minutes}:${seconds}`;
                    document.getElementById('progressBar').value = (currentTime / duration) * 100;
                });
                
                // Set up progress bar scrubbing
                document.getElementById('progressBar').addEventListener('input', function() {
                    if (audioElement.duration) {
                        audioElement.currentTime = (this.value / 100) * audioElement.duration;
                    }
                });
                
                // Initialize p5.js visualizer
                new p5(function(p) {
                    let particles = [];
                    
                    p.setup = function() {
                        const container = document.getElementById('visualizerContainer');
                        const canvas = p.createCanvas(container.clientWidth, container.clientHeight);
                        canvas.parent('audioCanvas');
                        
                        // Initialize particles based on mode
                        initParticles();
                    };
                    
                    p.windowResized = function() {
                        const container = document.getElementById('visualizerContainer');
                        p.resizeCanvas(container.clientWidth, container.clientHeight);
                    };
                    
                    function initParticles() {
                        particles = [];
                        const count = currentMode === 'spatial' ? 500 : 200;
                        
                        for (let i = 0; i < count; i++) {
                            particles.push({
                                x: p.random(p.width),
                                y: p.random(p.height),
                                size: p.random(2, 6),
                                speed: p.random(0.5, 2),
                                angle: p.random(p.TWO_PI),
                                color: getRandomColor(),
                                freqIndex: Math.floor(p.random(analyser.frequencyBinCount))
                            });
                        }
                    }
                    
                    function getRandomColor() {
                        if (currentMode === 'chromesthesia') {
                            return p.color(
                                p.random(100, 255),
                                p.random(100, 255),
                                p.random(100, 255),
                                p.random(150, 220)
                            );
                        } else if (currentMode === 'tactile') {
                            return p.color(
                                100 + p.random(100),
                                100 + p.random(100),
                                255,
                                p.random(180, 220)
                            );
                        } else {
                            return p.color(
                                255,
                                100 + p.random(100),
                                100 + p.random(100),
                                p.random(180, 220)
                            );
                        }
                    }
                    
                    p.draw = function() {
                        p.background(0, 0, 0, 25);
                        
                        if (analyser && audioElement && !audioElement.paused) {
                            analyser.getByteFrequencyData(dataArray);
                            
                            // Draw particles based on current mode
                            if (currentMode === 'chromesthesia') {
                                drawChromesthesia();
                            } else if (currentMode === 'tactile') {
                                drawTactile();
                            } else {
                                drawSpatial();
                            }
                        }
                    };
                    
                    function drawChromesthesia() {
                        p.noStroke();
                        const centerX = p.width / 2;
                        const centerY = p.height / 2;
                        
                        particles.forEach(part => {
                            const freqValue = dataArray[part.freqIndex] / 255;
                            
                            // Move particle in circular pattern
                            part.angle += part.speed * 0.01;
                            part.x = centerX + Math.cos(part.angle) * (100 + freqValue * 200);
                            part.y = centerY + Math.sin(part.angle) * (100 + freqValue * 200);
                            
                            // Size and color change with frequency
                            const pulseSize = part.size * (1 + freqValue * 2);
                            part.color.setAlpha(150 + freqValue * 105);
                            
                            p.fill(part.color);
                            p.circle(part.x, part.y, pulseSize);
                        });
                    }
                    
                    function drawTactile() {
                        p.noStroke();
                        const centerY = p.height / 2;
                        
                        particles.forEach(part => {
                            const freqValue = dataArray[part.freqIndex] / 255;
                            
                            // Vertical wave motion
                            part.y = centerY + Math.sin(p.frameCount * 0.02 * part.speed + part.x * 0.01) * 100 * freqValue;
                            
                            // Pulsing effect
                            const pulseSize = part.size * (1 + freqValue * 3);
                            part.color.setAlpha(180 + freqValue * 75);
                            
                            p.fill(part.color);
                            p.ellipse(part.x, part.y, pulseSize, pulseSize * 0.6);
                            
                            // Move horizontally slightly
                            part.x += part.speed * 0.1;
                            if (part.x > p.width) part.x = 0;
                        });
                    }
                    
                    function drawSpatial() {
                        p.noStroke();
                        const centerX = p.width / 2;
                        const centerY = p.height / 2;
                        
                        particles.forEach(part => {
                            const freqValue = dataArray[part.freqIndex] / 255;
                            
                            // 3D effect with perspective
                            const z = freqValue * 5;
                            const scale = p.map(z, 0, 5, 0.2, 1.5);
                            const x = centerX + (part.x - centerX) * scale;
                            const y = centerY + (part.y - centerY) * scale;
                            
                            part.color.setAlpha(100 + freqValue * 155);
                            p.fill(part.color);
                            p.circle(x, y, part.size * scale);
                            
                            // Connect particles that are close
                            p.stroke(part.color);
                            p.strokeWeight(0.5);
                            particles.forEach(other => {
                                if (other !== part && p.dist(x, y, other.x, other.y) < 50 * scale) {
                                    p.line(x, y, other.x, other.y);
                                }
                            });
                        });
                    }
                }, 'audioCanvas');
            }
        });
        
        // Play/pause button
        document.getElementById('playBtn').addEventListener('click', function() {
            if (audioElement) {
                if (audioElement.paused) {
                    audioElement.play();
                    this.innerHTML = '<i data-feather="pause" class="mr-2"></i>Pause';
                    if (audioContext.state === 'suspended') {
                        audioContext.resume();
                    }
                } else {
                    audioElement.pause();
                    this.innerHTML = '<i data-feather="play" class="mr-2"></i>Play';
                }
                feather.replace();
            }
        });
        
        // Synesthesia mode buttons
        document.querySelectorAll('.synesthesia-mode').forEach(button => {
            button.addEventListener('click', function() {
                currentMode = this.dataset.mode;
                document.querySelectorAll('.synesthesia-mode').forEach(btn => {
                    btn.classList.remove('ring-2', 'ring-white');
                });
                this.classList.add('ring-2', 'ring-white');
                
                // Reinitialize visualization (handled in p5 instance)
            });
        });
    </script>
</body>
</html>