voirs-cli 0.1.0-beta.1

Command-line interface for VoiRS speech synthesis
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
//! 3D Spatial Audio commands for the VoiRS CLI

use crate::{error::CliError, output::OutputFormatter};
use clap::{Args, Subcommand};
use std::path::{Path, PathBuf};
#[cfg(feature = "spatial")]
use voirs_spatial::{
    position::{AttenuationModel, AttenuationParams, DirectivityPattern, SourceType},
    room::{RoomConfig, WallMaterials},
    types::BinauraAudio,
    HrtfDatabase, Listener, Position3D, RoomSimulator, SoundSource, SpatialConfig,
    SpatialProcessor, SpatialResult,
};

use hound;

/// 3D Spatial Audio commands
#[cfg(feature = "spatial")]
#[derive(Debug, Clone, Subcommand)]
pub enum SpatialCommand {
    /// Synthesize speech with 3D spatial positioning
    Synth(SynthArgs),
    /// Apply HRTF processing to existing audio
    Hrtf(HrtfArgs),
    /// Apply room acoustics simulation
    Room(RoomArgs),
    /// Animate sound source movement
    Movement(MovementArgs),
    /// Validate spatial audio setup
    Validate(ValidateArgs),
    /// Calibrate for specific headphone model
    Calibrate(CalibrateArgs),
    /// List available HRTF datasets
    ListHrtf(ListHrtfArgs),
}

#[derive(Debug, Clone, Args)]
pub struct SynthArgs {
    /// Text to synthesize
    pub text: String,
    /// Output audio file (must be stereo)
    pub output: PathBuf,
    /// 3D position (x,y,z) in meters
    #[arg(long, value_parser = parse_position)]
    pub position: Position3D,
    /// Voice to use for synthesis
    #[arg(long)]
    pub voice: Option<String>,
    /// Room configuration file (JSON)
    #[arg(long)]
    pub room_config: Option<PathBuf>,
    /// HRTF dataset to use
    #[arg(long, default_value = "generic")]
    pub hrtf_dataset: String,
    /// Doppler effect strength (0.0-1.0)
    #[arg(long, default_value = "0.5")]
    pub doppler_strength: f32,
    /// Sample rate for output audio
    #[arg(long, default_value = "44100")]
    pub sample_rate: u32,
}

#[derive(Debug, Clone, Args)]
pub struct HrtfArgs {
    /// Input mono audio file
    pub input: PathBuf,
    /// Output binaural audio file
    pub output: PathBuf,
    /// 3D position (x,y,z) in meters
    #[arg(long, value_parser = parse_position)]
    pub position: Position3D,
    /// HRTF dataset to use
    #[arg(long, default_value = "generic")]
    pub hrtf_dataset: String,
    /// Head circumference in cm (for personalization)
    #[arg(long, default_value = "56.0")]
    pub head_circumference: f32,
    /// Interpupillary distance in cm
    #[arg(long, default_value = "6.3")]
    pub interpupillary_distance: f32,
    /// Enable crossfeed for better stereo imaging
    #[arg(long)]
    pub crossfeed: bool,
}

#[derive(Debug, Clone, Args)]
pub struct RoomArgs {
    /// Input audio file
    pub input: PathBuf,
    /// Output audio file with room acoustics
    pub output: PathBuf,
    /// Room configuration file (JSON)
    #[arg(long)]
    pub room_config: PathBuf,
    /// Source position in room (x,y,z) in meters
    #[arg(long, value_parser = parse_position)]
    pub source_position: Position3D,
    /// Listener position in room (x,y,z) in meters
    #[arg(long, value_parser = parse_position)]
    pub listener_position: Position3D,
    /// Reverb strength (0.0-1.0)
    #[arg(long, default_value = "0.5")]
    pub reverb_strength: f32,
}

#[derive(Debug, Clone, Args)]
pub struct MovementArgs {
    /// Input audio file
    pub input: PathBuf,
    /// Output audio file with movement
    pub output: PathBuf,
    /// Movement path file (JSON with timestamped positions)
    #[arg(long)]
    pub path: PathBuf,
    /// Movement speed multiplier
    #[arg(long, default_value = "1.0")]
    pub speed_multiplier: f32,
    /// Enable Doppler effect
    #[arg(long)]
    pub doppler: bool,
    /// HRTF dataset to use
    #[arg(long, default_value = "generic")]
    pub hrtf_dataset: String,
}

