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 currentTrackIndex = 0;
|
||||||
let currentTrack = null;
|
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 () => {
|
onMount(async () => {
|
||||||
await loadTracks();
|
await loadTracks();
|
||||||
});
|
});
|
||||||
@ -33,7 +58,8 @@
|
|||||||
try {
|
try {
|
||||||
loadingProgress = 10;
|
loadingProgress = 10;
|
||||||
|
|
||||||
const trackFiles = ['mgxleepe.flac', 'jifmachinecd.flac'];
|
const trackFiles = ['mgxleepe.flac', 'jifmachinecd.flac', 'psshvec.flac', 'zimaooes.flac'];
|
||||||
|
shuffleInPlace(trackFiles);
|
||||||
tracks = [];
|
tracks = [];
|
||||||
|
|
||||||
for (let i = 0; i < trackFiles.length; i++) {
|
for (let i = 0; i < trackFiles.length; i++) {
|
||||||
@ -41,7 +67,8 @@
|
|||||||
loadingProgress = 20 + (i * 60 / trackFiles.length);
|
loadingProgress = 20 + (i * 60 / trackFiles.length);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/music/${filename}`);
|
const url = `/music/${filename}`;
|
||||||
|
const response = await fetch(url, { cache: 'force-cache' });
|
||||||
if (!response.ok) continue;
|
if (!response.ok) continue;
|
||||||
|
|
||||||
const arrayBuffer = await response.arrayBuffer();
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
@ -49,7 +76,7 @@
|
|||||||
|
|
||||||
tracks.push({
|
tracks.push({
|
||||||
filename,
|
filename,
|
||||||
src: `/music/${filename}`,
|
src: url,
|
||||||
metadata,
|
metadata,
|
||||||
title: metadata.common.title || filename.replace('.flac', ''),
|
title: metadata.common.title || filename.replace('.flac', ''),
|
||||||
artist: metadata.common.artist || 'Unknown Artist',
|
artist: metadata.common.artist || 'Unknown Artist',
|
||||||
@ -67,7 +94,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadingProgress = 90;
|
loadingProgress = 90;
|
||||||
currentTrack = tracks[0];
|
currentTrackIndex = getSecureRandomIntExclusive(tracks.length);
|
||||||
|
currentTrack = tracks[currentTrackIndex];
|
||||||
loadingProgress = 100;
|
loadingProgress = 100;
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@ -166,6 +194,7 @@
|
|||||||
on:timeupdate={updateTime}
|
on:timeupdate={updateTime}
|
||||||
on:loadedmetadata={updateTime}
|
on:loadedmetadata={updateTime}
|
||||||
on:ended={nextTrack}
|
on:ended={nextTrack}
|
||||||
|
preload="auto"
|
||||||
></audio>
|
></audio>
|
||||||
|
|
||||||
{#if loading}
|
{#if loading}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user