-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
94 lines (83 loc) · 2.96 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Visualizer</title>
</head>
<body>
<audio src="aventadorraw - Trim.mp3"></audio>
<div class="box">
<div class="visualizer"></div>
<div class="center-circle"></div>
</div>
<div class="play">
<div class="btn btn-play"></div>
</div>
<script>
const btn = document.querySelector(".btn");
const audio = document.querySelector("audio");
const visualizer = document.querySelector(".visualizer");
let audioContextStarted = false;
let ctx;
btn.addEventListener("click", () => {
if (!audioContextStarted) {
ctx = new (window.AudioContext || window.webkitAudioContext)();
const analyser = ctx.createAnalyser();
const source = ctx.createMediaElementSource(audio);
source.connect(analyser);
source.connect(ctx.destination);
analyser.fftSize = 256;
const bufferLength = analyser.frequencyBinCount;
let dataArray = new Uint8Array(bufferLength);
let elements = [];
for (let i = 0; i < bufferLength; i++) {
const element = document.createElement("span");
element.classList.add("element");
elements.push(element);
visualizer.appendChild(element);
}
const clamp = (num, min, max) => {
if (num >= max) return max;
if (num <= min) return min;
return num;
};
const update = () => {
requestAnimationFrame(update);
analyser.getByteFrequencyData(dataArray);
const startindex = 10;
for (let i = 0; i < bufferLength; i++) {
let item = dataArray[i];
let height = clamp(item, 65, 140);
let index = (i + startindex) % bufferLength;
elements[index].style.height = `${height}px`;
elements[index].style.transform = `rotate(${
i * (360 / bufferLength)
}deg) translateY(-50%)`;
}
};
update();
audioContextStarted = true;
}
// Play atau pause the fucking audio
if (audio.paused) {
audio.currentTime = 0; // Start from stupid 0
audio.play();
} else {
audio.pause();
audio.currentTime = 0; // Set to first audio
}
btn.classList.toggle("btn-play");
btn.classList.toggle("btn-pause");
audio.addEventListener("ended", () => {
// audio back to the reset
btn.classList.remove("btn-pause");
btn.classList.add("btn-play");
audio.currentTime = 0; // Reset audio to the fucking first
});
});
</script>
</body>
</html>