1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! Core player implementation
//!
//! The Player struct orchestrates decoding, output, and playback control.
use std::path::PathBuf;
use std::sync::{ Arc, RwLock };
use std::sync::atomic::{ AtomicBool, AtomicU64, Ordering };
use std::thread;
use std::time::Duration;
use rubato::{ FastFixedOut, PolynomialDegree, Resampler };
use thiserror::Error;
use crate::decoder::{ AudioMetadata, Decoder };
use crate::output::{ AudioOutput, SampleBuffer };
use crate::playlist::Playlist;
/// Converts planar samples back to interleaved format.
/// [[L0, L1, ...], [R0, R1, ...]] → [L0, R0, L1, R1, ...]
fn interleave( channels: &[Vec<f32>] ) -> Vec<f32> {
if channels.is_empty() || channels[ 0 ].is_empty() {
return Vec::new();
}
let frames = channels[ 0 ].len();
let num_ch = channels.len();
let mut out = Vec::with_capacity( frames * num_ch );
for f in 0..frames {
for ch in channels {
out.push( ch[ f ] );
}
}
out
}
/// Errors that can occur during playback.
#[derive( Debug, Error )]
pub enum PlayerError {
#[error( "Failed to open file: {0}" )]
FileOpen( String ),
#[error( "Decode error: {0}" )]
Decode( String ),
#[error( "Audio output error: {0}" )]
Output( String ),
#[error( "No track loaded" )]
NoTrack,
}
/// Current playback state.
#[derive( Debug, Clone, Copy, PartialEq, Eq )]
pub enum PlaybackState {
Stopped,
Playing,
Paused,
}
/// Events emitted by the player for UI updates.
#[derive( Debug, Clone )]
pub enum PlayerEvent {
TrackChanged { path: PathBuf },
StateChanged { state: PlaybackState },
PositionChanged { position: Duration, duration: Duration },
TrackEnded,
Error { message: String },
}
/// Wrapper around AudioOutput that allows it to be stored in shared state.
///
/// SAFETY: AudioOutput must only be accessed from the thread where it was created.
/// The Player ensures this by only accessing the output from the main thread.
#[allow( dead_code )] // Field is kept alive for its Drop impl which stops the audio stream
struct AudioOutputHandle( AudioOutput );
// SAFETY: We guarantee AudioOutput is only used from the main thread.
// cpal::Stream's raw pointers are only accessed by the audio callback thread
// which is managed internally by cpal.
unsafe impl Send for AudioOutputHandle {}
unsafe impl Sync for AudioOutputHandle {}
/// Shared playback state between main thread and decode thread.
struct PlaybackHandle {
stop_flag: Arc<AtomicBool>,
sample_buffer: Arc<SampleBuffer>,
#[allow( dead_code )] // Kept alive for its Drop impl which stops the audio stream
output: AudioOutputHandle,
thread: Option<thread::JoinHandle<()>>,
/// Number of frames (samples / channels) decoded so far
frames_played: Arc<AtomicU64>,
/// Sample rate of the source file
sample_rate: u32,
/// Total duration of the track
duration: Option<Duration>,
/// Flag set when track ends naturally (EOF reached)
track_ended: Arc<AtomicBool>,
/// Metadata extracted from the audio file
metadata: AudioMetadata,
}
/// Core audio player.
pub struct Player {
state: Arc<RwLock<PlaybackState>>,
current_track: Arc<RwLock<Option<PathBuf>>>,
playlist: Arc<RwLock<Playlist>>,
playback: Arc<RwLock<Option<PlaybackHandle>>>,
/// Volume level (0.0 to 1.5), persisted across track changes
volume: Arc<RwLock<f32>>,
}
impl Player {
/// Creates a new Player instance.
pub fn new() -> Result<Self, PlayerError> {
Ok( Self {
state: Arc::new( RwLock::new( PlaybackState::Stopped ) ),
current_track: Arc::new( RwLock::new( None ) ),
playlist: Arc::new( RwLock::new( Playlist::new() ) ),
playback: Arc::new( RwLock::new( None ) ),
volume: Arc::new( RwLock::new( 1.0 ) ),
})
}
/// Starts playback of the specified file.
pub fn play( &self, path: PathBuf ) -> Result<(), PlayerError> {
// Stop any current playback
self.stop()?;
tracing::info!( "Playing: {:?}", path );
// Open the decoder
let mut decoder = Decoder::open( &path )
.map_err( |e| PlayerError::FileOpen( e.to_string() ) )?;
let source_sample_rate = decoder.sample_rate();
let channels = decoder.channels() as u16;
let duration = decoder.duration().map( |secs| Duration::from_secs_f64( secs ) );
let metadata = decoder.metadata();
// Create audio output - this also creates the sample buffer with proper channel config
let ( output, sample_buffer ) = AudioOutput::new( source_sample_rate, channels )
.map_err( |e| PlayerError::Output( e.to_string() ) )?;
// Apply stored volume to new sample buffer
let vol = *self.volume.read().unwrap();
sample_buffer.set_volume( vol );
let target_sample_rate = output.sample_rate();
output.play().map_err( |e| PlayerError::Output( e.to_string() ) )?;
// Create resampler if sample rates don't match
let resampler = if source_sample_rate != target_sample_rate {
tracing::info!(
"Resampling: {} Hz → {} Hz",
source_sample_rate,
target_sample_rate
);
// Use FastFixedOut which handles variable input sizes
let resampler = FastFixedOut::<f32>::new(
target_sample_rate as f64 / source_sample_rate as f64,
2.0, // max relative input/output size ratio
PolynomialDegree::Cubic,
1024, // output chunk size
channels as usize,
).map_err( |e| PlayerError::Output( format!( "Failed to create resampler: {}", e ) ) )?;
Some( resampler )
} else {
None
};
// Set up control flags and position tracking
let stop_flag = Arc::new( AtomicBool::new( false ) );
let frames_played = Arc::new( AtomicU64::new( 0 ) );
let track_ended = Arc::new( AtomicBool::new( false ) );
// Clone for the decode thread
let stop_flag_clone = Arc::clone( &stop_flag );
let sample_buffer_clone = Arc::clone( &sample_buffer );
let state_clone = Arc::clone( &self.state );
let frames_played_clone = Arc::clone( &frames_played );
let track_ended_clone = Arc::clone( &track_ended );
// Spawn decode thread
let thread = thread::spawn( move || {
Self::decode_loop(
decoder,
sample_buffer_clone,
stop_flag_clone,
state_clone,
resampler,
frames_played_clone,
track_ended_clone,
);
});
// Store playback handle
{
let mut playback = self.playback.write().unwrap();
*playback = Some( PlaybackHandle {
stop_flag,
sample_buffer,
output: AudioOutputHandle( output ),
thread: Some( thread ),
frames_played,
sample_rate: source_sample_rate,
duration,
track_ended,
metadata,
});
}
// Update state
{
let mut track = self.current_track.write().unwrap();
*track = Some( path );
}
{
let mut state = self.state.write().unwrap();
*state = PlaybackState::Playing;
}
Ok(())
}
/// The decode loop that runs in a separate thread.
fn decode_loop(
mut decoder: Decoder,
sample_buffer: Arc<SampleBuffer>,
stop_flag: Arc<AtomicBool>,
state: Arc<RwLock<PlaybackState>>,
mut resampler: Option<FastFixedOut<f32>>,
frames_played: Arc<AtomicU64>,
track_ended: Arc<AtomicBool>,
) {
let channels = decoder.channels();
// Input buffer for resampler (stores planar samples per channel)
let mut resample_input: Vec<Vec<f32>> = ( 0..channels ).map( |_| Vec::new() ).collect();
loop {
// Check for stop signal
if stop_flag.load( Ordering::Relaxed ) {
tracing::debug!( "Decode loop: stop signal received" );
break;
}
// Check for pause signal - if paused, just sleep
if sample_buffer.is_paused() {
thread::sleep( Duration::from_millis( 10 ) );
continue;
}
// Check if output buffer has room
// Don't decode too far ahead - keep about 50ms buffered
let target_buffer = ( decoder.sample_rate() as usize * channels ) / 20;
if sample_buffer.len() > target_buffer {
thread::sleep( Duration::from_millis( 5 ) );
continue;
}
// Decode next chunk
match decoder.decode_next() {
Ok( Some( samples ) ) => {
// Track position based on source frames (before resampling)
let source_frames = samples.len() / channels;
frames_played.fetch_add( source_frames as u64, Ordering::Relaxed );
// Apply resampling if needed
let output_samples = if let Some( ref mut resampler ) = resampler {
// Add new samples to input buffer (convert interleaved to planar)
for chunk in samples.chunks( channels ) {
for ( ch_idx, sample ) in chunk.iter().enumerate() {
if ch_idx < resample_input.len() {
resample_input[ ch_idx ].push( *sample );
}
}
}
// Process when we have enough input frames
let mut output_interleaved = Vec::new();
while resample_input[ 0 ].len() >= resampler.input_frames_next() {
let needed = resampler.input_frames_next();
// Extract needed frames from input buffer
let input_chunk: Vec<Vec<f32>> = resample_input
.iter_mut()
.map( |ch| ch.drain( ..needed ).collect() )
.collect();
// Resample
match resampler.process( &input_chunk, None ) {
Ok( resampled ) => {
output_interleaved.extend( interleave( &resampled ) );
}
Err( e ) => {
tracing::error!( "Resample error: {}", e );
// Put samples back on error
for ( ch_idx, samples ) in input_chunk.into_iter().enumerate() {
for sample in samples.into_iter().rev() {
resample_input[ ch_idx ].insert( 0, sample );
}
}
break;
}
}
}
output_interleaved
} else {
samples
};
// Push samples to buffer
if !output_samples.is_empty() {
let mut offset = 0;
while offset < output_samples.len() && !stop_flag.load( Ordering::Relaxed ) {
let pushed = sample_buffer.push( &output_samples[ offset.. ] );
offset += pushed;
if pushed == 0 {
// Buffer full, wait a bit
thread::sleep( Duration::from_millis( 5 ) );
}
}
}
}
Ok( None ) => {
// EOF - flush any remaining samples in resample buffer
if let Some( ref mut resampler ) = resampler {
if !resample_input[ 0 ].is_empty() {
// Use process_partial for remaining samples
match resampler.process_partial( Some( &resample_input ), None ) {
Ok( resampled ) => {
let output_interleaved = interleave( &resampled );
let mut offset = 0;
while offset < output_interleaved.len() && !stop_flag.load( Ordering::Relaxed ) {
let pushed = sample_buffer.push( &output_interleaved[ offset.. ] );
offset += pushed;
if pushed == 0 {
thread::sleep( Duration::from_millis( 5 ) );
}
}
}
Err( e ) => tracing::error!( "Final resample error: {}", e ),
}
}
}
// Wait for buffer to drain, then signal end
tracing::info!( "Decode loop: reached end of file" );
while !sample_buffer.is_empty() && !stop_flag.load( Ordering::Relaxed ) {
thread::sleep( Duration::from_millis( 10 ) );
}
// Signal that track ended naturally (not stopped by user)
track_ended.store( true, Ordering::Relaxed );
// Update state to stopped
{
let mut s = state.write().unwrap();
*s = PlaybackState::Stopped;
}
break;
}
Err( e ) => {
tracing::error!( "Decode error: {}", e );
break;
}
}
}
tracing::debug!( "Decode loop: exiting" );
}
/// Pauses playback.
pub fn pause( &self ) -> Result<(), PlayerError> {
let playback = self.playback.read().unwrap();
if let Some( ref handle ) = *playback {
handle.sample_buffer.set_paused( true );
let mut state = self.state.write().unwrap();
*state = PlaybackState::Paused;
tracing::info!( "Paused" );
}
Ok(())
}
/// Resumes playback.
pub fn resume( &self ) -> Result<(), PlayerError> {
let playback = self.playback.read().unwrap();
if let Some( ref handle ) = *playback {
handle.sample_buffer.set_paused( false );
let mut state = self.state.write().unwrap();
*state = PlaybackState::Playing;
tracing::info!( "Resumed" );
}
Ok(())
}
/// Stops playback.
pub fn stop( &self ) -> Result<(), PlayerError> {
let mut playback = self.playback.write().unwrap();
if let Some( mut handle ) = playback.take() {
// Signal stop
handle.stop_flag.store( true, Ordering::Relaxed );
handle.sample_buffer.clear();
// Wait for thread to finish
if let Some( thread ) = handle.thread.take() {
let _ = thread.join();
}
// AudioOutput is dropped here, which stops the cpal stream
tracing::info!( "Stopped" );
}
// Update state
{
let mut state = self.state.write().unwrap();
*state = PlaybackState::Stopped;
}
{
let mut track = self.current_track.write().unwrap();
*track = None;
}
Ok(())
}
/// Gets the current playback state.
pub fn state( &self ) -> PlaybackState {
*self.state.read().unwrap()
}
/// Gets the current track path, if any.
pub fn current_track( &self ) -> Option<PathBuf> {
self.current_track.read().unwrap().clone()
}
/// Gets a reference to the playlist.
pub fn playlist( &self ) -> Arc<RwLock<Playlist>> {
Arc::clone( &self.playlist )
}
/// Gets the current playback position.
pub fn position( &self ) -> Duration {
let playback = self.playback.read().unwrap();
if let Some( ref handle ) = *playback {
let frames = handle.frames_played.load( Ordering::Relaxed );
let seconds = frames as f64 / handle.sample_rate as f64;
Duration::from_secs_f64( seconds )
} else {
Duration::ZERO
}
}
/// Gets the total duration of the current track.
pub fn duration( &self ) -> Option<Duration> {
let playback = self.playback.read().unwrap();
playback.as_ref().and_then( |h| h.duration )
}
/// Gets the metadata of the current track.
pub fn metadata( &self ) -> Option<AudioMetadata> {
let playback = self.playback.read().unwrap();
playback.as_ref().map( |h| h.metadata.clone() )
}
/// Gets the visualization data (RMS amplitudes for frequency bars).
pub fn vis_data( &self ) -> Option<[f32; crate::output::VIS_BARS]> {
let playback = self.playback.read().unwrap();
playback.as_ref().map( |h| h.sample_buffer.vis_data() )
}
/// Sets the volume level (0.0 = mute, 1.0 = normal, >1.0 = boost).
pub fn set_volume( &self, volume: f32 ) {
// Store volume for future tracks
{
let mut vol = self.volume.write().unwrap();
*vol = volume;
}
// Apply to current playback if any
let playback = self.playback.read().unwrap();
if let Some( ref handle ) = *playback {
handle.sample_buffer.set_volume( volume );
}
}
/// Gets the current volume level.
pub fn volume( &self ) -> f32 {
*self.volume.read().unwrap()
}
/// Returns true if the current track ended naturally (EOF reached).
/// This is reset when a new track starts playing.
pub fn track_ended( &self ) -> bool {
let playback = self.playback.read().unwrap();
playback.as_ref()
.map( |h| h.track_ended.load( Ordering::Relaxed ) )
.unwrap_or( false )
}
/// Plays the next track in the playlist.
/// Returns Ok(true) if a track was started, Ok(false) if no next track.
pub fn play_next( &self ) -> Result<bool, PlayerError> {
let next_track = {
let mut playlist = self.playlist.write().unwrap();
playlist.next().cloned()
};
if let Some( path ) = next_track {
self.play( path )?;
Ok( true )
} else {
Ok( false )
}
}
/// Plays the previous track in the playlist.
/// Returns Ok(true) if a track was started, Ok(false) if no previous track.
pub fn play_previous( &self ) -> Result<bool, PlayerError> {
let prev_track = {
let mut playlist = self.playlist.write().unwrap();
playlist.previous().cloned()
};
if let Some( path ) = prev_track {
self.play( path )?;
Ok( true )
} else {
Ok( false )
}
}
/// Seeks to a specific position in the current track.
///
/// This works by stopping playback, reopening the file at the seek position,
/// and resuming playback.
pub fn seek( &self, position: Duration ) -> Result<(), PlayerError> {
let current_track = self.current_track().ok_or( PlayerError::NoTrack )?;
let was_playing = self.state() == PlaybackState::Playing;
// Stop current playback
self.stop()?;
// Reopen and seek
tracing::info!( "Seeking to {:?} in {:?}", position, current_track );
// Open the decoder
let mut decoder = Decoder::open( ¤t_track )
.map_err( |e| PlayerError::FileOpen( e.to_string() ) )?;
// Seek to position
decoder.seek( position.as_secs_f64() )
.map_err( |e| PlayerError::Decode( e.to_string() ) )?;
let source_sample_rate = decoder.sample_rate();
let channels = decoder.channels() as u16;
let duration = decoder.duration().map( |secs| Duration::from_secs_f64( secs ) );
let metadata = decoder.metadata();
// Create audio output
let ( output, sample_buffer ) = AudioOutput::new( source_sample_rate, channels )
.map_err( |e| PlayerError::Output( e.to_string() ) )?;
// Apply stored volume to new sample buffer
let vol = *self.volume.read().unwrap();
sample_buffer.set_volume( vol );
let target_sample_rate = output.sample_rate();
output.play().map_err( |e| PlayerError::Output( e.to_string() ) )?;
// Create resampler if sample rates don't match
let resampler = if source_sample_rate != target_sample_rate {
let resampler = FastFixedOut::<f32>::new(
target_sample_rate as f64 / source_sample_rate as f64,
2.0,
PolynomialDegree::Cubic,
1024,
channels as usize,
).map_err( |e| PlayerError::Output( format!( "Failed to create resampler: {}", e ) ) )?;
Some( resampler )
} else {
None
};
// Set up control flags - start with frames_played at the seek position
let stop_flag = Arc::new( AtomicBool::new( false ) );
let seek_frames = ( position.as_secs_f64() * source_sample_rate as f64 ) as u64;
let frames_played = Arc::new( AtomicU64::new( seek_frames ) );
let track_ended = Arc::new( AtomicBool::new( false ) );
// Clone for the decode thread
let stop_flag_clone = Arc::clone( &stop_flag );
let sample_buffer_clone = Arc::clone( &sample_buffer );
let state_clone = Arc::clone( &self.state );
let frames_played_clone = Arc::clone( &frames_played );
let track_ended_clone = Arc::clone( &track_ended );
// Start paused if we were paused before
if !was_playing {
sample_buffer.set_paused( true );
}
// Spawn decode thread
let thread = thread::spawn( move || {
Self::decode_loop(
decoder,
sample_buffer_clone,
stop_flag_clone,
state_clone,
resampler,
frames_played_clone,
track_ended_clone,
);
});
// Store playback handle
{
let mut playback = self.playback.write().unwrap();
*playback = Some( PlaybackHandle {
stop_flag,
sample_buffer,
output: AudioOutputHandle( output ),
thread: Some( thread ),
frames_played,
sample_rate: source_sample_rate,
duration,
track_ended,
metadata,
});
}
// Update state
{
let mut track = self.current_track.write().unwrap();
*track = Some( current_track );
}
{
let mut state = self.state.write().unwrap();
*state = if was_playing { PlaybackState::Playing } else { PlaybackState::Paused };
}
Ok(())
}
}
impl Default for Player {
fn default() -> Self {
Self::new().expect( "Failed to create player" )
}
}
impl Drop for Player {
fn drop( &mut self ) {
// Ensure playback is stopped when player is dropped
let _ = self.stop();
}
}