mirror of
https://github.com/pvrzz/atmosphere.git
synced 2026-11-29 05:22:26 +00:00
Song info, save queue as playlist, sleep timer, resume long tracks
- Song info: format, bitrate, sample rate, channels, size, plays, dates, loudness and location - Save the current queue as a playlist from the queue drawer - Sleep timer (15 min to 2 h, or end of this song) that fades out gently; "end of this song" stops exactly at the end instead of starting the next one - Songs over 20 minutes, like DJ mixes, pick up where you left off
This commit is contained in:
@@ -240,6 +240,7 @@ async function init() {
|
||||
bindUpdates();
|
||||
bindPlaybackSettings();
|
||||
bindSearch();
|
||||
bindTools();
|
||||
bindStats();
|
||||
await listen("atmosphere://job", (e) => onJob(e.payload));
|
||||
await listen("atmosphere://probe", (e) => onProbe(e.payload));
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
@@ -341,6 +341,7 @@
|
||||
<button class="icon-btn" id="fxBtn" aria-label="Sound and effects" aria-pressed="false" aria-expanded="false" title="Sound, presets, pitch and tempo"><span class="i" data-icon="adjustments-horizontal"></span></button>
|
||||
<button class="icon-btn" id="lyricsBtn" aria-label="Lyrics" aria-pressed="false" title="Lyrics (L)"><span class="i" data-icon="microphone"></span></button>
|
||||
<button class="icon-btn" id="queueBtn" aria-label="Queue" aria-pressed="false" title="Queue (Q)"><span class="i" data-icon="list"></span></button>
|
||||
<button class="icon-btn" id="sleepBtn" aria-label="Sleep timer" aria-pressed="false" title="Sleep timer" aria-haspopup="menu"><span class="i" data-icon="moon"></span></button>
|
||||
<button class="icon-btn" id="miniBtn" aria-label="Mini player" title="Mini player (P)"><span class="i" data-icon="picture-in-picture"></span></button>
|
||||
<button class="icon-btn" id="tvBtn" aria-label="TV mode" title="TV mode (F)"><span class="i" data-icon="device-tv"></span></button>
|
||||
<button class="icon-btn" id="muteBtn" aria-label="Mute"></button>
|
||||
@@ -396,5 +397,6 @@
|
||||
<script src="connect.js"></script>
|
||||
<script src="updates.js"></script>
|
||||
<script src="search.js"></script>
|
||||
<script src="tools.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -365,6 +365,7 @@ function trackMenu(e, t, ctx, i) {
|
||||
albumKey && { label: "Go to album", run: () => openLib("album", albumKey) },
|
||||
{ label: "Go to artist", run: () => openLib("artist", lnorm(t.albumArtist || primaryArtist(t.artist))) },
|
||||
"-",
|
||||
{ label: "Song info…", run: () => songInfo(t) },
|
||||
{ label: "Edit info…", run: () => editInfo(t) },
|
||||
{ label: "Show lyrics", run: () => { if (Player.current?.id !== t.id) playList(ctx.list, i); openLyrics(); } },
|
||||
{ label: "Show in folder", run: () => invoke("reveal", { path: t.path }) },
|
||||
|
||||
+1
-1
@@ -253,7 +253,7 @@ function renderQueue() {
|
||||
cover(t.art, "lt-cover"), h("div", { class: "lt-main" }, h("span", { class: "lt-title" }, t.title), h("span", { class: "lt-sub" }, t.artist)),
|
||||
isNow ? h("span", { class: "flag" }, "Now") : h("button", { class: "icon-btn", "aria-label": `Remove ${t.title}`, onclick: () => Player.removeAt(i) }, icon("x")));
|
||||
panel.replaceChildren(
|
||||
h("div", { class: "drawer-head" }, h("h2", {}, "Queue"), h("div", { class: "row" }, next.length ? h("button", { class: "btn small ghost", onclick: () => Player.clearUpNext() }, "Clear") : null, h("button", { class: "icon-btn", "aria-label": "Close", onclick: closeDrawers }, icon("x")))),
|
||||
h("div", { class: "drawer-head" }, h("h2", {}, "Queue"), h("div", { class: "row" }, Player.queue.length > 1 ? h("button", { class: "btn small ghost", title: "Save the queue as a playlist", onclick: saveQueueAsPlaylist }, icon("playlist-add"), "Save") : null, next.length ? h("button", { class: "btn small ghost", onclick: () => Player.clearUpNext() }, "Clear") : null, h("button", { class: "icon-btn", "aria-label": "Close", onclick: closeDrawers }, icon("x")))),
|
||||
h("div", { class: "drawer-body" },
|
||||
now ? row(now, Player.index, true) : h("p", { class: "muted pad" }, "Nothing playing."),
|
||||
next.length ? h("h3", { class: "q-sub" }, `Up next · ${next.length}`) : null,
|
||||
|
||||
+8
-1
@@ -37,6 +37,7 @@ const Player = {
|
||||
standbyIndex: -1,
|
||||
deckNodes: null,
|
||||
transitionTimer: 0,
|
||||
stopAfterCurrent: false,
|
||||
crossfade: 0,
|
||||
leveling: false,
|
||||
ctx: null,
|
||||
@@ -290,7 +291,7 @@ const Player = {
|
||||
|
||||
scheduleTransition() {
|
||||
const a = this.audio;
|
||||
if (this.transitionTimer || a.paused || this.standbyIndex < 0 || !isFinite(a.duration)) return;
|
||||
if (this.transitionTimer || this.stopAfterCurrent || a.paused || this.standbyIndex < 0 || !isFinite(a.duration)) return;
|
||||
const rate = a.playbackRate || 1;
|
||||
const remaining = (a.duration - a.currentTime) / rate;
|
||||
const fade = this.fadeFor();
|
||||
@@ -596,6 +597,12 @@ Player.on = (type, fn) => Player.decks.forEach((el) => el.addEventListener(type,
|
||||
on("ended", () => {
|
||||
Player.cancelTransition();
|
||||
Player.flushListen();
|
||||
if (Player.stopAfterCurrent) {
|
||||
Player.stopAfterCurrent = false;
|
||||
Player.emit("state");
|
||||
Player.emit("sleep");
|
||||
return;
|
||||
}
|
||||
if (Player.standbyIndex >= 0 && Player.standbyIndex === Player.nextIndex()) Player.load(Player.standbyIndex);
|
||||
else Player.next(true);
|
||||
});
|
||||
|
||||
@@ -1129,3 +1129,15 @@ body.mini-mode > *:not(#mini):not(.toasts):not(.modal-wrap):not(script) { displa
|
||||
.about .source { margin-top: var(--s2); font-size: 12px; }
|
||||
.row.between { justify-content: space-between; align-items: center; gap: var(--s3); }
|
||||
.row.between p { margin: 0; flex: 1; }
|
||||
|
||||
/* song info and sleep */
|
||||
.info-head { display: grid; grid-template-columns: 64px minmax(0, 1fr); gap: var(--s3); align-items: center; }
|
||||
.info-art { width: 64px; height: 64px; border-radius: 8px; object-fit: cover; }
|
||||
.info-title { font: 650 16px/1.3 var(--font-display); }
|
||||
.stack.tight { gap: 2px; }
|
||||
.info-grid { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 6px var(--s4); margin: 0; font-size: 13px; }
|
||||
.info-grid dt { color: var(--text-3); }
|
||||
.info-grid dd { margin: 0; color: var(--text); overflow-wrap: anywhere; }
|
||||
.info-path { display: grid; gap: var(--s2); justify-items: start; }
|
||||
.info-path code { font-size: 12px; color: var(--text-2); overflow-wrap: anywhere; padding: 6px 10px; border-radius: 8px; background: var(--surface-2, var(--surface)); border: 1px solid var(--hairline); width: 100%; box-sizing: border-box; }
|
||||
#sleepBtn[aria-pressed="true"] { color: var(--accent); }
|
||||
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
// atmosphere · tools.js · Everyday player tools: song info, save the queue as a playlist, sleep timer and resuming long tracks
|
||||
const Sleep = { until: 0, endOfSong: false, timer: 0, tick: 0, fading: false };
|
||||
const RESUME_KEY = "atmo.resume";
|
||||
const RESUME_MIN_MS = 20 * 60 * 1000;
|
||||
|
||||
function fmtDate(ms) {
|
||||
return ms ? new Intl.DateTimeFormat(undefined, { dateStyle: "medium", timeStyle: "short" }).format(new Date(ms)) : "Never";
|
||||
}
|
||||
|
||||
function songInfo(t) {
|
||||
const last = Lib.data?.lastPlayed?.[t.id];
|
||||
const rate = t.sampleRate ? `${(t.sampleRate / 1000).toFixed(t.sampleRate % 1000 ? 1 : 0)} kHz` : null;
|
||||
const ch = t.channels === 1 ? "mono" : t.channels === 2 ? "stereo" : t.channels ? `${t.channels} channels` : null;
|
||||
const rows = [
|
||||
["Format", [String(t.format || "").toUpperCase(), t.bitrate ? `${t.bitrate} kbps` : null, rate, ch].filter(Boolean).join(" · ")],
|
||||
["Length", fmtDur(t.durationMs || 0)],
|
||||
["Size", t.size ? fmtBytes(t.size) : null],
|
||||
["Album", [t.album, t.trackNo ? `track ${t.trackNo}${t.trackTotal ? ` of ${t.trackTotal}` : ""}` : null, t.discNo > 1 ? `disc ${t.discNo}` : null].filter(Boolean).join(" · ") || null],
|
||||
["Year", t.year ? String(t.year) : null],
|
||||
["Genre", t.genre || null],
|
||||
["Plays", String(Lib.plays[t.id] || 0)],
|
||||
["Last played", last ? fmtDate(last * (last < 1e12 ? 1000 : 1)) : "Never"],
|
||||
["Added", t.added ? fmtDate(t.added * (t.added < 1e12 ? 1000 : 1)) : null],
|
||||
["Lyrics", t.hasLyrics ? "Saved with the song" : "Not saved yet"],
|
||||
["Loudness", typeof t.gainDb === "number" ? `${t.gainDb > 0 ? "+" : ""}${t.gainDb} dB to even out` : "Not measured yet"],
|
||||
["Playlist", t.playlist || null],
|
||||
].filter(([, v]) => v);
|
||||
const body = h("div", { class: "stack" },
|
||||
h("div", { class: "info-head" }, cover(t.art, "info-art", t.title), h("div", { class: "stack tight" }, h("strong", { class: "info-title" }, t.title), h("span", { class: "muted" }, t.artist || "Unknown artist"))),
|
||||
h("dl", { class: "info-grid" }, ...rows.flatMap(([k, v]) => [h("dt", {}, k), h("dd", {}, v)])),
|
||||
h("div", { class: "info-path" }, h("code", {}, t.path), h("button", { class: "btn small", type: "button", onclick: () => invoke("reveal", { path: t.path }) }, icon("folder-open"), "Show in folder")));
|
||||
modal("Song info", body, [{ label: "Edit info…", value: "edit" }, { label: "Close", value: false, primary: true }]).then((v) => v === "edit" && editInfo(t));
|
||||
}
|
||||
|
||||
async function saveQueueAsPlaylist() {
|
||||
const tracks = Player.queue.filter(Boolean);
|
||||
if (!tracks.length) return toast("The queue is empty.", "");
|
||||
const d = new Date();
|
||||
const input = h("input", { class: "text wide", value: `Queue ${d.toLocaleDateString()}`, "aria-label": "Playlist name", spellcheck: "false" });
|
||||
const ok = await modal("Save queue as a playlist", h("div", { class: "stack" }, input, h("p", { class: "muted small" }, `${plural(tracks.length, "song")}, in the order they're queued.`)), [{ label: "Cancel", value: false }, { label: "Save", value: true, primary: true }]);
|
||||
const name = input.value.trim();
|
||||
if (!ok || !name) return;
|
||||
try {
|
||||
const created = await invoke("playlist_create", { name });
|
||||
await invoke("playlist_add", { name: created, ids: tracks.map((t) => t.id) });
|
||||
toast(`Saved “${created}”`, "ok");
|
||||
} catch (e) {
|
||||
toast(errText(e), "err");
|
||||
}
|
||||
}
|
||||
|
||||
function sleepActive() {
|
||||
return Sleep.endOfSong || Sleep.until > Date.now();
|
||||
}
|
||||
|
||||
function sleepLabel() {
|
||||
if (Sleep.endOfSong) return "Stops after this song";
|
||||
const left = Math.max(0, Sleep.until - Date.now());
|
||||
const m = Math.ceil(left / 60000);
|
||||
return m >= 60 ? `Stops in ${Math.floor(m / 60)} h ${m % 60} min` : `Stops in ${m} min`;
|
||||
}
|
||||
|
||||
function renderSleep() {
|
||||
const b = $("#sleepBtn");
|
||||
if (!b) return;
|
||||
const on = sleepActive();
|
||||
b.setAttribute("aria-pressed", String(on));
|
||||
b.title = on ? `Sleep timer: ${sleepLabel().toLowerCase()}` : "Sleep timer";
|
||||
}
|
||||
|
||||
function cancelSleep(quiet = false) {
|
||||
clearTimeout(Sleep.timer);
|
||||
clearInterval(Sleep.tick);
|
||||
Sleep.until = 0;
|
||||
Sleep.endOfSong = false;
|
||||
Player.stopAfterCurrent = false;
|
||||
renderSleep();
|
||||
if (!quiet) toast("Sleep timer off", "");
|
||||
}
|
||||
|
||||
async function sleepNow() {
|
||||
if (Sleep.fading) return;
|
||||
Sleep.fading = true;
|
||||
const start = Player.volume;
|
||||
const steps = 40;
|
||||
for (let i = 1; i <= steps; i++) {
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
Player.setVolume(start * (1 - i / steps));
|
||||
}
|
||||
Player.pause();
|
||||
Player.setVolume(start);
|
||||
Sleep.fading = false;
|
||||
cancelSleep(true);
|
||||
toast("Sleep timer stopped the music. Good night.", "");
|
||||
}
|
||||
|
||||
function setSleep(minutes) {
|
||||
cancelSleep(true);
|
||||
if (minutes === "song") {
|
||||
Sleep.endOfSong = true;
|
||||
Player.stopAfterCurrent = true;
|
||||
Player.cancelTransition();
|
||||
} else {
|
||||
Sleep.until = Date.now() + minutes * 60000;
|
||||
Sleep.timer = setTimeout(sleepNow, Math.max(0, minutes * 60000 - 8000));
|
||||
Sleep.tick = setInterval(renderSleep, 30000);
|
||||
}
|
||||
renderSleep();
|
||||
toast(sleepLabel(), "ok");
|
||||
}
|
||||
|
||||
function sleepMenu(e) {
|
||||
e.stopPropagation();
|
||||
const r = e.currentTarget.getBoundingClientRect();
|
||||
const opt = (label, v) => ({ label, run: () => setSleep(v) });
|
||||
menuAt(r.left, r.top - 8, [
|
||||
sleepActive() ? { label: `${sleepLabel()} · Turn off`, run: () => cancelSleep() } : null,
|
||||
sleepActive() ? "-" : null,
|
||||
opt("15 minutes", 15), opt("30 minutes", 30), opt("45 minutes", 45), opt("1 hour", 60), opt("2 hours", 120),
|
||||
opt("End of this song", "song"),
|
||||
].filter(Boolean));
|
||||
}
|
||||
|
||||
function resumeMap() {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(RESUME_KEY) || "{}");
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function saveResume() {
|
||||
const t = Player.current;
|
||||
const a = Player.audio;
|
||||
if (!t || (t.durationMs || 0) < RESUME_MIN_MS || !isFinite(a.duration)) return;
|
||||
const map = resumeMap();
|
||||
if (a.currentTime > 30 && a.currentTime < a.duration * 0.97) map[t.id] = Math.floor(a.currentTime);
|
||||
else delete map[t.id];
|
||||
const keys = Object.keys(map);
|
||||
if (keys.length > 60) delete map[keys[0]];
|
||||
try {
|
||||
localStorage.setItem(RESUME_KEY, JSON.stringify(map));
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function bindTools() {
|
||||
$("#sleepBtn")?.addEventListener("click", sleepMenu);
|
||||
let lastSave = 0;
|
||||
window.addEventListener("player", (e) => {
|
||||
const type = e.detail.type;
|
||||
if (type === "time" && Date.now() - lastSave > 10000) {
|
||||
lastSave = Date.now();
|
||||
saveResume();
|
||||
}
|
||||
if (type === "sleep" && Sleep.endOfSong) {
|
||||
cancelSleep(true);
|
||||
toast("Sleep timer stopped the music. Good night.", "");
|
||||
}
|
||||
if (type === "track") {
|
||||
if (Sleep.endOfSong && !Player.stopAfterCurrent) Player.stopAfterCurrent = true;
|
||||
const t = Player.current;
|
||||
const at = t && (t.durationMs || 0) >= RESUME_MIN_MS ? resumeMap()[t.id] : 0;
|
||||
if (at) {
|
||||
const seekTo = Math.max(0, at - 5);
|
||||
const apply = () => {
|
||||
if (Player.current?.id === t.id && Player.audio.currentTime < 5) {
|
||||
Player.seek(seekTo);
|
||||
toast(`Picked up where you left off (${fmtDur(seekTo * 1000)})`, "");
|
||||
}
|
||||
};
|
||||
Player.audio.readyState >= 1 ? apply() : Player.audio.addEventListener("loadedmetadata", apply, { once: true });
|
||||
}
|
||||
}
|
||||
});
|
||||
Player.on("pause", saveResume);
|
||||
renderSleep();
|
||||
}
|
||||
Reference in New Issue
Block a user