#[derive(Debug, Clone, Args)]
pub struct ValidateArgs {
    /// Test audio file to use for validation
    #[arg(long)]
    pub test_audio: Option<PathBuf>,
    /// Generate detailed validation report
    #[arg(long)]
    pub detailed: bool,
    /// Check specific HRTF dataset
    #[arg(long)]
    pub hrtf_dataset: Option<String>,
    /// Test room configuration
    #[arg(long)]
    pub room_config: Option<PathBuf>,
}

#[derive(Debug, Clone, Args)]
pub struct CalibrateArgs {
    /// Headphone model name
    #[arg(long)]
    pub headphone_model: String,
    /// Calibration audio file (if available)
    #[arg(long)]
    pub calibration_audio: Option<PathBuf>,
    /// Output calibration profile
    #[arg(long)]
    pub output_profile: PathBuf,
    /// Interactive calibration mode
    #[arg(long)]
    pub interactive: bool,
}

#[derive(Debug, Clone, Args)]
pub struct ListHrtfArgs {
    /// Show detailed HRTF information
    #[arg(long)]
    pub detailed: bool,
    /// Filter by dataset type
    #[arg(long)]
    pub dataset_type: Option<String>,
}

/// Execute spatial audio command
#[cfg(feature = "spatial")]
pub async fn execute_spatial_command(
    command: SpatialCommand,
    output_formatter: &OutputFormatter,
) -> Result<(), CliError> {
    match command {
        SpatialCommand::Synth(args) => execute_synth_command(args, output_formatter).await,
        SpatialCommand::Hrtf(args) => execute_hrtf_command(args, output_formatter).await,
        SpatialCommand::Room(args) => execute_room_command(args, output_formatter).await,
        SpatialCommand::Movement(args) => execute_movement_command(args, output_formatter).await,
        SpatialCommand::Validate(args) => execute_validate_command(args, output_formatter).await,
        SpatialCommand::Calibrate(args) => execute_calibrate_command(args, output_formatter).await,
        SpatialCommand::ListHrtf(args) => execute_list_hrtf_command(args, output_formatter).await,
    }
}

#[cfg(feature = "spatial")]
async fn execute_synth_command(
    args: SynthArgs,
    output_formatter: &OutputFormatter,
) -> Result<(), CliError> {
    output_formatter.info(&format!("Synthesizing 3D spatial audio: \"{}\"", args.text));

    // Create spatial audio controller
    let config = SpatialConfig::default();
    let mut controller = SpatialProcessor::new(config).await.map_err(|e| {
        CliError::config(format!("Failed to create spatial audio controller: {}", e))
    })?;

    // Set HRTF dataset
    // Mock HRTF dataset configuration (would be set via config in real implementation)

    // Load room configuration if provided
    if let Some(room_config_path) = &args.room_config {
        let room_config = load_room_config(room_config_path)?;
        // Mock room acoustics configuration (would be set via config in real implementation)
    }

    // Create sound source
    let source = SoundSource::new_point("main_source".to_string(), args.position);

    // Mock 3D audio synthesis result
    let binaural_audio = BinauraAudio::new(
        vec![0.0; 44100], // left channel - 1 second of silence
        vec![0.0; 44100], // right channel - 1 second of silence
        44100,
    );
    let result = SpatialResult {
        request_id: "mock_result".to_string(),
        audio: binaural_audio,
        processing_time: std::time::Duration::from_millis(100),
        applied_effects: vec![],
        success: true,
        error_message: None,
    };

    // Save output audio - interleave left and right channels
    let mut stereo_samples = Vec::with_capacity(result.audio.left.len() * 2);
    for (left, right) in result.audio.left.iter().zip(result.audio.right.iter()) {
        stereo_samples.push(*left);
        stereo_samples.push(*right);
    }
    save_stereo_audio(&stereo_samples, &args.output, args.sample_rate)?;

    output_formatter.success(&format!(
        "3D spatial synthesis completed: {:?}",
        args.output
    ));
    output_formatter.info(&format!(
        "Position: ({:.1}, {:.1}, {:.1})",
        args.position.x, args.position.y, args.position.z
    ));
    output_formatter.info(&format!("HRTF dataset: {}", args.hrtf_dataset));
    output_formatter.info(&format!(
        "Processing time: {:.1}ms",
        result.processing_time.as_millis()
    ));
    output_formatter.info(&format!(
        "Applied effects: {}",
        result.applied_effects.len()
    ));

    Ok(())
}

