zim-studio 0.8.5

A Terminal-Based Audio Project Scaffold and Metadata System
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
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
880
881
882
883
884
885
886
887
//! Audio playback engine with real-time sample monitoring.
//!
//! This module provides the core audio functionality for the player, handling
//! file loading, playback control, and real-time audio sample streaming for
//! visualization. It supports multiple audio formats (WAV, FLAC) and provides
//! progress tracking and seeking capabilities.

use rodio::{OutputStream, OutputStreamBuilder, Sink, Source};
use std::error::Error;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;
use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
    mpsc,
};
use std::time::Duration;

// Type alias for the audio engine creation result
type AudioEngineResult = Result<(AudioEngine, mpsc::Receiver<Vec<f32>>), Box<dyn Error>>;

pub struct AudioInfo {
    pub channels: u16,
    pub sample_rate: u32,
}

pub struct AudioEngine {
    _stream: OutputStream,
    sink: Sink,
    samples_tx: mpsc::Sender<Vec<f32>>,
    pub info: Option<AudioInfo>,
    pub duration: Option<Duration>,
    samples_played: Arc<AtomicUsize>,
    total_samples: usize,
    current_file_path: Option<String>,
    cached_aiff_data: Option<crate::media::metadata::AiffData>,
}

impl AudioEngine {
    pub fn new() -> AudioEngineResult {
        // Create output stream using rodio 0.21 API
        let stream = OutputStreamBuilder::open_default_stream()
            .map_err(|e| Box::new(e) as Box<dyn Error>)?;
        let sink = Sink::connect_new(stream.mixer());
        let (samples_tx, samples_rx) = mpsc::channel();

        Ok((
            Self {
                _stream: stream,
                sink,
                samples_tx,
                info: None,
                duration: None,
                samples_played: Arc::new(AtomicUsize::new(0)),
                total_samples: 0,
                current_file_path: None,
                cached_aiff_data: None,
            },
            samples_rx,
        ))
    }

    pub fn load_file(&mut self, path: &Path) -> Result<(), Box<dyn Error>> {
        // Stop any currently playing audio
        self.sink.stop();

        // Create a new sink for the new file
        // Note: We can't easily recreate the sink from stored stream in rodio 0.21
        // For now, we'll clear the current sink
        self.sink.stop();

        // Reset position tracking
        self.samples_played.store(0, Ordering::Relaxed);

        // Store the file path for seeking
        self.current_file_path = Some(path.to_string_lossy().to_string());

        // Open and decode the file
        let file = BufReader::new(File::open(path)?);

        // Try to decode based on extension
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_lowercase())
            .unwrap_or_default();

        match ext.as_str() {
            "wav" => {
                let decoder = hound::WavReader::new(file)?;
                self.play_wav(decoder)?;
            }
            "flac" => {
                self.play_flac(path)?;
            }
            "aif" | "aiff" => {
                self.play_aiff(path)?;
            }
            _ => return Err(format!("Unsupported audio format: {ext}").into()),
        }

