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
//! Core spatial audio processing functionality

use crate::config::SpatialConfig;
use crate::hrtf::HrtfProcessor;
use crate::memory::{MemoryConfig, MemoryManager};
use crate::position::{Listener, SoundSource};
use crate::room::RoomSimulator;
use crate::types::{BinauraAudio, Position3D, SpatialEffect, SpatialRequest, SpatialResult};
use scirs2_core::ndarray::{Array1, Array2};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Main spatial audio processor
pub struct SpatialProcessor {
    /// Configuration
    config: SpatialConfig,
    /// HRTF processor
    hrtf_processor: Arc<RwLock<HrtfProcessor>>,
    /// Room simulator
    room_simulator: Arc<RwLock<RoomSimulator>>,
    /// Active sound sources
    sound_sources: Arc<RwLock<HashMap<String, SoundSource>>>,
    /// Listener
    listener: Arc<RwLock<Listener>>,
    /// Processing state
    processing_state: ProcessingState,
    /// Memory manager for optimization
    memory_manager: Arc<MemoryManager>,
}

/// Builder for SpatialProcessor
pub struct SpatialProcessorBuilder {
    config: Option<SpatialConfig>,
    hrtf_database_path: Option<std::path::PathBuf>,
    memory_config: Option<MemoryConfig>,
}

/// Internal processing state
#[derive(Debug)]
struct ProcessingState {
    /// Sample buffers
    #[allow(dead_code)]
    input_buffer: Array1<f32>,
    #[allow(dead_code)]
    output_buffer: Array2<f32>,
    /// Processing statistics
    processed_frames: u64,
    total_processing_time: Duration,
}

/// Distance attenuation model
#[derive(Debug, Clone, Copy)]
pub enum AttenuationModel {
    /// Linear attenuation
    Linear,
    /// Inverse distance law
    InverseDistance,
    /// Inverse square law
    InverseSquare,
    /// Custom exponential model
    Exponential(f32),
}

/// Doppler effect processor
#[allow(dead_code)]
struct DopplerProcessor {
    /// Previous positions for velocity calculation
    previous_positions: HashMap<String, (Position3D, Instant)>,
    /// Speed of sound
    speed_of_sound: f32,
    /// Sample rate
    sample_rate: f32,
}

impl SpatialProcessor {
    /// Create new spatial processor with configuration
    pub async fn new(config: SpatialConfig) -> crate::Result<Self> {
        Self::with_memory_config(config, MemoryConfig::default()).await
    }

    /// Create new spatial processor with spatial and memory configurations
    pub async fn with_memory_config(
        config: SpatialConfig,
        memory_config: MemoryConfig,
    ) -> crate::Result<Self> {
        config.validate()?;

        let hrtf_processor = Arc::new(RwLock::new(
            HrtfProcessor::new(config.hrtf_database_path.clone()).await?,
        ));

        let room_simulator = Arc::new(RwLock::new(RoomSimulator::new(
            config.room_dimensions,
            config.reverb_time,
        )?));

        let processing_state = ProcessingState {
            input_buffer: Array1::zeros(config.buffer_size),
            output_buffer: Array2::zeros((2, config.buffer_size)),
            processed_frames: 0,
            total_processing_time: Duration::ZERO,
        };

        // Initialize memory manager with provided configuration
        let memory_manager = Arc::new(MemoryManager::new(memory_config));

        Ok(Self {
            config,
            hrtf_processor,
            room_simulator,
            sound_sources: Arc::new(RwLock::new(HashMap::new())),
            listener: Arc::new(RwLock::new(Listener::default())),
            processing_state,
            memory_manager,
        })
    }

