mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-22 07:15:47 +00:00
cb1a110afb
Relocate the playback/queue/transport/audio-output engine out of the type-first
store/ + utils/playback/ + utils/audio/ dirs into a cohesive src/features/playback/,
structure-preserving:
store/<x> -> features/playback/store/<x>
store/audioListenerSetup/<x> -> features/playback/store/audioListenerSetup/<x>
utils/playback/<x> -> features/playback/utils/playback/<x>
utils/audio/<x> -> features/playback/utils/audio/<x>
184 files moved (107 source + 77 tests), 365 consumers rewritten. Pure move — no
behavior change, no state-split (the playerStore state-split stays a separate M5
question). Enabled by this session's decouple seams (artist/offline/orbit/auth →
core registries), so the engine carries no inbound core->feature inversion: store/
now holds only the 50 cross-cutting global stores (auth family, the seams, library
index, UI/settings stores).
KEPT OUT of the move (would re-create global->engine edges): the 3 pure config
helpers utils/audio/{loudnessPreAnalysisSlider,hiResCrossfadeResample} +
utils/playback/autodjOverlapCap (authStore + settings UI read them — they stay in
utils/). Ambiguous view-state stores (eqStore, queueToolbarStore,
playerBarLayoutStore) stay global (no engine imports).
Consumers use DEEP paths (@/features/playback/...), no barrel — matches the lib/
approach and avoids barrel-mock-collapse across the 140 usePlayerStore consumers.
Two tolerated type-only core->feature edges remain (localPlaybackStore->QueueItemRef,
localPlaybackMigration->HotCacheEntry, both erased).
tsc 0, lint 0, full suite 319/2353 green, iron-rule clean (no runtime store->feature
import). Behavior-touching only via the prerequisite bridge seam (already QA-flagged);
the move itself is pure.
102 lines
4.2 KiB
TypeScript
102 lines
4.2 KiB
TypeScript
import type { Track } from '@/features/playback/store/playerStoreTypes';
|
|
import { invoke } from '@tauri-apps/api/core';
|
|
import { setDeferHotCachePrefetch } from '@/utils/cache/hotCacheGate';
|
|
import {
|
|
getPlaybackIndexKey,
|
|
playbackCacheKeyForTrack,
|
|
} from '@/features/playback/utils/playback/playbackServer';
|
|
import { resolvePlaybackUrl } from '@/features/playback/utils/playback/resolvePlaybackUrl';
|
|
import { resolveReplayGainDb } from '@/features/playback/utils/audio/resolveReplayGainDb';
|
|
import { audioPlayHiResBlendArgs } from '@/utils/audio/hiResCrossfadeResample';
|
|
import { useAuthStore } from '@/store/authStore';
|
|
import { getPlayGeneration, setIsAudioPaused } from '@/features/playback/store/engineState';
|
|
import { touchHotCacheOnPlayback } from '@/features/playback/store/hotCacheTouch';
|
|
import { isReplayGainActive, loudnessGainDbForEngineBind } from '@/features/playback/store/loudnessGainCache';
|
|
import { playbackSourceHintForResolvedUrl, recordEnginePlayUrl } from '@/features/playback/store/playbackUrlRouting';
|
|
import { usePlayerStore } from '@/features/playback/store/playerStore';
|
|
|
|
/**
|
|
* Load a track into the Rust engine at `atSeconds`, optionally leaving transport
|
|
* playing or paused. Shared by queue-undo restore and cold-start paused prepare.
|
|
*/
|
|
export function engineLoadTrackAtPosition(opts: {
|
|
generation: number;
|
|
track: Track;
|
|
queue: Track[];
|
|
queueIndex: number;
|
|
atSeconds: number;
|
|
wantPlaying: boolean;
|
|
}): void {
|
|
const { generation, track, queue, queueIndex, atSeconds, wantPlaying } = opts;
|
|
const authState = useAuthStore.getState();
|
|
const vol = usePlayerStore.getState().volume;
|
|
const coldPrev = queueIndex > 0 ? queue[queueIndex - 1] : null;
|
|
const coldNext = queueIndex + 1 < queue.length ? queue[queueIndex + 1] : null;
|
|
const replayGainDb = resolveReplayGainDb(
|
|
track, coldPrev, coldNext,
|
|
isReplayGainActive(), authState.replayGainMode,
|
|
);
|
|
const replayGainPeak = isReplayGainActive() ? (track.replayGainPeak ?? null) : null;
|
|
const playbackCacheSid = playbackCacheKeyForTrack(track);
|
|
const playbackIndexKey = playbackCacheKeyForTrack(track) || getPlaybackIndexKey();
|
|
const url = resolvePlaybackUrl(track.id, playbackCacheSid);
|
|
recordEnginePlayUrl(track.id, url);
|
|
usePlayerStore.setState({
|
|
currentPlaybackSource: playbackSourceHintForResolvedUrl(track.id, playbackCacheSid, url),
|
|
});
|
|
const keepPreloadHint = usePlayerStore.getState().enginePreloadedTrackId === track.id;
|
|
const startPaused = !wantPlaying;
|
|
setDeferHotCachePrefetch(true);
|
|
invoke('audio_play', {
|
|
url,
|
|
volume: vol,
|
|
durationHint: track.duration,
|
|
replayGainDb,
|
|
replayGainPeak,
|
|
loudnessGainDb: loudnessGainDbForEngineBind(track.id),
|
|
preGainDb: authState.replayGainPreGainDb,
|
|
fallbackDb: authState.replayGainFallbackDb,
|
|
manual: false,
|
|
...audioPlayHiResBlendArgs(authState),
|
|
analysisTrackId: track.id,
|
|
serverId: playbackIndexKey || null,
|
|
streamFormatSuffix: track.suffix ?? null,
|
|
startPaused,
|
|
})
|
|
.then(() => {
|
|
if (getPlayGeneration() !== generation) return;
|
|
if (keepPreloadHint) {
|
|
usePlayerStore.setState({ enginePreloadedTrackId: null });
|
|
}
|
|
const dur = track.duration && track.duration > 0 ? track.duration : null;
|
|
const seekTo = Math.max(0, atSeconds);
|
|
const canSeek = seekTo > 0.05 && (dur == null || seekTo < dur - 0.05);
|
|
const afterSeek = () => {
|
|
if (getPlayGeneration() !== generation) return;
|
|
if (!wantPlaying) {
|
|
if (!startPaused) {
|
|
invoke('audio_pause').catch(console.error);
|
|
}
|
|
setIsAudioPaused(true);
|
|
usePlayerStore.setState({ isPlaying: false });
|
|
} else {
|
|
setIsAudioPaused(false);
|
|
}
|
|
};
|
|
if (canSeek) {
|
|
void invoke('audio_seek', { seconds: seekTo }).then(afterSeek).catch(afterSeek);
|
|
} else {
|
|
afterSeek();
|
|
}
|
|
})
|
|
.catch((err: unknown) => {
|
|
if (getPlayGeneration() !== generation) return;
|
|
console.error('[psysonic] engineLoadTrackAtPosition failed:', err);
|
|
usePlayerStore.setState({ isPlaying: false });
|
|
})
|
|
.finally(() => {
|
|
setDeferHotCachePrefetch(false);
|
|
});
|
|
touchHotCacheOnPlayback(track.id, playbackCacheSid);
|
|
}
|