driftfm 0.1.0

A blazing-fast cyber-synthwave internet radio player & smart tape recorder TUI
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
use rodio::{Decoder, OutputStream, Sink};
use std::io::{Read, Write};
use std::sync::mpsc;
use std::sync::atomic::{AtomicU64, AtomicU8, AtomicBool, Ordering};
use std::sync::{Arc, Mutex, Condvar};
use std::collections::VecDeque;
use std::time::Duration;
use std::fs::File;
use std::path::PathBuf;

/// Commands sent from the UI thread to the audio thread.
#[derive(Debug, Clone)]
pub enum AudioCommand {
    Play(String), // URL
    Pause,
    Resume,
    Stop,
    SetVolume(f32), // 0.0 — 1.0
    StartRecording {
        recording_dir: String,
        category: String,
        keep_snippets: bool,
        min_song_duration_secs: u32,
    },
    StopRecording,
}

/// Status updates sent from the audio thread back to the UI.
#[derive(Debug, Clone)]
pub enum AudioStatus {
    Playing,
    Paused,
    Stopped,
    Error(String),
    Connecting,
    TrackChanged { url: String, title: String },
    RecordingStateChanged { state: u8, filepath: Option<String> }, // 0 = Off, 1 = Pending, 2 = Active
    BufferLevel { percent: u8, seconds: u32 },
}

/// Shared thread-safe recording configuration state
pub struct RecordStateShared {
    pub state: AtomicU8, // 0 = Off, 1 = Pending, 2 = Active
    pub recording_dir: Mutex<String>,
    pub category: Mutex<String>,
    pub keep_snippets: AtomicBool,
    pub min_song_duration_secs: std::sync::atomic::AtomicU32,
}

/// Bounded Producer-Consumer circular byte queue (Resiliency Buffer)
struct BufferQueue {
    queue: Mutex<VecDeque<u8>>,
    cv_read: Condvar,
    cv_write: Condvar,
    capacity: usize,
    disconnected: AtomicBool,
}

impl BufferQueue {
    fn new(capacity: usize) -> Self {
        Self {
            queue: Mutex::new(VecDeque::with_capacity(capacity)),
            cv_read: Condvar::new(),
            cv_write: Condvar::new(),
            capacity,
            disconnected: AtomicBool::new(false),
        }
    }

    fn push(&self, bytes: &[u8]) {
        let mut queue = self.queue.lock().unwrap();
        // Block downloader thread if buffer capacity limit is reached
        while queue.len() + bytes.len() > self.capacity && !self.disconnected.load(Ordering::SeqCst) {
            queue = self.cv_write.wait(queue).unwrap();
        }
        if self.disconnected.load(Ordering::SeqCst) {
            return;
        }
        queue.extend(bytes);
        self.cv_read.notify_all();
    }

    fn pop(&self, buf: &mut [u8]) -> std::io::Result<usize> {
        let mut queue = self.queue.lock().unwrap();
        while queue.is_empty() && !self.disconnected.load(Ordering::SeqCst) {
            queue = self.cv_read.wait(queue).unwrap();
        }
        if queue.is_empty() && self.disconnected.load(Ordering::SeqCst) {
            return Ok(0); // EOF or network disconnection
        }

        let count = std::cmp::min(buf.len(), queue.len());
        for slot in buf.iter_mut().take(count) {
            *slot = queue.pop_front().unwrap();
        }
        self.cv_write.notify_all();
        Ok(count)
    }

    fn len(&self) -> usize {
        self.queue.lock().unwrap().len()
    }

    fn set_disconnected(&self, disc: bool) {
        self.disconnected.store(disc, Ordering::SeqCst);
        let _queue = self.queue.lock().unwrap();
        // Wake up both consumer and producer to terminate gracefully
        self.cv_read.notify_all();
        self.cv_write.notify_all();
    }
}

/// Handle to communicate with the audio engine running on a background thread.
pub struct AudioEngine {
    cmd_tx: mpsc::Sender<AudioCommand>,
    pub status_rx: mpsc::Receiver<AudioStatus>,
}