    /// Process spatial audio request
    pub async fn process_request(
        &mut self,
        request: SpatialRequest,
    ) -> crate::Result<SpatialResult> {
        let start_time = Instant::now();

        // Validate request
        request.validate()?;

        // Store needed values before moving request.audio
        let source_position = request.source_position;
        let effects = request.effects.clone();
        let request_id = request.id.clone();
        let sample_rate = request.sample_rate;

        // Convert input audio to Array1
        let input_audio = Array1::from_vec(request.audio);

        // Initialize output channels
        let mut left_channel = Array1::zeros(input_audio.len());
        let mut right_channel = Array1::zeros(input_audio.len());

        // Get listener information
        let listener = self.listener.read().await;
        let listener_position = listener.position();
        let listener_orientation = listener.orientation();
        drop(listener);

        // Calculate relative position
        let relative_position = self.calculate_relative_position(
            &source_position,
            &listener_position,
            &listener_orientation,
        );

        // Apply requested effects
        for effect in &effects {
            match effect {
                SpatialEffect::Hrtf => {
                    self.apply_hrtf(
                        &input_audio,
                        &mut left_channel,
                        &mut right_channel,
                        &relative_position,
                    )
                    .await?;
                }
                SpatialEffect::DistanceAttenuation => {
                    let distance = source_position.distance_to(&listener_position);
                    let attenuation = self.calculate_distance_attenuation(distance);
                    left_channel *= attenuation;
                    right_channel *= attenuation;
                }
                SpatialEffect::Reverb => {
                    self.apply_reverb(&mut left_channel, &mut right_channel, &source_position)
                        .await?;
                }
                SpatialEffect::Doppler => {
                    self.apply_doppler_effect(
                        &mut left_channel,
                        &mut right_channel,
                        &source_position,
                    )
                    .await?;
                }
                SpatialEffect::AirAbsorption => {
                    let distance = source_position.distance_to(&listener_position);
                    self.apply_air_absorption(&mut left_channel, &mut right_channel, distance);
                }
            }
        }

        // Create result
        let processing_time = start_time.elapsed();
        self.processing_state.processed_frames += 1;
        self.processing_state.total_processing_time += processing_time;

        let binaural_audio =
            BinauraAudio::new(left_channel.to_vec(), right_channel.to_vec(), sample_rate);

        Ok(SpatialResult::success(
            request_id,
            binaural_audio,
            processing_time,
            effects,
        ))
    }

    /// Add sound source
    pub async fn add_sound_source(&self, id: String, source: SoundSource) {
        let mut sources = self.sound_sources.write().await;
        sources.insert(id, source);
    }

    /// Remove sound source
    pub async fn remove_sound_source(&self, id: &str) {
        let mut sources = self.sound_sources.write().await;
        sources.remove(id);
    }

    /// Update listener position and orientation
    pub async fn update_listener(&self, position: Position3D, orientation: (f32, f32, f32)) {
        let mut listener = self.listener.write().await;
        listener.set_position(position);
        listener.set_orientation(orientation);
    }

    /// Get processing statistics
    pub fn get_stats(&self) -> ProcessingStats {
        ProcessingStats {
            processed_frames: self.processing_state.processed_frames,
            total_processing_time: self.processing_state.total_processing_time,
            average_processing_time: if self.processing_state.processed_frames > 0 {
                self.processing_state.total_processing_time
                    / self.processing_state.processed_frames as u32
            } else {
                Duration::ZERO
            },
        }
    }

    /// Calculate relative position from listener perspective
    fn calculate_relative_position(
        &self,
        source_pos: &Position3D,
        listener_pos: &Position3D,
        listener_orientation: &(f32, f32, f32),
    ) -> Position3D {
        // Calculate position relative to listener
        let mut relative = Position3D::new(
            source_pos.x - listener_pos.x,
            source_pos.y - listener_pos.y,
            source_pos.z - listener_pos.z,
        );

        // Apply listener orientation rotation
        let (yaw, _pitch, _roll) = *listener_orientation;

        // Simplified rotation (yaw only for now)
        let cos_yaw = yaw.cos();
        let sin_yaw = yaw.sin();

        let rotated_x = relative.x * cos_yaw + relative.z * sin_yaw;
        let rotated_z = -relative.x * sin_yaw + relative.z * cos_yaw;

        relative.x = rotated_x;
        relative.z = rotated_z;

        relative
    }

