Skip to main content

voirs_spatial/multiuser/
types.rs

1//! # Multi-user Spatial Audio System
2//!
3//! This module provides shared spatial audio environments for multiple users,
4//! enabling collaborative and social spatial audio experiences.
5
6use crate::types::{Position3D, SpatialResult};
7use crate::{Error, Result};
8use serde::{Deserialize, Serialize};
9use std::collections::{HashMap, HashSet, VecDeque};
10use std::sync::{Arc, RwLock};
11use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
12
13/// Unique identifier for users in the multi-user environment
14pub type UserId = String;
15/// Unique identifier for audio sources
16pub type SourceId = String;
17/// Unique identifier for rooms/environments
18pub type RoomId = String;
19
20/// Configuration for multi-user spatial audio environment
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct MultiUserConfig {
23    /// Maximum number of users per room
24    pub max_users_per_room: usize,
25    /// Maximum number of audio sources per user
26    pub max_sources_per_user: usize,
27    /// Network synchronization interval in milliseconds
28    pub sync_interval_ms: u64,
29    /// Maximum latency tolerance in milliseconds
30    pub max_latency_ms: f64,
31    /// Voice activity detection threshold
32    pub voice_activity_threshold: f32,
33    /// Spatial audio processing quality (0.0-1.0)
34    pub audio_quality: f32,
35    /// Enable position interpolation for smooth movement
36    pub position_interpolation: bool,
37    /// Maximum distance for audio interaction
38    pub max_audio_distance: f32,
39    /// Distance-based volume attenuation curve
40    pub attenuation_curve: MultiUserAttenuationCurve,
41    /// Privacy and security settings
42    pub privacy_settings: PrivacySettings,
43    /// Bandwidth optimization settings
44    pub bandwidth_settings: BandwidthSettings,
45}
46
47/// Distance-based audio attenuation curves
48#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
49pub enum MultiUserAttenuationCurve {
50    /// Linear attenuation (simple distance falloff)
51    Linear,
52    /// Inverse distance attenuation (1/distance)
53    InverseDistance,
54    /// Inverse square law (1/distance^2)
55    InverseSquare,
56    /// Logarithmic attenuation for natural feeling
57    Logarithmic,
58    /// Custom curve with configurable parameters
59    Custom {
60        /// Rate of volume decrease with distance
61        rolloff: f32,
62        /// Distance at which attenuation begins
63        reference_distance: f32,
64    },
65}
66
67/// Privacy and security settings for multi-user environments
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct PrivacySettings {
70    /// Enable end-to-end encryption for audio streams
71    pub encryption_enabled: bool,
72    /// Allow recording of multi-user sessions
73    pub recording_allowed: bool,
74    /// Enable mute controls for users
75    pub mute_controls_enabled: bool,
76    /// Enable spatial zones with restricted access
77    pub spatial_zones_enabled: bool,
78    /// User permission levels
79    pub permission_system: PermissionSystem,
80    /// Anonymization settings
81    pub anonymization: AnonymizationSettings,
82}
83
84/// Permission system for user roles and capabilities
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct PermissionSystem {
87    /// Enable role-based access control
88    pub rbac_enabled: bool,
89    /// Default role for new users
90    pub default_role: UserRole,
91    /// Role-specific permissions
92    pub role_permissions: HashMap<UserRole, Vec<Permission>>,
93}
94
95/// User roles in the multi-user environment
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
97pub enum UserRole {
98    /// Regular participant with basic permissions
99    Participant,
100    /// Moderator with additional controls
101    Moderator,
102    /// Administrator with full permissions
103    Administrator,
104    /// Observer with read-only access
105    Observer,
106    /// Presenter with enhanced audio capabilities
107    Presenter,
108    /// Guest with limited permissions
109    Guest,
110}
111
112/// Specific permissions that can be granted to users
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
114pub enum Permission {
115    /// Can speak and be heard by others
116    Speak,
117    /// Can move freely in the spatial environment
118    Move,
119    /// Can mute/unmute other users
120    MuteOthers,
121    /// Can kick users from the room
122    KickUsers,
123    /// Can modify room settings
124    ModifyRoom,
125    /// Can create new audio sources
126    CreateSources,
127    /// Can record the session
128    Record,
129    /// Can access private spatial zones
130    AccessPrivateZones,
131    /// Can broadcast to all users
132    Broadcast,
133    /// Can moderate content
134    Moderate,
135}
136
137/// Anonymization settings for user privacy
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct AnonymizationSettings {
140    /// Replace user identifiers with anonymous IDs
141    pub anonymous_ids: bool,
142    /// Obfuscate precise positioning data
143    pub position_obfuscation: bool,
144    /// Remove temporal patterns from audio
145    pub temporal_obfuscation: bool,
146    /// Use voice modulation for identity protection
147    pub voice_modulation: bool,
148}
149
150/// Bandwidth optimization settings
151#[derive(Debug, Clone, Serialize, Deserialize)]
152pub struct BandwidthSettings {
153    /// Adaptive bitrate based on network conditions
154    pub adaptive_bitrate: bool,
155    /// Maximum bandwidth per user in kbps
156    pub max_bandwidth_kbps: u32,
157    /// Compression level (0-10, higher = more compression)
158    pub compression_level: u8,
159    /// Enable proximity-based quality adjustment
160    pub proximity_quality_scaling: bool,
161    /// Low bandwidth mode settings
162    pub low_bandwidth_mode: LowBandwidthMode,
163}
164
165/// Low bandwidth mode configuration
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub struct LowBandwidthMode {
168    /// Enable low bandwidth mode
169    pub enabled: bool,
170    /// Reduced sample rate for audio
171    pub sample_rate: u32,
172    /// Reduced bit depth
173    pub bit_depth: u16,
174    /// Maximum number of simultaneous audio streams
175    pub max_streams: usize,
176    /// Disable spatial effects to save bandwidth
177    pub disable_spatial_effects: bool,
178}
179
180/// User in the multi-user spatial audio environment
181#[derive(Debug, Clone)]
182pub struct MultiUserUser {
183    /// Unique user identifier
184    pub id: UserId,
185    /// Display name for the user
186    pub display_name: String,
187    /// Current position in 3D space
188    pub position: Position3D,
189    /// Orientation as quaternion (w, x, y, z)
190    pub orientation: [f32; 4],
191    /// Velocity for movement prediction
192    pub velocity: Position3D,
193    /// User role and permissions
194    pub role: UserRole,
195    /// Whether user is currently speaking
196    pub is_speaking: bool,
197    /// Audio sources owned by this user
198    pub audio_sources: HashMap<SourceId, MultiUserAudioSource>,
199    /// User's connection status
200    pub connection_status: ConnectionStatus,
201    /// Last update timestamp
202    pub last_update: Instant,
203    /// User-specific audio settings
204    pub audio_settings: UserAudioSettings,
205    /// Network statistics for this user
206    pub network_stats: NetworkStats,
207    /// Spatial zones the user has access to
208    pub accessible_zones: Vec<SpatialZone>,
209    /// List of friends (user IDs)
210    pub friends: HashSet<UserId>,
211}
212
213/// Connection status for users
214#[derive(Debug, Clone, Copy, PartialEq, Eq)]
215pub enum ConnectionStatus {
216    /// Connected and active
217    Connected,
218    /// Connected but experiencing latency issues
219    Unstable,
220    /// Temporarily disconnected, attempting reconnection
221    Reconnecting,
222    /// Disconnected
223    Disconnected,
224    /// Connection timed out
225    TimedOut,
226}
227
228/// Audio source in multi-user environment
229#[derive(Debug, Clone)]
230pub struct MultiUserAudioSource {
231    /// Unique source identifier
232    pub id: SourceId,
233    /// Owner of this audio source
234    pub owner_id: UserId,
235    /// Source position in 3D space
236    pub position: Position3D,
237    /// Source type and characteristics
238    pub source_type: AudioSourceType,
239    /// Volume level (0.0-1.0)
240    pub volume: f32,
241    /// Whether the source is currently active
242    pub is_active: bool,
243    /// Spatial audio properties
244    pub spatial_properties: SpatialProperties,
245    /// Access control for this source
246    pub access_control: SourceAccessControl,
247    /// Quality settings for this source
248    pub quality_settings: SourceQualitySettings,
249}
250
251/// Types of audio sources in multi-user environment
252#[derive(Debug, Clone, Copy, PartialEq, Eq)]
253pub enum AudioSourceType {
254    /// User's voice audio
255    Voice,
256    /// Music or sound effects
257    Media,
258    /// Environmental/ambient audio
259    Ambient,
260    /// Screen sharing audio
261    ScreenShare,
262    /// Notification sounds
263    Notification,
264    /// Interactive audio objects
265    Interactive,
266}
267
268/// Spatial properties for audio sources
269#[derive(Debug, Clone)]
270pub struct SpatialProperties {
271    /// Directional audio pattern
272    pub directivity: DirectionalPattern,
273    /// Distance at which audio starts attenuating
274    pub reference_distance: f32,
275    /// Rate of attenuation with distance
276    pub rolloff_factor: f32,
277    /// Maximum distance for audio audibility
278    pub max_distance: f32,
279    /// Doppler effect strength
280    pub doppler_factor: f32,
281    /// Room acoustics interaction
282    pub room_interaction: bool,
283}
284
285/// Audio directional patterns
286#[derive(Debug, Clone, Copy)]
287pub enum DirectionalPattern {
288    /// Omnidirectional (equal in all directions)
289    Omnidirectional,
290    /// Cardioid (heart-shaped, directional)
291    Cardioid,
292    /// Bidirectional (figure-8 pattern)
293    Bidirectional,
294    /// Cone-shaped directional pattern
295    Cone {
296        /// Inner cone angle in radians
297        inner_angle: f32,
298        /// Outer cone angle in radians
299        outer_angle: f32,
300        /// Volume multiplier outside the outer cone
301        outer_gain: f32,
302    },
303}
304
305/// Access control for audio sources
306#[derive(Debug, Clone)]
307pub struct SourceAccessControl {
308    /// Who can hear this source
309    pub visibility: SourceVisibility,
310    /// Specific users who can access (if visibility is Whitelist)
311    pub allowed_users: Vec<UserId>,
312    /// Users who are blocked from accessing
313    pub blocked_users: Vec<UserId>,
314    /// Spatial zones where this source is audible
315    pub spatial_zones: Vec<SpatialZone>,
316}
317
318/// Visibility settings for audio sources
319#[derive(Debug, Clone, Copy, PartialEq, Eq)]
320pub enum SourceVisibility {
321    /// Everyone can hear
322    Public,
323    /// Only friends/contacts can hear
324    Friends,
325    /// Only specific users can hear (whitelist)
326    Whitelist,
327    /// Private to owner only
328    Private,
329    /// Moderators and above can hear
330    Moderators,
331}
332
333/// Quality settings for individual audio sources
334#[derive(Debug, Clone)]
335pub struct SourceQualitySettings {
336    /// Bitrate for this source
337    pub bitrate_kbps: u32,
338    /// Spatial audio quality level
339    pub spatial_quality: f32,
340    /// Noise reduction level
341    pub noise_reduction: f32,
342    /// Echo cancellation strength
343    pub echo_cancellation: f32,
344    /// Dynamic range compression
345    pub compression_ratio: f32,
346}
347
348/// User-specific audio settings
349#[derive(Debug, Clone)]
350pub struct UserAudioSettings {
351    /// Overall volume multiplier
352    pub master_volume: f32,
353    /// Voice chat volume
354    pub voice_volume: f32,
355    /// Media/effects volume
356    pub media_volume: f32,
357    /// Spatial audio intensity
358    pub spatial_intensity: f32,
359    /// Personal HRTF profile identifier
360    pub hrtf_profile: Option<String>,
361    /// Microphone settings
362    pub microphone_settings: MicrophoneSettings,
363    /// Hearing accessibility options
364    pub accessibility: AccessibilitySettings,
365}
366
367/// Microphone settings for users
368#[derive(Debug, Clone)]
369pub struct MicrophoneSettings {
370    /// Input gain level
371    pub gain: f32,
372    /// Noise gate threshold
373    pub noise_gate_threshold: f32,
374    /// Automatic gain control
375    pub auto_gain_control: bool,
376    /// Push-to-talk mode
377    pub push_to_talk: bool,
378    /// Voice activation detection sensitivity
379    pub vad_sensitivity: f32,
380}
381
382/// Accessibility settings for hearing-impaired users
383#[derive(Debug, Clone)]
384pub struct AccessibilitySettings {
385    /// Enable visual audio indicators
386    pub visual_indicators: bool,
387    /// Enhance high-frequency audio
388    pub high_frequency_boost: f32,
389    /// Reduce background noise
390    pub background_noise_reduction: f32,
391    /// Convert speech to text
392    pub speech_to_text: bool,
393    /// Haptic feedback for audio events
394    pub haptic_feedback: bool,
395}
396
397/// Network performance statistics
398#[derive(Debug, Clone, Default)]
399pub struct NetworkStats {
400    /// Round-trip time in milliseconds
401    pub rtt_ms: f64,
402    /// Packet loss percentage
403    pub packet_loss_percent: f32,
404    /// Jitter in milliseconds
405    pub jitter_ms: f64,
406    /// Bandwidth utilization in kbps
407    pub bandwidth_usage_kbps: u32,
408    /// Number of reconnection attempts
409    pub reconnect_count: u32,
410    /// Audio dropouts in the last minute
411    pub audio_dropouts: u32,
412}
413
414/// Spatial zone for access control and audio routing
415#[derive(Debug, Clone, PartialEq)]
416pub struct SpatialZone {
417    /// Zone identifier
418    pub id: String,
419    /// Zone name
420    pub name: String,
421    /// Zone type
422    pub zone_type: ZoneType,
423    /// Geometric bounds of the zone
424    pub bounds: ZoneBounds,
425    /// Required permissions to enter
426    pub required_permissions: Vec<Permission>,
427    /// Audio properties within this zone
428    pub audio_properties: ZoneAudioProperties,
429}
430
431/// Types of spatial zones
432#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
433pub enum ZoneType {
434    /// General meeting/conversation area
435    Meeting,
436    /// Private conversation zone
437    Private,
438    /// Presentation/stage area
439    Presentation,
440    /// Quiet zone with reduced audio
441    Quiet,
442    /// High-priority broadcast zone
443    Broadcast,
444    /// Social gathering area
445    Social,
446}
447
448/// Geometric bounds for spatial zones
449#[derive(Debug, Clone, PartialEq)]
450pub enum ZoneBounds {
451    /// Spherical zone with center and radius
452    Sphere {
453        /// Center point of the sphere
454        center: Position3D,
455        /// Radius of the sphere
456        radius: f32,
457    },
458    /// Cubic zone with min and max coordinates
459    Box {
460        /// Minimum corner coordinates
461        min: Position3D,
462        /// Maximum corner coordinates
463        max: Position3D,
464    },
465    /// Cylindrical zone with center, radius, and height
466    Cylinder {
467        /// Center point of the cylinder base
468        center: Position3D,
469        /// Radius of the cylinder
470        radius: f32,
471        /// Height of the cylinder
472        height: f32,
473    },
474    /// Polygonal zone defined by vertices
475    Polygon {
476        /// List of vertices defining the polygon boundary
477        vertices: Vec<Position3D>,
478    },
479}
480
481/// Audio properties specific to spatial zones
482#[derive(Debug, Clone, PartialEq)]
483pub struct ZoneAudioProperties {
484    /// Volume multiplier for all audio in this zone
485    pub volume_multiplier: f32,
486    /// Acoustic properties (reverb, echo, etc.)
487    pub acoustic_settings: AcousticSettings,
488    /// Whether audio from this zone can be heard outside
489    pub audio_isolation: bool,
490    /// Maximum number of simultaneous speakers
491    pub max_speakers: Option<usize>,
492    /// Priority level for audio processing
493    pub priority: u8,
494}
495
496/// Acoustic settings for zones
497#[derive(Debug, Clone, PartialEq)]
498pub struct AcousticSettings {
499    /// Reverb amount (0.0-1.0)
500    pub reverb: f32,
501    /// Echo delay in milliseconds
502    pub echo_delay_ms: f32,
503    /// Echo feedback amount
504    pub echo_feedback: f32,
505    /// Room size simulation
506    pub room_size: f32,
507    /// Damping factor for reflections
508    pub damping: f32,
509}
510
511/// Multi-user spatial audio environment manager
512pub struct MultiUserEnvironment {
513    /// Configuration for this environment
514    pub(crate) config: MultiUserConfig,
515    /// All users in the environment
516    pub(crate) users: Arc<RwLock<HashMap<UserId, MultiUserUser>>>,
517    /// All audio sources in the environment
518    pub(crate) sources: Arc<RwLock<HashMap<SourceId, MultiUserAudioSource>>>,
519    /// Spatial zones in the environment
520    pub(crate) zones: Arc<RwLock<HashMap<String, SpatialZone>>>,
521    /// Network synchronization manager
522    pub(crate) sync_manager: SynchronizationManager,
523    /// Audio processing pipeline
524    pub(crate) audio_processor: MultiUserAudioProcessor,
525    /// Performance metrics
526    pub(crate) metrics: Arc<RwLock<MultiUserMetrics>>,
527    /// Event history for debugging and analysis
528    pub(crate) event_history: Arc<RwLock<VecDeque<MultiUserEvent>>>,
529}
530
531/// Synchronization manager for network coordination
532pub struct SynchronizationManager {
533    /// Synchronization clock
534    pub(crate) clock: Arc<RwLock<SynchronizedClock>>,
535    /// User position interpolation
536    pub(crate) position_interpolator: PositionInterpolator,
537    /// Network latency compensation
538    pub(crate) latency_compensator: LatencyCompensator,
539}
540
541/// Synchronized clock for multi-user coordination
542#[derive(Debug)]
543pub struct SynchronizedClock {
544    /// Local time reference
545    pub(crate) local_time: Instant,
546    /// Network-synchronized time offset
547    pub(crate) time_offset_ms: i64,
548    /// Clock synchronization accuracy
549    pub(crate) sync_accuracy_ms: f64,
550    /// Last synchronization timestamp
551    pub(crate) last_sync: Instant,
552}
553
554/// Position interpolation for smooth user movement
555pub struct PositionInterpolator {
556    /// User position histories
557    pub(crate) position_histories: HashMap<UserId, VecDeque<PositionSnapshot>>,
558    /// Interpolation method
559    pub(crate) interpolation_method: InterpolationMethod,
560    /// Prediction horizon in milliseconds
561    pub(crate) prediction_horizon_ms: f64,
562}
563
564/// Position snapshot with timestamp
565#[derive(Debug, Clone)]
566pub struct PositionSnapshot {
567    /// Position in 3D space
568    pub position: Position3D,
569    /// Orientation quaternion
570    pub orientation: [f32; 4],
571    /// Velocity vector
572    pub velocity: Position3D,
573    /// Timestamp of this snapshot
574    pub timestamp: Instant,
575    /// Network latency when this was received
576    pub latency_ms: f64,
577}
578
579/// Interpolation methods for position smoothing
580#[derive(Debug, Clone, Copy, PartialEq)]
581pub enum InterpolationMethod {
582    /// Linear interpolation
583    Linear,
584    /// Cubic spline interpolation
585    CubicSpline,
586    /// Kalman filter-based prediction
587    Kalman,
588    /// Physics-based prediction
589    Physics,
590}
591
592/// Network latency compensation system
593pub struct LatencyCompensator {
594    /// Per-user latency estimates
595    pub(crate) user_latencies: HashMap<UserId, f64>,
596    /// Compensation strategies
597    pub(crate) compensation_method: CompensationMethod,
598    /// Maximum compensation amount
599    pub(crate) max_compensation_ms: f64,
600}
601
602/// Latency compensation methods
603#[derive(Debug, Clone, Copy)]
604pub enum CompensationMethod {
605    /// Simple time-shift compensation
606    TimeShift,
607    /// Predictive compensation with motion models
608    Predictive,
609    /// Adaptive compensation based on network conditions
610    Adaptive,
611    /// No compensation (for low-latency networks)
612    None,
613}
614
615/// Multi-user audio processing pipeline
616pub struct MultiUserAudioProcessor {
617    /// Audio mixing engine
618    pub(crate) mixer: SpatialAudioMixer,
619    /// Voice activity detection
620    pub(crate) vad: VoiceActivityDetector,
621    /// Audio effects processor
622    pub(crate) effects: AudioEffectsProcessor,
623    /// Compression and encoding
624    pub(crate) codec: AudioCodec,
625}
626
627/// Spatial audio mixing for multiple users and sources
628pub struct SpatialAudioMixer {
629    /// Current listener position (for each user)
630    pub(crate) listener_positions: HashMap<UserId, Position3D>,
631    /// Audio source management
632    pub(crate) source_manager: Arc<RwLock<HashMap<SourceId, MixerAudioSource>>>,
633    /// Mixing configuration
634    pub(crate) mixer_config: MixerConfig,
635}
636
637/// Audio source representation for the mixer
638#[derive(Debug, Clone)]
639pub struct MixerAudioSource {
640    /// Source identifier
641    pub id: SourceId,
642    /// Current audio buffer
643    pub buffer: Vec<f32>,
644    /// Spatial position
645    pub position: Position3D,
646    /// Audio properties
647    pub properties: SpatialProperties,
648    /// Processing state
649    pub processing_state: SourceProcessingState,
650}
651
652/// Processing state for audio sources
653#[derive(Debug, Clone)]
654pub struct SourceProcessingState {
655    /// Current playback position
656    pub playback_position: usize,
657    /// Volume envelope state
658    pub envelope_state: f32,
659    /// Spatial processing parameters
660    pub spatial_params: SpatialProcessingParams,
661    /// Last update timestamp
662    pub last_update: Instant,
663}
664
665/// Spatial processing parameters
666#[derive(Debug, Clone)]
667pub struct SpatialProcessingParams {
668    /// Distance-based attenuation
669    pub distance_attenuation: f32,
670    /// Doppler shift factor
671    pub doppler_shift: f32,
672    /// Spatial position in listener coordinates
673    pub listener_relative_position: Position3D,
674    /// Occlusion factors
675    pub occlusion: f32,
676}
677
678/// Mixer configuration
679#[derive(Debug, Clone)]
680pub struct MixerConfig {
681    /// Maximum number of simultaneous sources
682    pub max_sources: usize,
683    /// Audio buffer size
684    pub buffer_size: usize,
685    /// Sample rate
686    pub sample_rate: u32,
687    /// Spatial processing quality
688    pub spatial_quality: f32,
689    /// CPU optimization level
690    pub optimization_level: OptimizationLevel,
691}
692
693/// CPU optimization levels for audio processing
694#[derive(Debug, Clone, Copy)]
695pub enum OptimizationLevel {
696    /// Highest quality, highest CPU usage
697    Quality,
698    /// Balanced quality and performance
699    Balanced,
700    /// Performance optimized, reduced quality
701    Performance,
702    /// Lowest CPU usage, basic quality
703    MinimalCpu,
704}
705
706/// Voice activity detection system
707pub struct VoiceActivityDetector {
708    /// Detection algorithm
709    pub(crate) algorithm: VadAlgorithm,
710    /// Per-user VAD state
711    pub(crate) user_states: HashMap<UserId, VadState>,
712    /// Detection thresholds
713    pub(crate) thresholds: VadThresholds,
714}
715
716/// Voice activity detection algorithms
717#[derive(Debug, Clone, Copy, PartialEq)]
718pub enum VadAlgorithm {
719    /// Energy-based detection
720    Energy,
721    /// Spectral analysis based
722    Spectral,
723    /// Machine learning based
724    MachineLearning,
725    /// Combination of multiple methods
726    Hybrid,
727}
728
729/// VAD state for individual users
730#[derive(Debug, Clone)]
731pub struct VadState {
732    /// Current speaking state
733    pub is_speaking: bool,
734    /// Confidence level (0.0-1.0)
735    pub confidence: f32,
736    /// Recent audio energy levels
737    pub energy_history: VecDeque<f32>,
738    /// Speaking duration
739    pub speaking_duration: Duration,
740    /// Silence duration
741    pub silence_duration: Duration,
742}
743
744/// VAD detection thresholds
745#[derive(Debug, Clone)]
746pub struct VadThresholds {
747    /// Energy threshold for voice detection
748    pub energy_threshold: f32,
749    /// Minimum speaking duration
750    pub min_speaking_duration_ms: u64,
751    /// Minimum silence duration
752    pub min_silence_duration_ms: u64,
753    /// Confidence threshold
754    pub confidence_threshold: f32,
755}
756
757/// Audio effects processing for multi-user environments
758pub struct AudioEffectsProcessor {
759    /// Available audio effects
760    pub(crate) effects: HashMap<String, Box<dyn AudioEffect>>,
761    /// Per-user effect chains
762    pub(crate) user_effects: HashMap<UserId, Vec<String>>,
763    /// Zone-based effects
764    pub(crate) zone_effects: HashMap<String, Vec<String>>,
765}
766
767/// Trait for audio effects
768pub trait AudioEffect {
769    /// Process audio samples
770    fn process(&mut self, input: &[f32], output: &mut [f32]) -> Result<()>;
771
772    /// Get effect parameters
773    fn parameters(&self) -> HashMap<String, f32>;
774
775    /// Set effect parameters
776    fn set_parameters(&mut self, params: HashMap<String, f32>) -> Result<()>;
777
778    /// Reset effect state
779    fn reset(&mut self);
780}
781
782/// Audio codec for compression and encoding
783pub struct AudioCodec {
784    /// Encoding format
785    pub(crate) format: AudioFormat,
786    /// Compression settings
787    pub(crate) compression: CompressionSettings,
788    /// Per-user codec state
789    pub(crate) codec_states: HashMap<UserId, CodecState>,
790}
791
792/// Supported audio formats
793#[derive(Debug, Clone, Copy)]
794pub enum AudioFormat {
795    /// Opus codec (recommended for real-time)
796    Opus,
797    /// AAC codec
798    Aac,
799    /// MP3 codec
800    Mp3,
801    /// Raw PCM (uncompressed)
802    Pcm,
803}
804
805/// Compression settings for audio codec
806#[derive(Debug, Clone)]
807pub struct CompressionSettings {
808    /// Bitrate in kbps
809    pub bitrate_kbps: u32,
810    /// Complexity level (0-10)
811    pub complexity: u8,
812    /// Variable bitrate mode
813    pub variable_bitrate: bool,
814    /// Low latency mode
815    pub low_latency: bool,
816}
817
818/// Per-user codec state
819#[derive(Debug)]
820pub struct CodecState {
821    /// Encoder state
822    pub encoder_state: Vec<u8>,
823    /// Decoder state
824    pub decoder_state: Vec<u8>,
825    /// Frame buffer
826    pub frame_buffer: VecDeque<Vec<u8>>,
827    /// Timing information
828    pub timing: CodecTiming,
829}
830
831/// Codec timing information
832#[derive(Debug, Clone)]
833pub struct CodecTiming {
834    /// Encoding latency
835    pub encode_latency_ms: f64,
836    /// Decoding latency
837    pub decode_latency_ms: f64,
838    /// Buffer latency
839    pub buffer_latency_ms: f64,
840    /// Total latency
841    pub total_latency_ms: f64,
842}
843
844/// Performance metrics for multi-user environment
845#[derive(Debug, Clone, Default)]
846pub struct MultiUserMetrics {
847    /// Number of active users
848    pub active_users: usize,
849    /// Number of active audio sources
850    pub active_sources: usize,
851    /// Average network latency
852    pub avg_latency_ms: f64,
853    /// Audio processing CPU usage
854    pub audio_cpu_usage: f32,
855    /// Memory usage in MB
856    pub memory_usage_mb: f32,
857    /// Network bandwidth usage
858    pub bandwidth_usage_kbps: u32,
859    /// Audio quality metrics
860    pub audio_quality: AudioQualityMetrics,
861    /// Synchronization accuracy
862    pub sync_accuracy_ms: f64,
863    /// Number of reconnections in last hour
864    pub reconnections_per_hour: u32,
865}
866
867/// Audio quality metrics
868#[derive(Debug, Clone, Default)]
869pub struct AudioQualityMetrics {
870    /// Signal-to-noise ratio
871    pub snr_db: f32,
872    /// Total harmonic distortion
873    pub thd_percent: f32,
874    /// Audio dropouts per minute
875    pub dropouts_per_minute: f32,
876    /// Perceived audio quality (MOS scale 1-5)
877    pub perceived_quality: f32,
878}
879
880/// Events in the multi-user environment
881#[derive(Debug, Clone)]
882pub enum MultiUserEvent {
883    /// User joined the environment
884    UserJoined {
885        /// ID of the user who joined
886        user_id: UserId,
887        /// When the user joined
888        timestamp: SystemTime,
889        /// Initial position of the user
890        position: Position3D,
891    },
892    /// User left the environment
893    UserLeft {
894        /// ID of the user who left
895        user_id: UserId,
896        /// When the user left
897        timestamp: SystemTime,
898        /// Reason for leaving
899        reason: DisconnectReason,
900    },
901    /// User moved in space
902    UserMoved {
903        /// ID of the user who moved
904        user_id: UserId,
905        /// When the movement occurred
906        timestamp: SystemTime,
907        /// Previous position
908        old_position: Position3D,
909        /// New position
910        new_position: Position3D,
911    },
912    /// User started speaking
913    UserStartedSpeaking {
914        /// ID of the user who started speaking
915        user_id: UserId,
916        /// When speaking started
917        timestamp: SystemTime,
918        /// Voice activity detection confidence level
919        confidence: f32,
920    },
921    /// User stopped speaking
922    UserStoppedSpeaking {
923        /// ID of the user who stopped speaking
924        user_id: UserId,
925        /// When speaking stopped
926        timestamp: SystemTime,
927        /// Duration of the speaking session
928        duration: Duration,
929    },
930    /// Audio source created
931    SourceCreated {
932        /// ID of the created source
933        source_id: SourceId,
934        /// ID of the user who created the source
935        user_id: UserId,
936        /// When the source was created
937        timestamp: SystemTime,
938        /// Type of the audio source
939        source_type: AudioSourceType,
940    },
941    /// Audio source removed
942    SourceRemoved {
943        /// ID of the removed source
944        source_id: SourceId,
945        /// When the source was removed
946        timestamp: SystemTime,
947        /// Reason for removal
948        reason: String,
949    },
950    /// Network event (latency, packet loss, etc.)
951    NetworkEvent {
952        /// ID of the affected user
953        user_id: UserId,
954        /// When the event occurred
955        timestamp: SystemTime,
956        /// Type of network event
957        event_type: NetworkEventType,
958        /// Measured value for the event
959        value: f64,
960    },
961}
962
963/// Reasons for user disconnection
964#[derive(Debug, Clone, Copy)]
965pub enum DisconnectReason {
966    /// User voluntarily left
967    UserLeft,
968    /// Network connection lost
969    NetworkTimeout,
970    /// Kicked by moderator
971    Kicked,
972    /// Technical error
973    Error,
974    /// Server shutdown
975    ServerShutdown,
976}
977
978/// Types of network events
979#[derive(Debug, Clone, Copy)]
980pub enum NetworkEventType {
981    /// Latency measurement
982    Latency,
983    /// Packet loss percentage
984    PacketLoss,
985    /// Jitter measurement
986    Jitter,
987    /// Bandwidth measurement
988    Bandwidth,
989    /// Connection established
990    ConnectionEstablished,
991    /// Connection lost
992    ConnectionLost,
993}