impl AudioEngine {
    /// Spawn the audio engine on a dedicated OS thread.
    pub fn spawn() -> Self {
        let (cmd_tx, cmd_rx) = mpsc::channel::<AudioCommand>();
        let (status_tx, status_rx) = mpsc::channel::<AudioStatus>();

        std::thread::spawn(move || {
            audio_loop(cmd_rx, status_tx);
        });

        Self { cmd_tx, status_rx }
    }

    pub fn send(&self, cmd: AudioCommand) {
        let _ = self.cmd_tx.send(cmd);
    }
}

/// The main audio loop. Pure blocking I/O on a dedicated OS thread.
fn audio_loop(
    cmd_rx: mpsc::Receiver<AudioCommand>,
    status_tx: mpsc::Sender<AudioStatus>,
) {
    // Keep OutputStream alive for the lifetime of this thread.
    let (_stream, handle) = match OutputStream::try_default() {
        Ok(s) => s,
        Err(e) => {
            let _ = status_tx.send(AudioStatus::Error(format!("Soundcard error: {}", e)));
            return;
        }
    };

    let mut current_sink: Option<Sink> = None;
    let mut connect_thread: Option<std::thread::JoinHandle<Result<Sink, String>>> = None;
    
    // Concurrency guard to abandon stale threads instantly
    let active_conn_id = Arc::new(AtomicU64::new(0));
    let mut current_conn_id: u64 = 0;

    // Shared thread-safe recording control state
    let record_state = Arc::new(RecordStateShared {
        state: AtomicU8::new(0), // Default: Off
        recording_dir: Mutex::new(String::new()),
        category: Mutex::new(String::new()),
        keep_snippets: AtomicBool::new(false),
        min_song_duration_secs: std::sync::atomic::AtomicU32::new(90),
    });

    // Premium non-blocking volume crossfade/ramping parameters
    let mut target_volume: f32 = 0.8;
    let mut current_fade_volume: Option<f32> = None;
    let mut pending_action: Option<AudioCommand> = None;

    loop {
        // Non-blocking check for commands (10ms poll)
        match cmd_rx.recv_timeout(Duration::from_millis(10)) {
            Ok(cmd) => {
                match cmd {
                    AudioCommand::Play(url) => {
                        if current_sink.is_some() {
                            pending_action = Some(AudioCommand::Play(url));
                        } else {
                            drop(connect_thread.take());

                            // Cancel any previous in-flight connection thread
                            current_conn_id += 1;
                            active_conn_id.store(current_conn_id, Ordering::SeqCst);

                            let _ = status_tx.send(AudioStatus::Connecting);

                            let handle_clone = handle.clone();
                            let status_tx_clone = status_tx.clone();
                            let conn_id = current_conn_id;
                            let active_conn_id_clone = active_conn_id.clone();
                            let record_state_clone = record_state.clone();
                            
                            connect_thread = Some(std::thread::spawn(move || {
                                connect_and_decode(&url, &handle_clone, status_tx_clone, conn_id, active_conn_id_clone, record_state_clone)
                            }));
                        }
                    }
                    AudioCommand::Pause => {
                        if current_sink.is_some() {
                            pending_action = Some(AudioCommand::Pause);
                        } else {
                            let _ = status_tx.send(AudioStatus::Paused);
                        }
                    }
                    AudioCommand::Resume => {
                        if let Some(ref sink) = current_sink {
                            sink.play();
                            let _ = status_tx.send(AudioStatus::Playing);
                            // Smooth fade-in
                            current_fade_volume = Some(0.0);
                        }
                    }
                    AudioCommand::Stop => {
                        if current_sink.is_some() {
                            pending_action = Some(AudioCommand::Stop);
                        } else {
                            active_conn_id.store(0, Ordering::SeqCst); // abandon in-flight
                            connect_thread = None;
                            let _ = status_tx.send(AudioStatus::Stopped);
                        }
                    }
                    AudioCommand::SetVolume(vol) => {
                        target_volume = vol;
                        if current_fade_volume.is_none() && pending_action.is_none() {
                            if let Some(ref sink) = current_sink {
                                sink.set_volume(vol);
                            }
                        }
                    }
                    AudioCommand::StartRecording { recording_dir, category, keep_snippets, min_song_duration_secs } => {
                        *record_state.recording_dir.lock().unwrap() = recording_dir;
                        *record_state.category.lock().unwrap() = category;
                        record_state.keep_snippets.store(keep_snippets, Ordering::SeqCst);
                        record_state.min_song_duration_secs.store(min_song_duration_secs, Ordering::SeqCst);
                        record_state.state.store(1, Ordering::SeqCst); // Transition to Pending
                        let _ = status_tx.send(AudioStatus::RecordingStateChanged { state: 1, filepath: None });
                    }
                    AudioCommand::StopRecording => {
                        record_state.state.store(0, Ordering::SeqCst); // Transition to Off
                        let _ = status_tx.send(AudioStatus::RecordingStateChanged { state: 0, filepath: None });
                    }
                }
            }
            Err(mpsc::RecvTimeoutError::Timeout) => {}
            Err(mpsc::RecvTimeoutError::Disconnected) => {
                break;
            }
        }

        // Process pending action / non-blocking fade-out
        if pending_action.is_some() {
            if let Some(ref sink) = current_sink {
                let current_vol = sink.volume();
                if current_vol <= 0.05 {
                    // Fade out completed! Execute pending command
                    sink.set_volume(0.0);
                    let cmd = pending_action.take().unwrap();
                    match cmd {
                        AudioCommand::Play(url) => {
                            // Cancel any previous in-flight connection thread
                            current_conn_id += 1;
                            active_conn_id.store(current_conn_id, Ordering::SeqCst);

                            let _ = status_tx.send(AudioStatus::Connecting);

                            let handle_clone = handle.clone();
                            let status_tx_clone = status_tx.clone();
                            let conn_id = current_conn_id;
                            let active_conn_id_clone = active_conn_id.clone();
                            let record_state_clone = record_state.clone();
                            
                            // Stop current sink
                            if let Some(old_sink) = current_sink.take() {
                                old_sink.stop();
                            }
                            drop(connect_thread.take());

                            connect_thread = Some(std::thread::spawn(move || {
                                connect_and_decode(&url, &handle_clone, status_tx_clone, conn_id, active_conn_id_clone, record_state_clone)
                            }));
                        }
                        AudioCommand::Stop => {
                            active_conn_id.store(0, Ordering::SeqCst); // abandon in-flight
                            connect_thread = None;
                            if let Some(old_sink) = current_sink.take() {
                                old_sink.stop();
                            }
                            let _ = status_tx.send(AudioStatus::Stopped);
                        }
                        AudioCommand::Pause => {
                            sink.pause();
                            let _ = status_tx.send(AudioStatus::Paused);
                        }
                        _ => {}
                    }
                } else {
                    // Exponential step-down for beautiful natural dimming
                    let step = current_vol * 0.15; // smooth 15% dimming step
                    sink.set_volume((current_vol - step).max(0.0));
                }
            } else {
                // No active sink, just execute pending immediately
                let cmd = pending_action.take().unwrap();
                match cmd {
                    AudioCommand::Play(url) => {
                        current_conn_id += 1;
                        active_conn_id.store(current_conn_id, Ordering::SeqCst);

                        let _ = status_tx.send(AudioStatus::Connecting);

                        let handle_clone = handle.clone();
                        let status_tx_clone = status_tx.clone();
                        let conn_id = current_conn_id;
                        let active_conn_id_clone = active_conn_id.clone();
                        let record_state_clone = record_state.clone();
                        
                        drop(connect_thread.take());

                        connect_thread = Some(std::thread::spawn(move || {
                            connect_and_decode(&url, &handle_clone, status_tx_clone, conn_id, active_conn_id_clone, record_state_clone)
                        }));
                    }
                    AudioCommand::Stop => {
                        active_conn_id.store(0, Ordering::SeqCst);
                        connect_thread = None;
                        let _ = status_tx.send(AudioStatus::Stopped);
                    }
                    AudioCommand::Pause => {
                        let _ = status_tx.send(AudioStatus::Paused);
                    }
                    _ => {}
                }
            }
        }

        // Process non-blocking fade-in
        if pending_action.is_none() && current_fade_volume.is_some() {
            if let Some(ref sink) = current_sink {
                let current_vol = sink.volume();
                if (current_vol - target_volume).abs() <= 0.03 {
                    sink.set_volume(target_volume);
                    current_fade_volume = None;
                } else {
                    // Exponential step-up towards target_volume for organic swell
                    let step = (target_volume - current_vol) * 0.15;
                    sink.set_volume(current_vol + step);
                }
            } else {
                current_fade_volume = None;
            }
        }

        // Check if a pending connection has completed
        if let Some(ref handle) = connect_thread {
            if handle.is_finished() {
                let finished = connect_thread.take().unwrap();
                match finished.join() {
                    Ok(Ok(sink)) => {
                        // Start playing at 0.0 volume, trigger exponential swell
                        sink.set_volume(0.0);
                        current_sink = Some(sink);
                        let _ = status_tx.send(AudioStatus::Playing);
                        current_fade_volume = Some(0.0);
                    }
                    Ok(Err(e)) => {
                        // Stale thread errors are ignored (they are "Abandoned" or cancelled)
                        if e != "Abandoned" {
                            let _ = status_tx.send(AudioStatus::Error(e));
                        }
                    }
                    Err(_) => {
                        let _ = status_tx.send(AudioStatus::Error(
                            "Connection thread panicked".into(),
                        ));
                    }
                }
            }
        }

        // Check if current playback ended
        if let Some(ref sink) = current_sink {
            if sink.empty() {
                current_sink = None;
                let _ = status_tx.send(AudioStatus::Stopped);
            }
        }
    }
}

