voirs-spatial 0.1.0-rc.1

3D spatial audio and HRTF processing 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
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
//! Audio device configuration and voice processing types

use super::types::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Instant, SystemTime};

/// Audio settings for telepresence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TelepresenceAudioSettings {
    /// Input device configuration
    pub input_device: AudioDeviceConfig,

    /// Output device configuration
    pub output_device: AudioDeviceConfig,

    /// Voice processing settings
    pub voice_processing: VoiceProcessingSettings,

    /// Audio quality preferences
    pub quality_preferences: AudioQualityPreferences,

    /// Codec preferences
    pub codec_preferences: CodecPreferences,
}

/// Audio device configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioDeviceConfig {
    /// Device identifier
    pub device_id: Option<String>,

    /// Sample rate (Hz)
    pub sample_rate: u32,

    /// Buffer size (samples)
    pub buffer_size: usize,

    /// Channel count
    pub channels: u8,

    /// Bit depth
    pub bit_depth: u8,

    /// Device-specific settings
    pub device_settings: HashMap<String, String>,
}

/// Voice processing settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceProcessingSettings {
    /// Automatic gain control
    pub agc_enabled: bool,

    /// Noise suppression
    pub noise_suppression: NoiseSuppressionSettings,

    /// Echo cancellation
    pub echo_cancellation: EchoCancellationSettings,

    /// Voice activity detection
    pub vad_settings: VadSettings,

    /// Audio enhancement
    pub enhancement: AudioEnhancementSettings,

    /// Spatialization settings
    pub spatialization: VoiceSpatializationSettings,
}

/// Noise suppression configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NoiseSuppressionSettings {
    /// Enable noise suppression
    pub enabled: bool,

    /// Suppression strength (0.0-1.0)
    pub strength: f32,

    /// Suppression algorithm
    pub algorithm: NoiseSuppressionAlgorithm,

    /// Adaptive learning
    pub adaptive: bool,

    /// Stationary noise suppression
    pub stationary_suppression: f32,

    /// Non-stationary noise suppression
    pub non_stationary_suppression: f32,
}

/// Echo cancellation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EchoCancellationSettings {
    /// Enable echo cancellation
    pub enabled: bool,

    /// Cancellation strength (0.0-1.0)
    pub strength: f32,

    /// Echo cancellation algorithm
    pub algorithm: EchoCancellationAlgorithm,

    /// Tail length (samples)
    pub tail_length: usize,

    /// Adaptation rate
    pub adaptation_rate: f32,

    /// Non-linear processing
    pub non_linear_processing: bool,
}

/// Voice Activity Detection settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VadSettings {
    /// Enable VAD
    pub enabled: bool,

    /// Detection sensitivity (0.0-1.0)
    pub sensitivity: f32,

    /// VAD algorithm
    pub algorithm: VadAlgorithm,

    /// Minimum voice duration (ms)
    pub min_voice_duration: f32,

    /// Minimum silence duration (ms)
    pub min_silence_duration: f32,

    /// Hangover time (ms)
    pub hangover_time: f32,
}

/// Audio enhancement settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioEnhancementSettings {
    /// Enable enhancement
    pub enabled: bool,

    /// Dynamic range compression
    pub dynamic_range_compression: CompressionSettings,

    /// Equalization
    pub equalization: EqualizationSettings,

    /// Bandwidth extension
    pub bandwidth_extension: BandwidthExtensionSettings,

    /// Comfort noise generation
    pub comfort_noise: ComfortNoiseSettings,
}

/// Dynamic range compression settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionSettings {
    /// Enable compression
    pub enabled: bool,

    /// Compression ratio
    pub ratio: f32,

    /// Threshold (dB)
    pub threshold: f32,

    /// Attack time (ms)
    pub attack_time: f32,

    /// Release time (ms)
    pub release_time: f32,

    /// Makeup gain (dB)
    pub makeup_gain: f32,
}

