pub struct AudioRef<'a, S: FrameFree<RawFrame = NDIlib_audio_frame_v3_t>> { /* private fields */ }Expand description
A zero-copy borrowed audio frame, generic over its free strategy S.
This type wraps an RAII Guard that owns the NDI frame buffer lifetime,
exposing a safe, zero-copy view of the audio data. The frame is automatically
freed when dropped, via whichever NDIlib_*_free_audio* call the strategy S
encodes.
The shared accessors and the Debug impl are written exactly once here; the
two public spellings are aliases that layer their differing format/owned/empty
policy on top:
AudioFrameRef<'rx>— frames captured from aReceiver(always concrete:format() -> AudioFormat,to_owned() -> AudioFrame).FrameSyncAudioRef<'fs>— frames captured from aFrameSync, which additionally model a validated empty query/no-source state (is_empty,Option-returningformat/to_owned).
Key characteristics:
- Zero allocations: References NDI SDK buffers directly
- Zero copies: No memcpy of audio samples
- RAII lifetime: Exactly one free per frame, enforced at compile time
- Not
Send: Prevents accidental cross-thread use of FFI buffers
Implementations§
Source§impl<'a, S: FrameFree<RawFrame = NDIlib_audio_frame_v3_t>> AudioRef<'a, S>
impl<'a, S: FrameFree<RawFrame = NDIlib_audio_frame_v3_t>> AudioRef<'a, S>
Sourcepub fn sample_rate(&self) -> i32
pub fn sample_rate(&self) -> i32
Get the sample rate in Hz.
Sourcepub fn num_channels(&self) -> i32
pub fn num_channels(&self) -> i32
Get the number of audio channels.
Sourcepub fn num_samples(&self) -> i32
pub fn num_samples(&self) -> i32
Get the number of samples per channel.
Sourcepub fn channel_stride_in_bytes(&self) -> i32
pub fn channel_stride_in_bytes(&self) -> i32
Get the channel stride in bytes.
Sourcepub fn data(&self) -> &[f32]
pub fn data(&self) -> &[f32]
Get a zero-copy view of the audio data as 32-bit floats.
This returns a slice directly into the NDI SDK’s buffer. No allocation or memcpy is performed.
§Safety Guarantee
The slice length is computed once at construction time using checked
arithmetic and validated against MAX_AUDIO_BYTES. This eliminates
the possibility of integer overflow or unbounded slice creation. A
validated empty (query/no-source) layout yields an empty slice.
Sourcepub fn channel_data(&self, channel: usize) -> Option<&[f32]>
pub fn channel_data(&self, channel: usize) -> Option<&[f32]>
Get the zero-copy samples for a single channel.
This respects channel_stride_in_bytes, so it works for tightly packed
and strided planar FLTP audio.
Source§impl<'rx> AudioRef<'rx, AudioKind>
impl<'rx> AudioRef<'rx, AudioKind>
Sourcepub fn format(&self) -> AudioFormat
pub fn format(&self) -> AudioFormat
Get the audio format (FourCC code).
This is guaranteed to be a valid, supported format since it’s validated during construction.
Sourcepub fn to_owned(&self) -> Result<AudioFrame>
pub fn to_owned(&self) -> Result<AudioFrame>
Convert this borrowed frame to an owned AudioFrame.
This performs a single memcpy of the audio data and metadata, allowing the frame to outlive the NDI buffer and be sent across threads.
Source§impl<'fs> AudioRef<'fs, FrameSyncAudioFree>
impl<'fs> AudioRef<'fs, FrameSyncAudioFree>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true for a valid query/no-source state with no sample buffer.
Sourcepub fn format(&self) -> Option<AudioFormat>
pub fn format(&self) -> Option<AudioFormat>
Get the audio format (FourCC code), or None for an empty query/no-source state.
Sourcepub fn to_owned(&self) -> Result<Option<AudioFrame>>
pub fn to_owned(&self) -> Result<Option<AudioFrame>>
Convert this borrowed frame to an owned AudioFrame.
This performs a single memcpy of the audio data and metadata, allowing the frame to outlive the NDI buffer and be sent across threads.
Returns Ok(None) for a valid query/no-source state with no sample
buffer to own.