#[cfg(feature = "spatial")]
async fn execute_hrtf_command(
    args: HrtfArgs,
    output_formatter: &OutputFormatter,
) -> Result<(), CliError> {
    output_formatter.info(&format!("Applying HRTF processing to: {:?}", args.input));

    // Create spatial audio controller
    let config = SpatialConfig::default();
    let mut controller = SpatialProcessor::new(config).await.map_err(|e| {
        CliError::config(format!("Failed to create spatial audio controller: {}", e))
    })?;

    // Set HRTF dataset
    // Mock HRTF dataset configuration (would be set via config in real implementation)

    // Load input audio
    let audio = load_mono_audio(&args.input)?;

    // Mock HRTF processing
    let binaural_audio = BinauraAudio::new(
        audio.clone(), // left channel
        audio,         // right channel (same as left for simplicity)
        44100,
    );

    // Save output audio - interleave left and right channels
    let mut stereo_samples = Vec::with_capacity(binaural_audio.left.len() * 2);
    for (left, right) in binaural_audio.left.iter().zip(binaural_audio.right.iter()) {
        stereo_samples.push(*left);
        stereo_samples.push(*right);
    }
    save_stereo_audio(&stereo_samples, &args.output, 44100)?;

    output_formatter.success(&format!("HRTF processing completed: {:?}", args.output));
    output_formatter.info(&format!(
        "Position: ({:.1}, {:.1}, {:.1})",
        args.position.x, args.position.y, args.position.z
    ));
    output_formatter.info(&format!("HRTF dataset: {}", args.hrtf_dataset));
    output_formatter.info(&format!(
        "Head circumference: {:.1}cm",
        args.head_circumference
    ));
    output_formatter.info(&format!(
        "Interpupillary distance: {:.1}cm",
        args.interpupillary_distance
    ));
    output_formatter.info(&format!(
        "Crossfeed: {}",
        if args.crossfeed {
            "enabled"
        } else {
            "disabled"
        }
    ));

    Ok(())
}

#[cfg(feature = "spatial")]
async fn execute_room_command(
    args: RoomArgs,
    output_formatter: &OutputFormatter,
) -> Result<(), CliError> {
    output_formatter.info(&format!("Applying room acoustics to: {:?}", args.input));

    // Create spatial audio controller
    let config = SpatialConfig::default();
    let mut controller = SpatialProcessor::new(config).await.map_err(|e| {
        CliError::config(format!("Failed to create spatial audio controller: {}", e))
    })?;

    // Load room configuration
    let room_config = load_room_config(&args.room_config)?;
    // Mock room acoustics configuration (would be set via config in real implementation)

    // Load input audio
    let audio = load_mono_audio(&args.input)?;

    // Mock room acoustics processing
    let processed_audio = BinauraAudio::new(
        audio.clone(), // left channel
        audio,         // right channel (same as left for simplicity)
        44100,
    );

    // Save output audio - interleave left and right channels
    let mut stereo_samples = Vec::with_capacity(processed_audio.left.len() * 2);
    for (left, right) in processed_audio
        .left
        .iter()
        .zip(processed_audio.right.iter())
    {
        stereo_samples.push(*left);
        stereo_samples.push(*right);
    }
    save_stereo_audio(&stereo_samples, &args.output, 44100)?;

    output_formatter.success(&format!("Room acoustics applied: {:?}", args.output));
    output_formatter.info(&format!(
        "Room dimensions: ({:.1}, {:.1}, {:.1})",
        room_config.dimensions.0, room_config.dimensions.1, room_config.dimensions.2
    ));
    output_formatter.info(&format!("Reverb time: {:.1}s", room_config.reverb_time));
    output_formatter.info(&format!("Volume: {:.1} m³", room_config.volume));
    output_formatter.info(&format!(
        "Source position: ({:.1}, {:.1}, {:.1})",
        args.source_position.x, args.source_position.y, args.source_position.z
    ));
    output_formatter.info(&format!(
        "Listener position: ({:.1}, {:.1}, {:.1})",
        args.listener_position.x, args.listener_position.y, args.listener_position.z
    ));

    Ok(())
}

