add new tracks + autoshuffle
This commit is contained in:
parent
c1212f93d5
commit
6c2997daf1
BIN
public/music/psshvec.flac
Normal file
BIN
public/music/psshvec.flac
Normal file
Binary file not shown.
BIN
public/music/zimaooes.flac
Normal file
BIN
public/music/zimaooes.flac
Normal file
Binary file not shown.
@ -18,6 +18,31 @@
|
||||
let currentTrackIndex = 0;
|
||||
let currentTrack = null;
|
||||
|
||||
function getSecureRandomIntExclusive(max) {
|
||||
if (!max || max <= 0) return 0;
|
||||
const cryptoObj = typeof window !== 'undefined' && (window.crypto || window.msCrypto);
|
||||
if (cryptoObj && cryptoObj.getRandomValues) {
|
||||
const maxUint32 = 4294967296;
|
||||
const threshold = maxUint32 - (maxUint32 % max);
|
||||
const buf = new Uint32Array(1);
|
||||
for (;;) {
|
||||
cryptoObj.getRandomValues(buf);
|
||||
const r = buf[0];
|
||||
if (r < threshold) return r % max;
|
||||
}
|
||||
}
|
||||
return Math.floor(Math.random() * max);
|
||||
}
|
||||
|
||||
function shuffleInPlace(arr) {
|
||||
for (let i = arr.length - 1; i > 0; i--) {
|
||||
const j = getSecureRandomIntExclusive(i + 1);
|
||||
const tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
await loadTracks();
|
||||
});
|
||||
@ -33,7 +58,8 @@
|
||||
try {
|
||||
loadingProgress = 10;
|
||||
|
||||
const trackFiles = ['mgxleepe.flac', 'jifmachinecd.flac'];
|
||||
const trackFiles = ['mgxleepe.flac', 'jifmachinecd.flac', 'psshvec.flac', 'zimaooes.flac'];
|
||||
shuffleInPlace(trackFiles);
|
||||
tracks = [];
|
||||
|
||||
for (let i = 0; i < trackFiles.length; i++) {
|
||||
@ -41,7 +67,8 @@
|
||||
loadingProgress = 20 + (i * 60 / trackFiles.length);
|
||||
|
||||
try {
|
||||
const response = await fetch(`/music/${filename}`);
|
||||
const url = `/music/${filename}`;
|
||||
const response = await fetch(url, { cache: 'force-cache' });
|
||||
if (!response.ok) continue;
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
@ -49,7 +76,7 @@
|
||||
|
||||
tracks.push({
|
||||
filename,
|
||||
src: `/music/${filename}`,
|
||||
src: url,
|
||||
metadata,
|
||||
title: metadata.common.title || filename.replace('.flac', ''),
|
||||
artist: metadata.common.artist || 'Unknown Artist',
|
||||
@ -67,7 +94,8 @@
|
||||
}
|
||||
|
||||
loadingProgress = 90;
|
||||
currentTrack = tracks[0];
|
||||
currentTrackIndex = getSecureRandomIntExclusive(tracks.length);
|
||||
currentTrack = tracks[currentTrackIndex];
|
||||
loadingProgress = 100;
|
||||
|
||||
setTimeout(() => {
|
||||
@ -166,6 +194,7 @@
|
||||
on:timeupdate={updateTime}
|
||||
on:loadedmetadata={updateTime}
|
||||
on:ended={nextTrack}
|
||||
preload="auto"
|
||||
></audio>
|
||||
|
||||
{#if loading}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user