        Ok(())
    }

    fn play_wav(
        &mut self,
        reader: hound::WavReader<BufReader<File>>,
    ) -> Result<(), Box<dyn Error>> {
        let spec = reader.spec();

        log::info!(
            "WAV format: {:?}, sample format: {:?}",
            spec,
            spec.sample_format
        );

        // Store audio info
        self.info = Some(AudioInfo {
            channels: spec.channels,
            sample_rate: spec.sample_rate,
        });

        // Create a monitoring source that sends samples to visualization
        let source = WavSource::new(reader, self.samples_tx.clone(), self.samples_played.clone())?;

        // Get duration from source
        self.duration = source.total_duration();
        self.total_samples = source.current_samples.len();

        log::info!(
            "WAV loaded: {} total samples, duration: {:?}",
            self.total_samples,
            self.duration
        );

        // Play through rodio
        self.sink.append(source);

        log::info!(
            "Playing WAV: {} Hz, {} channels, {} bits",
            spec.sample_rate,
            spec.channels,
            spec.bits_per_sample
        );

        Ok(())
    }

    fn play_flac(&mut self, path: &Path) -> Result<(), Box<dyn Error>> {
        let reader = claxon::FlacReader::open(path)?;
        let info = reader.streaminfo();

        // Store audio info
        self.info = Some(AudioInfo {
            channels: info.channels as u16,
            sample_rate: info.sample_rate,
        });

        // Create FLAC source
        let source = FlacSource::new(reader, self.samples_tx.clone(), self.samples_played.clone())?;

        // Get duration from source
        self.duration = source.total_duration();
        self.total_samples = source.current_samples.len();

        log::info!(
            "FLAC loaded: {} total samples, duration: {:?}",
            self.total_samples,
            self.duration
        );

        // Play through rodio
        self.sink.append(source);

        log::info!(
            "Playing FLAC: {} Hz, {} channels",
            info.sample_rate,
            info.channels
        );

        Ok(())
    }

    fn play_aiff(&mut self, path: &Path) -> Result<(), Box<dyn Error>> {
        // Load full AIFF file for best seek performance and seamless playback
        log::info!("Loading AIFF file: {}", path.display());
        let aiff_data = crate::media::metadata::read_aiff_data(path)?;

        log::info!(
            "AIFF loaded: {} total samples",
            aiff_data.audio_samples.len()
        );

        let source = AiffSource::from_data(
            aiff_data.clone(),
            self.samples_tx.clone(),
            self.samples_played.clone(),
        )?;

        // Cache the full data for fast seeking
        self.cached_aiff_data = Some(aiff_data.clone());

        self.info = Some(AudioInfo {
            sample_rate: source.sample_rate(),
            channels: source.channels(),
        });

        // Store the file path for seeking
        self.current_file_path = Some(path.to_string_lossy().to_string());

        // Set duration and total samples from our parser
        self.duration = source.total_duration();
        self.total_samples = source.total_samples();

        let calculated_duration = source.total_duration();
        log::info!(
            "AIFF loaded: {} Hz, {} channels, {} samples loaded (of {} total), calculated duration: {:?}",
            source.sample_rate(),
            source.channels(),
            aiff_data.audio_samples.len(),
            source.total_samples(),
            calculated_duration
        );

        // Log duration in seconds for easy comparison with QuickTime
        if let Some(duration) = calculated_duration {
            log::info!("AIFF duration: {:.2} seconds", duration.as_secs_f64());
        }

        // Play through rodio
        self.sink.append(source);

        Ok(())
    }

    pub fn play(&self) {
        self.sink.play();
    }

    pub fn pause(&self) {
        self.sink.pause();
    }

    pub fn get_progress(&self) -> f32 {
        // Return progress as 0.0 to 1.0
        if self.total_samples > 0 {
            let played = self.samples_played.load(Ordering::Relaxed);
            let progress = played as f32 / self.total_samples as f32;
            progress.min(1.0)
        } else {
            0.0
        }
    }

    fn load_file_from_position(
        &mut self,
        path: &Path,
        start_sample: usize,
    ) -> Result<(), Box<dyn Error>> {
        // Open and decode the file
        let file = BufReader::new(File::open(path)?);

        // Try to decode based on extension
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_lowercase())
            .unwrap_or_default();

        match ext.as_str() {
            "wav" => {
                let decoder = hound::WavReader::new(file)?;
                self.play_wav_from_position(decoder, start_sample)?;
            }
            "flac" => {
                self.play_flac_from_position(path, start_sample)?;
            }
            "aif" | "aiff" => {
                self.play_aiff_from_position(path, start_sample)?;
            }
            _ => return Err(format!("Unsupported audio format: {ext}").into()),
        }

        Ok(())
    }

    fn play_wav_from_position(
        &mut self,
        reader: hound::WavReader<BufReader<File>>,
        start_sample: usize,
    ) -> Result<(), Box<dyn Error>> {
        let _spec = reader.spec();

        // Create a monitoring source that sends samples to visualization
        let mut source =
            WavSource::new(reader, self.samples_tx.clone(), self.samples_played.clone())?;

        // Skip to the start position
        source.skip_to(start_sample);

        // Play through rodio
        self.sink.append(source);

        log::info!("Playing WAV from sample: {start_sample}");

        Ok(())
    }

    fn play_flac_from_position(
        &mut self,
        path: &Path,
        start_sample: usize,
    ) -> Result<(), Box<dyn Error>> {
        let reader = claxon::FlacReader::open(path)?;

        // Create FLAC source
        let mut source =
            FlacSource::new(reader, self.samples_tx.clone(), self.samples_played.clone())?;

        // Skip to the start position
        source.skip_to(start_sample);

        // Play through rodio
        self.sink.append(source);

        log::info!("Playing FLAC from sample: {start_sample}");

        Ok(())
    }

    fn play_aiff_from_position(
        &mut self,
        _path: &Path,
        start_sample: usize,
    ) -> Result<(), Box<dyn Error>> {
        // Use cached AIFF data for fast seeking
        if let Some(aiff_data) = &self.cached_aiff_data {
            let mut source = AiffSource::from_data(
                aiff_data.clone(),
                self.samples_tx.clone(),
                self.samples_played.clone(),
            )?;

            // Skip to the start position
            source.skip_to(start_sample);

            // Play through rodio
            self.sink.append(source);

            log::info!("Playing AIFF from sample: {start_sample}");
            Ok(())
        } else {
            Err("No cached AIFF data available for seeking".into())
        }
    }

    pub fn seek_relative(&mut self, seconds: f32) -> Result<(), Box<dyn Error>> {
        // Seek forward or backward by seconds
        if let Some(info) = &self.info {
            let samples_per_second = info.sample_rate as f32 * info.channels as f32;
            let sample_offset = (seconds * samples_per_second) as isize;

            let current = self.samples_played.load(Ordering::Relaxed) as isize;
            let new_position = (current + sample_offset).max(0) as usize;
            let new_position = new_position.min(self.total_samples);

            // Since rodio doesn't support seeking, we need to reload the file at the new position
            if let Some(path) = self.current_file_path.clone() {
                let was_playing = !self.sink.is_paused();

                // Stop current playback
                self.sink.stop();

                // Create a new sink
                // Note: We can't easily recreate the sink from stored stream in rodio 0.21
                // For now, we'll clear the current sink
                self.sink.stop();

                // Update position counter
                self.samples_played.store(new_position, Ordering::Relaxed);

                // Reload the file starting from the new position
                self.load_file_from_position(Path::new(&path), new_position)?;

                if was_playing {
                    self.play();
                }

                log::info!(
                    "Seek to sample {} ({}%)",
                    new_position,
                    (new_position as f32 / self.total_samples as f32 * 100.0) as u32
                );
            }
        }
        Ok(())
    }
}

