@doitian/dsh-music 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/netease.js +12 -1
- package/lib/panel.html +121 -36
- package/lib/router.js +38 -2
- package/package.json +1 -1
package/lib/netease.js
CHANGED
|
@@ -529,11 +529,22 @@ export class Netease {
|
|
|
529
529
|
return data?.data?.[0];
|
|
530
530
|
}
|
|
531
531
|
|
|
532
|
-
/**
|
|
532
|
+
/**
|
|
533
|
+
* Synced lyric text. `noLyric: true` marks an instrumental.
|
|
534
|
+
*
|
|
535
|
+
* A refusal (rate limiting, a risk check) carries no lyric either, so it
|
|
536
|
+
* throws rather than reading as an instrumental the caches would keep.
|
|
537
|
+
*/
|
|
533
538
|
async lyric(id) {
|
|
534
539
|
const data = await this.call('/api/song/lyric', {
|
|
535
540
|
query: { id: Number(id), lv: 1, kv: 1, tv: -1 },
|
|
536
541
|
});
|
|
542
|
+
if (data?.code !== 200) {
|
|
543
|
+
throw new NeteaseError(data?.message || `lyrics refused (code ${data?.code})`, {
|
|
544
|
+
code: data?.code,
|
|
545
|
+
endpoint: 'song/lyric',
|
|
546
|
+
});
|
|
547
|
+
}
|
|
537
548
|
return {
|
|
538
549
|
lrc: data?.lrc?.lyric ?? '',
|
|
539
550
|
translated: data?.tlyric?.lyric ?? '',
|
package/lib/panel.html
CHANGED
|
@@ -182,9 +182,10 @@
|
|
|
182
182
|
.secondary { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12px; }
|
|
183
183
|
.secondary select, .secondary > button { padding: 4px 8px; font-size: 12px; }
|
|
184
184
|
#quality-info { color: var(--muted); }
|
|
185
|
+
.volume-control { display: flex; align-items: center; gap: 8px; flex: none; }
|
|
185
186
|
#mute { background: none; border-color: transparent; padding: 4px; color: var(--muted); }
|
|
186
187
|
#mute:hover:not(:disabled) { color: var(--fg); }
|
|
187
|
-
#volume {
|
|
188
|
+
#volume { width: 96px; flex: none; }
|
|
188
189
|
|
|
189
190
|
.info-card { display: flex; flex-direction: column; min-height: 0; }
|
|
190
191
|
.tabs { display: flex; gap: 14px; }
|
|
@@ -401,8 +402,10 @@
|
|
|
401
402
|
<select id="quality" data-i18n-title="qualityTitle" title="Streaming quality"></select>
|
|
402
403
|
<span id="quality-info"></span>
|
|
403
404
|
<span class="spacer"></span>
|
|
404
|
-
<
|
|
405
|
-
|
|
405
|
+
<div class="volume-control">
|
|
406
|
+
<button id="mute" data-i18n-title="mute" title="Mute"></button>
|
|
407
|
+
<input type="range" id="volume" min="0" max="1" step="0.01" value="0.8" />
|
|
408
|
+
</div>
|
|
406
409
|
</div>
|
|
407
410
|
|
|
408
411
|
<div class="card info-card">
|
|
@@ -926,6 +929,10 @@
|
|
|
926
929
|
let lyricTrackId = null;
|
|
927
930
|
/** Bumped per request so a slow reply for a skipped track is discarded. */
|
|
928
931
|
let lyricRequest = 0;
|
|
932
|
+
/** Lyric answers by track, the last 30 kept, so a prefetched track paints without a loading flash. */
|
|
933
|
+
const lyricCache = new Map();
|
|
934
|
+
/** Lyric requests in flight, so playback and a prefetch share one. */
|
|
935
|
+
const lyricLoads = new Map();
|
|
929
936
|
/** Local-storage key for which pane the info card shows: `details` or `lyrics`. */
|
|
930
937
|
const INFO_VIEW_PREF = 'dsh-music.info.view';
|
|
931
938
|
let infoView = readInfoViewPref();
|
|
@@ -1221,6 +1228,7 @@
|
|
|
1221
1228
|
syncInfo(track?.id ?? null);
|
|
1222
1229
|
loadIntro();
|
|
1223
1230
|
prefetchNextDetails();
|
|
1231
|
+
prefetchNextLyrics();
|
|
1224
1232
|
setIcon($('play'), state.playing ? 'pause' : 'play', { filled: true });
|
|
1225
1233
|
setText($('mode'), { list: t('modeList'), single: t('modeSingle'), shuffle: t('modeShuffle') }[state.mode] || t('modeList'));
|
|
1226
1234
|
$('queue-clear').disabled = (state.queue?.length ?? 0) <= (state.current ? 1 : 0);
|
|
@@ -1637,18 +1645,70 @@
|
|
|
1637
1645
|
return detailsLoads.get(id);
|
|
1638
1646
|
}
|
|
1639
1647
|
|
|
1648
|
+
/**
|
|
1649
|
+
* The track that plays after this one, wrapping past the end as list
|
|
1650
|
+
* playback does. Shuffle has no known next track, and repeat-one's next
|
|
1651
|
+
* is this one, so both answer null.
|
|
1652
|
+
*/
|
|
1653
|
+
function nextTrack() {
|
|
1654
|
+
if (!state?.current || state.mode !== 'list') return null;
|
|
1655
|
+
const next = state.queue?.[(state.index + 1) % state.queue.length];
|
|
1656
|
+
return next?.id !== state.current.id ? next : null;
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1640
1659
|
/**
|
|
1641
1660
|
* Fetch the next track's details ahead, so they show the moment it
|
|
1642
1661
|
* starts. Lazy like the introduction: only after the shown track's
|
|
1643
1662
|
* details have painted, and only while the details are shown.
|
|
1644
|
-
* Shuffle has no known next track, and repeat-one's next is this one.
|
|
1645
1663
|
*/
|
|
1646
1664
|
function prefetchNextDetails() {
|
|
1647
1665
|
if (infoView !== 'details' || !detailsTrackId || !detailsCache.has(detailsTrackId)) return;
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
if (
|
|
1651
|
-
|
|
1666
|
+
const next = nextTrack();
|
|
1667
|
+
if (!next?.id) return;
|
|
1668
|
+
if (detailsCache.has(next.id)) {
|
|
1669
|
+
prefetchNextIntro();
|
|
1670
|
+
return;
|
|
1671
|
+
}
|
|
1672
|
+
if (detailsLoads.has(next.id)) return;
|
|
1673
|
+
fetchDetails(next.id).then(prefetchNextIntro).catch(() => { /* the track's own load retries */ });
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
/**
|
|
1677
|
+
* Write the next track's introduction ahead. Each one is a model call,
|
|
1678
|
+
* so it waits until this track is past the skip window, when the next
|
|
1679
|
+
* is likely to be heard. At the end of the queue the DJ tops up before
|
|
1680
|
+
* playback wraps, so the first track is not written for while it is on.
|
|
1681
|
+
*/
|
|
1682
|
+
function prefetchNextIntro() {
|
|
1683
|
+
if (infoView !== 'details' || !state?.playing || detailsTrackId !== state.current?.id) return;
|
|
1684
|
+
// Match the host's skip window: 30 seconds, or a quarter of a longer track.
|
|
1685
|
+
if (positionMs() < Math.max(30_000, durationMs() * 0.25)) return;
|
|
1686
|
+
if (state.dj?.enabled && state.index + 1 >= state.queue.length) return;
|
|
1687
|
+
const next = nextTrack();
|
|
1688
|
+
if (next?.id && detailsCache.has(next.id)) void fetchIntro(next.id, LANG);
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
/** One track's lyrics, fetched once however many ask, and kept. */
|
|
1692
|
+
function fetchLyrics(id) {
|
|
1693
|
+
if (!lyricLoads.has(id)) {
|
|
1694
|
+
const load = api('/lyric/' + id)
|
|
1695
|
+
.then((data) => {
|
|
1696
|
+
lyricCache.set(id, data);
|
|
1697
|
+
if (lyricCache.size > 30) lyricCache.delete(lyricCache.keys().next().value);
|
|
1698
|
+
return data;
|
|
1699
|
+
})
|
|
1700
|
+
.finally(() => lyricLoads.delete(id));
|
|
1701
|
+
lyricLoads.set(id, load);
|
|
1702
|
+
}
|
|
1703
|
+
return lyricLoads.get(id);
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
/** Fetch the next track's lyrics ahead, once the shown ones have painted and while the lyrics are shown. */
|
|
1707
|
+
function prefetchNextLyrics() {
|
|
1708
|
+
if (infoView !== 'lyrics' || lyricTrackId !== state?.current?.id || !lyricCache.has(lyricTrackId)) return;
|
|
1709
|
+
const next = nextTrack();
|
|
1710
|
+
if (!next?.id || lyricCache.has(next.id) || lyricLoads.has(next.id)) return;
|
|
1711
|
+
fetchLyrics(next.id).catch(() => { /* the track's own load retries */ });
|
|
1652
1712
|
}
|
|
1653
1713
|
|
|
1654
1714
|
async function loadDetails(id) {
|
|
@@ -1694,23 +1754,30 @@
|
|
|
1694
1754
|
*
|
|
1695
1755
|
* Lazy on purpose: a model call takes seconds, so it waits until the
|
|
1696
1756
|
* details have painted — playback and the NetEase facts never wait on
|
|
1697
|
-
* it
|
|
1757
|
+
* it. Prefetching the next introduction uses the same cache.
|
|
1698
1758
|
*/
|
|
1699
|
-
|
|
1759
|
+
function loadIntro() {
|
|
1700
1760
|
if (infoView !== 'details' || !detailsTrackId || !detailsCache.has(detailsTrackId)) return;
|
|
1701
|
-
const
|
|
1702
|
-
|
|
1761
|
+
const cached = intros.get(`${detailsTrackId}:${LANG}`);
|
|
1762
|
+
if (typeof cached === 'object') introsUnavailable = !cached.text;
|
|
1763
|
+
void fetchIntro(detailsTrackId, LANG);
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
async function fetchIntro(id, lang) {
|
|
1767
|
+
const key = `${id}:${lang}`;
|
|
1703
1768
|
if (intros.has(key)) return;
|
|
1704
1769
|
intros.set(key, 'pending');
|
|
1705
1770
|
if (intros.size > 60) intros.delete(intros.keys().next().value);
|
|
1706
1771
|
let text = '';
|
|
1707
1772
|
try {
|
|
1708
|
-
const answer = await api(`/intro/${id}?lang=${
|
|
1773
|
+
const answer = await api(`/intro/${id}?lang=${lang}`);
|
|
1709
1774
|
text = typeof answer?.text === 'string' ? answer.text : '';
|
|
1710
1775
|
} catch { /* NetEase's prose stands in */ }
|
|
1711
1776
|
intros.set(key, { text });
|
|
1712
|
-
|
|
1713
|
-
|
|
1777
|
+
if (infoView === 'details' && detailsTrackId === id && LANG === lang) {
|
|
1778
|
+
introsUnavailable = !text;
|
|
1779
|
+
repaintDetails({ keepScroll: true });
|
|
1780
|
+
}
|
|
1714
1781
|
}
|
|
1715
1782
|
|
|
1716
1783
|
/** NetEase dates are midnight in China; read them there, as YYYY-MM-DD. */
|
|
@@ -1799,35 +1866,45 @@
|
|
|
1799
1866
|
const request = ++lyricRequest;
|
|
1800
1867
|
lyric = null;
|
|
1801
1868
|
lyricLine = -1;
|
|
1869
|
+
if (lyricCache.has(id)) {
|
|
1870
|
+
paintLyricData(lyricCache.get(id));
|
|
1871
|
+
setTimeout(prefetchNextLyrics);
|
|
1872
|
+
return;
|
|
1873
|
+
}
|
|
1802
1874
|
$('lyrics').innerHTML = `<p class="hint">${esc(t('loading'))}</p>`;
|
|
1803
1875
|
try {
|
|
1804
|
-
const data = await
|
|
1876
|
+
const data = await fetchLyrics(id);
|
|
1805
1877
|
// Skipping tracks faster than NetEase answers would otherwise let an
|
|
1806
1878
|
// earlier reply land on the newer track's pane.
|
|
1807
|
-
if (request !== lyricRequest) return;
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
return;
|
|
1811
|
-
}
|
|
1812
|
-
const lines = [];
|
|
1813
|
-
for (const raw of data.lrc.split('\n')) {
|
|
1814
|
-
const matches = [...raw.matchAll(/\[(\d+):(\d+(?:[.:]\d+)?)\]/g)];
|
|
1815
|
-
const text = raw.replace(/\[[^\]]*\]/g, '').trim();
|
|
1816
|
-
if (matches.length === 0) continue;
|
|
1817
|
-
for (const match of matches) {
|
|
1818
|
-
const seconds = Number(match[1]) * 60 + Number(String(match[2]).replace(':', '.'));
|
|
1819
|
-
lines.push({ at: seconds * 1000, text });
|
|
1820
|
-
}
|
|
1821
|
-
}
|
|
1822
|
-
lines.sort((a, b) => a.at - b.at);
|
|
1823
|
-
lyric = lines;
|
|
1824
|
-
$('lyrics').innerHTML = lines.map((line, index) => `<p data-line="${index}">${esc(line.text || '♪')}</p>`).join('');
|
|
1879
|
+
if (request !== lyricRequest || lyricTrackId !== id) return;
|
|
1880
|
+
paintLyricData(data);
|
|
1881
|
+
setTimeout(prefetchNextLyrics);
|
|
1825
1882
|
} catch (error) {
|
|
1826
|
-
if (request !== lyricRequest) return;
|
|
1883
|
+
if (request !== lyricRequest || lyricTrackId !== id) return;
|
|
1827
1884
|
$('lyrics').innerHTML = `<p class="hint">${esc(error.message)}</p>`;
|
|
1828
1885
|
}
|
|
1829
1886
|
}
|
|
1830
1887
|
|
|
1888
|
+
function paintLyricData(data) {
|
|
1889
|
+
if (data.noLyric || !data.lrc) {
|
|
1890
|
+
$('lyrics').innerHTML = `<p class="hint">${esc(t('instrumental'))}</p>`;
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
const lines = [];
|
|
1894
|
+
for (const raw of data.lrc.split('\n')) {
|
|
1895
|
+
const matches = [...raw.matchAll(/\[(\d+):(\d+(?:[.:]\d+)?)\]/g)];
|
|
1896
|
+
const text = raw.replace(/\[[^\]]*\]/g, '').trim();
|
|
1897
|
+
if (matches.length === 0) continue;
|
|
1898
|
+
for (const match of matches) {
|
|
1899
|
+
const seconds = Number(match[1]) * 60 + Number(String(match[2]).replace(':', '.'));
|
|
1900
|
+
lines.push({ at: seconds * 1000, text });
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
lines.sort((a, b) => a.at - b.at);
|
|
1904
|
+
lyric = lines;
|
|
1905
|
+
$('lyrics').innerHTML = lines.map((line, index) => `<p data-line="${index}">${esc(line.text || '♪')}</p>`).join('');
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1831
1908
|
function paintLyrics() {
|
|
1832
1909
|
if (!lyric || infoView !== 'lyrics') return;
|
|
1833
1910
|
const at = positionMs();
|
|
@@ -2218,6 +2295,14 @@
|
|
|
2218
2295
|
|
|
2219
2296
|
// ------------------------------------------------------------------ wiring
|
|
2220
2297
|
$('play').onclick = () => control({ action: 'toggle' });
|
|
2298
|
+
document.addEventListener('keydown', (event) => {
|
|
2299
|
+
if (event.key !== ' ' || event.defaultPrevented || event.isComposing ||
|
|
2300
|
+
event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
|
|
2301
|
+
const focused = document.activeElement;
|
|
2302
|
+
if (focused?.isContentEditable || focused?.matches?.('input, textarea, select, button, [role="textbox"]')) return;
|
|
2303
|
+
event.preventDefault();
|
|
2304
|
+
if (!event.repeat) void control({ action: 'toggle' });
|
|
2305
|
+
});
|
|
2221
2306
|
$('next').onclick = () => control({ action: 'next' });
|
|
2222
2307
|
$('prev').onclick = () => control({ action: 'previous' });
|
|
2223
2308
|
$('mode').onclick = () => control({ action: 'mode', value: { list: 'shuffle', shuffle: 'single', single: 'list' }[state?.mode || 'list'] });
|
|
@@ -2403,7 +2488,7 @@
|
|
|
2403
2488
|
// Progress and lyric highlighting follow the audio element, which lives
|
|
2404
2489
|
// in the shell document and reports nothing here, so they need their own
|
|
2405
2490
|
// repaint clock.
|
|
2406
|
-
setInterval(() => { paintProgress(); paintLyrics(); }, 250);
|
|
2491
|
+
setInterval(() => { paintProgress(); paintLyrics(); prefetchNextIntro(); }, 250);
|
|
2407
2492
|
window.addEventListener('beforeunload', () => {
|
|
2408
2493
|
clearInterval(qrTimer);
|
|
2409
2494
|
events?.close();
|
package/lib/router.js
CHANGED
|
@@ -34,6 +34,9 @@ const URL_CACHE_MS = 15 * 60 * 1000;
|
|
|
34
34
|
/** Song details are kept this long: a wiki or an introduction is edited rarely. */
|
|
35
35
|
const INFO_TTL_MS = 6 * 60 * 60 * 1000;
|
|
36
36
|
|
|
37
|
+
/** Lyrics are kept a day: they are rarely corrected, and "instrumental" is an answer too. */
|
|
38
|
+
const LYRIC_TTL_MS = 24 * 60 * 60 * 1000;
|
|
39
|
+
|
|
37
40
|
/** A written introduction is kept longer: each one costs a model call. */
|
|
38
41
|
const INTRO_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
39
42
|
|
|
@@ -146,11 +149,19 @@ export class MusicRouter {
|
|
|
146
149
|
this.lastAudio = null;
|
|
147
150
|
/** Song details for the panel, which barely change; each costs up to six requests. */
|
|
148
151
|
this.infoCache = new TtlCache({ maxEntries: 200 });
|
|
152
|
+
/** Lyrics, so the panel's prefetch of the next track's costs nothing when it plays. */
|
|
153
|
+
this.lyricCache = new TtlCache({ maxEntries: 200 });
|
|
149
154
|
/** Model-written introductions, per track and language. */
|
|
150
155
|
this.introCache = new TtlCache({ maxEntries: 400 });
|
|
151
156
|
this.versions = new SnapshotVersions();
|
|
152
157
|
this.feed = new StateFeed({ snapshot: () => this.snapshot(), logger });
|
|
153
|
-
|
|
158
|
+
/** The current track, next track and level last pre-resolved, so a position report does not repeat it. */
|
|
159
|
+
this.audioPrefetchKey = null;
|
|
160
|
+
this.unsubscribe = player.subscribe(() => {
|
|
161
|
+
this.feed.changed();
|
|
162
|
+
this.prefetchNextAudio();
|
|
163
|
+
});
|
|
164
|
+
this.prefetchNextAudio();
|
|
154
165
|
}
|
|
155
166
|
|
|
156
167
|
/** Close the event streams; the route is being withdrawn. */
|
|
@@ -183,6 +194,28 @@ export class MusicRouter {
|
|
|
183
194
|
|
|
184
195
|
// ------------------------------------------------------------------ audio
|
|
185
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Resolve the next track's CDN URL while this one plays, so a track change
|
|
199
|
+
* skips that round trip. Only in list mode: shuffle has no known next track,
|
|
200
|
+
* and repeat-one's next is this one. Runs whether or not the panel is open.
|
|
201
|
+
*/
|
|
202
|
+
prefetchNextAudio() {
|
|
203
|
+
const current = this.player.current();
|
|
204
|
+
const next = this.player.mode === 'list'
|
|
205
|
+
? this.player.queue[(this.player.index + 1) % this.player.queue.length]
|
|
206
|
+
: null;
|
|
207
|
+
if (!this.player.playing || !current || !next || next.id === current.id) {
|
|
208
|
+
this.audioPrefetchKey = null;
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const level = this.preferredLevel();
|
|
212
|
+
const key = `${current.id}:${next.id}:${level}`;
|
|
213
|
+
if (this.audioPrefetchKey === key) return;
|
|
214
|
+
this.audioPrefetchKey = key;
|
|
215
|
+
// Keep speculative failures out of playback diagnostics and avoid retrying on every report.
|
|
216
|
+
void Promise.resolve().then(() => this.resolveAudio(next.id, level)).catch(() => {});
|
|
217
|
+
}
|
|
218
|
+
|
|
186
219
|
/**
|
|
187
220
|
* Resolve (and cache) the signed CDN URL for one track at one level.
|
|
188
221
|
*
|
|
@@ -606,7 +639,9 @@ export class MusicRouter {
|
|
|
606
639
|
}
|
|
607
640
|
if (route.startsWith('/lyric/') && method === 'GET') {
|
|
608
641
|
const id = Number(route.slice('/lyric/'.length));
|
|
609
|
-
return sendJson(res,
|
|
642
|
+
if (!Number.isInteger(id) || id <= 0) return sendJson(res, 400, { error: 'invalid track id' });
|
|
643
|
+
const lyric = await this.lyricCache.get(`lyric:${id}`, LYRIC_TTL_MS, () => this.api.lyric(id));
|
|
644
|
+
return sendJson(res, 200, lyric);
|
|
610
645
|
}
|
|
611
646
|
if (route.startsWith('/info/') && method === 'GET') {
|
|
612
647
|
const id = Number(route.slice('/info/'.length));
|
|
@@ -731,6 +766,7 @@ export class MusicRouter {
|
|
|
731
766
|
});
|
|
732
767
|
}
|
|
733
768
|
this.store.updateSettings({ level });
|
|
769
|
+
this.prefetchNextAudio();
|
|
734
770
|
this.logger?.info?.(`[music] streaming quality set to ${level} from the player`);
|
|
735
771
|
// The running stream keeps its bytes; the next load or seek uses this.
|
|
736
772
|
return sendJson(res, 200, await this.snapshot());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doitian/dsh-music",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "NetEase Cloud Music (music.163.com) player and AI DJ for DeepSeek Harness: a sidebar page with in-app playback, QR sign-in, and agent tools that curate the queue.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|