forked from kaischallenberg/monstercatVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrols.js
More file actions
222 lines (195 loc) · 7.21 KB
/
controls.js
File metadata and controls
222 lines (195 loc) · 7.21 KB
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
let stopAfterCurrent = false;
document.addEventListener("DOMContentLoaded", () => {
console.log("Controls.js: Initializing...");
// Get DOM elements
const audio = document.getElementById("audio");
const progressBar = document.getElementById("progress-bar");
const progressContainer = document.getElementById("progress-container");
const timeDisplay = document.getElementById("time-display");
const btnPlay = document.getElementById("btn-play");
const btnPause = document.getElementById("btn-pause");
const btnStopAfter = document.getElementById("btn-stop-after");
const btnStop = document.getElementById("btn-stop");
const btnNext = document.getElementById("btn-next");
const btnVolDown = document.getElementById("btn-vol-down");
const btnVolUp = document.getElementById("btn-vol-up");
const volDisplay = document.getElementById("vol-display");
if (!btnPlay || !audio) {
console.error("Controls.js: Critical elements missing!");
return;
}
// Volume logic
function updateVolume(newVol) {
// Clamp between 0 and 1
newVol = Math.max(0, Math.min(1, newVol));
audio.volume = newVol;
volDisplay.textContent = Math.round(newVol * 100) + "%";
// Save to localStorage if desired, for now just update UI
}
// Initialize volume (default 0.5 or current)
updateVolume(audio.volume);
btnVolDown.addEventListener("click", () => {
updateVolume(audio.volume - 0.05);
});
btnVolUp.addEventListener("click", () => {
updateVolume(audio.volume + 0.05);
});
// Button click handlers
btnPlay.addEventListener("click", () => {
console.log("Play clicked");
audio.play().catch(e => console.error("Play failed:", e));
updateTimeDisplay();
});
btnPause.addEventListener("click", () => {
console.log("Pause clicked");
audio.pause();
});
btnStopAfter.addEventListener("click", () => {
console.log("Stop After clicked");
stopAfterCurrent = true;
btnStopAfter.style.background = "rgba(0, 212, 170, 0.5)";
});
btnStop.addEventListener("click", () => {
console.log("Stop clicked");
stopAfterCurrent = false;
btnStopAfter.style.background = "";
audio.pause();
audio.currentTime = 0;
updateProgress();
updateTimeDisplay();
});
btnNext.addEventListener("click", async () => {
console.log("Next clicked");
stopAfterCurrent = false;
btnStopAfter.style.background = "";
if (typeof getSong === "function") {
await getSong();
// Wait a bit for the src to be set and potentially start loading
setTimeout(() => {
audio.play().catch(e => console.error("Auto-play next failed:", e));
}, 100);
}
});
// Progress bar click handler
progressContainer.addEventListener("click", (e) => {
console.log("Progress bar clicked");
const rect = progressContainer.getBoundingClientRect();
const percent = (e.clientX - rect.left) / rect.width;
audio.currentTime = percent * audio.duration;
});
// Update progress bar and time display
function updateProgress() {
if (audio.duration) {
const percent = (audio.currentTime / audio.duration) * 100;
progressBar.style.width = percent + "%";
}
}
function formatTime(seconds) {
if (isNaN(seconds)) return "0:00";
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, "0")}`;
}
function updateTimeDisplay() {
const current = formatTime(audio.currentTime);
const duration = formatTime(audio.duration);
timeDisplay.textContent = `${current} / ${duration}`;
}
// Audio event listeners
audio.addEventListener("timeupdate", () => {
updateProgress();
updateTimeDisplay();
});
audio.addEventListener("ended", async () => {
if (stopAfterCurrent) {
stopAfterCurrent = false;
btnStopAfter.style.background = "";
audio.pause();
audio.currentTime = 0;
} else {
// Default behavior - play next song
if (typeof getSong === "function") {
await getSong();
setTimeout(() => {
audio.play().catch(e => console.error("Auto-advance failed:", e));
}, 100);
}
}
updateProgress();
updateTimeDisplay();
});
audio.addEventListener("loadedmetadata", () => {
updateTimeDisplay();
});
audio.addEventListener("play", () => {
btnPlay.style.background = "rgba(0, 212, 170, 0.5)";
});
audio.addEventListener("pause", () => {
btnPlay.style.background = "";
});
// Hotkey handlers
document.addEventListener("keydown", (e) => {
// Ignore if user is typing in an input
if (e.target.tagName === "INPUT" || e.target.tagName === "TEXTAREA") return;
switch (e.key) {
case "F3": // Play
e.preventDefault();
audio.play();
break;
case "F4": // Stop after current
e.preventDefault();
stopAfterCurrent = true;
btnStopAfter.style.background = "rgba(0, 212, 170, 0.5)";
break;
case "F5": // Pause
e.preventDefault();
audio.pause();
break;
case "F6": // Stop
e.preventDefault();
stopAfterCurrent = false;
btnStopAfter.style.background = "";
audio.pause();
audio.currentTime = 0;
updateProgress();
updateTimeDisplay();
break;
case "F7": // Next
e.preventDefault();
stopAfterCurrent = false;
btnStopAfter.style.background = "";
if (typeof getSong === "function") {
getSong();
}
break;
case " ": // Spacebar for play/pause
e.preventDefault();
if (audio.paused) {
audio.play();
} else {
audio.pause();
}
break;
case "ArrowRight": // Seek forward 5 seconds
e.preventDefault();
audio.currentTime = Math.min(audio.currentTime + 5, audio.duration);
break;
case "ArrowLeft": // Seek backward 5 seconds
e.preventDefault();
audio.currentTime = Math.max(audio.currentTime - 5, 0);
break;
case "ArrowUp": // Volume Up
e.preventDefault();
updateVolume(audio.volume + 0.05);
break;
case "ArrowDown": // Volume Down
e.preventDefault();
updateVolume(audio.volume - 0.05);
break;
}
});
// Initialize
updateProgress();
updateTimeDisplay();
console.log("Controls.js: Initialized");
});