// Custom source that monitors samples for visualization
struct WavSource {
    samples_tx: mpsc::Sender<Vec<f32>>,
    sample_rate: u32,
    channels: u16,
    bits_per_sample: u16,
    current_samples: Vec<i32>, // Use i32 to handle up to 24-bit
    position: usize,
    monitor_buffer: Vec<f32>,
    samples_played: Arc<AtomicUsize>,
}

impl WavSource {
    fn new(
        mut reader: hound::WavReader<BufReader<File>>,
        samples_tx: mpsc::Sender<Vec<f32>>,
        samples_played: Arc<AtomicUsize>,
    ) -> Result<Self, Box<dyn Error>> {
        let spec = reader.spec();

        // Read samples based on bit depth
        let samples = match spec.bits_per_sample {
            16 => {
                let samples: Result<Vec<i16>, _> = reader.samples().collect();
                samples?.into_iter().map(|s| s as i32).collect()
            }
            24 => {
                let samples: Result<Vec<i32>, _> = reader.samples().collect();
                samples?
            }
            32 => {
                let samples: Result<Vec<i32>, _> = reader.samples().collect();
                samples?
            }
            8 => {
                let samples: Result<Vec<i8>, _> = reader.samples().collect();
                samples?.into_iter().map(|s| (s as i32) << 8).collect()
            }
            _ => return Err(format!("Unsupported bit depth: {}", spec.bits_per_sample).into()),
        };

        Ok(Self {
            samples_tx,
            sample_rate: spec.sample_rate,
            channels: spec.channels,
            bits_per_sample: spec.bits_per_sample,
            current_samples: samples,
            position: 0,
            monitor_buffer: Vec::with_capacity(1024),
            samples_played,
        })
    }

