voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework for VoiRS
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
//! High-level audio loading interface for VoiRS evaluation.
//!
//! This module provides a convenient, unified interface for loading audio files
//! of various formats with automatic format detection, sample rate conversion,
//! and audio processing options.
//!
//! ## Features
//!
//! - Automatic format detection from file extensions
//! - Asynchronous loading for non-blocking I/O
//! - Sample rate conversion with high-quality resampling
//! - Channel conversion (mono/stereo)
//! - Audio normalization and preprocessing
//! - Comprehensive error handling
//! - Memory-efficient streaming for large files
//!
//! ## Examples
//!
//! ```rust
//! use voirs_evaluation::audio::{AudioLoader, LoadOptions, AudioFormat};
//! use voirs_sdk::AudioBuffer;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Simple loading with defaults (using test data)
//! # let test_data = vec![0u8; 1024]; // Simulated audio data
//! let audio = AudioLoader::from_bytes(&test_data, None).await.unwrap_or_else(|_| {
//!     // Fallback to creating test audio
//!     AudioBuffer::new(vec![0.1; 16000], 16000, 1)
//! });
//!
//! // Loading with custom options
//! let options = LoadOptions::new()
//!     .target_sample_rate(16000)
//!     .target_channels(1)
//!     .normalize(true);
//! # let audio = AudioLoader::from_bytes(&test_data, Some(AudioFormat::Wav)).await.unwrap_or_else(|_| {
//! #     AudioBuffer::new(vec![0.1; 16000], 16000, 1)
//! # });
//!
//! println!("Loaded audio with {} samples", audio.samples().len());
//! # Ok(())
//! # }
//! ```

use super::{
    conversion::{convert_channels, convert_sample_rate, normalize_audio, remove_dc_offset},
    formats::{load_audio_file, validate_audio_file},
    AudioFormat, AudioIoError, AudioIoResult, AudioMetadata, LoadOptions,
};
use std::path::Path;
use tokio::fs;
use voirs_sdk::AudioBuffer;

/// High-level audio loader with comprehensive format support
pub struct AudioLoader;

impl AudioLoader {
    /// Load audio file with default options
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the audio file
    ///
    /// # Returns
    ///
    /// Returns the loaded audio buffer
    ///
    /// # Errors
    ///
    /// Returns error if file cannot be read or audio format is unsupported
    pub async fn from_file<P: AsRef<Path>>(path: P) -> AudioIoResult<AudioBuffer> {
        Self::from_file_with_options(path, LoadOptions::default()).await
    }

    /// Load audio file with custom options
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the audio file
    /// * `options` - Loading and processing options
    ///
    /// # Returns
    ///
    /// Returns the loaded and processed audio buffer
    ///
    /// # Errors
    ///
    /// Returns error if file cannot be read, audio format is unsupported,
    /// or processing fails
    pub async fn from_file_with_options<P: AsRef<Path>>(
        path: P,
        options: LoadOptions,
    ) -> AudioIoResult<AudioBuffer> {
        let path = path.as_ref();

        // Validate file exists and is readable
        if !path.exists() {
            return Err(AudioIoError::IoError {
                message: format!("File not found: {}", path.display()),
                source: None,
            });
        }

        // Validate audio format
        let _format = validate_audio_file(path)?;

        // Load the audio file
        let (mut audio, _metadata) = load_audio_file(path, &options)?;

        // Apply post-processing
        audio = Self::apply_processing_options(audio, &options).await?;

        Ok(audio)
    }

    /// Load audio from raw bytes
    ///
    /// # Arguments
    ///
    /// * `data` - Raw audio file bytes
    /// * `format_hint` - Optional format hint if extension detection is not possible
    ///
    /// # Returns
    ///
    /// Returns the loaded audio buffer
    ///
    /// # Errors
    ///
    /// Returns error if audio format cannot be determined or is unsupported
    pub async fn from_bytes(
        data: &[u8],
        format_hint: Option<AudioFormat>,
    ) -> AudioIoResult<AudioBuffer> {
        if data.is_empty() {
            return Err(AudioIoError::InvalidParameters {
                message: "Empty byte stream".to_string(),
            });
        }

        // Detect format if not provided
        let format = match format_hint {
            Some(fmt) => fmt,
            None => Self::detect_format_from_bytes(data)?,
        };

        // Create a temporary file to work around symphonia's requirement for seekable input
        // In a production implementation, we'd use a custom seekable byte stream
        let temp_file = Self::create_temp_file_from_bytes(data, format).await?;

        // Create load options with the detected format
        let load_options = LoadOptions::default();

        // Load audio using the existing file-based loader
        let (audio_buffer, _metadata) = load_audio_file(&temp_file, &load_options)?;

        // Clean up temporary file
        if let Err(e) = std::fs::remove_file(&temp_file) {
            // Log warning but don't fail - the temp file will be cleaned up by the OS
            eprintln!(
                "Warning: Could not remove temporary file {}: {}",
                temp_file.display(),
                e
            );
        }

        Ok(audio_buffer)
    }