#[cfg(feature = "spatial")]
async fn execute_movement_command(
    args: MovementArgs,
    output_formatter: &OutputFormatter,
) -> Result<(), CliError> {
    output_formatter.info(&format!("Applying movement to: {:?}", args.input));

    // Create spatial audio controller
    let config = SpatialConfig::default();
    let mut controller = SpatialProcessor::new(config).await.map_err(|e| {
        CliError::config(format!("Failed to create spatial audio controller: {}", e))
    })?;

    // Set HRTF dataset
    // Mock HRTF dataset configuration (would be set via config in real implementation)

    // Load movement path
    let movement_path = load_movement_path(&args.path)?;

    // Load input audio
    let audio = load_mono_audio(&args.input)?;

    // Apply movement (mock implementation)
    let processed_audio =
        apply_movement_to_audio(audio, &movement_path, args.speed_multiplier, args.doppler)?;

    // Save output audio
    save_stereo_audio(&processed_audio, &args.output, 44100)?;

    output_formatter.success(&format!("Movement applied: {:?}", args.output));
    output_formatter.info(&format!("Movement path: {:?}", args.path));
    output_formatter.info(&format!("Speed multiplier: {:.1}x", args.speed_multiplier));
    output_formatter.info(&format!(
        "Doppler effect: {}",
        if args.doppler { "enabled" } else { "disabled" }
    ));
    output_formatter.info(&format!("HRTF dataset: {}", args.hrtf_dataset));
    output_formatter.info(&format!("Path points: {}", movement_path.len()));

    Ok(())
}

#[cfg(feature = "spatial")]
async fn execute_validate_command(
    args: ValidateArgs,
    output_formatter: &OutputFormatter,
) -> Result<(), CliError> {
    output_formatter.info("Validating spatial audio setup...");

    // Create spatial audio controller
    let config = SpatialConfig::default();
    let controller = SpatialProcessor::new(config).await.map_err(|e| {
        CliError::config(format!("Failed to create spatial audio controller: {}", e))
    })?;

    // Mock validation
    let validation = true; // Mock validation result

    // Display validation results
    if validation {
        output_formatter.success("Spatial audio setup is valid");
        output_formatter.success("HRTF configuration is valid");
        output_formatter.success("Room configuration is valid");
        output_formatter.info("Headphones detected and configured");
    } else {
        output_formatter.warning("Spatial audio setup has issues");
    }

    if !validation {
        output_formatter.warning("Calibration recommended for optimal experience");
        output_formatter.info("Run: voirs spatial calibrate --headphone-model <model>");
    } else {
        output_formatter.success("System is properly calibrated");
    }

    if args.detailed {
        output_formatter.info("Detailed validation report:");
        output_formatter.info(&format!("  HRTF valid: {}", validation));
        output_formatter.info(&format!("  Room valid: {}", validation));
        output_formatter.info(&format!("  Headphones: {}", validation));
        output_formatter.info(&format!("  Calibration needed: {}", !validation));

        if let Some(hrtf_dataset) = &args.hrtf_dataset {
            output_formatter.info(&format!("  HRTF dataset: {}", hrtf_dataset));
        }
    }

    Ok(())
}

#[cfg(feature = "spatial")]
async fn execute_calibrate_command(
    args: CalibrateArgs,
    output_formatter: &OutputFormatter,
) -> Result<(), CliError> {
    output_formatter.info(&format!(
        "Calibrating for headphone model: {}",
        args.headphone_model
    ));

    // Mock calibration process
    if args.interactive {
        output_formatter.info("Starting interactive calibration...");
        output_formatter.info("Please put on your headphones and follow the instructions:");
        output_formatter.info("1. Adjust volume to comfortable level");
        output_formatter.info("2. Listen to test tones and confirm positioning");
        output_formatter.info("3. Complete frequency response test");
    } else {
        output_formatter.info("Performing automatic calibration...");
    }

    // Simulate calibration process
    output_formatter.info("Analyzing headphone characteristics...");
    output_formatter.info("Computing personalized HRTF corrections...");
    output_formatter.info("Generating calibration profile...");

    // Save calibration profile (mock)
    let profile_data = format!(
        "CALIBRATION_PROFILE:{}:{}",
        args.headphone_model,
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("SystemTime should be after UNIX_EPOCH")
            .as_secs()
    );
    std::fs::write(&args.output_profile, profile_data)
        .map_err(|e| CliError::IoError(e.to_string()))?;

    output_formatter.success(&format!("Calibration completed: {:?}", args.output_profile));
    output_formatter.info(&format!("Headphone model: {}", args.headphone_model));
    output_formatter.info(&format!(
        "Calibration mode: {}",
        if args.interactive {
            "interactive"
        } else {
            "automatic"
        }
    ));

    if let Some(calibration_audio) = &args.calibration_audio {
        output_formatter.info(&format!("Used calibration audio: {:?}", calibration_audio));
    }

    Ok(())
}

