File size: 1,829 Bytes
27630e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { AudioPlayer } from './audioPlayer.js';
import { Visualizer } from './visualizer.js';

const audioContext = new (window.AudioContext || window.webkitAudioContext)();

const audioPlayer = new AudioPlayer(audioContext, 'http://stream-174.zeno.fm/c58ssug3668uv?zt=eyJhbGciOiJIUzI1NiJ9.eyJzdHJlYW0iOiJjNThzc3VnMzY2OHV2IiwiaG9zdCI6InN0cmVhbS0xNzQuemVuby5mbSIsInJ0dGwiOjUsImp0aSI6IjY0U1pWa1RFVGJ5aS16VVJjZUE0c2ciLCJpYXQiOjE3MzkyNzc4NzQsImV4cCI6MTczOTI3NzkzNH0.fOQetCgM5m28EmqCIxLYh0hkPzetEDO8ixqTH0cNOw0');
const visualizer = new Visualizer(audioContext, 'visualizer');

audioPlayer.connectAnalyser(visualizer.analyser);

const playButton = document.getElementById('playButton');
const playIcon = playButton.querySelector('.play-icon');
const pauseIcon = playButton.querySelector('.pause-icon');

playButton.addEventListener('click', () => {
  if (audioPlayer.isPlaying) {
    audioPlayer.pause();
    playIcon.style.display = 'block';
    pauseIcon.style.display = 'none';
  } else {
    audioPlayer.play();
    playIcon.style.display = 'none';
    pauseIcon.style.display = 'block';
  }
});

function lerp(start, end, amt) {
  return (1 - amt) * start + amt * end;
}

function updateBackgroundColor(amplitude) {
  // Convert hex colors to RGB
  const startColor = {
    r: 0x1f, g: 0x03, b: 0x6b
  };
  const endColor = {
    r: 0x6a, g: 0x4a, b: 0xc2
  };
  
  // Interpolate between colors based on amplitude
  const r = Math.round(lerp(startColor.r, endColor.r, amplitude));
  const g = Math.round(lerp(startColor.g, endColor.g, amplitude));
  const b = Math.round(lerp(startColor.b, endColor.b, amplitude));
  
  document.body.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}

function animate() {
  const amplitude = visualizer.draw();
  updateBackgroundColor(amplitude);
  requestAnimationFrame(animate);
}
animate();