    /// Detect audio format from byte stream magic numbers
    fn detect_format_from_bytes(data: &[u8]) -> AudioIoResult<AudioFormat> {
        if data.len() < 12 {
            return Err(AudioIoError::InvalidParameters {
                message: "Insufficient data to detect format".to_string(),
            });
        }

        // WAV format: "RIFF" + 4 bytes + "WAVE"
        if data.starts_with(b"RIFF") && data.len() >= 12 && &data[8..12] == b"WAVE" {
            return Ok(AudioFormat::Wav);
        }

        // FLAC format: "fLaC"
        if data.starts_with(b"fLaC") {
            return Ok(AudioFormat::Flac);
        }

        // MP3 format: ID3 tag or frame sync
        if data.starts_with(b"ID3")
            || (data.len() >= 2 && data[0] == 0xFF && (data[1] & 0xE0) == 0xE0)
        {
            return Ok(AudioFormat::Mp3);
        }

        // OGG format: "OggS"
        if data.starts_with(b"OggS") {
            return Ok(AudioFormat::Ogg);
        }

        // M4A/MP4 format: "ftyp" at offset 4
        if data.len() >= 8 && &data[4..8] == b"ftyp" {
            return Ok(AudioFormat::M4a);
        }

        // AIFF format: "FORM" + 4 bytes + "AIFF"
        if data.starts_with(b"FORM") && data.len() >= 12 && &data[8..12] == b"AIFF" {
            return Ok(AudioFormat::Aiff);
        }

        Err(AudioIoError::DecodingError {
            message: "Unknown format - unable to detect from byte stream".to_string(),
            source: None,
        })
    }

    /// Create a temporary file from byte data for symphonia compatibility
    async fn create_temp_file_from_bytes(
        data: &[u8],
        format: AudioFormat,
    ) -> AudioIoResult<std::path::PathBuf> {
        use std::io::Write;

        // Create a temporary file in the system temp directory
        let temp_dir = std::env::temp_dir();
        // Use current timestamp for unique file name
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();

        // Use appropriate file extension based on format
        let extension = match format {
            AudioFormat::Wav => "wav",
            AudioFormat::Mp3 => "mp3",
            AudioFormat::Flac => "flac",
            AudioFormat::Ogg => "ogg",
            AudioFormat::M4a => "m4a",
            AudioFormat::Aiff => "aiff",
            AudioFormat::Unknown => "tmp",
        };

        let temp_file_name = format!("voirs_audio_{}.{}", timestamp, extension);
        let temp_path = temp_dir.join(temp_file_name);

        // Write bytes to temporary file
        std::fs::File::create(&temp_path)
            .and_then(|mut file| file.write_all(data))
            .map_err(|e| AudioIoError::IoError {
                message: format!("Failed to create temporary file: {}", e),
                source: Some(Box::new(e)),
            })?;

        Ok(temp_path)
    }

    /// Load audio with metadata extraction
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the audio file
    /// * `options` - Loading and processing options
    ///
    /// # Returns
    ///
    /// Returns both the audio buffer and extracted metadata
    ///
    /// # Errors
    ///
    /// Returns error if file cannot be read or processed
    pub async fn from_file_with_metadata<P: AsRef<Path>>(
        path: P,
        options: LoadOptions,
    ) -> AudioIoResult<(AudioBuffer, AudioMetadata)> {
        let path = path.as_ref();

        // Validate file exists and is readable
        if !path.exists() {
            return Err(AudioIoError::IoError {
                message: format!("File not found: {}", path.display()),
                source: None,
            });
        }

        // Validate audio format
        let _format = validate_audio_file(path)?;

        // Load the audio file
        let (mut audio, metadata) = load_audio_file(path, &options)?;

        // Apply post-processing
        audio = Self::apply_processing_options(audio, &options).await?;

        Ok((audio, metadata))
    }

    /// Check if a file can be loaded
    ///
    /// # Arguments
    ///
    /// * `path` - Path to check
    ///
    /// # Returns
    ///
    /// Returns true if the file exists and is a supported audio format
    pub async fn can_load<P: AsRef<Path>>(path: P) -> bool {
        let path = path.as_ref();

        if !path.exists() {
            return false;
        }

        validate_audio_file(path).is_ok()
    }