/// Connect to a stream URL and create a playable Sink, with automatic backoff retries.
fn connect_and_decode(
    url: &str,
    handle: &rodio::OutputStreamHandle,
    status_tx: mpsc::Sender<AudioStatus>,
    conn_id: u64,
    active_conn_id: Arc<AtomicU64>,
    record_state: Arc<RecordStateShared>,
) -> Result<Sink, String> {
    let mut retries = 0;
    let max_retries = 5;
    let mut backoff = Duration::from_secs(1);

    loop {
        // Double check cancellation
        if active_conn_id.load(Ordering::SeqCst) != conn_id {
            return Err("Abandoned".into());
        }

        match try_connect_and_decode_once(url, handle, status_tx.clone(), conn_id, active_conn_id.clone(), record_state.clone()) {
            Ok(sink) => return Ok(sink),
            Err(e) => {
                if e == "Abandoned" {
                    return Err("Abandoned".into());
                }

                retries += 1;
                if retries >= max_retries {
                    return Err(format!("Failed after {} retries: {}", max_retries, e));
                }

                // Notify UI about tuning status retry
                let _ = status_tx.send(AudioStatus::Connecting);

                // Sleep with backoff, checking for abandonment every 100ms
                let sleep_step = Duration::from_millis(100);
                let steps = (backoff.as_millis() / sleep_step.as_millis()) as usize;
                for _ in 0..steps {
                    if active_conn_id.load(Ordering::SeqCst) != conn_id {
                        return Err("Abandoned".into());
                    }
                    std::thread::sleep(sleep_step);
                }

                backoff = (backoff * 2).min(Duration::from_secs(8));
            }
        }
    }
}

