oxidio-core 1.0.0

Core audio playback engine for Oxidio
Documentation
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
//! Playlist and queue management
//!
//! Handles track ordering, shuffle, repeat, and queue operations.

use std::fs::{ self, File };
use std::io::{ BufRead, BufReader, Write };
use std::path::{ Path, PathBuf };

use thiserror::Error;


/// Errors that can occur with playlist operations.
#[derive( Debug, Error )]
pub enum PlaylistError {
    #[error( "IO error: {0}" )]
    Io( #[from] std::io::Error ),

    #[error( "Invalid playlist format" )]
    InvalidFormat,
}


/// Repeat mode for the playlist.
#[derive( Debug, Clone, Copy, PartialEq, Eq, Default )]
pub enum RepeatMode {
    #[default]
    Off,
    One,
    All,
}


/// Session state for persistence across restarts.
#[derive( Debug, Clone )]
pub struct SessionState {
    pub playlist_name: String,
    pub track_index: Option<usize>,
    pub shuffle: bool,
    pub repeat: RepeatMode,
    pub volume: f32,
}


/// Playlist/queue manager.
#[derive( Debug, Default )]
pub struct Playlist {
    tracks: Vec<PathBuf>,
    current_index: Option<usize>,
    shuffle: bool,
    repeat: RepeatMode,
    // Shuffle order (indices into tracks)
    shuffle_order: Vec<usize>,
    shuffle_position: usize,
}


impl Playlist {
    /// Creates a new empty playlist.
    pub fn new() -> Self {
        Self::default()
    }


    /// Adds a track to the end of the playlist.
    pub fn add( &mut self, path: PathBuf ) {
        self.tracks.push( path );
        self.regenerate_shuffle_order();
    }


    /// Adds multiple tracks to the playlist.
    pub fn add_many( &mut self, paths: impl IntoIterator<Item = PathBuf> ) {
        self.tracks.extend( paths );
        self.regenerate_shuffle_order();
    }


    /// Clears the playlist.
    pub fn clear( &mut self ) {
        self.tracks.clear();
        self.current_index = None;
        self.shuffle_order.clear();
        self.shuffle_position = 0;
    }


    /// Removes a track at the specified index.
    pub fn remove( &mut self, index: usize ) -> Option<PathBuf> {
        if index >= self.tracks.len() {
            return None;
        }

        let removed = self.tracks.remove( index );

        // Adjust current index if needed
        if let Some( current ) = self.current_index {
            if index < current {
                self.current_index = Some( current - 1 );
            } else if index == current {
                self.current_index = None;
            }
        }

        self.regenerate_shuffle_order();
        Some( removed )
    }


    /// Gets the current track.
    pub fn current( &self ) -> Option<&PathBuf> {
        self.current_index.and_then( |i| self.tracks.get( i ) )
    }


    /// Advances to the next track.
    ///
    /// Returns the next track path, or None if at the end (and repeat is off).
    pub fn next( &mut self ) -> Option<&PathBuf> {
        if self.tracks.is_empty() {
            return None;
        }

        let next_index = if self.shuffle {
            self.shuffle_position += 1;
            if self.shuffle_position >= self.shuffle_order.len() {
                match self.repeat {
                    RepeatMode::Off => return None,
                    RepeatMode::All => {
                        self.regenerate_shuffle_order();
                        self.shuffle_position = 0;
                    }
                    RepeatMode::One => {
                        self.shuffle_position -= 1;
                    }
                }
            }
            self.shuffle_order.get( self.shuffle_position ).copied()
        } else {
            match self.repeat {
                RepeatMode::One => self.current_index,
                RepeatMode::Off | RepeatMode::All => {
                    let current = self.current_index.unwrap_or( 0 );
                    let next = current + 1;
                    if next >= self.tracks.len() {
                        match self.repeat {
                            RepeatMode::Off => return None,
                            RepeatMode::All => Some( 0 ),
                            RepeatMode::One => unreachable!(),
                        }
                    } else {
                        Some( next )
                    }
                }
            }
        };

        self.current_index = next_index;
        self.current()
    }