/// Equalization settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EqualizationSettings {
    /// Enable EQ
    pub enabled: bool,

    /// EQ bands
    pub bands: Vec<EqBand>,

    /// EQ type
    pub eq_type: EqualizationType,

    /// Adaptive EQ
    pub adaptive: bool,
}

/// EQ band configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EqBand {
    /// Center frequency (Hz)
    pub frequency: f32,

    /// Gain (dB)
    pub gain: f32,

    /// Q factor
    pub q_factor: f32,

    /// Band type
    pub band_type: EqBandType,
}

/// Bandwidth extension settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BandwidthExtensionSettings {
    /// Enable bandwidth extension
    pub enabled: bool,

    /// Target bandwidth (Hz)
    pub target_bandwidth: f32,

    /// Extension algorithm
    pub algorithm: BandwidthExtensionAlgorithm,

    /// Extension strength
    pub strength: f32,
}

/// Comfort noise settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComfortNoiseSettings {
    /// Enable comfort noise
    pub enabled: bool,

    /// Noise level (dB)
    pub level: f32,

    /// Noise color
    pub color: NoiseColor,

    /// Adaptive level
    pub adaptive_level: bool,
}

/// Voice spatialization settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceSpatializationSettings {
    /// Enable spatialization
    pub enabled: bool,

    /// HRTF personalization
    pub hrtf_personalization: HrtfPersonalizationSettings,

    /// Room simulation
    pub room_simulation: super::room::RoomSimulationSettings,

    /// Distance modeling
    pub distance_modeling: super::room::DistanceModelingSettings,

    /// Doppler effects
    pub doppler_effects: super::room::DopplerEffectsSettings,
}

/// HRTF personalization for voice
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HrtfPersonalizationSettings {
    /// Enable personalization
    pub enabled: bool,

    /// User measurements
    pub measurements: Option<UserMeasurements>,

    /// Personalization method
    pub method: PersonalizationMethod,

    /// Adaptation strength
    pub adaptation_strength: f32,
}

/// User physical measurements for HRTF
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserMeasurements {
    /// Head circumference (cm)
    pub head_circumference: f32,

    /// Pinna length (cm)
    pub pinna_length: f32,

    /// Pinna width (cm)
    pub pinna_width: f32,

    /// Torso width (cm)
    pub torso_width: f32,

    /// Custom measurements
    pub custom_measurements: HashMap<String, f32>,
}

/// Audio quality preferences
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioQualityPreferences {
    /// Preferred quality level
    pub quality_level: QualityLevel,

    /// Adaptive quality
    pub adaptive_quality: bool,

    /// Latency priority
    pub latency_priority: LatencyPriority,

    /// Bandwidth constraints
    pub bandwidth_constraints: BandwidthConstraints,
}

/// Bandwidth constraints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BandwidthConstraints {
    /// Maximum bandwidth (kbps)
    pub max_bandwidth: u32,

    /// Minimum bandwidth (kbps)
    pub min_bandwidth: u32,

    /// Adaptive bandwidth
    pub adaptive: bool,

    /// Bandwidth measurement interval (ms)
    pub measurement_interval: u32,
}

/// Codec preferences
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodecPreferences {
    /// Preferred codecs in priority order
    pub preferred_codecs: Vec<AudioCodec>,

    /// Codec-specific settings
    pub codec_settings: HashMap<AudioCodec, CodecSettings>,

    /// Fallback behavior
    pub fallback_behavior: CodecFallbackBehavior,
}

/// Codec-specific settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodecSettings {
    /// Bitrate (kbps)
    pub bitrate: u32,

    /// Complexity level
    pub complexity: u8,

    /// Variable bitrate
    pub variable_bitrate: bool,

    /// Forward error correction
    pub fec: bool,

    /// Codec-specific parameters
    pub parameters: HashMap<String, String>,
}

/// Frequency filter settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FrequencyFilterSettings {
    /// High-pass cutoff (Hz)
    pub highpass_cutoff: f32,

    /// Low-pass cutoff (Hz)
    pub lowpass_cutoff: f32,

    /// Notch filters
    pub notch_filters: Vec<NotchFilter>,
}