fn try_connect_and_decode_once(
    url: &str,
    handle: &rodio::OutputStreamHandle,
    status_tx: mpsc::Sender<AudioStatus>,
    conn_id: u64,
    active_conn_id: Arc<AtomicU64>,
    record_state: Arc<RecordStateShared>,
) -> Result<Sink, String> {
    let client = reqwest::blocking::Client::builder()
        .timeout(None)
        .connect_timeout(Duration::from_secs(5))
        .user_agent("DriftFM/0.1.0")
        .build()
        .map_err(|e| format!("HTTP client error: {}", e))?;

    if active_conn_id.load(Ordering::SeqCst) != conn_id {
        return Err("Abandoned".into());
    }

    let response = client
        .get(url)
        .header("Icy-MetaData", "1")
        .send()
        .map_err(|e| format!("Connection failed: {}", e))?;

    if !response.status().is_success() {
        return Err(format!("HTTP {}", response.status()));
    }

    if active_conn_id.load(Ordering::SeqCst) != conn_id {
        return Err("Abandoned".into());
    }

    // Decouple download from decoding: Spawn Bounded Producer-Consumer resiliences
    let buffer_capacity = 1024 * 1024; // 1 MB circular byte queue
    let queue = Arc::new(BufferQueue::new(buffer_capacity));
    
    let queue_clone = queue.clone();
    let active_conn_id_clone = active_conn_id.clone();
    let conn_id_clone = conn_id;
    let status_tx_clone = status_tx.clone();
    let mut response_reader = response;

    std::thread::spawn(move || {
        let mut buf = [0u8; 8192];
        loop {
            if active_conn_id_clone.load(Ordering::SeqCst) != conn_id_clone {
                queue_clone.set_disconnected(true);
                break;
            }
            match response_reader.read(&mut buf) {
                Ok(0) => {
                    queue_clone.set_disconnected(true);
                    break;
                }
                Ok(n) => {
                    queue_clone.push(&buf[..n]);

                    // Send circular buffer progress telemetries to UI
                    let len = queue_clone.len();
                    let cap = queue_clone.capacity;
                    let percent = ((len * 100) / cap) as u8;
                    // Assume 16 KB/sec standard decoding bandwidth
                    let seconds = (len / 16000) as u32;
                    let _ = status_tx_clone.send(AudioStatus::BufferLevel { percent, seconds });
                }
                Err(_) => {
                    queue_clone.set_disconnected(true);
                    break;
                }
            }
        }
    });

    let reader = StreamReader::new(url.to_string(), queue, status_tx, conn_id, active_conn_id, record_state);

    let source = Decoder::new(reader)
        .map_err(|e| format!("Decode error: {}", e))?;

    let sink = Sink::try_new(handle)
        .map_err(|e| format!("Sink error: {}", e))?;

    sink.append(source);

    Ok(sink)
}