    /// Goes to the previous track.
    pub fn previous( &mut self ) -> Option<&PathBuf> {
        if self.tracks.is_empty() {
            return None;
        }

        let prev_index = if self.shuffle {
            if self.shuffle_position > 0 {
                self.shuffle_position -= 1;
                self.shuffle_order.get( self.shuffle_position ).copied()
            } else {
                self.shuffle_order.first().copied()
            }
        } else {
            let current = self.current_index.unwrap_or( 0 );
            if current > 0 {
                Some( current - 1 )
            } else if self.repeat == RepeatMode::All {
                Some( self.tracks.len() - 1 )
            } else {
                Some( 0 )
            }
        };

        self.current_index = prev_index;
        self.current()
    }


    /// Jumps to a specific track by index.
    pub fn jump_to( &mut self, index: usize ) -> Option<&PathBuf> {
        if index < self.tracks.len() {
            self.current_index = Some( index );
            self.current()
        } else {
            None
        }
    }


    /// Sets shuffle mode.
    pub fn set_shuffle( &mut self, shuffle: bool ) {
        if shuffle != self.shuffle {
            self.shuffle = shuffle;
            if shuffle {
                self.regenerate_shuffle_order();
            }
        }
    }


    /// Gets shuffle mode.
    pub fn shuffle( &self ) -> bool {
        self.shuffle
    }


    /// Sets repeat mode.
    pub fn set_repeat( &mut self, repeat: RepeatMode ) {
        self.repeat = repeat;
    }


    /// Gets repeat mode.
    pub fn repeat( &self ) -> RepeatMode {
        self.repeat
    }


    /// Gets all tracks in the playlist.
    pub fn tracks( &self ) -> &[PathBuf] {
        &self.tracks
    }


    /// Gets the number of tracks.
    pub fn len( &self ) -> usize {
        self.tracks.len()
    }


    /// Returns true if the playlist is empty.
    pub fn is_empty( &self ) -> bool {
        self.tracks.is_empty()
    }


    /// Gets the current track index.
    pub fn current_index( &self ) -> Option<usize> {
        self.current_index
    }


    /// Moves a track from one position to another.
    ///
    /// @param from - Source index
    /// @param to - Destination index
    ///
    /// @returns true if the move was successful
    pub fn move_track( &mut self, from: usize, to: usize ) -> bool {
        if from >= self.tracks.len() || to >= self.tracks.len() {
            return false;
        }

        if from == to {
            return true;
        }

        let track = self.tracks.remove( from );
        self.tracks.insert( to, track );

        // Adjust current index if affected
        if let Some( current ) = self.current_index {
            if current == from {
                self.current_index = Some( to );
            } else if from < current && current <= to {
                self.current_index = Some( current - 1 );
            } else if to <= current && current < from {
                self.current_index = Some( current + 1 );
            }
        }

        self.regenerate_shuffle_order();
        true
    }


    /// Removes duplicate tracks from the playlist, keeping the first occurrence.
    ///
    /// @returns The number of duplicates removed
    pub fn dedup( &mut self ) -> usize {
        use std::collections::HashSet;

        let original_len = self.tracks.len();
        let mut seen = HashSet::new();
        let mut new_tracks = Vec::with_capacity( original_len );
        let mut index_map = Vec::with_capacity( original_len );

        for ( old_idx, track ) in self.tracks.drain( .. ).enumerate() {
            if seen.insert( track.clone() ) {
                index_map.push(( old_idx, new_tracks.len() ));
                new_tracks.push( track );
            }
        }

        self.tracks = new_tracks;

        // Adjust current index if needed
        if let Some( current ) = self.current_index {
            self.current_index = index_map.iter()
                .find( |( old, _ )| *old == current )
                .map( |( _, new )| *new );
        }

        self.regenerate_shuffle_order();
        original_len - self.tracks.len()
    }


    /// Saves the playlist to a file (M3U format).
    pub fn save( &self, path: &Path ) -> Result<(), PlaylistError> {
        let mut file = File::create( path )?;

        // Write M3U header
        writeln!( file, "#EXTM3U" )?;

        for track in &self.tracks {
            // Write path as-is (supports both local and UNC paths)
            writeln!( file, "{}", track.display() )?;
        }

        Ok(())
    }