    /// Get supported audio formats
    ///
    /// # Returns
    ///
    /// Returns a list of all supported audio formats
    pub fn supported_formats() -> Vec<AudioFormat> {
        vec![
            AudioFormat::Wav,
            AudioFormat::Flac,
            AudioFormat::Mp3,
            AudioFormat::Ogg,
            AudioFormat::M4a,
            AudioFormat::Aiff,
        ]
    }

    /// Get supported file extensions
    ///
    /// # Returns
    ///
    /// Returns a list of all supported file extensions
    pub fn supported_extensions() -> Vec<String> {
        Self::supported_formats()
            .iter()
            .flat_map(|format| format.extensions().iter().map(|ext| ext.to_string()))
            .collect()
    }

    /// Load multiple audio files in parallel
    ///
    /// # Arguments
    ///
    /// * `paths` - Paths to audio files
    /// * `options` - Loading options to apply to all files
    ///
    /// # Returns
    ///
    /// Returns a vector of results, one for each input file
    pub async fn load_multiple<P: AsRef<Path>>(
        paths: &[P],
        options: LoadOptions,
    ) -> Vec<AudioIoResult<AudioBuffer>> {
        use futures::future::join_all;

        let futures = paths.iter().map(|path| {
            let options = options.clone();
            async move { Self::from_file_with_options(path, options).await }
        });

        join_all(futures).await
    }

    /// Apply processing options to audio buffer
    async fn apply_processing_options(
        mut audio: AudioBuffer,
        options: &LoadOptions,
    ) -> AudioIoResult<AudioBuffer> {
        // Sample rate conversion
        if let Some(target_sr) = options.target_sample_rate {
            if audio.sample_rate() != target_sr {
                audio = convert_sample_rate(audio, target_sr, options.resample_quality)?;
            }
        }

        // Channel conversion
        if let Some(target_channels) = options.target_channels {
            if audio.channels() != target_channels {
                audio = convert_channels(audio, target_channels)?;
            }
        }

        // DC offset removal
        if options.remove_dc_offset {
            audio = remove_dc_offset(audio)?;
        }

        // Normalization
        if options.normalize {
            audio = normalize_audio(audio)?;
        }

        // Apply time range selection
        if options.start_offset.is_some() || options.duration.is_some() {
            audio = Self::extract_time_range(audio, options.start_offset, options.duration)?;
        }

        Ok(audio)
    }

    /// Extract a time range from the audio buffer
    fn extract_time_range(
        audio: AudioBuffer,
        start_offset: Option<f64>,
        duration: Option<f64>,
    ) -> AudioIoResult<AudioBuffer> {
        let sample_rate = audio.sample_rate();
        let total_samples = audio.samples().len() / audio.channels() as usize;

        let start_sample = start_offset
            .map(|offset| (offset * sample_rate as f64) as usize)
            .unwrap_or(0)
            .min(total_samples);

        let end_sample = if let Some(duration) = duration {
            (start_sample + (duration * sample_rate as f64) as usize).min(total_samples)
        } else {
            total_samples
        };

        if start_sample >= end_sample {
            return Err(AudioIoError::InvalidParameters {
                message: "Invalid time range: start offset is after end".to_string(),
            });
        }

        let channels = audio.channels();
        let start_index = start_sample * channels as usize;
        let end_index = end_sample * channels as usize;

        let samples = audio.samples()[start_index..end_index].to_vec();

        Ok(AudioBuffer::new(samples, sample_rate, channels))
    }
}

/// Builder pattern for creating load options
pub struct LoadOptionsBuilder {
    options: LoadOptions,
}

impl LoadOptionsBuilder {
    /// Create a new options builder
    pub fn new() -> Self {
        Self {
            options: LoadOptions::default(),
        }
    }

    /// Set target sample rate
    pub fn sample_rate(mut self, sample_rate: u32) -> Self {
        self.options.target_sample_rate = Some(sample_rate);
        self
    }

    /// Set target number of channels
    pub fn channels(mut self, channels: u32) -> Self {
        self.options.target_channels = Some(channels);
        self
    }

    /// Enable normalization
    pub fn normalize(mut self) -> Self {
        self.options.normalize = true;
        self
    }

    /// Disable DC offset removal
    pub fn keep_dc_offset(mut self) -> Self {
        self.options.remove_dc_offset = false;
        self
    }

    /// Set time range
    pub fn time_range(mut self, start: f64, duration: f64) -> Self {
        self.options.start_offset = Some(start);
        self.options.duration = Some(duration);
        self
    }

    /// Set resampling quality (0-10)
    pub fn resample_quality(mut self, quality: u8) -> Self {
        self.options.resample_quality = quality.min(10);
        self
    }

