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)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ pub struct TrackInfo {
|
||||
pub track: TrackDto,
|
||||
pub url: String,
|
||||
pub format: Format,
|
||||
/// ReplayGain track gain in dB, if enabled and available.
|
||||
pub replaygain_db: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@@ -47,6 +49,8 @@ pub struct PlayerStatus {
|
||||
/// Set by the player loop when a seek command arrives; cleared by the decode thread.
|
||||
pub seek_requested: Arc<AtomicBool>,
|
||||
pub seek_target_secs: Arc<AtomicU64>,
|
||||
/// Linear gain factor to apply (1.0 = unity). Updated each time a new track starts.
|
||||
pub replaygain_gain: Arc<std::sync::Mutex<f32>>,
|
||||
}
|
||||
|
||||
impl PlayerStatus {
|
||||
@@ -60,6 +64,7 @@ impl PlayerStatus {
|
||||
track_finished: Arc::new(AtomicBool::new(false)),
|
||||
seek_requested: Arc::new(AtomicBool::new(false)),
|
||||
seek_target_secs: Arc::new(AtomicU64::new(0)),
|
||||
replaygain_gain: Arc::new(std::sync::Mutex::new(1.0)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,10 +111,6 @@ impl Player {
|
||||
self.cmd_tx.send(cmd).ok();
|
||||
}
|
||||
|
||||
pub fn play_track(&self, track: TrackDto, url: String, format: Format) {
|
||||
self.send(PlayerCommand::Play(TrackInfo { track, url, format }));
|
||||
}
|
||||
|
||||
pub fn pause(&self) {
|
||||
self.send(PlayerCommand::Pause);
|
||||
}
|
||||
@@ -133,68 +134,77 @@ impl Player {
|
||||
}
|
||||
}
|
||||
|
||||
/// The player loop runs on a single dedicated OS thread.
|
||||
/// It owns the `AudioOutput` locally so there are no Send constraints.
|
||||
/// Decoding is performed inline; the command channel is polled via try_recv
|
||||
/// inside the decode loop to handle Pause/Resume/Seek/Stop/Play without
|
||||
/// tearng down and re-opening the audio device between tracks.
|
||||
fn player_loop(rx: std::sync::mpsc::Receiver<PlayerCommand>, status: PlayerStatus) {
|
||||
let mut stop_flag = Arc::new(AtomicBool::new(true));
|
||||
use std::sync::mpsc::RecvTimeoutError;
|
||||
|
||||
let mut audio_output: Option<output::AudioOutput> = None;
|
||||
let paused = Arc::new(AtomicBool::new(false));
|
||||
// pending_info holds a Play command that interrupted an ongoing decode
|
||||
let mut pending_info: Option<TrackInfo> = None;
|
||||
|
||||
loop {
|
||||
match rx.recv_timeout(Duration::from_millis(100)) {
|
||||
Ok(cmd) => match cmd {
|
||||
PlayerCommand::Play(info) => {
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
stop_flag = Arc::new(AtomicBool::new(false));
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
|
||||
*status.state.lock().unwrap() = PlayerState::Playing;
|
||||
*status.current_track.lock().unwrap() = Some(info.track.clone());
|
||||
if let Some(dur) = info.track.duration {
|
||||
status.duration_secs.store(dur as u64, Ordering::Relaxed);
|
||||
'outer: loop {
|
||||
// Wait for a Play command (or use one that was interrupted)
|
||||
let info = if let Some(p) = pending_info.take() {
|
||||
p
|
||||
} else {
|
||||
loop {
|
||||
match rx.recv_timeout(Duration::from_millis(100)) {
|
||||
Ok(PlayerCommand::Play(info)) => break info,
|
||||
Ok(PlayerCommand::Stop) => {
|
||||
audio_output = None;
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
*status.state.lock().unwrap() = PlayerState::Idle;
|
||||
*status.current_track.lock().unwrap() = None;
|
||||
status.position_secs.store(0, Ordering::Relaxed);
|
||||
status.duration_secs.store(0, Ordering::Relaxed);
|
||||
}
|
||||
status.position_secs.store(0, Ordering::Relaxed);
|
||||
Ok(PlayerCommand::SetVolume(v)) => {
|
||||
status.volume.store(v, Ordering::Relaxed);
|
||||
}
|
||||
Ok(PlayerCommand::Seek(s)) => {
|
||||
status.seek_target_secs.store(s, Ordering::Relaxed);
|
||||
status.seek_requested.store(true, Ordering::SeqCst);
|
||||
}
|
||||
Ok(_) => {} // Pause/Resume ignored when idle
|
||||
Err(RecvTimeoutError::Timeout) => {}
|
||||
Err(RecvTimeoutError::Disconnected) => break 'outer,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let status_c = status.clone();
|
||||
let stop_c = stop_flag.clone();
|
||||
let paused_c = paused.clone();
|
||||
// Compute ReplayGain factor
|
||||
let rg_factor = info.replaygain_db
|
||||
.map(|db| 10f32.powf(db as f32 / 20.0))
|
||||
.unwrap_or(1.0);
|
||||
*status.replaygain_gain.lock().unwrap() = rg_factor;
|
||||
|
||||
std::thread::spawn(move || {
|
||||
match decoder::play_track(&info.url, &status_c, &stop_c, &paused_c) {
|
||||
Ok(()) => {
|
||||
if !stop_c.load(Ordering::SeqCst) {
|
||||
*status_c.state.lock().unwrap() = PlayerState::Idle;
|
||||
status_c.track_finished.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("playback error: {e}");
|
||||
*status_c.state.lock().unwrap() =
|
||||
PlayerState::Error(e.to_string());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
PlayerCommand::Pause => {
|
||||
paused.store(true, Ordering::SeqCst);
|
||||
*status.state.lock().unwrap() = PlayerState::Paused;
|
||||
}
|
||||
PlayerCommand::Resume => {
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
*status.state.lock().unwrap() = PlayerState::Playing;
|
||||
}
|
||||
PlayerCommand::Stop => {
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
*status.state.lock().unwrap() = PlayerState::Idle;
|
||||
*status.current_track.lock().unwrap() = None;
|
||||
status.position_secs.store(0, Ordering::Relaxed);
|
||||
status.duration_secs.store(0, Ordering::Relaxed);
|
||||
}
|
||||
PlayerCommand::SetVolume(_) => {}
|
||||
PlayerCommand::Seek(secs) => {
|
||||
status.seek_target_secs.store(secs, Ordering::Relaxed);
|
||||
status.seek_requested.store(true, Ordering::SeqCst);
|
||||
}
|
||||
},
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
|
||||
*status.state.lock().unwrap() = PlayerState::Playing;
|
||||
*status.current_track.lock().unwrap() = Some(info.track.clone());
|
||||
if let Some(dur) = info.track.duration {
|
||||
status.duration_secs.store(dur as u64, Ordering::Relaxed);
|
||||
}
|
||||
status.position_secs.store(0, Ordering::Relaxed);
|
||||
paused.store(false, Ordering::SeqCst);
|
||||
|
||||
match decoder::play_track_inline(&info.url, &status, &paused, &mut audio_output, &rx) {
|
||||
Ok(Some(next_info)) => {
|
||||
// Interrupted by a new Play — loop immediately with reused audio output
|
||||
pending_info = Some(next_info);
|
||||
}
|
||||
Ok(None) => {
|
||||
// Track finished naturally
|
||||
*status.state.lock().unwrap() = PlayerState::Idle;
|
||||
status.track_finished.store(true, Ordering::SeqCst);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("playback error: {e}");
|
||||
*status.state.lock().unwrap() = PlayerState::Error(e.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ const RING_BUFFER_SIZE: usize = 32 * 1024;
|
||||
pub struct AudioOutput {
|
||||
ring_buf_producer: rb::Producer<f32>,
|
||||
_stream: cpal::Stream,
|
||||
pub sample_rate: u32,
|
||||
pub channels: usize,
|
||||
}
|
||||
|
||||
impl AudioOutput {
|
||||
@@ -50,6 +52,8 @@ impl AudioOutput {
|
||||
Ok(Self {
|
||||
ring_buf_producer: producer,
|
||||
_stream: stream,
|
||||
sample_rate,
|
||||
channels,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user