Skip to main content

voirs_spatial/gaming/
types.rs

1//! Core types and enums for gaming audio integration
2
3use crate::position::SoundSource;
4use crate::types::Position3D;
5use serde::{Deserialize, Serialize};
6use std::time::Instant;
7
8/// Gaming engine types supported by VoiRS
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10pub enum GameEngine {
11    /// Unity 3D engine
12    Unity,
13    /// Unreal Engine 4/5
14    Unreal,
15    /// Godot engine
16    Godot,
17    /// Custom C/C++ engine
18    Custom,
19    /// Web-based engine (Three.js, Babylon.js, etc.)
20    WebEngine,
21    /// Console-specific engines
22    Console,
23    /// PlayStation 4/5 specific
24    PlayStation,
25    /// Xbox One/Series X|S specific
26    Xbox,
27    /// Nintendo Switch specific
28    NintendoSwitch,
29}
30
31/// Gaming audio source handle
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub struct GameAudioSource {
34    /// Unique source ID
35    pub id: u32,
36    /// Source category for mixing
37    pub category: AudioCategory,
38    /// Priority level (0 = highest, 255 = lowest)
39    pub priority: u8,
40}
41
42/// Audio categories for gaming
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
44pub enum AudioCategory {
45    /// Player character sounds
46    Player,
47    /// Non-player character sounds
48    Npc,
49    /// Environmental sounds
50    Environment,
51    /// Music and soundtrack
52    Music,
53    /// Sound effects
54    Sfx,
55    /// UI sounds
56    Ui,
57    /// Voice and dialogue
58    Voice,
59    /// Ambient sounds
60    Ambient,
61}
62
63/// Distance attenuation settings for gaming
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct AttenuationSettings {
66    /// Minimum distance (no attenuation)
67    pub min_distance: f32,
68    /// Maximum distance (silence)
69    pub max_distance: f32,
70    /// Attenuation curve (linear, logarithmic, exponential)
71    pub curve: AttenuationCurve,
72    /// Attenuation factor
73    pub factor: f32,
74}
75
76impl Default for AttenuationSettings {
77    fn default() -> Self {
78        Self {
79            min_distance: 1.0,
80            max_distance: 100.0,
81            curve: AttenuationCurve::Logarithmic,
82            factor: 1.0,
83        }
84    }
85}
86
87/// Distance attenuation curves
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
89pub enum AttenuationCurve {
90    /// Linear attenuation curve
91    Linear,
92    /// Logarithmic attenuation curve
93    Logarithmic,
94    /// Exponential attenuation curve
95    Exponential,
96    /// Custom attenuation curve
97    Custom,
98}
99
100/// Gaming performance metrics
101#[derive(Debug, Clone, Default)]
102pub struct GamingMetrics {
103    /// Audio processing time per frame (ms)
104    pub audio_time_ms: f32,
105    /// Number of active sources
106    pub active_sources: u32,
107    /// Memory usage (MB)
108    pub memory_usage_mb: f32,
109    /// CPU usage percentage
110    pub cpu_usage_percent: f32,
111    /// GPU usage percentage (if available)
112    pub gpu_usage_percent: Option<f32>,
113    /// Dropped frames count
114    pub dropped_frames: u32,
115    /// Average FPS
116    pub fps: f32,
117    /// Audio latency (ms)
118    pub latency_ms: f32,
119}
120
121/// Internal audio source data
122#[derive(Debug, Clone)]
123pub(super) struct GameAudioSourceData {
124    /// Source handle
125    pub handle: GameAudioSource,
126    /// Spatial source
127    pub spatial_source: SoundSource,
128    /// Audio buffer
129    pub audio_data: Vec<f32>,
130    /// Playback state
131    pub state: PlaybackState,
132    /// Volume level
133    pub volume: f32,
134    /// Loop settings
135    pub looping: bool,
136    /// Distance attenuation settings
137    pub attenuation: AttenuationSettings,
138}
139
140/// Playback state
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142pub(super) enum PlaybackState {
143    Stopped,
144    Playing,
145    Paused,
146    Finished,
147}
148
149/// Frame timing helper
150#[derive(Debug)]
151pub(super) struct FrameTimer {
152    pub last_frame: Instant,
153    pub frame_count: u32,
154    pub fps_accumulator: f32,
155    pub fps_timer: Instant,
156}
157
158impl Default for FrameTimer {
159    fn default() -> Self {
160        let now = Instant::now();
161        Self {
162            last_frame: now,
163            frame_count: 0,
164            fps_accumulator: 0.0,
165            fps_timer: now,
166        }
167    }
168}