    fn skip_to(&mut self, sample_position: usize) {
        self.position = sample_position.min(self.current_samples.len());
    }
}

impl Iterator for WavSource {
    type Item = f32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.position >= self.current_samples.len() {
            return None;
        }

        let sample = self.current_samples[self.position];
        self.position += 1;

        // Update samples played counter
        let _count = self.samples_played.fetch_add(1, Ordering::Relaxed);

        // Convert to f32 (rodio 0.21+ uses f32 samples)
        let sample_f32 = match self.bits_per_sample {
            16 => sample as f32 / 32768.0,       // i16 max
            24 => sample as f32 / 8388608.0,     // 24-bit max (2^23)
            32 => sample as f32 / 2147483648.0,  // i32 max
            8 => (sample << 8) as f32 / 32768.0, // Shift 8-bit and normalize
            _ => sample as f32 / 32768.0,
        };

        // Store normalized sample for visualization
        self.monitor_buffer.push(sample_f32);

        // Send visualization data in chunks (keeping stereo interleaving)
        // For stereo: buffer will contain L,R,L,R,L,R...
        let chunk_size = if self.channels > 1 { 2048 } else { 1024 };
        if self.monitor_buffer.len() >= chunk_size {
            let _ = self.samples_tx.send(self.monitor_buffer.clone());
            self.monitor_buffer.clear();
        }

        Some(sample_f32)
    }
}

impl Source for WavSource {
    fn current_span_len(&self) -> Option<usize> {
        None
    }

    fn channels(&self) -> u16 {
        self.channels
    }

    fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    fn total_duration(&self) -> Option<Duration> {
        let total_samples = self.current_samples.len() as u64;
        let duration_secs = total_samples as f64 / (self.sample_rate as f64 * self.channels as f64);
        Some(Duration::from_secs_f64(duration_secs))
    }
}

// FLAC source with monitoring
struct FlacSource {
    samples_tx: mpsc::Sender<Vec<f32>>,
    sample_rate: u32,
    channels: u32,
    bits_per_sample: u32,
    current_samples: Vec<i32>,
    position: usize,
    monitor_buffer: Vec<f32>,
    samples_played: Arc<AtomicUsize>,
}

impl FlacSource {
    fn new<R: Read>(
        mut reader: claxon::FlacReader<R>,
        samples_tx: mpsc::Sender<Vec<f32>>,
        samples_played: Arc<AtomicUsize>,
    ) -> Result<Self, Box<dyn Error>> {
        let info = reader.streaminfo();

        // Read all samples
        let mut samples = Vec::new();
        for sample in reader.samples() {
            samples.push(sample?);
        }

        Ok(Self {
            samples_tx,
            sample_rate: info.sample_rate,
            channels: info.channels,
            bits_per_sample: info.bits_per_sample,
            current_samples: samples,
            position: 0,
            monitor_buffer: Vec::with_capacity(1024),
            samples_played,
        })
    }

    fn skip_to(&mut self, sample_position: usize) {
        self.position = sample_position.min(self.current_samples.len());
    }
}

impl Iterator for FlacSource {
    type Item = f32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.position >= self.current_samples.len() {
            return None;
        }

        let sample = self.current_samples[self.position];
        self.position += 1;

        // Update samples played counter
        let _count = self.samples_played.fetch_add(1, Ordering::Relaxed);

        // Convert to f32 (rodio 0.21+ uses f32 samples)
        let sample_f32 = match self.bits_per_sample {
            16 => sample as f32 / 32768.0,     // i16 max
            24 => sample as f32 / 8388608.0,   // 24-bit max (2^23)
            _ => sample as f32 / 2147483648.0, // 32-bit max
        };

        // Store normalized sample for visualization
        self.monitor_buffer.push(sample_f32);