    /// Apply HRTF processing
    async fn apply_hrtf(
        &self,
        input: &Array1<f32>,
        left_output: &mut Array1<f32>,
        right_output: &mut Array1<f32>,
        position: &Position3D,
    ) -> crate::Result<()> {
        let hrtf_processor = self.hrtf_processor.read().await;
        hrtf_processor
            .process_position(input, left_output, right_output, position)
            .await
    }

    /// Apply reverb effect
    async fn apply_reverb(
        &self,
        left_channel: &mut Array1<f32>,
        right_channel: &mut Array1<f32>,
        source_position: &Position3D,
    ) -> crate::Result<()> {
        let room_simulator = self.room_simulator.read().await;
        room_simulator
            .process_reverb(left_channel, right_channel, source_position)
            .await
    }

    /// Apply Doppler effect
    async fn apply_doppler_effect(
        &self,
        left_channel: &mut Array1<f32>,
        right_channel: &mut Array1<f32>,
        _source_position: &Position3D,
    ) -> crate::Result<()> {
        // Simplified Doppler effect implementation
        // In a real implementation, this would require velocity tracking
        let doppler_factor = 1.0; // Placeholder

        *left_channel *= doppler_factor;
        *right_channel *= doppler_factor;

        Ok(())
    }

    /// Calculate distance attenuation
    fn calculate_distance_attenuation(&self, distance: f32) -> f32 {
        if !self.config.enable_distance_attenuation {
            return 1.0;
        }

        if distance <= 1.0 {
            return 1.0;
        }

        if distance >= self.config.max_distance {
            return 0.0;
        }

        // Inverse distance law with minimum distance
        1.0 / distance.max(1.0)
    }

    /// Apply air absorption
    fn apply_air_absorption(
        &self,
        left_channel: &mut Array1<f32>,
        right_channel: &mut Array1<f32>,
        distance: f32,
    ) {
        if !self.config.enable_air_absorption {
            return;
        }

        // Simplified air absorption model
        // Higher frequencies are absorbed more
        let absorption_factor = (-0.01 * distance).exp();

        *left_channel *= absorption_factor;
        *right_channel *= absorption_factor;
    }

    /// Get memory manager for advanced memory operations
    pub fn memory_manager(&self) -> &Arc<MemoryManager> {
        &self.memory_manager
    }

    /// Get memory usage statistics
    pub async fn memory_stats(&self) -> crate::memory::MemoryStatistics {
        self.memory_manager.get_memory_stats().await
    }

    /// Check for memory pressure and clean up if needed
    pub async fn optimize_memory(&self) -> bool {
        self.memory_manager.check_memory_pressure().await
    }
}

impl SpatialProcessorBuilder {
    /// Create new builder
    pub fn new() -> Self {
        Self {
            config: None,
            hrtf_database_path: None,
            memory_config: None,
        }
    }

    /// Set configuration
    pub fn config(mut self, config: SpatialConfig) -> Self {
        self.config = Some(config);
        self
    }

    /// Set HRTF database path
    pub fn hrtf_database_path(mut self, path: std::path::PathBuf) -> Self {
        self.hrtf_database_path = Some(path);
        self
    }

    /// Set memory configuration
    pub fn memory_config(mut self, config: MemoryConfig) -> Self {
        self.memory_config = Some(config);
        self
    }

    /// Build the spatial processor
    pub async fn build(self) -> crate::Result<SpatialProcessor> {
        let mut config = self.config.unwrap_or_default();

        if let Some(path) = self.hrtf_database_path {
            config.hrtf_database_path = Some(path);
        }

        let memory_config = self.memory_config.unwrap_or_default();
        SpatialProcessor::with_memory_config(config, memory_config).await
    }
}

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

/// Processing statistics
#[derive(Debug, Clone)]
pub struct ProcessingStats {
    /// Total processed frames
    pub processed_frames: u64,
    /// Total processing time
    pub total_processing_time: Duration,
    /// Average processing time per frame
    pub average_processing_time: Duration,
}

impl DopplerProcessor {
    /// Create new Doppler processor
    #[allow(dead_code)]
    fn new(speed_of_sound: f32, sample_rate: f32) -> Self {
        Self {
            previous_positions: HashMap::new(),
            speed_of_sound,
            sample_rate,
        }
    }