#[cfg(feature = "spatial")]
async fn execute_list_hrtf_command(
    args: ListHrtfArgs,
    output_formatter: &OutputFormatter,
) -> Result<(), CliError> {
    output_formatter.info("Available HRTF datasets:");

    let datasets = get_available_hrtf_datasets(args.dataset_type.as_deref())?;

    for dataset in datasets {
        if args.detailed {
            output_formatter.info(&format!("  {}: {}", dataset.name, dataset.description));
            output_formatter.info(&format!("    Type: {}", dataset.dataset_type));
            output_formatter.info(&format!("    Quality: {}", dataset.quality));
            output_formatter.info(&format!("    Size: {}", dataset.size));
        } else {
            output_formatter.info(&format!("  {}", dataset.name));
        }
    }

    Ok(())
}

// Helper functions

fn parse_position(s: &str) -> Result<Position3D, String> {
    let parts: Vec<&str> = s.split(',').collect();
    if parts.len() != 3 {
        return Err("Position must be in format 'x,y,z'".to_string());
    }

    let x = parts[0]
        .trim()
        .parse::<f32>()
        .map_err(|_| "Invalid x coordinate")?;
    let y = parts[1]
        .trim()
        .parse::<f32>()
        .map_err(|_| "Invalid y coordinate")?;
    let z = parts[2]
        .trim()
        .parse::<f32>()
        .map_err(|_| "Invalid z coordinate")?;

    Ok(Position3D { x, y, z })
}

fn load_room_config(path: &PathBuf) -> Result<RoomConfig, CliError> {
    // Load room configuration from JSON file
    let file = std::fs::File::open(path)
        .map_err(|e| CliError::IoError(format!("Failed to open room config file: {}", e)))?;

    let config: RoomConfig = serde_json::from_reader(file)
        .map_err(|e| CliError::IoError(format!("Failed to parse room config JSON: {}", e)))?;

    // Validate the configuration
    if config.dimensions.0 <= 0.0 || config.dimensions.1 <= 0.0 || config.dimensions.2 <= 0.0 {
        return Err(CliError::ValidationError(
            "Room dimensions must be positive values".to_string(),
        ));
    }

    if config.reverb_time < 0.0 || config.reverb_time > 10.0 {
        return Err(CliError::ValidationError(
            "Reverb time must be between 0 and 10 seconds".to_string(),
        ));
    }

    if config.temperature < -50.0 || config.temperature > 50.0 {
        return Err(CliError::ValidationError(
            "Temperature must be between -50°C and 50°C".to_string(),
        ));
    }

    if config.humidity < 0.0 || config.humidity > 100.0 {
        return Err(CliError::ValidationError(
            "Humidity must be between 0% and 100%".to_string(),
        ));
    }

    Ok(config)
}

fn load_mono_audio(path: &PathBuf) -> Result<Vec<f32>, CliError> {
    // Load audio file using hound
    let mut reader = hound::WavReader::open(path)
        .map_err(|e| CliError::IoError(format!("Failed to open audio file: {}", e)))?;

    let spec = reader.spec();

    // Check if audio is mono or needs conversion
    if spec.channels > 2 {
        return Err(CliError::ValidationError(format!(
            "Audio file has {} channels, expected mono (1) or stereo (2)",
            spec.channels
        )));
    }

    // Read samples based on bit depth
    let samples: Vec<f32> = match spec.sample_format {
        hound::SampleFormat::Int => {
            let max_value = (1i32 << (spec.bits_per_sample - 1)) as f32;
            reader
                .samples::<i32>()
                .collect::<Result<Vec<_>, _>>()
                .map_err(|e| CliError::IoError(format!("Failed to read audio samples: {}", e)))?
                .into_iter()
                .map(|s| s as f32 / max_value)
                .collect()
        }
        hound::SampleFormat::Float => reader
            .samples::<f32>()
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| CliError::IoError(format!("Failed to read audio samples: {}", e)))?,
    };

    // Convert stereo to mono if needed by averaging channels
    if spec.channels == 2 {
        let mono_samples: Vec<f32> = samples
            .chunks(2)
            .map(|chunk| (chunk[0] + chunk.get(1).unwrap_or(&0.0)) / 2.0)
            .collect();
        Ok(mono_samples)
    } else {
        Ok(samples)
    }
}

