euv-core 0.52.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Configuration parameters for `AudioOutputBackend` stereo PCM mixing and ring buffering.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct AudioOutputConfig {
    /// Output PCM sample rate in Hertz (clamped to `[8_000, 192_000]`).
    #[get(type(copy))]
    pub(crate) sample_rate_hz: u32,
    /// Output channel count (stereo mixer outputs `2` interleaved L/R channels).
    #[get(type(copy))]
    pub(crate) channels: u16,
    /// Stereo frame capacity of the internal PCM ring buffer.
    #[get(type(copy))]
    pub(crate) ring_frames: usize,
    /// Maximum number of concurrent synthesized voices.
    #[get(type(copy))]
    pub(crate) max_voices: usize,
    /// Master output linear gain clamped to `[0.0, 2.0]`.
    #[get(type(copy))]
    pub(crate) master_gain: f32,
}

/// Real-time telemetry snapshot exported by `AudioOutputBackend`.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct AudioOutputMetrics {
    /// Active output driver kind (`PullMixer` or verified `CoreAudioAuhal`).
    #[get(type(copy))]
    pub(crate) driver_kind: AudioDriverKind,
    /// Current backend lifecycle state.
    #[get(type(copy))]
    pub(crate) state: AudioOutputState,
    /// Number of currently sounding voices.
    #[get(type(copy))]
    pub(crate) active_voices: usize,
    /// Cumulative stereo frames synthesized by the mixer.
    #[get(type(copy))]
    pub(crate) frames_mixed: u64,
    /// Cumulative `AudioCommand` items ingested from `AudioBus` or direct enqueue calls.
    #[get(type(copy))]
    pub(crate) commands_ingested: u64,
    /// Number of stereo frames currently buffered in the PCM ring buffer.
    #[get(type(copy))]
    pub(crate) ring_buffered_frames: usize,
    /// Cumulative ring buffer underruns when pull requests exceeded buffered frames.
    #[get(type(copy))]
    pub(crate) ring_underruns: u64,
    /// Cumulative ring buffer frame overwrites when producer outpaced consumer.
    #[get(type(copy))]
    pub(crate) ring_overruns: u64,
    /// Cumulative samples attenuated by the soft-knee anti-clipping limiter.
    #[get(type(copy))]
    pub(crate) clipped_samples: u64,
    /// Peak post-limiter absolute sample amplitude observed (`<= 1.0`).
    #[get(type(copy))]
    pub(crate) peak_amplitude: f32,
}

/// Active synthesized stereo voice generated from one `AudioCommand`.
#[derive(Clone, Copy, Data, Debug)]
pub(crate) struct AudioSynthVoice {
    /// Sound cue waveform preset.
    #[get(type(copy))]
    pub(crate) cue: SoundCue,
    /// Left channel linear gain after equal-power stereo panning.
    #[get(type(copy))]
    pub(crate) left_gain: f32,
    /// Right channel linear gain after equal-power stereo panning.
    #[get(type(copy))]
    pub(crate) right_gain: f32,
    /// Playback pitch multiplier.
    #[get(type(copy))]
    pub(crate) pitch: f32,
    /// Oscillator phase accumulator in radians.
    #[get(type(copy))]
    pub(crate) phase: f32,
    /// Elapsed frames synthesized for this voice.
    #[get(type(copy))]
    pub(crate) elapsed_frames: u32,
    /// Total envelope duration in frames.
    #[get(type(copy))]
    pub(crate) duration_frames: u32,
    /// Deterministic PRNG state for noise-based cues (`Explosion`, `Dash`).
    #[get(type(copy))]
    pub(crate) noise_state: u32,
    /// Monotonic age sequence used for oldest-voice stealing.
    #[get(type(copy))]
    pub(crate) birth_order: u64,
}

/// Bounded interleaved stereo `f32` ring buffer transferring mixed frames to pull or hardware callbacks.
#[derive(Clone, Data, Debug)]
pub(crate) struct StereoPcmRingBuffer {
    /// Interleaved stereo storage (`capacity_frames * 2` floats).
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) samples: Vec<f32>,
    /// Ring buffer capacity in stereo frames.
    #[get(type(copy))]
    pub(crate) capacity_frames: usize,
    /// Next read frame index modulo `capacity_frames`.
    #[get(type(copy))]
    pub(crate) read_frame: usize,
    /// Next write frame index modulo `capacity_frames`.
    #[get(type(copy))]
    pub(crate) write_frame: usize,
    /// Number of valid unread stereo frames currently stored.
    #[get(type(copy))]
    pub(crate) buffered_frames: usize,
    /// Cumulative dropped frames due to write overrun.
    #[get(type(copy))]
    pub(crate) overruns: u64,
    /// Cumulative silent frames substituted due to read underrun.
    #[get(type(copy))]
    pub(crate) underruns: u64,
}

/// Shared synchronized mixer state accessed by both game-thread producers and audio-thread consumers.
#[derive(Data, Debug)]
pub(crate) struct AudioMixerCore {
    /// Sanitized audio mixer configuration.
    #[get(type(copy))]
    pub(crate) config: AudioOutputConfig,
    /// Active polyphonic synthesized voices.
    pub(crate) voices: Vec<AudioSynthVoice>,
    /// Interleaved stereo f32 PCM output ring buffer.
    pub(crate) ring: StereoPcmRingBuffer,
    /// Global output mute toggle.
    #[get(type(copy))]
    pub(crate) muted: bool,
    /// Monotonic voice creation sequence counter.
    #[get(type(copy))]
    pub(crate) next_birth_order: u64,
    /// Cumulative stereo frames synthesized.
    #[get(type(copy))]
    pub(crate) frames_mixed: u64,
    /// Cumulative `AudioCommand` items ingested.
    #[get(type(copy))]
    pub(crate) commands_ingested: u64,
    /// Cumulative samples limited by the anti-clipping stage.
    #[get(type(copy))]
    pub(crate) clipped_samples: u64,
    /// Peak post-limiter amplitude observed (`<= 1.0`).
    #[get(type(copy))]
    pub(crate) peak_amplitude: f32,
}