/// StreamReader consuming from thread-safe ring-buffer and stripping metadata boundaries
struct StreamReader {
    url: String,
    queue: Arc<BufferQueue>,
    pos: u64,
    metaint: Option<usize>,
    bytes_until_meta: usize,
    status_tx: mpsc::Sender<AudioStatus>,
    conn_id: u64,
    active_conn_id: Arc<AtomicU64>,
    
    // Recording state trackers
    record_state: Arc<RecordStateShared>,
    active_writer: Option<File>,
    active_file_path: Option<String>,
    active_track_title: Option<String>,
    active_track_start_time: Option<std::time::Instant>,
}

impl StreamReader {
    fn new(
        url: String,
        queue: Arc<BufferQueue>,
        status_tx: mpsc::Sender<AudioStatus>,
        conn_id: u64,
        active_conn_id: Arc<AtomicU64>,
        record_state: Arc<RecordStateShared>,
    ) -> Self {
        // We will default to a heuristic 16000 bytes boundary check if metaint is unknown,
        // but typically internet radio servers provide metaint headers.
        Self {
            url,
            queue,
            pos: 0,
            metaint: Some(16000), // Standard default if header not found
            bytes_until_meta: 16000,
            status_tx,
            conn_id,
            active_conn_id,
            record_state,
            active_writer: None,
            active_file_path: None,
            active_track_title: None,
            active_track_start_time: None,
        }
    }

    fn read_metadata_block(&mut self) -> std::io::Result<()> {
        let mut length_byte = [0u8; 1];
        self.queue.pop(&mut length_byte)?;
        let length = length_byte[0] as usize * 16;

        if length > 0 {
            let mut meta_buf = vec![0u8; length];
            self.queue.pop(&mut meta_buf)?;
            if let Ok(meta_str) = String::from_utf8(meta_buf) {
                if let Some(title) = parse_stream_title(&meta_str) {
                    // Send main UI track change signal
                    let _ = self.status_tx.send(AudioStatus::TrackChanged {
                        url: self.url.clone(),
                        title: title.clone(),
                    });

                    // Trigger smart recording track boundary splitting!
                    self.handle_track_change(&title);
                }
            }
        }
        Ok(())
    }

    fn handle_track_change(&mut self, new_title: &str) {
        let record = self.record_state.clone();
        let mut state = record.state.load(Ordering::SeqCst);

        // If Pending next track start -> transition to Active!
        if state == 1 {
            record.state.store(2, Ordering::SeqCst);
            state = 2;
        }

        if state == 2 {
            // Close the old recorded segment cleanly
            self.close_recording_file();
            // Start recording the new segmented track!
            self.open_recording_file(new_title);
        }
    }