    /// Loads a playlist from a file (M3U format).
    pub fn load( path: &Path ) -> Result<Self, PlaylistError> {
        let file = File::open( path )?;
        let reader = BufReader::new( file );

        let mut playlist = Self::new();

        for line in reader.lines() {
            let line = line?;
            let trimmed = line.trim();

            // Skip empty lines and comments
            if trimmed.is_empty() || trimmed.starts_with( '#' ) {
                continue;
            }

            // Add the track path
            playlist.add( PathBuf::from( trimmed ) );
        }

        Ok( playlist )
    }


    /// Gets the default playlist directory.
    /// Uses Music/Oxidio on Windows, or ~/.local/share/oxidio/playlists on Linux.
    pub fn playlist_dir() -> Option<PathBuf> {
        #[cfg( target_os = "windows" )]
        {
            dirs::audio_dir().map( |d| d.join( "Oxidio" ) )
        }
        #[cfg( not( target_os = "windows" ) )]
        {
            dirs::data_local_dir().map( |d| d.join( "oxidio" ).join( "playlists" ) )
        }
    }


    /// Ensures the playlist directory exists.
    pub fn ensure_playlist_dir() -> Option<PathBuf> {
        let dir = Self::playlist_dir()?;
        fs::create_dir_all( &dir ).ok()?;
        Some( dir )
    }


    /// Gets the session file path for storing last playlist state.
    pub fn session_file() -> Option<PathBuf> {
        Self::playlist_dir().map( |d| d.join( ".session" ) )
    }


    /// Saves session state (current playlist file, track index, shuffle, repeat, volume).
    pub fn save_session( state: &SessionState ) -> Result<(), PlaylistError> {
        if let Some( session_path ) = Self::session_file() {
            if let Some( parent ) = session_path.parent() {
                fs::create_dir_all( parent )?;
            }
            let mut file = File::create( session_path )?;
            writeln!( file, "playlist={}", state.playlist_name )?;
            writeln!( file, "track={}", state.track_index.map( |i| i.to_string() ).unwrap_or_default() )?;
            writeln!( file, "shuffle={}", if state.shuffle { "1" } else { "0" } )?;
            writeln!( file, "repeat={}", match state.repeat {
                RepeatMode::Off => "off",
                RepeatMode::One => "one",
                RepeatMode::All => "all",
            })?;
            writeln!( file, "volume={}", ( state.volume * 100.0 ).round() as i32 )?;
        }
        Ok(())
    }


    /// Loads session state.
    pub fn load_session() -> Option<SessionState> {
        let session_path = Self::session_file()?;
        let file = File::open( session_path ).ok()?;
        let reader = BufReader::new( file );

        let mut playlist_name = String::new();
        let mut track_index = None;
        let mut shuffle = false;
        let mut repeat = RepeatMode::Off;
        let mut volume = 1.0_f32;

        for line in reader.lines().map_while( Result::ok ) {
            if let Some(( key, value )) = line.split_once( '=' ) {
                match key.trim() {
                    "playlist" => playlist_name = value.trim().to_string(),
                    "track" => track_index = value.trim().parse().ok(),
                    "shuffle" => shuffle = value.trim() == "1",
                    "repeat" => repeat = match value.trim() {
                        "one" | "1" => RepeatMode::One,
                        "all" | "2" => RepeatMode::All,
                        _ => RepeatMode::Off,
                    },
                    "volume" => volume = value.trim().parse::<i32>().map( |v| v as f32 / 100.0 ).unwrap_or( 1.0 ),
                    _ => {}
                }
            }
        }

        if playlist_name.is_empty() {
            return None;
        }

        Some( SessionState {
            playlist_name,
            track_index,
            shuffle,
            repeat,
            volume,
        })
    }


    fn regenerate_shuffle_order( &mut self ) {
        use std::collections::hash_map::RandomState;
        use std::hash::{ BuildHasher, Hasher };

        self.shuffle_order = ( 0..self.tracks.len() ).collect();

        // Simple Fisher-Yates shuffle
        let hasher = RandomState::new();
        for i in ( 1..self.shuffle_order.len() ).rev() {
            let mut h = hasher.build_hasher();
            h.write_usize( i );
            let j = h.finish() as usize % ( i + 1 );
            self.shuffle_order.swap( i, j );
        }

        self.shuffle_position = 0;
    }
}