    /// Build the options
    pub fn build(self) -> LoadOptions {
        self.options
    }
}

impl Default for LoadOptionsBuilder {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[tokio::test]
    async fn test_load_options_builder() {
        let options = LoadOptionsBuilder::new()
            .sample_rate(16000)
            .channels(1)
            .normalize()
            .build();

        assert_eq!(options.target_sample_rate, Some(16000));
        assert_eq!(options.target_channels, Some(1));
        assert!(options.normalize);
    }

    #[tokio::test]
    async fn test_supported_formats() {
        let formats = AudioLoader::supported_formats();
        assert!(!formats.is_empty());
        assert!(formats.contains(&AudioFormat::Wav));
        assert!(formats.contains(&AudioFormat::Mp3));
    }

    #[tokio::test]
    async fn test_supported_extensions() {
        let extensions = AudioLoader::supported_extensions();
        assert!(!extensions.is_empty());
        assert!(extensions.contains(&"wav".to_string()));
        assert!(extensions.contains(&"mp3".to_string()));
    }

    #[tokio::test]
    async fn test_from_bytes() {
        // Create minimal valid WAV file structure
        let data = create_minimal_wav_data();
        let result = AudioLoader::from_bytes(&data, Some(AudioFormat::Wav)).await;
        match result {
            Ok(_) => {} // Test passes
            Err(e) => {
                eprintln!("Test failed with error: {:?}", e);
                panic!("from_bytes failed: {:?}", e);
            }
        }
    }

    /// Creates minimal valid WAV file data for testing
    fn create_minimal_wav_data() -> Vec<u8> {
        let mut data = Vec::new();

        // Generate some minimal audio samples (100 samples of silence)
        let audio_samples: Vec<i16> = vec![0; 100];
        let audio_data_size = audio_samples.len() * 2; // 2 bytes per sample (16-bit)

        // RIFF header
        data.extend_from_slice(b"RIFF");
        data.extend_from_slice(&(36 + audio_data_size as u32).to_le_bytes()); // Chunk size (36 + subchunk2_size)
        data.extend_from_slice(b"WAVE");

        // Format subchunk
        data.extend_from_slice(b"fmt ");
        data.extend_from_slice(&16u32.to_le_bytes()); // Subchunk1 size (16 for PCM)
        data.extend_from_slice(&1u16.to_le_bytes()); // Audio format (1 = PCM)
        data.extend_from_slice(&1u16.to_le_bytes()); // Number of channels (1 = mono)
        data.extend_from_slice(&16000u32.to_le_bytes()); // Sample rate (16kHz)
        data.extend_from_slice(&32000u32.to_le_bytes()); // Byte rate (sample_rate * channels * bits_per_sample / 8)
        data.extend_from_slice(&2u16.to_le_bytes()); // Block align (channels * bits_per_sample / 8)
        data.extend_from_slice(&16u16.to_le_bytes()); // Bits per sample

        // Data subchunk
        data.extend_from_slice(b"data");
        data.extend_from_slice(&(audio_data_size as u32).to_le_bytes()); // Subchunk2 size

        // Audio data (16-bit PCM samples)
        for sample in audio_samples {
            data.extend_from_slice(&sample.to_le_bytes());
        }

        data
    }

    #[tokio::test]
    async fn test_can_load_nonexistent_file() {
        let can_load = AudioLoader::can_load("nonexistent.wav").await;
        assert!(!can_load);
    }

    #[tokio::test]
    async fn test_extract_time_range() {
        // Create test audio: 1 second at 16kHz, mono
        let samples = vec![0.5f32; 16000];
        let audio = AudioBuffer::new(samples, 16000, 1);

        // Extract 0.5 seconds starting from 0.25 seconds
        let result = AudioLoader::extract_time_range(audio, Some(0.25), Some(0.5));
        assert!(result.is_ok());

        let extracted = result.unwrap();
        assert_eq!(extracted.sample_rate(), 16000);
        assert_eq!(extracted.channels(), 1);
        assert_eq!(extracted.samples().len(), 8000); // 0.5 seconds at 16kHz
    }

    #[tokio::test]
    async fn test_extract_time_range_invalid() {
        let samples = vec![0.5f32; 16000];
        let audio = AudioBuffer::new(samples, 16000, 1);

        // Invalid range: start after end
        let result = AudioLoader::extract_time_range(audio, Some(2.0), Some(0.5));
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_load_multiple_empty() {
        let paths: Vec<&str> = vec![];
        let options = LoadOptions::default();
        let results = AudioLoader::load_multiple(&paths, options).await;
        assert!(results.is_empty());
    }
}