    fn open_recording_file(&mut self, title: &str) {
        let recording_dir = self.record_state.recording_dir.lock().unwrap().clone();
        let category = self.record_state.category.lock().unwrap().clone();

        if recording_dir.is_empty() {
            return;
        }

        let clean_category = sanitize_filename(&category);
        let clean_title = sanitize_filename(title);

        let mut path = PathBuf::from(&recording_dir);
        path.push(&clean_category);

        if let Err(e) = std::fs::create_dir_all(&path) {
            let _ = self.status_tx.send(AudioStatus::Error(format!("Failed to create folders: {}", e)));
            return;
        }

        // Auto-detect extension based on stream URL heuristics
        let ext = if self.url.contains("aac") || self.url.contains("m4a") {
            "aac"
        } else {
            "mp3"
        };

        path.push(format!("{}.{}", clean_title, ext));

        match File::create(&path) {
            Ok(file) => {
                self.active_writer = Some(file);
                self.active_file_path = Some(path.to_string_lossy().to_string());
                self.active_track_title = Some(title.to_string());
                self.active_track_start_time = Some(std::time::Instant::now());

                // Notify UI state to render red flashes
                let _ = self.status_tx.send(AudioStatus::RecordingStateChanged {
                    state: 2,
                    filepath: Some(path.to_string_lossy().to_string()),
                });
            }
            Err(e) => {
                let _ = self.status_tx.send(AudioStatus::Error(format!("Failed to create segment file: {}", e)));
            }
        }
    }

    fn close_recording_file(&mut self) {
        if let Some(mut file) = self.active_writer.take() {
            let _ = file.flush();
            drop(file); // Closes file handle

            let duration = self.active_track_start_time.take()
                .map(|t| t.elapsed())
                .unwrap_or(std::time::Duration::ZERO);

            let keep_snippets = self.record_state.keep_snippets.load(Ordering::SeqCst);
            let min_secs = self.record_state.min_song_duration_secs.load(Ordering::SeqCst);

            let mut is_ad_or_speech = false;
            if let Some(ref title) = self.active_track_title {
                let t_upper = title.to_uppercase();
                if t_upper.contains("ADVERT")
                    || t_upper.contains("COMMERCIAL")
                    || t_upper.contains("STATION ID")
                    || t_upper.contains("DJ SPEECH")
                    || t_upper.contains("NEWS CAST")
                    || t_upper.contains("NEWS UPDATE")
                    || t_upper.contains("WEATHER REPORT")
                    || t_upper.contains("TRAFFIC REPORT")
                    || t_upper.trim().is_empty()
                {
                    is_ad_or_speech = true;
                }
            } else {
                is_ad_or_speech = true;
            }

            let is_short = duration.as_secs() < min_secs as u64;

            if let Some(ref filepath) = self.active_file_path {
                if (is_short || is_ad_or_speech) && !keep_snippets {
                    // Purge file from disk!
                    if let Err(e) = std::fs::remove_file(filepath) {
                        let _ = self.status_tx.send(AudioStatus::Error(format!("Failed to delete partial file: {}", e)));
                    } else {
                        let title = self.active_track_title.as_deref().unwrap_or("Unknown Track");
                        let reason = if is_ad_or_speech { "Speech/Ad Filter" } else { "Short Snippet" };
                        let _ = self.status_tx.send(AudioStatus::Error(format!(
                            "🗑️ Discarded {} - {} ({:.1}s)",
                            reason, title, duration.as_secs_f32()
                        )));
                    }
                } else {
                    // Inject high-fidelity metadata tags into completed MP3 tracks
                    if filepath.ends_with(".mp3") {
                        if let Some(ref title) = self.active_track_title {
                            let _ = inject_id3_tags(filepath, title);
                        }
                    }
                }
            }
        }
        self.active_file_path = None;
        self.active_track_title = None;
        self.active_track_start_time = None;
    }
}

impl std::io::Read for StreamReader {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        // Exit early if this thread has been abandoned
        if self.active_conn_id.load(Ordering::SeqCst) != self.conn_id {
            self.close_recording_file();
            return Err(std::io::Error::other("Abandoned"));
        }

        // Sync recording file state with global config toggles
        let global_state = self.record_state.state.load(Ordering::SeqCst);
        if global_state == 0 && self.active_writer.is_some() {
            self.close_recording_file();
        }