fn save_stereo_audio(audio: &[f32], path: &PathBuf, sample_rate: u32) -> Result<(), CliError> {
    let spec = hound::WavSpec {
        channels: 2,
        sample_rate,
        bits_per_sample: 16,
        sample_format: hound::SampleFormat::Int,
    };

    let mut writer = hound::WavWriter::create(path, spec)
        .map_err(|e| CliError::IoError(format!("Failed to create stereo audio writer: {}", e)))?;

    // Convert to interleaved stereo
    for chunk in audio.chunks(2) {
        let left = chunk.first().unwrap_or(&0.0);
        let right = chunk.get(1).unwrap_or(&0.0);

        let left_i16 = (left * 32767.0) as i16;
        let right_i16 = (right * 32767.0) as i16;

        writer
            .write_sample(left_i16)
            .map_err(|e| CliError::IoError(format!("Failed to write left channel: {}", e)))?;
        writer
            .write_sample(right_i16)
            .map_err(|e| CliError::IoError(format!("Failed to write right channel: {}", e)))?;
    }

    writer
        .finalize()
        .map_err(|e| CliError::IoError(format!("Failed to finalize stereo audio file: {}", e)))?;

    Ok(())
}

#[derive(Debug, Clone)]
struct MovementPoint {
    position: Position3D,
    time: f32,
}

fn load_movement_path(path: &Path) -> Result<Vec<MovementPoint>, CliError> {
    // Mock implementation - in reality would load JSON movement path
    Ok(vec![
        MovementPoint {
            position: Position3D {
                x: -5.0,
                y: 0.0,
                z: 0.0,
            },
            time: 0.0,
        },
        MovementPoint {
            position: Position3D {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            },
            time: 1.0,
        },
        MovementPoint {
            position: Position3D {
                x: 5.0,
                y: 0.0,
                z: 0.0,
            },
            time: 2.0,
        },
    ])
}

fn apply_movement_to_audio(
    audio: Vec<f32>,
    _movement_path: &[MovementPoint],
    _speed_multiplier: f32,
    _doppler: bool,
) -> Result<Vec<f32>, CliError> {
    // Mock implementation - in reality would apply movement and Doppler effect
    Ok(audio)
}

#[derive(Debug)]
struct HrtfDataset {
    name: String,
    description: String,
    dataset_type: String,
    quality: String,
    size: String,
}

fn get_available_hrtf_datasets(
    dataset_type_filter: Option<&str>,
) -> Result<Vec<HrtfDataset>, CliError> {
    let mut datasets = vec![
        HrtfDataset {
            name: "generic".to_string(),
            description: "Generic HRTF dataset suitable for most users".to_string(),
            dataset_type: "generic".to_string(),
            quality: "good".to_string(),
            size: "small".to_string(),
        },
        HrtfDataset {
            name: "kemar".to_string(),
            description: "MIT KEMAR database with high-quality measurements".to_string(),
            dataset_type: "research".to_string(),
            quality: "excellent".to_string(),
            size: "large".to_string(),
        },
        HrtfDataset {
            name: "cipic".to_string(),
            description: "CIPIC database with diverse subject measurements".to_string(),
            dataset_type: "research".to_string(),
            quality: "excellent".to_string(),
            size: "very_large".to_string(),
        },
        HrtfDataset {
            name: "custom".to_string(),
            description: "Custom HRTF dataset for specific applications".to_string(),
            dataset_type: "custom".to_string(),
            quality: "variable".to_string(),
            size: "variable".to_string(),
        },
    ];

    if let Some(filter) = dataset_type_filter {
        datasets.retain(|d| d.dataset_type == filter);
    }

    Ok(datasets)
}