/// Notch filter configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotchFilter {
    /// Center frequency (Hz)
    pub frequency: f32,

    /// Q factor
    pub q_factor: f32,

    /// Attenuation (dB)
    pub attenuation: f32,
}

/// Frequency response adjustment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FrequencyResponseAdjustment {
    /// Low frequency boost/cut (dB)
    pub low_freq_adjustment: f32,

    /// Mid frequency boost/cut (dB)
    pub mid_freq_adjustment: f32,

    /// High frequency boost/cut (dB)
    pub high_freq_adjustment: f32,
}

/// Audio format information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioFormat {
    /// Codec used
    pub codec: AudioCodec,

    /// Sample rate
    pub sample_rate: u32,

    /// Channels
    pub channels: u8,

    /// Bitrate
    pub bitrate: u32,

    /// Frame size
    pub frame_size: usize,
}

/// Audio metadata for transmission
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioMetadata {
    /// Timestamp
    pub timestamp: SystemTime,

    /// Sequence number
    pub sequence: u64,

    /// Audio format
    pub format: AudioFormat,

    /// Spatial information
    pub spatial_info: super::spatial::SpatialAudioInfo,

    /// Quality information
    pub quality_info: super::quality::QualityInfo,
}

/// Received audio data
#[derive(Debug, Clone)]
pub struct ReceivedAudio {
    /// User identifier
    pub user_id: String,

    /// Audio samples
    pub samples: Vec<f32>,

    /// Metadata
    pub metadata: AudioMetadata,

    /// Reception timestamp
    pub received_at: Instant,

    /// Processing status
    pub processing_status: ProcessingStatus,
}

/// Audio quality settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioQualitySettings {
    /// Sample rate (Hz)
    pub sample_rate: u32,

    /// Bit depth
    pub bit_depth: u8,

    /// Channel configuration
    pub channels: ChannelConfiguration,

    /// Dynamic range (dB)
    pub dynamic_range: f32,

    /// THD+N specification
    pub thd_n: f32,
}

/// Audio quality statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioQualityStats {
    /// Average quality score
    pub avg_quality: f32,

    /// Minimum quality
    pub min_quality: f32,

    /// Maximum quality
    pub max_quality: f32,

    /// Quality adaptations count
    pub adaptations: u32,

    /// Audio dropouts
    pub dropouts: u32,

    /// Compression efficiency
    pub compression_ratio: f32,
}

// Default implementations

impl Default for TelepresenceAudioSettings {
    fn default() -> Self {
        Self {
            input_device: AudioDeviceConfig::default(),
            output_device: AudioDeviceConfig::default(),
            voice_processing: VoiceProcessingSettings::default(),
            quality_preferences: AudioQualityPreferences::default(),
            codec_preferences: CodecPreferences::default(),
        }
    }
}

impl Default for AudioDeviceConfig {
    fn default() -> Self {
        Self {
            device_id: None,
            sample_rate: 48000,
            buffer_size: 1024,
            channels: 2,
            bit_depth: 16,
            device_settings: HashMap::new(),
        }
    }
}

impl Default for VoiceProcessingSettings {
    fn default() -> Self {
        Self {
            agc_enabled: true,
            noise_suppression: NoiseSuppressionSettings::default(),
            echo_cancellation: EchoCancellationSettings::default(),
            vad_settings: VadSettings::default(),
            enhancement: AudioEnhancementSettings::default(),
            spatialization: VoiceSpatializationSettings::default(),
        }
    }
}

impl Default for NoiseSuppressionSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            strength: 0.7,
            algorithm: NoiseSuppressionAlgorithm::Hybrid,
            adaptive: true,
            stationary_suppression: 0.8,
            non_stationary_suppression: 0.6,
        }
    }
}

impl Default for EchoCancellationSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            strength: 0.8,
            algorithm: EchoCancellationAlgorithm::NLMS,
            tail_length: 1024,
            adaptation_rate: 0.01,
            non_linear_processing: true,
        }
    }
}

