synesthesia / index.html
simplyc6's picture
Create a web-based audio visualizer that generates immersive, procedurally created visuals based on uploaded audio files. The system should leverage medically backed data on synesthesia, auditory perception, and cross-modal sensory integration to simulate how people with synesthesia may “see” music.
60beac4 verified
Raw History Blame Contribute Delete
16.9 kB
<!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>