use std::io::{Cursor, Read, Seek}; use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::time::{Duration, Instant}; use biquad::{Biquad, Coefficients, DirectForm2Transposed, ToHertz, Type as FilterType}; use rodio::{Sink, Source}; use rodio::source::UniformSourceIterator; use serde::Serialize; use symphonia::core::{ audio::{AudioBufferRef, SampleBuffer, SignalSpec}, codecs::{DecoderOptions, CODEC_TYPE_NULL}, formats::{FormatOptions, FormatReader, SeekMode, SeekTo}, io::{MediaSource, MediaSourceStream}, meta::MetadataOptions, probe::Hint, units::{self, Time}, }; use tauri::{AppHandle, Emitter, State}; // ─── 10-Band Graphic Equalizer ──────────────────────────────────────────────── const EQ_BANDS_HZ: [f32; 10] = [31.0, 62.0, 125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0, 16000.0]; const EQ_Q: f32 = 1.41; const EQ_CHECK_INTERVAL: usize = 1024; struct EqSource> { inner: S, sample_rate: u32, channels: u16, gains: Arc<[AtomicU32; 10]>, enabled: Arc, filters: [[DirectForm2Transposed; 2]; 10], current_gains: [f32; 10], sample_counter: usize, channel_idx: usize, } impl> EqSource { fn new(inner: S, gains: Arc<[AtomicU32; 10]>, enabled: Arc) -> Self { let sample_rate = inner.sample_rate(); let channels = inner.channels(); let filters = std::array::from_fn(|band| { let freq = EQ_BANDS_HZ[band].clamp(20.0, (sample_rate as f32 / 2.0) - 100.0); std::array::from_fn(|_| { let coeffs = Coefficients::::from_params( FilterType::PeakingEQ(0.0), (sample_rate as f32).hz(), freq.hz(), EQ_Q, ).unwrap_or_else(|_| Coefficients::::from_params( FilterType::PeakingEQ(0.0), (sample_rate as f32).hz(), 1000.0f32.hz(), EQ_Q, ).unwrap()); DirectForm2Transposed::::new(coeffs) }) }); Self { inner, sample_rate, channels, gains, enabled, filters, current_gains: [0.0; 10], sample_counter: 0, channel_idx: 0, } } fn refresh_if_needed(&mut self) { for band in 0..10 { let gain_db = f32::from_bits(self.gains[band].load(Ordering::Relaxed)); if (gain_db - self.current_gains[band]).abs() > 0.01 { self.current_gains[band] = gain_db; let freq = EQ_BANDS_HZ[band].clamp(20.0, (self.sample_rate as f32 / 2.0) - 100.0); if let Ok(coeffs) = Coefficients::::from_params( FilterType::PeakingEQ(gain_db), (self.sample_rate as f32).hz(), freq.hz(), EQ_Q, ) { for ch in 0..2 { self.filters[band][ch].update_coefficients(coeffs); } } } } } } impl> Iterator for EqSource { type Item = f32; fn next(&mut self) -> Option { let sample = self.inner.next()?; if self.sample_counter % EQ_CHECK_INTERVAL == 0 { self.refresh_if_needed(); } self.sample_counter = self.sample_counter.wrapping_add(1); if !self.enabled.load(Ordering::Relaxed) { self.channel_idx = (self.channel_idx + 1) % self.channels as usize; return Some(sample); } let ch = self.channel_idx.min(1); self.channel_idx = (self.channel_idx + 1) % self.channels as usize; let mut s = sample; for band in 0..10 { s = self.filters[band][ch].run(s); } Some(s.clamp(-1.0, 1.0)) } } impl> Source for EqSource { fn current_frame_len(&self) -> Option { self.inner.current_frame_len() } fn channels(&self) -> u16 { self.channels } fn sample_rate(&self) -> u32 { self.sample_rate } fn total_duration(&self) -> Option { self.inner.total_duration() } fn try_seek(&mut self, pos: Duration) -> Result<(), rodio::source::SeekError> { // Reset biquad filter state to avoid glitches after seek. for band in 0..10 { let gain_db = f32::from_bits(self.gains[band].load(Ordering::Relaxed)); self.current_gains[band] = gain_db; let freq = EQ_BANDS_HZ[band].clamp(20.0, (self.sample_rate as f32 / 2.0) - 100.0); if let Ok(coeffs) = Coefficients::::from_params( FilterType::PeakingEQ(gain_db), (self.sample_rate as f32).hz(), freq.hz(), EQ_Q, ) { for ch in 0..2 { self.filters[band][ch] = DirectForm2Transposed::::new(coeffs); } } } self.channel_idx = 0; self.sample_counter = 0; self.inner.try_seek(pos) } } // ─── DynSource — type-erased Source wrapper ─────────────────────────────────── // // Allows chaining differently-typed sources (with trimming applied) into a // single concrete type accepted by EqSource>. struct DynSource { inner: Box + Send>, channels: u16, sample_rate: u32, } impl DynSource { fn new(src: impl Source + Send + 'static) -> Self { let channels = src.channels(); let sample_rate = src.sample_rate(); Self { inner: Box::new(src), channels, sample_rate } } } impl Iterator for DynSource { type Item = f32; fn next(&mut self) -> Option { self.inner.next() } } impl Source for DynSource { fn current_frame_len(&self) -> Option { self.inner.current_frame_len() } fn channels(&self) -> u16 { self.channels } fn sample_rate(&self) -> u32 { self.sample_rate } fn total_duration(&self) -> Option { self.inner.total_duration() } fn try_seek(&mut self, pos: Duration) -> Result<(), rodio::source::SeekError> { self.inner.try_seek(pos) } } // ─── EqualPowerFadeIn — per-sample sin(t·π/2) fade-in envelope ─────────────── // // Applied to every new track: // • Crossfade: fade_dur = crossfade_secs → symmetric equal-power fade-in // • Hard cut: fade_dur = 5 ms → micro-fade eliminates DC-click // • Gapless: fade_dur = 0 → unity gain (no modification) // // gain(t) = sin(t · π/2), t ∈ [0, 1) // At t = 0 gain = 0, at t = 1 gain = 1. // Equal-power property: cos²+sin² = 1 → combined with cos fade-out on Track A // the total perceived loudness stays constant across the crossfade. struct EqualPowerFadeIn> { inner: S, sample_count: u64, fade_samples: u64, } impl> EqualPowerFadeIn { fn new(inner: S, fade_dur: Duration) -> Self { let sample_rate = inner.sample_rate(); let channels = inner.channels() as u64; let fade_samples = if fade_dur.is_zero() { 0 } else { (fade_dur.as_secs_f64() * sample_rate as f64 * channels as f64) as u64 }; Self { inner, sample_count: 0, fade_samples } } } impl> Iterator for EqualPowerFadeIn { type Item = f32; fn next(&mut self) -> Option { let sample = self.inner.next()?; let gain = if self.fade_samples == 0 || self.sample_count >= self.fade_samples { 1.0 } else { let t = self.sample_count as f32 / self.fade_samples as f32; (t * std::f32::consts::FRAC_PI_2).sin() }; self.sample_count += 1; Some((sample * gain).clamp(-1.0, 1.0)) } } impl> Source for EqualPowerFadeIn { fn current_frame_len(&self) -> Option { self.inner.current_frame_len() } fn channels(&self) -> u16 { self.inner.channels() } fn sample_rate(&self) -> u32 { self.inner.sample_rate() } fn total_duration(&self) -> Option { self.inner.total_duration() } fn try_seek(&mut self, pos: Duration) -> Result<(), rodio::source::SeekError> { // For mid-track seeks: skip straight to unity gain so the new position // plays at full volume immediately — no audible fade-in glitch. // For seeks to the very start (< 100 ms): keep the micro-fade to // suppress any DC-offset click from the fresh decode. if pos.as_millis() < 100 { self.sample_count = 0; } else { self.sample_count = self.fade_samples; } self.inner.try_seek(pos) } } // ─── TriggeredFadeOut — sample-level cos(t·π/2) fade-out triggered externally ─ // // Every track source is wrapped with this. It passes through at unity gain // until `trigger` is set to true, at which point it reads `fade_total_samples` // and applies a cos(t·π/2) envelope: // gain(t) = cos(t · π/2), t ∈ [0, 1] // At t = 0 gain = 1, at t = 1 gain = 0. // After the fade completes, returns None to exhaust the source. // // Combined with EqualPowerFadeIn (sin curve) on Track B, this gives a // symmetric constant-power crossfade: sin²+cos² = 1. struct TriggeredFadeOut> { inner: S, trigger: Arc, fade_total_samples: Arc, fade_progress: u64, fading: bool, cached_total: u64, } impl> TriggeredFadeOut { fn new(inner: S, trigger: Arc, fade_total_samples: Arc) -> Self { Self { inner, trigger, fade_total_samples, fade_progress: 0, fading: false, cached_total: 0, } } } impl> Iterator for TriggeredFadeOut { type Item = f32; fn next(&mut self) -> Option { // Check trigger on first fade sample only (avoid atomic load per sample). if !self.fading && self.trigger.load(Ordering::Relaxed) { self.fading = true; self.cached_total = self.fade_total_samples.load(Ordering::Relaxed).max(1); self.fade_progress = 0; } if self.fading { if self.fade_progress >= self.cached_total { // Fade complete — exhaust the source. return None; } let sample = self.inner.next()?; let t = self.fade_progress as f32 / self.cached_total as f32; let gain = (t * std::f32::consts::FRAC_PI_2).cos(); self.fade_progress += 1; Some((sample * gain).clamp(-1.0, 1.0)) } else { self.inner.next() } } } impl> Source for TriggeredFadeOut { fn current_frame_len(&self) -> Option { self.inner.current_frame_len() } fn channels(&self) -> u16 { self.inner.channels() } fn sample_rate(&self) -> u32 { self.inner.sample_rate() } fn total_duration(&self) -> Option { self.inner.total_duration() } fn try_seek(&mut self, pos: Duration) -> Result<(), rodio::source::SeekError> { // If we seek back during a fade, cancel the fade. if self.fading { self.fading = false; self.trigger.store(false, Ordering::Relaxed); } self.fade_progress = 0; self.inner.try_seek(pos) } } // ─── NotifyingSource — sets a flag when the inner iterator is exhausted ─────── // // This is the key mechanism for gapless: the progress task polls `done` to know // exactly when source N has finished inside the Sink, without relying on // wall-clock estimation or the unreliable `Sink::empty()`. struct NotifyingSource> { inner: S, done: Arc, signalled: bool, } impl> NotifyingSource { fn new(inner: S, done: Arc) -> Self { Self { inner, done, signalled: false } } } impl> Iterator for NotifyingSource { type Item = f32; fn next(&mut self) -> Option { let sample = self.inner.next(); if sample.is_none() && !self.signalled { self.signalled = true; self.done.store(true, Ordering::SeqCst); } sample } } impl> Source for NotifyingSource { fn current_frame_len(&self) -> Option { self.inner.current_frame_len() } fn channels(&self) -> u16 { self.inner.channels() } fn sample_rate(&self) -> u32 { self.inner.sample_rate() } fn total_duration(&self) -> Option { self.inner.total_duration() } fn try_seek(&mut self, pos: Duration) -> Result<(), rodio::source::SeekError> { // If we seek backwards the source is no longer exhausted. self.signalled = false; self.done.store(false, Ordering::SeqCst); self.inner.try_seek(pos) } } // ─── CountingSource — atomic sample counter for drift-free position tracking ─ // // Wraps the outermost source and increments a shared AtomicU64 on every sample. // The progress task reads this counter and divides by (sample_rate * channels) // to get the exact playback position — no wall-clock drift. struct CountingSource> { inner: S, counter: Arc, } impl> CountingSource { fn new(inner: S, counter: Arc) -> Self { Self { inner, counter } } } impl> Iterator for CountingSource { type Item = f32; fn next(&mut self) -> Option { let sample = self.inner.next(); if sample.is_some() { self.counter.fetch_add(1, Ordering::Relaxed); } sample } } impl> Source for CountingSource { fn current_frame_len(&self) -> Option { self.inner.current_frame_len() } fn channels(&self) -> u16 { self.inner.channels() } fn sample_rate(&self) -> u32 { self.inner.sample_rate() } fn total_duration(&self) -> Option { self.inner.total_duration() } fn try_seek(&mut self, pos: Duration) -> Result<(), rodio::source::SeekError> { // Reset counter only after confirming the inner seek succeeded. // If we reset first and the seek fails, the counter ends up at the // new position while the decoder is still at the old one — causing // a permanent desync between displayed time and actual audio. let result = self.inner.try_seek(pos); if result.is_ok() { let samples = (pos.as_secs_f64() * self.inner.sample_rate() as f64 * self.inner.channels() as f64) as u64; self.counter.store(samples, Ordering::Relaxed); } result } } // ─── SizedCursorSource — MediaSource with correct byte_len ──────────────────── // // rodio's internal ReadSeekSource wraps Cursor> but hardcodes // byte_len() → None. This tells symphonia "stream length unknown", which // prevents the FLAC demuxer from seeking (it validates seek offsets against // the total stream length from byte_len). MP3 is unaffected because its // demuxer uses Xing/LAME headers instead. // // This wrapper provides the actual byte length, fixing seek for all formats. struct SizedCursorSource { inner: Cursor>, len: u64, } impl Read for SizedCursorSource { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { self.inner.read(buf) } } impl Seek for SizedCursorSource { fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { self.inner.seek(pos) } } impl MediaSource for SizedCursorSource { fn is_seekable(&self) -> bool { true } fn byte_len(&self) -> Option { Some(self.len) } } // ─── SizedDecoder — symphonia decoder with correct byte_len ─────────────────── // // Replaces rodio::Decoder::new() which wraps the source in ReadSeekSource // (byte_len = None). This constructs the symphonia pipeline directly, // providing the correct byte_len via SizedCursorSource. // // Implements Iterator + Source — identical interface to // rodio::Decoder, so the rest of the source chain is unchanged. const DECODE_MAX_RETRIES: usize = 3; struct SizedDecoder { decoder: Box, current_frame_offset: usize, format: Box, total_duration: Option