    /// Calculate Doppler factor for a moving source
    #[allow(dead_code)]
    fn calculate_doppler_factor(
        &mut self,
        source_id: &str,
        current_position: Position3D,
        listener_position: Position3D,
    ) -> f32 {
        let now = Instant::now();

        if let Some((prev_pos, prev_time)) = self.previous_positions.get(source_id) {
            let time_diff = now.duration_since(*prev_time).as_secs_f32();
            if time_diff > 0.0 {
                // Calculate velocity towards listener
                let prev_distance = prev_pos.distance_to(&listener_position);
                let curr_distance = current_position.distance_to(&listener_position);
                let radial_velocity = (curr_distance - prev_distance) / time_diff;

                // Apply Doppler formula
                let factor = self.speed_of_sound / (self.speed_of_sound + radial_velocity);

                self.previous_positions
                    .insert(source_id.to_string(), (current_position, now));
                return factor.clamp(0.5, 2.0); // Limit extreme values
            }
        }

        self.previous_positions
            .insert(source_id.to_string(), (current_position, now));
        1.0
    }
}

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

    #[tokio::test]
    async fn test_spatial_processor_creation() {
        let config = SpatialConfig::default();
        let result = SpatialProcessor::new(config).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_spatial_processor_builder() {
        let processor = SpatialProcessorBuilder::new()
            .config(SpatialConfig::default())
            .build()
            .await;
        assert!(processor.is_ok());
    }

    #[tokio::test]
    async fn test_distance_attenuation() {
        let config = SpatialConfig::default();
        let processor = SpatialProcessor {
            config: config.clone(),
            hrtf_processor: Arc::new(RwLock::new(HrtfProcessor::new(None).await.unwrap())),
            room_simulator: Arc::new(RwLock::new(
                RoomSimulator::new((10.0, 8.0, 3.0), 1.2).unwrap(),
            )),
            sound_sources: Arc::new(RwLock::new(HashMap::new())),
            listener: Arc::new(RwLock::new(Listener::default())),
            processing_state: ProcessingState {
                input_buffer: Array1::zeros(1024),
                output_buffer: Array2::zeros((2, 1024)),
                processed_frames: 0,
                total_processing_time: Duration::ZERO,
            },
            memory_manager: Arc::new(MemoryManager::new(MemoryConfig::default())),
        };

        // Test distance attenuation
        assert_eq!(processor.calculate_distance_attenuation(1.0), 1.0);
        assert!(processor.calculate_distance_attenuation(2.0) < 1.0);
        assert!(
            processor.calculate_distance_attenuation(10.0)
                < processor.calculate_distance_attenuation(5.0)
        );
    }

    #[tokio::test]
    async fn test_relative_position_calculation() {
        let config = SpatialConfig::default();
        let processor = SpatialProcessor {
            config,
            hrtf_processor: Arc::new(RwLock::new(HrtfProcessor::new(None).await.unwrap())),
            room_simulator: Arc::new(RwLock::new(
                RoomSimulator::new((10.0, 8.0, 3.0), 1.2).unwrap(),
            )),
            sound_sources: Arc::new(RwLock::new(HashMap::new())),
            listener: Arc::new(RwLock::new(Listener::default())),
            processing_state: ProcessingState {
                input_buffer: Array1::zeros(1024),
                output_buffer: Array2::zeros((2, 1024)),
                processed_frames: 0,
                total_processing_time: Duration::ZERO,
            },
            memory_manager: Arc::new(MemoryManager::new(MemoryConfig::default())),
        };

        let source_pos = Position3D::new(5.0, 0.0, 0.0);
        let listener_pos = Position3D::new(0.0, 0.0, 0.0);
        let listener_orientation = (0.0, 0.0, 0.0);

        let relative_pos = processor.calculate_relative_position(
            &source_pos,
            &listener_pos,
            &listener_orientation,
        );

        assert_eq!(relative_pos.x, 5.0);
        assert_eq!(relative_pos.y, 0.0);
        assert_eq!(relative_pos.z, 0.0);
    }
}