euv_core/game/audio_output/struct.rs
1use super::*;
2
3/// Configuration parameters for `AudioOutputBackend` stereo PCM mixing and ring buffering.
4#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
5pub struct AudioOutputConfig {
6 /// Output PCM sample rate in Hertz (clamped to `[8_000, 192_000]`).
7 #[get(type(copy))]
8 pub(crate) sample_rate_hz: u32,
9 /// Output channel count (stereo mixer outputs `2` interleaved L/R channels).
10 #[get(type(copy))]
11 pub(crate) channels: u16,
12 /// Stereo frame capacity of the internal PCM ring buffer.
13 #[get(type(copy))]
14 pub(crate) ring_frames: usize,
15 /// Maximum number of concurrent synthesized voices.
16 #[get(type(copy))]
17 pub(crate) max_voices: usize,
18 /// Master output linear gain clamped to `[0.0, 2.0]`.
19 #[get(type(copy))]
20 pub(crate) master_gain: f32,
21}
22
23/// Real-time telemetry snapshot exported by `AudioOutputBackend`.
24#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
25pub struct AudioOutputMetrics {
26 /// Active output driver kind (`PullMixer` or verified `CoreAudioAuhal`).
27 #[get(type(copy))]
28 pub(crate) driver_kind: AudioDriverKind,
29 /// Current backend lifecycle state.
30 #[get(type(copy))]
31 pub(crate) state: AudioOutputState,
32 /// Number of currently sounding voices.
33 #[get(type(copy))]
34 pub(crate) active_voices: usize,
35 /// Cumulative stereo frames synthesized by the mixer.
36 #[get(type(copy))]
37 pub(crate) frames_mixed: u64,
38 /// Cumulative `AudioCommand` items ingested from `AudioBus` or direct enqueue calls.
39 #[get(type(copy))]
40 pub(crate) commands_ingested: u64,
41 /// Number of stereo frames currently buffered in the PCM ring buffer.
42 #[get(type(copy))]
43 pub(crate) ring_buffered_frames: usize,
44 /// Cumulative ring buffer underruns when pull requests exceeded buffered frames.
45 #[get(type(copy))]
46 pub(crate) ring_underruns: u64,
47 /// Cumulative ring buffer frame overwrites when producer outpaced consumer.
48 #[get(type(copy))]
49 pub(crate) ring_overruns: u64,
50 /// Cumulative samples attenuated by the soft-knee anti-clipping limiter.
51 #[get(type(copy))]
52 pub(crate) clipped_samples: u64,
53 /// Peak post-limiter absolute sample amplitude observed (`<= 1.0`).
54 #[get(type(copy))]
55 pub(crate) peak_amplitude: f32,
56}
57
58/// Active synthesized stereo voice generated from one `AudioCommand`.
59#[derive(Clone, Copy, Data, Debug)]
60pub(crate) struct AudioSynthVoice {
61 /// Sound cue waveform preset.
62 #[get(type(copy))]
63 pub(crate) cue: SoundCue,
64 /// Left channel linear gain after equal-power stereo panning.
65 #[get(type(copy))]
66 pub(crate) left_gain: f32,
67 /// Right channel linear gain after equal-power stereo panning.
68 #[get(type(copy))]
69 pub(crate) right_gain: f32,
70 /// Playback pitch multiplier.
71 #[get(type(copy))]
72 pub(crate) pitch: f32,
73 /// Oscillator phase accumulator in radians.
74 #[get(type(copy))]
75 pub(crate) phase: f32,
76 /// Elapsed frames synthesized for this voice.
77 #[get(type(copy))]
78 pub(crate) elapsed_frames: u32,
79 /// Total envelope duration in frames.
80 #[get(type(copy))]
81 pub(crate) duration_frames: u32,
82 /// Deterministic PRNG state for noise-based cues (`Explosion`, `Dash`).
83 #[get(type(copy))]
84 pub(crate) noise_state: u32,
85 /// Monotonic age sequence used for oldest-voice stealing.
86 #[get(type(copy))]
87 pub(crate) birth_order: u64,
88}
89
90/// Bounded interleaved stereo `f32` ring buffer transferring mixed frames to pull or hardware callbacks.
91#[derive(Clone, Data, Debug)]
92pub(crate) struct StereoPcmRingBuffer {
93 /// Interleaved stereo storage (`capacity_frames * 2` floats).
94 #[get(pub(crate))]
95 #[get_mut(pub(crate))]
96 #[set(skip)]
97 pub(crate) samples: Vec<f32>,
98 /// Ring buffer capacity in stereo frames.
99 #[get(type(copy))]
100 pub(crate) capacity_frames: usize,
101 /// Next read frame index modulo `capacity_frames`.
102 #[get(type(copy))]
103 pub(crate) read_frame: usize,
104 /// Next write frame index modulo `capacity_frames`.
105 #[get(type(copy))]
106 pub(crate) write_frame: usize,
107 /// Number of valid unread stereo frames currently stored.
108 #[get(type(copy))]
109 pub(crate) buffered_frames: usize,
110 /// Cumulative dropped frames due to write overrun.
111 #[get(type(copy))]
112 pub(crate) overruns: u64,
113 /// Cumulative silent frames substituted due to read underrun.
114 #[get(type(copy))]
115 pub(crate) underruns: u64,
116}
117
118/// Shared synchronized mixer state accessed by both game-thread producers and audio-thread consumers.
119#[derive(Data, Debug)]
120pub(crate) struct AudioMixerCore {
121 /// Sanitized audio mixer configuration.
122 #[get(type(copy))]
123 pub(crate) config: AudioOutputConfig,
124 /// Active polyphonic synthesized voices.
125 pub(crate) voices: Vec<AudioSynthVoice>,
126 /// Interleaved stereo f32 PCM output ring buffer.
127 pub(crate) ring: StereoPcmRingBuffer,
128 /// Global output mute toggle.
129 #[get(type(copy))]
130 pub(crate) muted: bool,
131 /// Monotonic voice creation sequence counter.
132 #[get(type(copy))]
133 pub(crate) next_birth_order: u64,
134 /// Cumulative stereo frames synthesized.
135 #[get(type(copy))]
136 pub(crate) frames_mixed: u64,
137 /// Cumulative `AudioCommand` items ingested.
138 #[get(type(copy))]
139 pub(crate) commands_ingested: u64,
140 /// Cumulative samples limited by the anti-clipping stage.
141 #[get(type(copy))]
142 pub(crate) clipped_samples: u64,
143 /// Peak post-limiter amplitude observed (`<= 1.0`).
144 #[get(type(copy))]
145 pub(crate) peak_amplitude: f32,
146}
147
148/// Native macOS CoreAudio AUHAL handle with RAII stop and disposal semantics.
149#[cfg(target_os = "macos")]
150#[derive(Data, Debug)]
151pub(crate) struct CoreAudioAuhalHandle {
152 /// Retained `AudioUnit` (`AudioComponentInstance`) raw pointer.
153 #[get(type(copy))]
154 pub(crate) audio_unit: *mut c_void,
155 /// Boxed `Arc<Mutex<AudioMixerCore>>` raw pointer owned until after `AudioComponentInstanceDispose`.
156 #[get(type(copy))]
157 pub(crate) callback_context: *mut c_void,
158 /// Whether `AudioOutputUnitStart` is currently active.
159 #[get(type(copy))]
160 pub(crate) running: bool,
161}
162
163/// Cross-platform wrapper holding an optional native macOS CoreAudio AUHAL handle.
164#[cfg(target_os = "macos")]
165#[derive(Data, Default)]
166pub(crate) struct NativeAudioDriverSlot {
167 /// Active macOS CoreAudio AUHAL driver handle when started natively.
168 #[get(skip)]
169 pub(crate) coreaudio: Option<CoreAudioAuhalHandle>,
170}
171
172/// Cross-platform placeholder slot on targets using deterministic software pull mixing.
173#[cfg(not(target_os = "macos"))]
174#[derive(Data, Default)]
175pub(crate) struct NativeAudioDriverSlot {
176 /// Marker state on targets using software pull mixing.
177 #[get(type(copy))]
178 pub(crate) active: bool,
179}
180
181/// CoreAudio `AudioComponentDescription` C ABI layout.
182#[cfg(target_os = "macos")]
183#[repr(C)]
184#[derive(Clone, Copy, Debug)]
185pub(crate) struct AudioComponentDescriptionC {
186 /// Component type FourCC (`kAudioUnitType_Output`).
187 pub(crate) component_type: u32,
188 /// Component subtype FourCC (`kAudioUnitSubType_DefaultOutput`).
189 pub(crate) component_sub_type: u32,
190 /// Component manufacturer FourCC (`kAudioUnitManufacturer_Apple`).
191 pub(crate) component_manufacturer: u32,
192 /// Component flags (`0`).
193 pub(crate) component_flags: u32,
194 /// Component flags mask (`0`).
195 pub(crate) component_flags_mask: u32,
196}
197
198/// CoreAudio `AudioStreamBasicDescription` C ABI layout.
199#[cfg(target_os = "macos")]
200#[repr(C)]
201#[derive(Clone, Copy, Debug)]
202pub(crate) struct AudioStreamBasicDescriptionC {
203 /// Nominal sample rate in Hz.
204 pub(crate) sample_rate: f64,
205 /// Stream format FourCC (`'lpcm'`).
206 pub(crate) format_id: u32,
207 /// Format bitmask (`kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked`).
208 pub(crate) format_flags: u32,
209 /// Byte stride per packet.
210 pub(crate) bytes_per_packet: u32,
211 /// Frame count per packet (`1` for uncompressed PCM).
212 pub(crate) frames_per_packet: u32,
213 /// Byte stride per stereo frame.
214 pub(crate) bytes_per_frame: u32,
215 /// Channels per frame (`2` for stereo).
216 pub(crate) channels_per_frame: u32,
217 /// Bit depth per channel (`32` for `f32`).
218 pub(crate) bits_per_channel: u32,
219 /// Reserved C struct padding (`0`).
220 pub(crate) reserved: u32,
221}
222
223/// CoreAudio `AURenderCallbackStruct` C ABI layout.
224#[cfg(target_os = "macos")]
225#[repr(C)]
226#[derive(Clone, Copy)]
227pub(crate) struct AuRenderCallbackStructC {
228 /// Real-time render callback entry point invoked by CoreAudio.
229 pub(crate) input_proc: unsafe extern "C" fn(
230 *mut c_void,
231 *mut u32,
232 *const c_void,
233 u32,
234 u32,
235 *mut AudioBufferListC,
236 ) -> i32,
237 /// Opaque pointer to the boxed `Arc<Mutex<AudioMixerCore>>`.
238 pub(crate) input_proc_ref_con: *mut c_void,
239}
240
241/// CoreAudio `AudioBuffer` C ABI layout.
242#[cfg(target_os = "macos")]
243#[repr(C)]
244#[derive(Clone, Copy)]
245pub(crate) struct AudioBufferC {
246 /// Interleaved channel count stored in `data`.
247 pub(crate) number_channels: u32,
248 /// Buffer byte length.
249 pub(crate) data_byte_size: u32,
250 /// Raw pointer to destination PCM sample buffer.
251 pub(crate) data: *mut c_void,
252}
253
254/// CoreAudio `AudioBufferList` C ABI layout.
255#[cfg(target_os = "macos")]
256#[repr(C)]
257#[derive(Clone, Copy)]
258pub(crate) struct AudioBufferListC {
259 /// Count of `AudioBuffer` descriptors present in the variable-length tail.
260 pub(crate) number_buffers: u32,
261 /// First `AudioBuffer` descriptor.
262 pub(crate) buffers: [AudioBufferC; 1],
263}
264
265/// Production stereo `f32` PCM audio mixer draining `AudioBus` with native macOS CoreAudio AUHAL output and deterministic pull mode.
266#[derive(Data)]
267pub struct AudioOutputBackend {
268 /// Active mixer configuration.
269 #[get(type(copy))]
270 pub(crate) config: AudioOutputConfig,
271 /// Shared mixer core and stereo PCM ring buffer.
272 #[get(pub(crate))]
273 #[get_mut(skip)]
274 #[set(skip)]
275 pub(crate) core: Arc<Mutex<AudioMixerCore>>,
276 /// Active driver kind (`PullMixer` or verified `CoreAudioAuhal`).
277 #[get(type(copy))]
278 pub(crate) driver_kind: AudioDriverKind,
279 /// Current backend lifecycle state.
280 #[get(type(copy))]
281 pub(crate) state: AudioOutputState,
282 /// Optional native OS hardware output handle.
283 #[get(pub(crate))]
284 #[get_mut(pub(crate))]
285 #[set(skip)]
286 pub(crate) native_slot: NativeAudioDriverSlot,
287}