        // Send visualization data in chunks (keeping stereo interleaving)
        let chunk_size = if self.channels > 1 { 2048 } else { 1024 };
        if self.monitor_buffer.len() >= chunk_size {
            let _ = self.samples_tx.send(self.monitor_buffer.clone());
            self.monitor_buffer.clear();
        }

        Some(sample_f32)
    }
}

impl Source for FlacSource {
    fn current_span_len(&self) -> Option<usize> {
        None
    }

    fn channels(&self) -> u16 {
        self.channels as u16
    }

    fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    fn total_duration(&self) -> Option<Duration> {
        let total_samples = self.current_samples.len() as u64;
        let duration_secs = total_samples as f64 / (self.sample_rate as f64 * self.channels as f64);
        Some(Duration::from_secs_f64(duration_secs))
    }
}

// AIFF source with monitoring
struct AiffSource {
    samples_tx: mpsc::Sender<Vec<f32>>,
    sample_rate: u32,
    channels: u16,
    bits_per_sample: u16,
    current_samples: Vec<i32>,
    position: usize,
    monitor_buffer: Vec<f32>,
    samples_played: Arc<AtomicUsize>,
}

impl AiffSource {
    #[allow(dead_code)]
    fn new(
        path: &Path,
        samples_tx: mpsc::Sender<Vec<f32>>,
        samples_played: Arc<AtomicUsize>,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let aiff_data = crate::media::metadata::read_aiff_data(path)?;
        Self::from_data(aiff_data, samples_tx, samples_played)
    }

    fn from_data(
        aiff_data: crate::media::metadata::AiffData,
        samples_tx: mpsc::Sender<Vec<f32>>,
        samples_played: Arc<AtomicUsize>,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(Self {
            samples_tx,
            sample_rate: aiff_data.sample_rate,
            channels: aiff_data.channels,
            bits_per_sample: aiff_data.bits_per_sample,
            current_samples: aiff_data.audio_samples,
            position: 0,
            monitor_buffer: Vec::new(),
            samples_played,
        })
    }

    fn skip_to(&mut self, sample_index: usize) {
        self.position = sample_index.min(self.current_samples.len());
    }

    fn total_samples(&self) -> usize {
        self.current_samples.len()
    }
}

impl Iterator for AiffSource {
    type Item = f32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.position >= self.current_samples.len() {
            return None;
        }

        let sample = self.current_samples[self.position];
        self.position += 1;

        // Update samples played counter
        let _count = self.samples_played.fetch_add(1, Ordering::Relaxed);

        // Convert to f32 (rodio 0.21+ uses f32 samples)
        let sample_f32 = match self.bits_per_sample {
            16 => sample as f32 / 32768.0,       // i16 max
            24 => sample as f32 / 8388608.0,     // 24-bit max (2^23)
            32 => sample as f32 / 2147483648.0,  // i32 max
            8 => (sample << 8) as f32 / 32768.0, // Shift 8-bit and normalize
            _ => sample as f32 / 32768.0,
        };

        // Store normalized sample for visualization
        self.monitor_buffer.push(sample_f32);

        // Send samples in chunks of 1024 for visualization
        if self.monitor_buffer.len() >= 1024 {
            let _ = self.samples_tx.send(self.monitor_buffer.clone());
            self.monitor_buffer.clear();
        }

        Some(sample_f32)
    }
}

impl rodio::Source for AiffSource {
    fn current_span_len(&self) -> Option<usize> {
        None
    }

    fn channels(&self) -> u16 {
        self.channels
    }

    fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    fn total_duration(&self) -> Option<Duration> {
        let total_samples = self.current_samples.len() as u64;
        let duration_secs = total_samples as f64 / (self.sample_rate as f64 * self.channels as f64);
        Some(Duration::from_secs_f64(duration_secs))
    }
}

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

    fn is_ci_environment() -> bool {
        // Check common CI environment variables
        std::env::var("CI").is_ok()
            || std::env::var("GITHUB_ACTIONS").is_ok()
            || std::env::var("TRAVIS").is_ok()
            || std::env::var("CIRCLECI").is_ok()
            || std::env::var("GITLAB_CI").is_ok()
            || std::env::var("BUILDKITE").is_ok()
            || std::env::var("DRONE").is_ok()
    }

    fn skip_if_no_audio() -> Result<(), Box<dyn Error>> {
        if is_ci_environment() {
            eprintln!("CI environment detected - skipping audio test");
            eprintln!(
                "CI={:?}, GITHUB_ACTIONS={:?}",
                std::env::var("CI").ok(),
                std::env::var("GITHUB_ACTIONS").ok()
            );
            return Err("Audio not available in CI".into());
        }
        Ok(())
    }

    #[test]
    fn test_new_audio_engine() {
        if skip_if_no_audio().is_err() {
            return;
        }

        let result = AudioEngine::new();

        // In CI or systems without audio, this might fail
        if result.is_err() {
            eprintln!("Skipping test: AudioEngine creation failed (no audio device?)");
            return;
        }

        let (engine, rx) = result.unwrap();
        assert!(engine.info.is_none());
        assert!(engine.duration.is_none());
        assert_eq!(engine.total_samples, 0);
        assert!(engine.current_file_path.is_none());

        // Channel should be ready to receive
        assert!(rx.try_recv().is_err()); // Should be empty but not disconnected
    }

    #[test]
    fn test_audio_engine_initial_state() {
        if skip_if_no_audio().is_err() {
            return;
        }

        // Try to create engine, skip test if it fails (likely due to no audio device)
        let result = AudioEngine::new();
        if result.is_err() {
            eprintln!("Skipping test: AudioEngine creation failed (no audio device?)");
            return;
        }

        let (engine, _rx) = result.unwrap();

        // Check initial progress
        assert_eq!(engine.get_progress(), 0.0);
    }

    #[test]
    fn test_load_nonexistent_file() {
        if skip_if_no_audio().is_err() {
            return;
        }

        let result = AudioEngine::new();
        if result.is_err() {
            eprintln!("Skipping test: AudioEngine creation failed (no audio device?)");
            return;
        }

        let (mut engine, _rx) = result.unwrap();
        let result = engine.load_file(Path::new("/nonexistent/file.wav"));

        assert!(result.is_err());
    }

    #[test]
    fn test_load_unsupported_format() {
        if skip_if_no_audio().is_err() {
            return;
        }

        let result = AudioEngine::new();
        if result.is_err() {
            eprintln!("Skipping test: AudioEngine creation failed (no audio device?)");
            return;
        }

        let (mut engine, _rx) = result.unwrap();
        let result = engine.load_file(Path::new("test.mp3"));

        assert!(result.is_err());
        // The actual error depends on whether the file exists or not
        // If file doesn't exist, we get a file system error
        // So just check that it fails
    }

    #[test]
    fn test_play_pause_commands() {
        if skip_if_no_audio().is_err() {
            return;
        }

        let result = AudioEngine::new();
        if result.is_err() {
            eprintln!("Skipping test: AudioEngine creation failed (no audio device?)");
            return;
        }

        let (engine, _rx) = result.unwrap();

        // Test that play and pause commands don't panic
        engine.play();
        engine.pause();

        // Can't test actual state without a loaded file
    }

    #[test]
    fn test_seek_without_file() {
        if skip_if_no_audio().is_err() {
            return;
        }

        let result = AudioEngine::new();
        if result.is_err() {
            eprintln!("Skipping test: AudioEngine creation failed (no audio device?)");
            return;
        }

        let (mut engine, _rx) = result.unwrap();

        // Seeking without a loaded file should fail gracefully
        let result = engine.seek_relative(5.0);
        assert!(result.is_ok()); // Actually returns Ok(()) when no info
    }

    #[test]
    fn test_progress_without_file() {
        if skip_if_no_audio().is_err() {
            return;
        }

        let result = AudioEngine::new();
        if result.is_err() {
            eprintln!("Skipping test: AudioEngine creation failed (no audio device?)");
            return;
        }

        let (engine, _rx) = result.unwrap();

        // Progress without file should be 0
        assert_eq!(engine.get_progress(), 0.0);
    }
}