Spaces:
Running
Running
File size: 20,448 Bytes
95a2ac2 |
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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spectral Voice Analyzer</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@keyframes pulse {
0% { opacity: 0.4; }
50% { opacity: 1; }
100% { opacity: 0.4; }
}
.pulse {
animation: pulse 1.5s infinite;
}
canvas {
background: linear-gradient(to bottom, #1e3a8a, #0f172a);
}
.bar-container {
height: 250px;
display: flex;
justify-content: center;
align-items: flex-end;
gap: 2px;
padding: 10px;
background: rgba(0,0,0,0.2);
border-radius: 8px;
}
.frequency-bar {
background: linear-gradient(to top, #3b82f6, #93c5fd);
width: 8px;
border-radius: 4px 4px 0 0;
transition: height 0.1s ease-out;
}
</style>
</head>
<body class="bg-gray-900 text-white min-h-screen">
<div class="container mx-auto px-4 py-8">
<header class="mb-8 text-center">
<h1 class="text-4xl font-bold mb-2 bg-gradient-to-r from-blue-400 to-purple-500 text-transparent bg-clip-text">
Spectral Voice Analyzer
</h1>
<p class="text-gray-400">Real-time frequency analysis of your voice</p>
</header>
<div class="flex flex-col lg:flex-row gap-8">
<div class="lg:w-1/3 bg-gray-800 rounded-lg p-6 shadow-lg">
<div class="flex items-center justify-between mb-6">
<h2 class="text-xl font-semibold">Controls</h2>
<div id="status" class="flex items-center">
<span class="h-3 w-3 rounded-full bg-red-500 mr-2"></span>
<span class="text-sm">Inactive</span>
</div>
</div>
<button id="toggleMic" class="w-full py-3 px-4 bg-blue-600 hover:bg-blue-700 rounded-lg font-medium mb-4 flex items-center justify-center">
<i class="fas fa-microphone mr-2"></i>
Start Analyzer
</button>
<div class="bg-gray-700 p-4 rounded-lg mb-4">
<div class="flex justify-between mb-2">
<span class="text-sm text-gray-300">Input Level</span>
<span id="inputLevel" class="text-sm font-mono">0%</span>
</div>
<div class="h-2.5 w-full bg-gray-600 rounded-full">
<div id="levelMeter" class="h-2.5 bg-green-500 rounded-full" style="width: 0%"></div>
</div>
</div>
<div class="grid grid-cols-2 gap-4">
<div class="bg-gray-700 p-4 rounded-lg">
<span class="text-sm text-gray-300 block mb-1">Frequency Range</span>
<select id="frequencyRange" class="w-full bg-gray-800 text-white rounded px-3 py-2 text-sm outline-none">
<option value="full">Full Spectrum</option>
<option value="voice">Voice Range (80-300Hz)</option>
<option value="high">High Frequencies</option>
<option value="low">Low Frequencies</option>
</select>
</div>
<div class="bg-gray-700 p-4 rounded-lg">
<span class="text-sm text-gray-300 block mb-1">Visual Style</span>
<select id="visualStyle" class="w-full bg-gray-800 text-white rounded px-3 py-2 text-sm outline-none">
<option value="bars">Bars</option>
<option value="wave">Waveform</option>
<option value="circle">Circular</option>
</select>
</div>
</div>
<div class="mt-6 p-4 bg-gray-700 rounded-lg">
<h3 class="text-sm font-medium mb-2 text-gray-300">Current Frequency</h3>
<div class="flex items-end">
<span id="dominantFreq" class="text-3xl font-bold mr-2">0</span>
<span class="text-lg">Hz</span>
</div>
</div>
</div>
<div class="lg:w-2/3">
<div class="bg-gray-800 rounded-lg overflow-hidden shadow-lg">
<div class="bg-gradient-to-b from-gray-700 to-gray-800">
<canvas id="visualizer" class="w-full h-64 lg:h-96"></canvas>
</div>
<div class="p-4 flex justify-between items-center border-t border-gray-700">
<div class="text-sm text-gray-400">
<span>FFT Size: </span>
<select id="fftSize" class="bg-gray-700 text-white rounded px-2 py-1 text-sm outline-none">
<option value="64">64</option>
<option value="128">128</option>
<option value="256" selected>256</option>
<option value="512">512</option>
<option value="1024">1024</option>
</select>
</div>
<div id="fpsCounter" class="text-sm text-gray-400">FPS: 0</div>
</div>
</div>
<div class="mt-4">
<h3 class="text-lg font-medium mb-2 text-gray-300">Frequency Distribution</h3>
<div id="barVisualizer" class="bar-container">
<!-- Bars will be generated by JavaScript -->
</div>
</div>
</div>
</div>
<footer class="mt-12 text-center text-sm text-gray-500">
<p>Speak into your microphone to see real-time spectral analysis</p>
</footer>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// Audio context and nodes
let audioContext;
let analyser;
let microphone;
let isActive = false;
let lastTime = 0;
let frames = 0;
let lastFpsUpdate = 0;
let fftSize = 256;
let barCount = 32;
// DOM elements
const toggleMicBtn = document.getElementById('toggleMic');
const statusElement = document.getElementById('status');
const inputLevelElement = document.getElementById('inputLevel');
const levelMeterElement = document.getElementById('levelMeter');
const visualizerCanvas = document.getElementById('visualizer');
const visualizerCtx = visualizerCanvas.getContext('2d');
const barVisualizer = document.getElementById('barVisualizer');
const dominantFreqElement = document.getElementById('dominantFreq');
const fftSizeSelect = document.getElementById('fftSize');
const frequencyRangeSelect = document.getElementById('frequencyRange');
const visualStyleSelect = document.getElementById('visualStyle');
const fpsCounter = document.getElementById('fpsCounter');
// Frequency data arrays
let frequencyData;
let timeDomainData;
// Initialize bars
const bars = [];
for (let i = 0; i < barCount; i++) {
const bar = document.createElement('div');
bar.className = 'frequency-bar';
bar.style.height = '0px';
barVisualizer.appendChild(bar);
bars.push(bar);
}
// Resize canvas
function resizeCanvas() {
visualizerCanvas.width = visualizerCanvas.offsetWidth;
visualizerCanvas.height = visualizerCanvas.offsetHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// FPS counter
function updateFps() {
const now = performance.now();
frames++;
if (now >= lastFpsUpdate + 1000) {
fpsCounter.textContent = `FPS: ${Math.round(frames * 1000 / (now - lastFpsUpdate))}`;
frames = 0;
lastFpsUpdate = now;
}
}
// Visualizer functions
function drawBars() {
const width = visualizerCanvas.width;
const height = visualizerCanvas.height;
const barWidth = width / barCount;
let maxFreq = 0;
visualizerCtx.clearRect(0, 0, width, height);
for (let i = 0; i < barCount; i++) {
const value = frequencyData[i] || 0;
const percent = value / 255;
const barHeight = percent * height;
maxFreq = Math.max(maxFreq, value);
// Set color based on frequency band
const hue = 240 - (i / barCount * 240);
visualizerCtx.fillStyle = `hsl(${hue}, 100%, 60%)`;
// Draw bar
visualizerCtx.fillRect(
i * barWidth,
height - barHeight,
barWidth * 0.8,
barHeight
);
}
// Update the bars in the secondary visualizer
for (let i = 0; i < barCount; i++) {
const value = frequencyData[i] || 0;
const percent = value / 255;
bars[i].style.height = `${percent * 100}%`;
}
// Find dominant frequency
let maxIndex = 0;
let maxValue = 0;
for (let i = 0; i < frequencyData.length; i++) {
if (frequencyData[i] > maxValue) {
maxValue = frequencyData[i];
maxIndex = i;
}
}
// Calculate dominant frequency in Hz
const sampleRate = audioContext.sampleRate;
const dominantFrequency = maxIndex * sampleRate / fftSize;
if (dominantFrequency > 50 && dominantFrequency < 4000) {
dominantFreqElement.textContent = Math.round(dominantFrequency);
}
}
function drawWaveform() {
const width = visualizerCanvas.width;
const height = visualizerCanvas.height;
const centerY = height / 2;
visualizerCtx.clearRect(0, 0, width, height);
visualizerCtx.beginPath();
visualizerCtx.strokeStyle = '#3b82f6';
visualizerCtx.lineWidth = 2;
for (let i = 0; i < timeDomainData.length; i++) {
const x = i / timeDomainData.length * width;
const y = (timeDomainData[i] / 255) * height;
if (i === 0) {
visualizerCtx.moveTo(x, y);
} else {
visualizerCtx.lineTo(x, y);
}
}
visualizerCtx.stroke();
}
function drawCircle() {
const width = visualizerCanvas.width;
const height = visualizerCanvas.height;
const centerX = width / 2;
const centerY = height / 2;
const radius = Math.min(width, height) * 0.4;
visualizerCtx.clearRect(0, 0, width, height);
// Draw frequency data as a circular spectrum
visualizerCtx.beginPath();
for (let i = 0; i < frequencyData.length; i++) {
const angle = (i / frequencyData.length) * Math.PI * 2;
const value = frequencyData[i] / 255;
const pointRadius = radius * (1 + value * 0.5);
const x = centerX + Math.cos(angle) * pointRadius;
const y = centerY + Math.sin(angle) * pointRadius;
if (i === 0) {
visualizerCtx.moveTo(x, y);
} else {
visualizerCtx.lineTo(x, y);
}
}
visualizerCtx.closePath();
visualizerCtx.fillStyle = 'rgba(59, 130, 246, 0.5)';
visualizerCtx.fill();
visualizerCtx.strokeStyle = '#3b82f6';
visualizerCtx.stroke();
// Draw center circle
visualizerCtx.beginPath();
visualizerCtx.arc(centerX, centerY, radius * 0.1, 0, Math.PI * 2);
visualizerCtx.fillStyle = 'rgba(59, 130, 246, 0.7)';
visualizerCtx.fill();
}
// Main visualization loop
function visualize() {
if (!isActive) return;
requestAnimationFrame(visualize);
analyser.getByteFrequencyData(frequencyData);
analyser.getByteTimeDomainData(timeDomainData);
// Calculate input level
let sum = 0;
for (let i = 0; i < timeDomainData.length; i++) {
const value = (timeDomainData[i] - 128) / 128;
sum += value * value;
}
const rms = Math.sqrt(sum / timeDomainData.length);
const level = Math.min(1, rms * 2);
levelMeterElement.style.width = `${level * 100}%`;
inputLevelElement.textContent = `${Math.round(level * 100)}%`;
// Update level meter color
levelMeterElement.className = level > 0.9 ?
'h-2.5 bg-red-500 rounded-full' :
level > 0.7 ?
'h-2.5 bg-yellow-500 rounded-full' :
'h-2.5 bg-green-500 rounded-full';
// Draw based on selected visual style
const style = visualStyleSelect.value;
if (style === 'wave') {
drawWaveform();
} else if (style === 'circle') {
drawCircle();
} else {
drawBars();
}
updateFps();
}
// Toggle microphone
async function toggleMicrophone() {
try {
if (!isActive) {
// Start analysis
audioContext = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioContext.createAnalyser();
analyser.fftSize = fftSize;
frequencyData = new Uint8Array(analyser.frequencyBinCount);
timeDomainData = new Uint8Array(analyser.frequencyBinCount);
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
microphone = audioContext.createMediaStreamSource(stream);
microphone.connect(analyser);
isActive = true;
toggleMicBtn.innerHTML = '<i class="fas fa-stop mr-2"></i> Stop Analyzer';
toggleMicBtn.className = 'w-full py-3 px-4 bg-red-600 hover:bg-red-700 rounded-lg font-medium mb-4 flex items-center justify-center';
statusElement.innerHTML = '<span class="h-3 w-3 rounded-full bg-green-500 mr-2 pulse"></span><span class="text-sm">Active</span>';
visualize();
} else {
// Stop analysis
if (microphone) {
microphone.disconnect();
}
if (audioContext) {
audioContext.close();
}
isActive = false;
toggleMicBtn.innerHTML = '<i class="fas fa-microphone mr-2"></i> Start Analyzer';
toggleMicBtn.className = 'w-full py-3 px-4 bg-blue-600 hover:bg-blue-700 rounded-lg font-medium mb-4 flex items-center justify-center';
statusElement.innerHTML = '<span class="h-3 w-3 rounded-full bg-red-500 mr-2"></span><span class="text-sm">Inactive</span>';
// Clear visualizer
visualizerCtx.clearRect(0, 0, visualizerCanvas.width, visualizerCanvas.height);
bars.forEach(bar => bar.style.height = '0px');
// Reset indicators
inputLevelElement.textContent = '0%';
levelMeterElement.style.width = '0%';
dominantFreqElement.textContent = '0';
fpsCounter.textContent = 'FPS: 0';
}
} catch (error) {
console.error('Error accessing microphone:', error);
alert('Could not access microphone. Please ensure you have granted microphone permissions.');
isActive = false;
toggleMicBtn.innerHTML = '<i class="fas fa-microphone mr-2"></i> Start Analyzer';
toggleMicBtn.className = 'w-full py-3 px-4 bg-blue-600 hover:bg-blue-700 rounded-lg font-medium mb-4 flex items-center justify-center';
statusElement.innerHTML = '<span class="h-3 w-3 rounded-full bg-red-500 mr-2"></span><span class="text-sm">Error</span>';
}
}
// Event listeners
toggleMicBtn.addEventListener('click', toggleMicrophone);
fftSizeSelect.addEventListener('change', () => {
fftSize = parseInt(fftSizeSelect.value);
if (analyser) {
analyser.fftSize = fftSize;
frequencyData = new Uint8Array(analyser.frequencyBinCount);
timeDomainData = new Uint8Array(analyser.frequencyBinCount);
}
});
// Inform users about microphone access
toggleMicBtn.addEventListener('mouseover', () => {
if (!isActive) {
toggleMicBtn.title = 'Click to start. You will be asked for microphone permission.';
}
});
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <a href="https://enzostvs-deepsite.hf.space" style="color: #fff;" target="_blank" >DeepSite</a> <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;"></p></body>
</html> |