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
|
(function () {
let isPlaying = false;
let currentTracks = [];
let cursor = 0;
let howlerInstance;
const retroWaveRu = "https://retrowave.ru";
let titleEl = document.getElementById("track-name");
let coverArtEl = document.getElementsByClassName("music-player")[0];
let refreshBtn = document.querySelector(".refresh");
let ovr = document.querySelector(".OVR");
let fullScreenBtn = document.querySelector(".fullscreen");
let terminalBtn = document.querySelector(".terminal-btn");
let terminalOverlay = document.getElementById("terminal-overlay");
let terminalInput = document.getElementById("terminal-input");
let terminalOutput = document.getElementById("terminal-output");
let terminalClose = document.querySelector(".terminal-close");
let errorEl = document.querySelector(".ERRORS");
let currentEffectIndex = 0;
const modalEl = document.getElementById("intro-modal");
// const uploadInfoEl = document.getElementById("upload-info");
const fileUploadEl = document.getElementById("file-upload");
let volume = 1;
let effectCanvas;
let starField;
let line3D;
const synthwaveColor = 0xff2975;
openDialog();
listenUploadFileChange();
function initDynamicTooltips() {
document.querySelectorAll("[title]").forEach((element) => {
const title = element.getAttribute("title");
element.removeAttribute("title");
element.setAttribute("data-title", title);
element.addEventListener("mouseenter", function (e) {
const tooltip = document.createElement("div");
tooltip.textContent = this.getAttribute("data-title");
tooltip.style.cssText = `
font-family: "VCR", sans-serif;
position: fixed;
background:rgb(255, 255, 255);
color: #000000;
border: 1px solidrgb(0, 0, 0);
padding: 4px 8px;
font-size: 12px;
border-radius: 3px;
z-index: 10000;
pointer-events: none;
white-space: nowrap;
`;
document.body.appendChild(tooltip);
this.tooltipEl = tooltip;
const rect = this.getBoundingClientRect();
tooltip.style.left =
rect.left + rect.width / 2 - tooltip.offsetWidth / 2 + "px";
tooltip.style.top = rect.top - tooltip.offsetHeight - 8 + "px";
});
element.addEventListener("mouseleave", function () {
if (this.tooltipEl) {
document.body.removeChild(this.tooltipEl);
this.tooltipEl = null;
}
});
});
}
document.addEventListener("DOMContentLoaded", initDynamicTooltips);
coverArtEl.addEventListener("click", (event) => {
const isNoPause = event.target.classList.contains("no-pause");
if (howlerInstance && !isNoPause) {
togglePlay();
}
});
// uploadInfoEl.addEventListener("click", () => {
// const confirmText = `Do you really want to change your playlist?\nThis will replace all your retrowave music history.\nIf you are sure about this, make sure to upload a valid json file probably downloaded using history link.`;
// if (confirm(confirmText)) {
// fileUploadEl.click();
// }
// });
function listenUploadFileChange() {
fileUploadEl.onchange = function () {
const selectedFile = fileUploadEl.files[0];
const reader = new FileReader();
reader.readAsText(selectedFile, "UTF-8");
reader.onload = function (event) {
try {
const uploadedPlaylist = JSON.parse(event.target.result);
localStorage.setItem("retrowave-history", event.target.result);
} catch (error) {
alert("malformed/invalid json file");
}
};
};
}
document.getElementById("initButton")?.addEventListener("click", async () => {
var hydra = new Hydra({ detectAudio: false });
modalEl.classList.remove("open");
getMusic().then(() => {
playMusic();
});
initHydra();
initControls();
});
document.getElementById("history").addEventListener("click", () => {
downloadHistory();
});
document.addEventListener("keyup", (event) => {
if (!terminalOverlay.classList.contains("hidden")) {
return;
}
const { key } = event;
switch (key) {
case " ":
togglePlay();
break;
case "w":
volumeUp();
break;
case "s":
volumeDown();
break;
case "n":
playNextTrack();
break;
case "f":
toggleFullScreen();
break;
case "h":
toggleControls();
break;
case "x":
toggleEverything();
break;
case "e":
rotateHydraEffect();
break;
case "t":
toggleTerminal();
break;
}
});
let touchstartX = 0;
let touchendX = 0;
let touchstartY = 0;
let touchendY = 0;
const musixPlayerEl = document.getElementsByClassName("music-player")[0];
if (musixPlayerEl) {
musixPlayerEl.addEventListener("touchstart", (e) => {
touchstartX = e.changedTouches[0].screenX;
touchstartY = e.changedTouches[0].screenY;
});
musixPlayerEl.addEventListener("touchend", (e) => {
touchendX = e.changedTouches[0].screenX;
touchendY = e.changedTouches[0].screenY;
checkDirection();
});
}
function checkDirection() {
if (touchendX < touchstartX) {
// Swipe Left
}
if (touchendX > touchstartX) {
// Swipe Right
}
if (touchendY < touchstartY) {
volumeUp();
}
if (touchendY > touchstartY) {
volumeDown();
}
}
function openDialog() {
modalEl.classList.add("open");
}
async function getMusic() {
try {
const res = await fetch(
`https://retrowave.ru/api/v1/tracks?limit=10&cursor=${cursor}`
).then((res) => res.json());
const {
body: { tracks, cursor: currentCursor },
} = res;
cursor = currentCursor;
currentTracks = tracks;
} catch (error) {
console.error("Error fetching music:", error);
showErrors(
"Failed to fetch music. Please check your internet connection or try again later."
);
throw error; // rethrow the error to be caught in the catch block below
}
}
function initPlayer() {}
function playMusic() {
const currentTrack = currentTracks[0];
const singleTrack = currentTrack.streamUrl;
const fullTrack = `${retroWaveRu}${singleTrack}`;
howlerInstance = new Howl({
src: [fullTrack],
html5: true,
onend: function () {
playNextTrack();
},
});
setVolume();
isPlaying = true;
window.hw = howlerInstance;
updateInfo(currentTrack);
addToHistory(currentTrack);
howlerInstance.play();
}
function volumeDown() {
if (volume > 0.1) {
volume -= 0.1;
}
setVolume();
}
function volumeUp() {
if (volume < 1) {
volume += 0.1;
}
setVolume();
}
function setVolume() {
howlerInstance.volume(volume);
}
function togglePlay() {
isPlaying = !isPlaying;
if (isPlaying) {
howlerInstance.play();
} else {
howlerInstance.pause();
}
}
function updateInfo(trackDetails) {
const trackTitle = trackDetails.title.toString().toUpperCase() || "Unknown Track";
titleEl.innerText = trackTitle;
document.title = `Now Playing... ${trackTitle}`;
coverArtEl.style.backgroundImage = `url("${retroWaveRu}${trackDetails.artworkUrl}")`;
ovr.innerHTML = `> ${trackTitle}`;
}
function getHistory() {
let localHistoryStore = localStorage.getItem("retrowave-history") || "[]";
let historyArray = JSON.parse(localHistoryStore);
return historyArray;
}
function addToHistory(trackDetails) {
let historyArray = getHistory();
historyArray.push(trackDetails);
localStorage.setItem("retrowave-history", JSON.stringify(historyArray));
}
function downloadHistory() {
const historyArray = getHistory();
let element = document.createElement("a");
let playListData = "#EXTM3U";
historyArray.forEach((musicData) => {
playListData = `${playListData}
#EXTINF:${Math.ceil(musicData.duration / 1000)}, ${musicData.title}
https://retrowave.ru/${musicData.streamUrl}
`;
});
element.setAttribute(
"href",
"data:audio/x-mpegurl;;charset=utf-8," + encodeURIComponent(playListData)
);
element.setAttribute("download", "retrowave_playlist.m3u");
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function playNextTrack() {
resetHowler();
currentTracks.shift();
if (currentTracks.length <= 3) {
getMusic();
}
playMusic();
}
function resetHowler(destroy = false) {
howlerInstance.stop();
if (destroy) {
howlerInstance.unload();
howlerInstance = null;
}
}
function initControls() {
refreshBtn.addEventListener("click", () => {
playNextTrack();
});
fullScreenBtn.addEventListener("click", () => {
toggleFullScreen();
});
terminalBtn.addEventListener("click", () => {
toggleTerminal();
});
terminalClose.addEventListener("click", () => {
closeTerminal();
});
terminalInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
executeCommand(terminalInput.value.trim());
terminalInput.value = "";
}
});
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && !terminalOverlay.classList.contains("hidden")) {
closeTerminal();
}
});
}
async function toggleFullScreen() {
if (!document.fullscreenElement) {
try {
await document?.documentElement?.requestFullscreen();
} catch (error) {
console.error("Error attempting to enable full-screen mode:", error);
showErrors(
"Failed to enter full-screen mode. Please check your browser settings."
);
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
}
}
}
function toggleControls() {
const toggleableElements = document.querySelectorAll(".toggleable");
toggleableElements.forEach((el) => {
el.classList.toggle("hidden");
});
}
function toggleEverything() {
musixPlayerEl.classList.toggle("hidden");
ovr.classList.toggle("hidden");
}
function showErrors(error) {
errorEl.classList.remove("hidden");
errorEl.innerText = error;
setTimeout(() => {
errorEl.innerText = "";
errorEl.classList.add("hidden");
}, 10000);
}
// function initCodef() {
// const width = window.innerWidth;
// const height = window.innerHeight;
// effectCanvas = new canvas(width, height, 'codef-canvas');
// starField = new starfield3D(effectCanvas, 500, 2, width, height, width/2, height/2, '#ffffff', 100, 0, 0);
// line3D = new codef3D(effectCanvas, 320, 75, 1, 1500 );
// line3D.line({x:-320, y:0, z:0},{x:320, y:0, z:0}, new LineBasicMaterial({ color: synthwaveColor, linewidth:2}));
// line3D.line({x: 0, y:-240, z:0},{x:0, y:240, z:0}, new LineBasicMaterial({ color: synthwaveColor, linewidth:2}));
// renderCodeFx();
// }
// function renderCodeFx() {
// effectCanvas.fill('#000000');
// line3D.group.rotation.x+=0.01;
// line3D.group.rotation.y+=0.02;
// line3D.group.rotation.z+=0.04;
// starField.draw();
// line3D.draw();
// requestAnimationFrame(renderCodeFx);
// }
const oscRotate = () => {
solid(0, 0).out();
setTimeout(() => {
osc(10).rotate(0.5).diff(osc(200)).out();
});
};
const rainbowWebcam = () => {
solid(0, 0).out();
setTimeout(() => {
s0.initCam();
src(s0).out(o0);
osc(10, 0.2, 0.8).diff(o0).out(o1);
render(o1);
});
};
const waveyzz = () => {
solid(0, 0).out();
setTimeout(() => {
osc(60, -0.015, 0.3)
.diff(osc(60, 0.08).rotate(Math.PI / 2))
.modulateScale(
noise(3.5, 0.25).modulateScale(
osc(15).rotate(() => Math.sin(time / 2))
),
0.6
)
.color(1, 0.5, 0.4)
.contrast(1.4)
.add(src(o0).modulate(o0, 0.04), 0.6)
.invert()
.brightness(0.1)
.contrast(1.2)
.modulateScale(osc(2), -0.2)
.out();
});
};
const vernoi = () => {
setTimeout(() => {
solid(0, 0).out();
voronoi(350, 0.15)
.modulateScale(osc(8).rotate(Math.sin(time)), 0.5)
.thresh(0.8)
.modulateRotate(osc(7), 0.4)
.thresh(0.7)
.diff(src(o0).scale(1.8))
.modulateScale(osc(2).modulateRotate(o0, 0.74))
.diff(
src(o0)
.rotate([-0.012, 0.01, -0.002, 0])
.scrollY(0, [-1 / 199800, 0].fast(0.7))
)
.brightness([-0.02, -0.17].smooth().fast(0.5)) //.modulate(o0, () => a.fft[1] * .2)
.out();
});
};
const hydraEffects = [vernoi, waveyzz, oscRotate];
function rotateHydraEffect() {
try {
hydraEffects[currentEffectIndex]();
currentEffectIndex = (currentEffectIndex + 1) % hydraEffects.length;
} catch (error) {
console.error("Error applying random Hydra effect:", error);
showErrors(
"Failed to apply random Hydra effect. Please check your browser compatibility or try again later."
);
}
}
function initHydra() {
try {
hydraEffects[0]();
} catch (error) {
console.error("Hydra initialization failed:", error);
showErrors(
"Hydra initialization failed. Please check your browser compatibility or try again later."
);
}
}
function toggleTerminal() {
terminalOverlay.classList.toggle("hidden");
if (!terminalOverlay.classList.contains("hidden")) {
terminalInput.focus();
}
}
function closeTerminal() {
terminalOverlay.classList.add("hidden");
}
function addTerminalLine(text, isCommand = false) {
const line = document.createElement("div");
line.className = "terminal-line";
if (isCommand) {
line.innerHTML = `<span style="color: #000000; font-weight: normal;">indrajith@retrowave:$ ${text}</span>`;
} else {
line.textContent = text;
}
terminalOutput.appendChild(line);
terminalOutput.scrollTop = terminalOutput.scrollHeight;
}
function executeCommand(command) {
if (!command) return;
addTerminalLine(command, true);
const args = command.toLowerCase().split(' ');
const cmd = args[0];
switch (cmd) {
case 'help':
addTerminalLine('Available commands:');
addTerminalLine(' help - Show this help message');
addTerminalLine(' play - Start/resume playback');
addTerminalLine(' pause - Pause playback');
addTerminalLine(' next - Skip to next track');
addTerminalLine(' volume [0-10] - Set volume (0-10)');
addTerminalLine(' status - Show current track info');
addTerminalLine(' history - Download playlist history');
addTerminalLine(' effect [list|name] - Change/list visual effects');
addTerminalLine(' fullscreen - Toggle fullscreen mode');
addTerminalLine(' clear - Clear terminal');
addTerminalLine(' exit - Close terminal');
break;
case 'play':
if (howlerInstance && !isPlaying) {
togglePlay();
addTerminalLine('Playback resumed.');
} else if (isPlaying) {
addTerminalLine('Already playing.');
} else {
addTerminalLine('No track loaded.');
}
break;
case 'pause':
if (howlerInstance && isPlaying) {
togglePlay();
addTerminalLine('Playback paused.');
} else {
addTerminalLine('Not currently playing.');
}
break;
case 'next':
if (howlerInstance) {
playNextTrack();
addTerminalLine('Skipped to next track.');
} else {
addTerminalLine('No track loaded.');
}
break;
case 'volume':
if (args[1]) {
const vol = parseInt(args[1]);
if (vol >= 0 && vol <= 10) {
volume = vol / 10;
if (howlerInstance) setVolume();
addTerminalLine(`Volume set to ${vol}/10`);
} else {
addTerminalLine('Volume must be between 0 and 10.');
}
} else {
addTerminalLine(`Current volume: ${Math.round(volume * 10)}/10`);
}
break;
case 'status':
if (currentTracks.length > 0) {
const track = currentTracks[0];
addTerminalLine(`Now playing: ${track.title}`);
addTerminalLine(`Status: ${isPlaying ? 'Playing' : 'Paused'}`);
addTerminalLine(`Volume: ${Math.round(volume * 10)}/10`);
} else {
addTerminalLine('No track loaded.');
}
break;
case 'history':
downloadHistory();
addTerminalLine('Playlist history downloaded.');
break;
case 'effect':
if (args[1]) {
if (args[1] === 'list') {
addTerminalLine('Available effects:');
addTerminalLine(' vernoi - Voronoi pattern effect');
addTerminalLine(' waveyzz - Wave synthesis effect');
addTerminalLine(' oscrotate - Oscillating rotation effect');
addTerminalLine('Usage: effect <effect_name> or effect list');
} else {
const effectName = args[1].toLowerCase();
let effectIndex = -1;
switch (effectName) {
case 'vernoi':
effectIndex = 0;
break;
case 'waveyzz':
effectIndex = 1;
break;
case 'oscrotate':
effectIndex = 2;
break;
default:
addTerminalLine(`Unknown effect: ${effectName}`);
addTerminalLine('Type "effect list" to see available effects.');
break;
}
if (effectIndex !== -1) {
try {
hydraEffects[effectIndex]();
currentEffectIndex = effectIndex;
addTerminalLine(`Effect changed to: ${effectName}`);
} catch (error) {
addTerminalLine('Failed to apply effect.');
}
}
}
} else {
rotateHydraEffect();
addTerminalLine('Visualization changed to next effect.');
}
break;
case 'fullscreen':
toggleFullScreen();
addTerminalLine('Toggled fullscreen mode.');
break;
case 'clear':
terminalOutput.innerHTML = '';
break;
case 'exit':
closeTerminal();
break;
default:
addTerminalLine(`Command not found: ${cmd}`);
addTerminalLine('Type "help" for available commands.');
break;
}
addTerminalLine('');
}
})();
|