/// Native macOS CoreAudio AUHAL handle with RAII stop and disposal semantics.
#[cfg(target_os = "macos")]
#[derive(Data, Debug)]
pub(crate) struct CoreAudioAuhalHandle {
    /// Retained `AudioUnit` (`AudioComponentInstance`) raw pointer.
    #[get(type(copy))]
    pub(crate) audio_unit: *mut c_void,
    /// Boxed `Arc<Mutex<AudioMixerCore>>` raw pointer owned until after `AudioComponentInstanceDispose`.
    #[get(type(copy))]
    pub(crate) callback_context: *mut c_void,
    /// Whether `AudioOutputUnitStart` is currently active.
    #[get(type(copy))]
    pub(crate) running: bool,
}

/// Cross-platform wrapper holding an optional native macOS CoreAudio AUHAL handle.
#[cfg(target_os = "macos")]
#[derive(Data, Default)]
pub(crate) struct NativeAudioDriverSlot {
    /// Active macOS CoreAudio AUHAL driver handle when started natively.
    #[get(skip)]
    pub(crate) coreaudio: Option<CoreAudioAuhalHandle>,
}

/// Cross-platform placeholder slot on targets using deterministic software pull mixing.
#[cfg(not(target_os = "macos"))]
#[derive(Data, Default)]
pub(crate) struct NativeAudioDriverSlot {
    /// Marker state on targets using software pull mixing.
    #[get(type(copy))]
    pub(crate) active: bool,
}

/// CoreAudio `AudioComponentDescription` C ABI layout.
#[cfg(target_os = "macos")]
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct AudioComponentDescriptionC {
    /// Component type FourCC (`kAudioUnitType_Output`).
    pub(crate) component_type: u32,
    /// Component subtype FourCC (`kAudioUnitSubType_DefaultOutput`).
    pub(crate) component_sub_type: u32,
    /// Component manufacturer FourCC (`kAudioUnitManufacturer_Apple`).
    pub(crate) component_manufacturer: u32,
    /// Component flags (`0`).
    pub(crate) component_flags: u32,
    /// Component flags mask (`0`).
    pub(crate) component_flags_mask: u32,
}

/// CoreAudio `AudioStreamBasicDescription` C ABI layout.
#[cfg(target_os = "macos")]
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct AudioStreamBasicDescriptionC {
    /// Nominal sample rate in Hz.
    pub(crate) sample_rate: f64,
    /// Stream format FourCC (`'lpcm'`).
    pub(crate) format_id: u32,
    /// Format bitmask (`kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked`).
    pub(crate) format_flags: u32,
    /// Byte stride per packet.
    pub(crate) bytes_per_packet: u32,
    /// Frame count per packet (`1` for uncompressed PCM).
    pub(crate) frames_per_packet: u32,
    /// Byte stride per stereo frame.
    pub(crate) bytes_per_frame: u32,
    /// Channels per frame (`2` for stereo).
    pub(crate) channels_per_frame: u32,
    /// Bit depth per channel (`32` for `f32`).
    pub(crate) bits_per_channel: u32,
    /// Reserved C struct padding (`0`).
    pub(crate) reserved: u32,
}

/// CoreAudio `AURenderCallbackStruct` C ABI layout.
#[cfg(target_os = "macos")]
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) struct AuRenderCallbackStructC {
    /// Real-time render callback entry point invoked by CoreAudio.
    pub(crate) input_proc: unsafe extern "C" fn(
        *mut c_void,
        *mut u32,
        *const c_void,
        u32,
        u32,
        *mut AudioBufferListC,
    ) -> i32,
    /// Opaque pointer to the boxed `Arc<Mutex<AudioMixerCore>>`.
    pub(crate) input_proc_ref_con: *mut c_void,
}

/// CoreAudio `AudioBuffer` C ABI layout.
#[cfg(target_os = "macos")]
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) struct AudioBufferC {
    /// Interleaved channel count stored in `data`.
    pub(crate) number_channels: u32,
    /// Buffer byte length.
    pub(crate) data_byte_size: u32,
    /// Raw pointer to destination PCM sample buffer.
    pub(crate) data: *mut c_void,
}

/// CoreAudio `AudioBufferList` C ABI layout.
#[cfg(target_os = "macos")]
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) struct AudioBufferListC {
    /// Count of `AudioBuffer` descriptors present in the variable-length tail.
    pub(crate) number_buffers: u32,
    /// First `AudioBuffer` descriptor.
    pub(crate) buffers: [AudioBufferC; 1],
}

/// Production stereo `f32` PCM audio mixer draining `AudioBus` with native macOS CoreAudio AUHAL output and deterministic pull mode.
#[derive(Data)]
pub struct AudioOutputBackend {
    /// Active mixer configuration.
    #[get(type(copy))]
    pub(crate) config: AudioOutputConfig,
    /// Shared mixer core and stereo PCM ring buffer.
    #[get(pub(crate))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) core: Arc<Mutex<AudioMixerCore>>,
    /// Active driver kind (`PullMixer` or verified `CoreAudioAuhal`).
    #[get(type(copy))]
    pub(crate) driver_kind: AudioDriverKind,
    /// Current backend lifecycle state.
    #[get(type(copy))]
    pub(crate) state: AudioOutputState,
    /// Optional native OS hardware output handle.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) native_slot: NativeAudioDriverSlot,
}