impl Default for VadSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            sensitivity: 0.7,
            algorithm: VadAlgorithm::Hybrid,
            min_voice_duration: 100.0,
            min_silence_duration: 200.0,
            hangover_time: 150.0,
        }
    }
}

impl Default for AudioEnhancementSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            dynamic_range_compression: CompressionSettings::default(),
            equalization: EqualizationSettings::default(),
            bandwidth_extension: BandwidthExtensionSettings::default(),
            comfort_noise: ComfortNoiseSettings::default(),
        }
    }
}

impl Default for CompressionSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            ratio: 3.0,
            threshold: -18.0,
            attack_time: 5.0,
            release_time: 50.0,
            makeup_gain: 2.0,
        }
    }
}

impl Default for EqualizationSettings {
    fn default() -> Self {
        Self {
            enabled: false,
            bands: vec![],
            eq_type: EqualizationType::Parametric,
            adaptive: false,
        }
    }
}

impl Default for BandwidthExtensionSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            target_bandwidth: 20000.0,
            algorithm: BandwidthExtensionAlgorithm::SpectralReplication,
            strength: 0.5,
        }
    }
}

impl Default for ComfortNoiseSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            level: -40.0,
            color: NoiseColor::Pink,
            adaptive_level: true,
        }
    }
}

impl Default for VoiceSpatializationSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            hrtf_personalization: HrtfPersonalizationSettings::default(),
            room_simulation: super::room::RoomSimulationSettings::default(),
            distance_modeling: super::room::DistanceModelingSettings::default(),
            doppler_effects: super::room::DopplerEffectsSettings::default(),
        }
    }
}

impl Default for HrtfPersonalizationSettings {
    fn default() -> Self {
        Self {
            enabled: false,
            measurements: None,
            method: PersonalizationMethod::Anthropometric,
            adaptation_strength: 0.5,
        }
    }
}

impl Default for AudioQualityPreferences {
    fn default() -> Self {
        Self {
            quality_level: QualityLevel::High,
            adaptive_quality: true,
            latency_priority: LatencyPriority::Medium,
            bandwidth_constraints: BandwidthConstraints::default(),
        }
    }
}

impl Default for BandwidthConstraints {
    fn default() -> Self {
        Self {
            max_bandwidth: 320, // 320 kbps
            min_bandwidth: 32,  // 32 kbps
            adaptive: true,
            measurement_interval: 1000, // 1 second
        }
    }
}

impl Default for CodecPreferences {
    fn default() -> Self {
        let mut codec_settings = HashMap::new();
        codec_settings.insert(
            AudioCodec::Opus,
            CodecSettings {
                bitrate: 128,
                complexity: 8,
                variable_bitrate: true,
                fec: true,
                parameters: HashMap::new(),
            },
        );

        Self {
            preferred_codecs: vec![AudioCodec::Opus, AudioCodec::AAC, AudioCodec::G722],
            codec_settings,
            fallback_behavior: CodecFallbackBehavior::NextPreferred,
        }
    }
}

impl Default for FrequencyFilterSettings {
    fn default() -> Self {
        Self {
            highpass_cutoff: 100.0, // 100 Hz
            lowpass_cutoff: 8000.0, // 8 kHz
            notch_filters: vec![],
        }
    }
}

impl Default for FrequencyResponseAdjustment {
    fn default() -> Self {
        Self {
            low_freq_adjustment: 0.0,
            mid_freq_adjustment: 1.0,   // Slight mid boost for intimacy
            high_freq_adjustment: -1.0, // Slight high cut for warmth
        }
    }
}

impl Default for AudioQualitySettings {
    fn default() -> Self {
        Self {
            sample_rate: 48000,
            bit_depth: 16,
            channels: ChannelConfiguration::Binaural,
            dynamic_range: 96.0, // 96 dB
            thd_n: 0.01,         // 0.01% THD+N
        }
    }
}