        let Some(metaint) = self.metaint else {
            let n = self.queue.pop(buf)?;
            self.pos += n as u64;

            // Write read bytes directly to tape recorder if currently Active
            if global_state == 2 {
                if let Some(ref mut file) = self.active_writer {
                    let _ = file.write_all(&buf[..n]);
                }
            }
            return Ok(n);
        };

        if self.bytes_until_meta == 0 {
            self.read_metadata_block()?;
            self.bytes_until_meta = metaint;
        }

        let max_to_read = buf.len().min(self.bytes_until_meta);
        let n = self.queue.pop(&mut buf[..max_to_read])?;
        self.pos += n as u64;
        self.bytes_until_meta -= n;

        // Write read bytes directly to tape recorder if currently Active
        if global_state == 2 {
            if let Some(ref mut file) = self.active_writer {
                let _ = file.write_all(&buf[..n]);
            }
        }

        Ok(n)
    }
}

impl std::io::Seek for StreamReader {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        match pos {
            std::io::SeekFrom::Current(0) | std::io::SeekFrom::Start(_) => Ok(self.pos),
            std::io::SeekFrom::Current(offset) if offset > 0 => {
                let mut remaining = offset as u64;
                let mut discard = [0u8; 8192];
                while remaining > 0 {
                    let to_read = remaining.min(discard.len() as u64) as usize;
                    let n = self.read(&mut discard[..to_read])?;
                    if n == 0 {
                        break;
                    }
                    remaining -= n as u64;
                }
                Ok(self.pos)
            }
            _ => Ok(self.pos),
        }
    }
}

/// Inject ID3 metadata frames into completed local recordings
fn inject_id3_tags(filepath: &str, title: &str) -> Result<(), Box<dyn std::error::Error>> {
    use id3::{Tag, TagLike, Version};

    let mut artist = "Unknown Artist".to_string();
    let mut track_title = title.to_string();

    if let Some(pos) = title.find(" - ") {
        artist = title[..pos].trim().to_string();
        track_title = title[pos + 3..].trim().to_string();
    }

    let mut tag = Tag::new();
    tag.set_artist(artist);
    tag.set_title(track_title);
    tag.set_album("DriftFM live capturing");

    tag.write_to_path(filepath, Version::Id3v24)?;
    Ok(())
}

/// Replace invalid filesystem characters to protect host OS filesystems
fn sanitize_filename(name: &str) -> String {
    name.chars()
        .map(|c| match c {
            '\\' | '/' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '-',
            other => other,
        })
        .collect()
}

/// Parse the `StreamTitle` field from an ICY metadata string.
fn parse_stream_title(meta: &str) -> Option<String> {
    let key = "StreamTitle='";
    if let Some(start_idx) = meta.find(key) {
        let value_start = start_idx + key.len();
        if let Some(end_idx) = meta[value_start..].find("';") {
            let title = &meta[value_start..value_start + end_idx];
            return Some(title.trim().to_string());
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sanitize_filename() {
        assert_eq!(sanitize_filename("normal_file.mp3"), "normal_file.mp3");
        assert_eq!(sanitize_filename("artist/song?.mp3"), "artist-song-.mp3");
        assert_eq!(sanitize_filename("windows\\invalid:name*char\".mp3"), "windows-invalid-name-char-.mp3");
        assert_eq!(sanitize_filename("<tag> | pipe.mp3"), "-tag- - pipe.mp3");
    }

    #[test]
    fn test_parse_stream_title() {
        // Valid metadata
        assert_eq!(
            parse_stream_title("StreamTitle='Lazerhawk - King of The Streets';StreamUrl='';"),
            Some("Lazerhawk - King of The Streets".to_string())
        );

        // Whitespace trim
        assert_eq!(
            parse_stream_title("StreamTitle='  Kavinsky - Nightcall  ';StreamUrl='';"),
            Some("Kavinsky - Nightcall".to_string())
        );

        // Missing StreamTitle
        assert_eq!(
            parse_stream_title("StreamUrl='';"),
            None
        );

        // Malformed or empty title
        assert_eq!(
            parse_stream_title("StreamTitle='';"),
            Some("".to_string())
        );
    }
}