File size: 40,286 Bytes
4d0a5da | 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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Audio Reactive Visualizer</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/p5@1.5.0/lib/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/RecordRTC/5.6.2/RecordRTC.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.custom-scrollbar::-webkit-scrollbar {
width: 8px;
height: 8px;
}
.custom-scrollbar::-webkit-scrollbar-track {
background: #1e293b;
}
.custom-scrollbar::-webkit-scrollbar-thumb {
background: #475569;
border-radius: 4px;
}
.custom-scrollbar::-webkit-scrollbar-thumb:hover {
background: #64748b;
}
.gradient-bg {
background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
}
.glow {
box-shadow: 0 0 15px rgba(59, 130, 246, 0.5);
}
.glow:hover {
box-shadow: 0 0 20px rgba(59, 130, 246, 0.8);
}
canvas {
display: block;
border-radius: 8px;
}
.pattern-preview {
width: 100%;
height: 80px;
border-radius: 6px;
cursor: pointer;
transition: all 0.2s;
}
.pattern-preview:hover {
transform: scale(1.02);
}
.slider-thumb::-webkit-slider-thumb {
-webkit-appearance: none;
width: 18px;
height: 18px;
border-radius: 50%;
background: #3b82f6;
cursor: pointer;
}
.slider-thumb:focus::-webkit-slider-thumb {
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.3);
}
</style>
</head>
<body class="gradient-bg text-slate-100 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-clip-text text-transparent bg-gradient-to-r from-blue-400 to-purple-500">Audio Reactive Visualizer</h1>
<p class="text-slate-300 max-w-2xl mx-auto">Create stunning audio-reactive visualizations with customizable patterns and real-time audio analysis. Import your music and export your creations.</p>
</header>
<div class="grid grid-cols-1 lg:grid-cols-4 gap-6">
<!-- Controls Panel -->
<div class="lg:col-span-1 bg-slate-800/50 rounded-xl p-6 shadow-lg border border-slate-700/50 custom-scrollbar overflow-y-auto max-h-screen">
<h2 class="text-xl font-semibold mb-4 flex items-center gap-2">
<i class="fas fa-sliders-h"></i> Controls
</h2>
<!-- Audio Input -->
<div class="mb-6">
<h3 class="text-sm font-medium mb-2 text-slate-300 flex items-center gap-2">
<i class="fas fa-music"></i> Audio Source
</h3>
<div class="flex flex-col gap-2">
<button id="fileInputBtn" class="bg-blue-600 hover:bg-blue-700 text-white py-2 px-4 rounded-lg transition flex items-center justify-center gap-2 glow">
<i class="fas fa-file-audio"></i> Import Audio
</button>
<input type="file" id="fileInput" accept="audio/*" class="hidden">
<button id="micInputBtn" class="bg-purple-600 hover:bg-purple-700 text-white py-2 px-4 rounded-lg transition flex items-center justify-center gap-2 glow">
<i class="fas fa-microphone"></i> Use Microphone
</button>
</div>
<div class="mt-3">
<div class="flex justify-between text-xs text-slate-400">
<span>Volume</span>
<span id="volumeValue">50%</span>
</div>
<input type="range" id="volumeSlider" min="0" max="100" value="50" class="w-full slider-thumb">
</div>
</div>
<!-- Pattern Selection -->
<div class="mb-6">
<h3 class="text-sm font-medium mb-2 text-slate-300 flex items-center gap-2">
<i class="fas fa-shapes"></i> Visual Pattern
</h3>
<div class="grid grid-cols-2 gap-3">
<div class="pattern-option" data-pattern="waveform">
<div class="pattern-preview bg-gradient-to-r from-blue-500 to-purple-600"></div>
<p class="text-xs text-center mt-1">Waveform</p>
</div>
<div class="pattern-option" data-pattern="particles">
<div class="pattern-preview bg-gradient-to-r from-green-500 to-teal-600"></div>
<p class="text-xs text-center mt-1">Particles</p>
</div>
<div class="pattern-option" data-pattern="rings">
<div class="pattern-preview bg-gradient-to-r from-orange-500 to-pink-600"></div>
<p class="text-xs text-center mt-1">Concentric</p>
</div>
<div class="pattern-option" data-pattern="bars">
<div class="pattern-preview bg-gradient-to-r from-yellow-500 to-red-600"></div>
<p class="text-xs text-center mt-1">Frequency</p>
</div>
</div>
</div>
<!-- Color Customization -->
<div class="mb-6">
<h3 class="text-sm font-medium mb-2 text-slate-300 flex items-center gap-2">
<i class="fas fa-palette"></i> Colors
</h3>
<div class="grid grid-cols-2 gap-3">
<div>
<label class="text-xs text-slate-400 block mb-1">Primary</label>
<input type="color" id="color1" value="#3b82f6" class="w-full h-8 rounded cursor-pointer">
</div>
<div>
<label class="text-xs text-slate-400 block mb-1">Secondary</label>
<input type="color" id="color2" value="#8b5cf6" class="w-full h-8 rounded cursor-pointer">
</div>
</div>
<div class="mt-3">
<div class="flex justify-between text-xs text-slate-400">
<span>Background Opacity</span>
<span id="bgOpacityValue">20%</span>
</div>
<input type="range" id="bgOpacitySlider" min="0" max="100" value="20" class="w-full slider-thumb">
</div>
</div>
<!-- Pattern Settings -->
<div class="mb-6">
<h3 class="text-sm font-medium mb-2 text-slate-300 flex items-center gap-2">
<i class="fas fa-cog"></i> Pattern Settings
</h3>
<div id="waveformSettings">
<div class="mb-2">
<div class="flex justify-between text-xs text-slate-400">
<span>Line Thickness</span>
<span id="waveThicknessValue">2</span>
</div>
<input type="range" id="waveThicknessSlider" min="1" max="10" value="2" class="w-full slider-thumb">
</div>
<div class="mb-2">
<div class="flex justify-between text-xs text-slate-400">
<span>Smoothness</span>
<span id="waveSmoothnessValue">50%</span>
</div>
<input type="range" id="waveSmoothnessSlider" min="0" max="100" value="50" class="w-full slider-thumb">
</div>
</div>
<div id="particlesSettings" class="hidden">
<div class="mb-2">
<div class="flex justify-between text-xs text-slate-400">
<span>Particle Count</span>
<span id="particleCountValue">150</span>
</div>
<input type="range" id="particleCountSlider" min="50" max="500" value="150" class="w-full slider-thumb">
</div>
<div class="mb-2">
<div class="flex justify-between text-xs text-slate-400">
<span>Particle Size</span>
<span id="particleSizeValue">3</span>
</div>
<input type="range" id="particleSizeSlider" min="1" max="10" value="3" class="w-full slider-thumb">
</div>
</div>
<div id="ringsSettings" class="hidden">
<div class="mb-2">
<div class="flex justify-between text-xs text-slate-400">
<span>Ring Count</span>
<span id="ringCountValue">8</span>
</div>
<input type="range" id="ringCountSlider" min="3" max="20" value="8" class="w-full slider-thumb">
</div>
<div class="mb-2">
<div class="flex justify-between text-xs text-slate-400">
<span>Ring Spacing</span>
<span id="ringSpacingValue">30</span>
</div>
<input type="range" id="ringSpacingSlider" min="10" max="100" value="30" class="w-full slider-thumb">
</div>
</div>
<div id="barsSettings" class="hidden">
<div class="mb-2">
<div class="flex justify-between text-xs text-slate-400">
<span>Bar Count</span>
<span id="barCountValue">64</span>
</div>
<input type="range" id="barCountSlider" min="16" max="128" value="64" class="w-full slider-thumb">
</div>
<div class="mb-2">
<div class="flex justify-between text-xs text-slate-400">
<span>Bar Width</span>
<span id="barWidthValue">8</span>
</div>
<input type="range" id="barWidthSlider" min="2" max="20" value="8" class="w-full slider-thumb">
</div>
</div>
</div>
<!-- Export Options -->
<div>
<h3 class="text-sm font-medium mb-2 text-slate-300 flex items-center gap-2">
<i class="fas fa-download"></i> Export
</h3>
<div class="flex flex-col gap-2">
<button id="exportImageBtn" class="bg-emerald-600 hover:bg-emerald-700 text-white py-2 px-4 rounded-lg transition flex items-center justify-center gap-2 glow">
<i class="fas fa-image"></i> Save Image
</button>
<button id="exportVideoBtn" class="bg-rose-600 hover:bg-rose-700 text-white py-2 px-4 rounded-lg transition flex items-center justify-center gap-2 glow">
<i class="fas fa-video"></i> Record Video
</button>
</div>
<div id="recordingControls" class="hidden mt-3">
<div class="flex justify-center gap-2">
<button id="stopRecordingBtn" class="bg-rose-600 hover:bg-rose-700 text-white py-1 px-3 rounded-lg text-sm flex items-center gap-1">
<i class="fas fa-stop"></i> Stop
</button>
<div id="recordingTimer" class="text-sm text-slate-300 flex items-center">
<i class="fas fa-circle text-rose-500 mr-1 animate-pulse"></i> 00:00
</div>
</div>
</div>
</div>
</div>
<!-- Visualizer Canvas -->
<div class="lg:col-span-3">
<div id="visualizer-container" class="bg-slate-900/50 rounded-xl p-4 shadow-lg border border-slate-700/50">
<div id="canvas-container" class="flex items-center justify-center">
<!-- p5 canvas will be inserted here -->
</div>
<div class="mt-4 flex justify-between items-center">
<div class="flex items-center gap-2">
<button id="playBtn" class="bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-full w-10 h-10 flex items-center justify-center glow">
<i class="fas fa-play"></i>
</button>
<button id="pauseBtn" class="bg-slate-700 hover:bg-slate-600 text-white p-2 rounded-full w-10 h-10 flex items-center justify-center">
<i class="fas fa-pause"></i>
</button>
<button id="stopBtn" class="bg-slate-700 hover:bg-slate-600 text-white p-2 rounded-full w-10 h-10 flex items-center justify-center">
<i class="fas fa-stop"></i>
</button>
</div>
<div id="songInfo" class="text-sm text-slate-400 italic">
No audio loaded
</div>
<div id="audioTime" class="text-sm text-slate-300">
0:00 / 0:00
</div>
</div>
</div>
<!-- Audio Spectrum Display -->
<div class="mt-6 bg-slate-800/50 rounded-xl p-4 shadow-lg border border-slate-700/50">
<h3 class="text-sm font-medium mb-2 text-slate-300 flex items-center gap-2">
<i class="fas fa-chart-bar"></i> Audio Spectrum
</h3>
<div id="spectrum-container" class="h-24">
<!-- Spectrum analyzer canvas will be inserted here -->
</div>
</div>
</div>
</div>
</div>
<script>
// Global variables
let audioContext, analyser, dataArray, source, audioElement;
let isPlaying = false;
let currentPattern = 'waveform';
let color1 = '#3b82f6', color2 = '#8b5cf6';
let bgOpacity = 20;
let waveThickness = 2, waveSmoothness = 50;
let particleCount = 150, particleSize = 3;
let ringCount = 8, ringSpacing = 30;
let barCount = 64, barWidth = 8;
let mediaRecorder, recordedChunks = [], recordingStartTime;
let recordingInterval;
// Pattern settings
const patternSettings = {
waveform: {
thickness: 2,
smoothness: 50
},
particles: {
count: 150,
size: 3
},
rings: {
count: 8,
spacing: 30
},
bars: {
count: 64,
width: 8
}
};
// Initialize p5.js sketches
new p5(function(p) {
let canvas, spectrumCanvas;
let fft, waveform;
let particles = [];
p.setup = function() {
// Main visualizer canvas
canvas = p.createCanvas(800, 450);
canvas.parent('canvas-container');
p.colorMode(p.HSB, 360, 100, 100, 1);
// Spectrum analyzer canvas
spectrumCanvas = p.createCanvas(800, 100);
spectrumCanvas.parent('spectrum-container');
// Initialize FFT for frequency analysis
fft = new p5.FFT();
// Initialize audio context if not already done
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
const bufferLength = analyser.frequencyBinCount;
dataArray = new Uint8Array(bufferLength);
}
// Initialize particles
initParticles();
};
p.draw = function() {
// Draw main visualizer
drawVisualizer();
// Draw spectrum analyzer
drawSpectrum();
// Update audio time display
updateAudioTime();
};
function drawVisualizer() {
// Get audio data
waveform = fft.waveform();
// Set background with opacity
p.background(10, bgOpacity);
// Draw selected pattern
switch(currentPattern) {
case 'waveform':
drawWaveform();
break;
case 'particles':
drawParticles();
break;
case 'rings':
drawRings();
break;
case 'bars':
drawBars();
break;
}
}
function drawWaveform() {
p.noFill();
p.strokeWeight(waveThickness);
// Create gradient for waveform
const gradient = p.drawingContext.createLinearGradient(0, 0, p.width, 0);
gradient.addColorStop(0, color1);
gradient.addColorStop(1, color2);
p.drawingContext.strokeStyle = gradient;
p.beginShape();
for (let i = 0; i < waveform.length; i++) {
// Apply smoothness
const x = p.map(i, 0, waveform.length, 0, p.width);
const y = p.map(waveform[i], -1, 1, p.height, 0);
// Smooth the line based on smoothness setting
if (i === 0) {
p.vertex(x, y);
} else {
const prevY = p.map(waveform[i-1], -1, 1, p.height, 0);
const smoothY = p.lerp(prevY, y, waveSmoothness / 100);
p.vertex(x, smoothY);
}
}
p.endShape();
}
function initParticles() {
particles = [];
for (let i = 0; i < particleCount; i++) {
particles.push({
x: p.random(p.width),
y: p.random(p.height),
vx: p.random(-1, 1),
vy: p.random(-1, 1),
size: particleSize,
color: p.lerpColor(p.color(color1), p.color(color2), p.random(1))
});
}
}
function drawParticles() {
const spectrum = fft.analyze();
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
// React to audio
const audioImpact = spectrum[Math.floor(p.map(i, 0, particles.length, 0, spectrum.length))] / 255;
// Update position with audio influence
particle.x += particle.vx * (1 + audioImpact * 2);
particle.y += particle.vy * (1 + audioImpact * 2);
// Wrap around edges
if (particle.x < 0) particle.x = p.width;
if (particle.x > p.width) particle.x = 0;
if (particle.y < 0) particle.y = p.height;
if (particle.y > p.height) particle.y = 0;
// Draw particle
p.noStroke();
p.fill(particle.color);
p.circle(particle.x, particle.y, particle.size * (1 + audioImpact * 2));
}
}
function drawRings() {
const spectrum = fft.analyze();
const centerX = p.width / 2;
const centerY = p.height / 2;
for (let i = 0; i < ringCount; i++) {
const radius = (i + 1) * ringSpacing;
const energy = spectrum[Math.floor(p.map(i, 0, ringCount, 0, spectrum.length))] / 255;
// Create gradient for each ring
const gradient = p.drawingContext.createRadialGradient(
centerX, centerY, radius - 5,
centerX, centerY, radius + 5
);
gradient.addColorStop(0, color1);
gradient.addColorStop(1, color2);
p.drawingContext.strokeStyle = gradient;
p.strokeWeight(5);
p.noFill();
p.circle(centerX, centerY, radius * (1 + energy * 0.5));
}
}
function drawBars() {
const spectrum = fft.analyze();
const binSize = Math.floor(spectrum.length / barCount);
for (let i = 0; i < barCount; i++) {
let sum = 0;
for (let j = 0; j < binSize; j++) {
sum += spectrum[i * binSize + j];
}
const avg = sum / binSize;
const energy = avg / 255;
const x = p.map(i, 0, barCount, 0, p.width);
const h = p.map(energy, 0, 1, 0, p.height * 0.8);
// Create gradient for each bar
const gradient = p.drawingContext.createLinearGradient(0, p.height - h, 0, p.height);
gradient.addColorStop(0, color1);
gradient.addColorStop(1, color2);
p.drawingContext.fillStyle = gradient;
p.noStroke();
p.rect(x, p.height - h, barWidth, h);
}
}
function drawSpectrum() {
const spectrum = fft.analyze();
p.background(15);
// Draw frequency spectrum
for (let i = 0; i < spectrum.length; i++) {
const x = p.map(i, 0, spectrum.length, 0, p.width);
const h = -p.height + p.map(spectrum[i], 0, 255, p.height, 0);
// Create gradient for spectrum
const gradient = p.drawingContext.createLinearGradient(0, 0, 0, p.height);
gradient.addColorStop(0, color1);
gradient.addColorStop(1, color2);
p.drawingContext.fillStyle = gradient;
p.noStroke();
p.rect(x, p.height, p.width / spectrum.length, h);
}
}
function updateAudioTime() {
if (audioElement && !isNaN(audioElement.duration)) {
const currentTime = audioElement.currentTime || 0;
const duration = audioElement.duration;
const currentMinutes = Math.floor(currentTime / 60);
const currentSeconds = Math.floor(currentTime % 60).toString().padStart(2, '0');
const durationMinutes = Math.floor(duration / 60);
const durationSeconds = Math.floor(duration % 60).toString().padStart(2, '0');
document.getElementById('audioTime').textContent =
`${currentMinutes}:${currentSeconds} / ${durationMinutes}:${durationSeconds}`;
} else {
document.getElementById('audioTime').textContent = '0:00 / 0:00';
}
}
});
// DOM event listeners
document.addEventListener('DOMContentLoaded', function() {
// Audio file input
document.getElementById('fileInputBtn').addEventListener('click', function() {
document.getElementById('fileInput').click();
});
document.getElementById('fileInput').addEventListener('change', function(e) {
if (e.target.files.length) {
loadAudioFile(e.target.files[0]);
}
});
// Microphone input
document.getElementById('micInputBtn').addEventListener('click', function() {
initMicrophone();
});
// Play/pause/stop controls
document.getElementById('playBtn').addEventListener('click', playAudio);
document.getElementById('pauseBtn').addEventListener('click', pauseAudio);
document.getElementById('stopBtn').addEventListener('click', stopAudio);
// Volume control
document.getElementById('volumeSlider').addEventListener('input', function(e) {
const volume = e.target.value / 100;
if (audioElement) audioElement.volume = volume;
document.getElementById('volumeValue').textContent = `${e.target.value}%`;
});
// Pattern selection
document.querySelectorAll('.pattern-option').forEach(option => {
option.addEventListener('click', function() {
currentPattern = this.getAttribute('data-pattern');
// Update UI
document.querySelectorAll('.pattern-option').forEach(opt => {
opt.querySelector('.pattern-preview').classList.remove('glow');
});
this.querySelector('.pattern-preview').classList.add('glow');
// Show relevant settings
document.querySelectorAll('[id$="Settings"]').forEach(el => {
el.classList.add('hidden');
});
document.getElementById(`${currentPattern}Settings`).classList.remove('hidden');
});
});
// Color controls
document.getElementById('color1').addEventListener('input', function(e) {
color1 = e.target.value;
});
document.getElementById('color2').addEventListener('input', function(e) {
color2 = e.target.value;
});
// Background opacity
document.getElementById('bgOpacitySlider').addEventListener('input', function(e) {
bgOpacity = e.target.value;
document.getElementById('bgOpacityValue').textContent = `${e.target.value}%`;
});
// Pattern-specific controls
// Waveform
document.getElementById('waveThicknessSlider').addEventListener('input', function(e) {
waveThickness = parseInt(e.target.value);
document.getElementById('waveThicknessValue').textContent = e.target.value;
});
document.getElementById('waveSmoothnessSlider').addEventListener('input', function(e) {
waveSmoothness = parseInt(e.target.value);
document.getElementById('waveSmoothnessValue').textContent = `${e.target.value}%`;
});
// Particles
document.getElementById('particleCountSlider').addEventListener('input', function(e) {
particleCount = parseInt(e.target.value);
document.getElementById('particleCountValue').textContent = e.target.value;
initParticles();
});
document.getElementById('particleSizeSlider').addEventListener('input', function(e) {
particleSize = parseInt(e.target.value);
document.getElementById('particleSizeValue').textContent = e.target.value;
});
// Rings
document.getElementById('ringCountSlider').addEventListener('input', function(e) {
ringCount = parseInt(e.target.value);
document.getElementById('ringCountValue').textContent = e.target.value;
});
document.getElementById('ringSpacingSlider').addEventListener('input', function(e) {
ringSpacing = parseInt(e.target.value);
document.getElementById('ringSpacingValue').textContent = e.target.value;
});
// Bars
document.getElementById('barCountSlider').addEventListener('input', function(e) {
barCount = parseInt(e.target.value);
document.getElementById('barCountValue').textContent = e.target.value;
});
document.getElementById('barWidthSlider').addEventListener('input', function(e) {
barWidth = parseInt(e.target.value);
document.getElementById('barWidthValue').textContent = e.target.value;
});
// Export buttons
document.getElementById('exportImageBtn').addEventListener('click', exportImage);
document.getElementById('exportVideoBtn').addEventListener('click', startRecording);
document.getElementById('stopRecordingBtn').addEventListener('click', stopRecording);
// Initialize with waveform pattern selected
document.querySelector('.pattern-option[data-pattern="waveform"]').click();
});
// Audio functions
function loadAudioFile(file) {
if (audioElement) {
audioElement.pause();
if (source) source.disconnect();
}
const fileURL = URL.createObjectURL(file);
audioElement = new Audio(fileURL);
audioElement.volume = document.getElementById('volumeSlider').value / 100;
// Set up audio context
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
if (analyser) {
source.disconnect();
}
source = audioContext.createMediaElementSource(audioElement);
analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
source.connect(analyser);
analyser.connect(audioContext.destination);
// Update UI
document.getElementById('songInfo').textContent = file.name;
document.getElementById('playBtn').classList.add('glow');
document.getElementById('pauseBtn').classList.remove('glow');
document.getElementById('stopBtn').classList.remove('glow');
// Set up event listeners
audioElement.addEventListener('play', function() {
isPlaying = true;
if (audioContext.state === 'suspended') {
audioContext.resume();
}
document.getElementById('playBtn').classList.add('glow');
document.getElementById('pauseBtn').classList.remove('glow');
document.getElementById('stopBtn').classList.remove('glow');
});
audioElement.addEventListener('pause', function() {
isPlaying = false;
document.getElementById('playBtn').classList.remove('glow');
document.getElementById('pauseBtn').classList.add('glow');
document.getElementById('stopBtn').classList.remove('glow');
});
audioElement.addEventListener('ended', function() {
isPlaying = false;
document.getElementById('playBtn').classList.remove('glow');
document.getElementById('pauseBtn').classList.remove('glow');
document.getElementById('stopBtn').classList.add('glow');
});
}
function initMicrophone() {
if (audioElement) {
audioElement.pause();
if (source) source.disconnect();
}
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(function(stream) {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
if (source) {
source.disconnect();
}
source = audioContext.createMediaStreamSource(stream);
analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
source.connect(analyser);
// Update UI
document.getElementById('songInfo').textContent = "Microphone Input";
document.getElementById('playBtn').classList.remove('glow');
document.getElementById('pauseBtn').classList.remove('glow');
document.getElementById('stopBtn').classList.remove('glow');
isPlaying = true;
// For microphone, we're always "playing"
if (audioContext.state === 'suspended') {
audioContext.resume();
}
})
.catch(function(err) {
console.error('Error accessing microphone:', err);
alert('Could not access microphone. Please check permissions.');
});
}
function playAudio() {
if (audioElement && !isPlaying) {
audioElement.play();
} else if (!audioElement && !isPlaying) {
alert('Please load an audio file or enable microphone first.');
}
}
function pauseAudio() {
if (audioElement && isPlaying) {
audioElement.pause();
}
}
function stopAudio() {
if (audioElement) {
audioElement.pause();
audioElement.currentTime = 0;
isPlaying = false;
document.getElementById('playBtn').classList.remove('glow');
document.getElementById('pauseBtn').classList.remove('glow');
document.getElementById('stopBtn').classList.add('glow');
}
}
// Export functions
function exportImage() {
const canvas = document.querySelector('#canvas-container canvas');
const link = document.createElement('a');
link.download = 'visualizer-snapshot.png';
link.href = canvas.toDataURL('image/png');
link.click();
}
function startRecording() {
const canvas = document.querySelector('#canvas-container canvas');
const stream = canvas.captureStream(30); // 30 FPS
recordedChunks = [];
mediaRecorder = new MediaRecorder(stream, {
mimeType: 'video/webm;codecs=vp9'
});
mediaRecorder.ondataavailable = function(e) {
if (e.data.size > 0) {
recordedChunks.push(e.data);
}
};
mediaRecorder.onstop = function() {
const blob = new Blob(recordedChunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.download = 'visualizer-recording.webm';
link.href = url;
link.click();
document.getElementById('recordingControls').classList.add('hidden');
clearInterval(recordingInterval);
};
mediaRecorder.start();
recordingStartTime = Date.now();
// Update timer
recordingInterval = setInterval(function() {
const elapsed = Math.floor((Date.now() - recordingStartTime) / 1000);
const minutes = Math.floor(elapsed / 60).toString().padStart(2, '0');
const seconds = (elapsed % 60).toString().padStart(2, '0');
document.getElementById('recordingTimer').innerHTML =
`<i class="fas fa-circle text-rose-500 mr-1 animate-pulse"></i> ${minutes}:${seconds}`;
}, 1000);
document.getElementById('recordingControls').classList.remove('hidden');
}
function stopRecording() {
if (mediaRecorder && mediaRecorder.state === 'recording') {
mediaRecorder.stop();
}
}
// Helper function to initialize particles (called from p5 sketch)
function initParticles() {
// This is just a placeholder - actual initialization happens in the p5 sketch
}
</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 <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - <a href="https://enzostvs-deepsite.hf.space?remix=bibbler/audio-reactive-visualizer" style="color: #fff;text-decoration: underline;" target="_blank" >🧬 Remix</a></p></body>
</html> |