feat: playlist management, gapless playback, ReplayGain, Qobuz theme
Playlist management: - Add/remove tracks from playlists via right-click context menu - Create new playlists (right-click Playlists sidebar header) - Delete playlists with confirmation dialog (right-click playlist item) - Playlist view removes track immediately on delete (optimistic) - Deleting currently-open playlist clears the track view Gapless playback: - Single long-running audio thread owns AudioOutput; CPAL stream stays open between tracks eliminating device teardown/startup gap - Decode runs inline on the audio thread; command channel polled via try_recv() so Pause/Resume/Seek/Stop/Play all work without spawning - New Play command arriving mid-decode is handled immediately, reusing the same audio output for zero-gap transition - Position timer reduced from 500 ms to 50 ms for faster track-end detection - URL/metadata prefetch: when gapless is enabled Qt pre-fetches the next track while the current one is still playing ReplayGain: - Toggled in Settings → Playback - replaygain_track_gain (dB) from track audio_info converted to linear gain factor and applied per-sample alongside volume Qobuz dark theme: - Background #191919, base #141414, accent #FFB232 (yellow-orange) - Selection highlight, slider fill, scrollbar hover all use #FFB232 - Links use Qobuz blue #46B3EE - Hi-res H badges updated to #FFB232 (from #FFD700) - Now-playing row uses #FFB232 (was Spotify green) - QSS stylesheet for scrollbars, menus, inputs, buttons, groups Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,7 @@ use symphonia::core::{
|
||||
units::Time,
|
||||
};
|
||||
|
||||
use crate::player::{output::AudioOutput, PlayerStatus};
|
||||
use super::{output::AudioOutput, PlayerCommand, PlayerStatus, TrackInfo};
|
||||
|
||||
/// First 512 KiB of stream kept in memory to support backward seeks during probing.
|
||||
const HEAD_SIZE: usize = 512 * 1024;
|
||||
@@ -127,13 +127,22 @@ impl MediaSource for HttpStreamSource {
|
||||
}
|
||||
}
|
||||
|
||||
/// Stream and decode audio from `url`. Runs on a dedicated OS thread.
|
||||
pub fn play_track(
|
||||
/// Decode and play `url` inline on the calling thread (the player loop).
|
||||
///
|
||||
/// `audio_output` is reused across calls if the sample rate and channel count match,
|
||||
/// keeping the CPAL stream open between tracks for gapless playback.
|
||||
///
|
||||
/// Returns:
|
||||
/// - `Ok(Some(TrackInfo))` — a new Play command arrived; start that track next.
|
||||
/// - `Ok(None)` — track finished naturally or was stopped.
|
||||
/// - `Err(_)` — unrecoverable playback error.
|
||||
pub fn play_track_inline(
|
||||
url: &str,
|
||||
status: &PlayerStatus,
|
||||
stop: &Arc<AtomicBool>,
|
||||
paused: &Arc<AtomicBool>,
|
||||
) -> Result<()> {
|
||||
audio_output: &mut Option<AudioOutput>,
|
||||
cmd_rx: &std::sync::mpsc::Receiver<PlayerCommand>,
|
||||
) -> Result<Option<TrackInfo>> {
|
||||
let response = reqwest::blocking::get(url)?;
|
||||
let content_length = response.content_length();
|
||||
let source = HttpStreamSource::new(response, content_length);
|
||||
@@ -160,19 +169,91 @@ pub fn play_track(
|
||||
.make(&track.codec_params, &DecoderOptions::default())
|
||||
.map_err(|e| anyhow::anyhow!("decoder init failed: {e}"))?;
|
||||
|
||||
let mut audio_output = AudioOutput::try_open(sample_rate, channels)?;
|
||||
|
||||
loop {
|
||||
if stop.load(Ordering::SeqCst) {
|
||||
break;
|
||||
// Reuse existing audio output if format matches; rebuild only on format change.
|
||||
if let Some(ao) = audio_output.as_ref() {
|
||||
if ao.sample_rate != sample_rate || ao.channels != channels {
|
||||
*audio_output = None; // will be recreated below
|
||||
}
|
||||
while paused.load(Ordering::SeqCst) {
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
if stop.load(Ordering::SeqCst) {
|
||||
return Ok(());
|
||||
}
|
||||
if audio_output.is_none() {
|
||||
*audio_output = Some(AudioOutput::try_open(sample_rate, channels)?);
|
||||
}
|
||||
let ao = audio_output.as_mut().unwrap();
|
||||
|
||||
let mut stopped = false;
|
||||
let mut next_track: Option<TrackInfo> = None;
|
||||
|
||||
'decode: loop {
|
||||
// Non-blocking command check — handle Pause/Resume/Seek/Stop/Play
|
||||
loop {
|
||||
match cmd_rx.try_recv() {
|
||||
Ok(PlayerCommand::Pause) => {
|
||||
paused.store(true, Ordering::SeqCst);
|
||||
*status.state.lock().unwrap() = super::PlayerState::Paused;
|
||||
}
|
||||
Ok(PlayerCommand::Resume) => {
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
*status.state.lock().unwrap() = super::PlayerState::Playing;
|
||||
}
|
||||
Ok(PlayerCommand::Seek(s)) => {
|
||||
status.seek_target_secs.store(s, Ordering::Relaxed);
|
||||
status.seek_requested.load(Ordering::SeqCst); // read-side fence
|
||||
status.seek_requested.store(true, Ordering::SeqCst);
|
||||
}
|
||||
Ok(PlayerCommand::SetVolume(v)) => {
|
||||
status.volume.store(v, Ordering::Relaxed);
|
||||
}
|
||||
Ok(PlayerCommand::Stop) => {
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
*status.state.lock().unwrap() = super::PlayerState::Idle;
|
||||
*status.current_track.lock().unwrap() = None;
|
||||
status.position_secs.store(0, Ordering::Relaxed);
|
||||
status.duration_secs.store(0, Ordering::Relaxed);
|
||||
stopped = true;
|
||||
break 'decode;
|
||||
}
|
||||
Ok(PlayerCommand::Play(info)) => {
|
||||
// New track requested — stop current and return it
|
||||
next_track = Some(info);
|
||||
break 'decode;
|
||||
}
|
||||
Err(std::sync::mpsc::TryRecvError::Empty) => break,
|
||||
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
|
||||
stopped = true;
|
||||
break 'decode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Spin while paused, but keep checking for commands
|
||||
while paused.load(Ordering::SeqCst) {
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
// Still check for Stop/Play while paused
|
||||
match cmd_rx.try_recv() {
|
||||
Ok(PlayerCommand::Resume) => {
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
*status.state.lock().unwrap() = super::PlayerState::Playing;
|
||||
}
|
||||
Ok(PlayerCommand::Stop) => {
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
stopped = true;
|
||||
break;
|
||||
}
|
||||
Ok(PlayerCommand::Play(info)) => {
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
next_track = Some(info);
|
||||
break 'decode;
|
||||
}
|
||||
Ok(PlayerCommand::SetVolume(v)) => {
|
||||
status.volume.store(v, Ordering::Relaxed);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if stopped { break 'decode; }
|
||||
}
|
||||
if stopped { break; }
|
||||
|
||||
// Handle seek
|
||||
if status.seek_requested.load(Ordering::SeqCst) {
|
||||
status.seek_requested.store(false, Ordering::SeqCst);
|
||||
let target = status.seek_target_secs.load(Ordering::Relaxed);
|
||||
@@ -190,8 +271,10 @@ pub fn play_track(
|
||||
|
||||
let packet = match format.next_packet() {
|
||||
Ok(p) => p,
|
||||
Err(SymphoniaError::IoError(e)) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
|
||||
break;
|
||||
Err(SymphoniaError::IoError(e))
|
||||
if e.kind() == std::io::ErrorKind::UnexpectedEof =>
|
||||
{
|
||||
break; // natural end of track
|
||||
}
|
||||
Err(SymphoniaError::ResetRequired) => {
|
||||
decoder.reset();
|
||||
@@ -205,13 +288,16 @@ pub fn play_track(
|
||||
}
|
||||
|
||||
if let Some(ts) = packet.ts().checked_div(sample_rate as u64) {
|
||||
status.position_secs.store(ts, std::sync::atomic::Ordering::Relaxed);
|
||||
status.position_secs.store(ts, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
match decoder.decode(&packet) {
|
||||
Ok(decoded) => {
|
||||
let volume = status.volume.load(Ordering::Relaxed) as f32 / 100.0;
|
||||
audio_output.write(decoded, volume, stop)?;
|
||||
let rg = *status.replaygain_gain.lock().unwrap();
|
||||
// Use a stop flag tied to new-track-incoming so write doesn't block
|
||||
let dummy_stop = Arc::new(AtomicBool::new(false));
|
||||
ao.write(decoded, (volume * rg).min(1.0), &dummy_stop)?;
|
||||
}
|
||||
Err(SymphoniaError::IoError(_)) => break,
|
||||
Err(SymphoniaError::DecodeError(e)) => eprintln!("decode error: {e}"),
|
||||
@@ -219,5 +305,10 @@ pub fn play_track(
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
if stopped {
|
||||
// On explicit stop, drop the audio output to silence immediately
|
||||
*audio_output = None;
|
||||
}
|
||||
|
||||
Ok(next_track)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user