Skip to content

Commit acaa047

Browse files
committed
[update] documentation.
1 parent 1a6793f commit acaa047

2 files changed

Lines changed: 232 additions & 0 deletions

File tree

crates/lambda-rs-platform/src/audio/cpal/device.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,37 @@ pub struct AudioCallbackInfo {
5050
/// underlying device output buffer for the current callback invocation.
5151
pub trait AudioOutputWriter {
5252
/// Return the output channel count for the current callback invocation.
53+
///
54+
/// # Returns
55+
/// The number of interleaved channels in the output buffer.
5356
fn channels(&self) -> u16;
5457
/// Return the number of frames in the output buffer for the current callback
5558
/// invocation.
59+
///
60+
/// # Returns
61+
/// The number of frames in the output buffer.
5662
fn frames(&self) -> usize;
5763
/// Clear the entire output buffer to silence.
64+
///
65+
/// # Returns
66+
/// `()` after clearing the output buffer to silence.
5867
fn clear(&mut self);
5968

6069
/// Write a normalized sample in the range `[-1.0, 1.0]`.
6170
///
6271
/// Implementations MUST clamp values outside `[-1.0, 1.0]`. Implementations
6372
/// MUST NOT panic for out-of-range indices and MUST perform no write in that
6473
/// case.
74+
///
75+
/// # Arguments
76+
/// - `frame_index`: The target frame index within the current callback
77+
/// buffer.
78+
/// - `channel_index`: The target channel index within the current callback
79+
/// buffer.
80+
/// - `sample`: A normalized sample in nominal range `[-1.0, 1.0]`.
81+
///
82+
/// # Returns
83+
/// `()` after attempting to write the sample.
6584
fn set_sample(
6685
&mut self,
6786
frame_index: usize,
@@ -132,6 +151,14 @@ impl<'buffer> InterleavedAudioOutputWriter<'buffer> {
132151
/// `channels` MUST match the channel count encoded in the output stream
133152
/// configuration. The frame count is derived from the buffer length and
134153
/// channel count.
154+
///
155+
/// # Arguments
156+
/// - `channels`: Interleaved output channel count.
157+
/// - `buffer`: A typed interleaved output buffer view for the current audio
158+
/// callback.
159+
///
160+
/// # Returns
161+
/// A writer that can clear and write normalized samples into `buffer`.
135162
#[allow(dead_code)]
136163
pub fn new(channels: u16, buffer: AudioOutputBuffer<'buffer>) -> Self {
137164
let channels_usize = channels as usize;
@@ -149,13 +176,22 @@ impl<'buffer> InterleavedAudioOutputWriter<'buffer> {
149176
}
150177

151178
/// Return the sample format of the current callback buffer.
179+
///
180+
/// # Returns
181+
/// The typed sample format for the current callback buffer.
152182
#[allow(dead_code)]
153183
pub fn sample_format(&self) -> AudioSampleFormat {
154184
return self.buffer.sample_format();
155185
}
156186
}
157187

158188
/// Clamp a normalized audio sample to the nominal output range `[-1.0, 1.0]`.
189+
///
190+
/// # Arguments
191+
/// - `sample`: A potentially out-of-range normalized sample.
192+
///
193+
/// # Returns
194+
/// The clamped sample in range `[-1.0, 1.0]`.
159195
#[allow(dead_code)]
160196
fn clamp_normalized_sample(sample: f32) -> f32 {
161197
if sample > 1.0 {
@@ -381,6 +417,9 @@ pub struct AudioDeviceBuilder {
381417

382418
impl AudioDeviceBuilder {
383419
/// Create a builder with engine defaults.
420+
///
421+
/// # Returns
422+
/// A builder with no explicit configuration requests.
384423
pub fn new() -> Self {
385424
return Self {
386425
sample_rate: None,
@@ -390,25 +429,54 @@ impl AudioDeviceBuilder {
390429
}
391430

392431
/// Request a specific sample rate (Hz).
432+
///
433+
/// # Arguments
434+
/// - `rate`: Requested sample rate in Hz.
435+
///
436+
/// # Returns
437+
/// The updated builder.
393438
pub fn with_sample_rate(mut self, rate: u32) -> Self {
394439
self.sample_rate = Some(rate);
395440
return self;
396441
}
397442

398443
/// Request a specific channel count.
444+
///
445+
/// # Arguments
446+
/// - `channels`: Requested interleaved channel count.
447+
///
448+
/// # Returns
449+
/// The updated builder.
399450
pub fn with_channels(mut self, channels: u16) -> Self {
400451
self.channels = Some(channels);
401452
return self;
402453
}
403454

404455
/// Attach a label for diagnostics.
456+
///
457+
/// # Arguments
458+
/// - `label`: A human-readable label used for diagnostics.
459+
///
460+
/// # Returns
461+
/// The updated builder.
405462
pub fn with_label(mut self, label: &str) -> Self {
406463
self.label = Some(label.to_string());
407464
return self;
408465
}
409466

410467
/// Initialize the default audio output device using the requested
411468
/// configuration.
469+
///
470+
/// This method selects a supported output configuration from the default
471+
/// output device and starts an output stream that plays silence.
472+
///
473+
/// # Returns
474+
/// An initialized audio output device handle. Dropping the handle stops
475+
/// output.
476+
///
477+
/// # Errors
478+
/// Returns an error when the host, device, or stream cannot be initialized,
479+
/// or when no supported output configuration satisfies the request.
412480
pub fn build(self) -> Result<AudioDevice, AudioError> {
413481
if let Some(sample_rate) = self.sample_rate {
414482
if sample_rate == 0 {
@@ -531,6 +599,20 @@ impl AudioDeviceBuilder {
531599
}
532600

533601
/// Initialize the default audio output device and play audio via a callback.
602+
///
603+
/// The callback is invoked from the platform audio thread. The callback MUST
604+
/// avoid blocking and MUST NOT allocate.
605+
///
606+
/// # Arguments
607+
/// - `callback`: A real-time callback invoked for each output buffer tick.
608+
///
609+
/// # Returns
610+
/// An initialized audio output device handle. Dropping the handle stops
611+
/// output.
612+
///
613+
/// # Errors
614+
/// Returns an error when the host, device, or stream cannot be initialized,
615+
/// or when no supported output configuration satisfies the request.
534616
pub fn build_with_output_callback<Callback>(
535617
self,
536618
callback: Callback,
@@ -695,6 +777,15 @@ impl Default for AudioDeviceBuilder {
695777
/// - Wraps the typed `cpal` output slice in an [`AudioOutputWriter`].
696778
/// - Clears the buffer to silence before invoking the callback.
697779
/// - Guarantees a single callback invocation per platform callback tick.
780+
///
781+
/// # Arguments
782+
/// - `channels`: Interleaved channel count for the output buffer.
783+
/// - `buffer`: A typed interleaved output buffer view.
784+
/// - `callback_info`: Stream metadata for the current callback invocation.
785+
/// - `callback`: The engine callback to invoke.
786+
///
787+
/// # Returns
788+
/// `()` after clearing the buffer and invoking the callback.
698789
fn invoke_output_callback_on_buffer<Callback>(
699790
channels: u16,
700791
buffer: AudioOutputBuffer<'_>,
@@ -713,6 +804,12 @@ fn invoke_output_callback_on_buffer<Callback>(
713804
///
714805
/// The current backend prefers `f32`, then `i16`, then `u16`. Any other format
715806
/// is treated as unsupported by this abstraction.
807+
///
808+
/// # Arguments
809+
/// - `sample_format`: The `cpal` sample format for a supported stream config.
810+
///
811+
/// # Returns
812+
/// A priority value where higher values are preferred.
716813
fn sample_format_priority(sample_format: cpal_backend::SampleFormat) -> u8 {
717814
match sample_format {
718815
cpal_backend::SampleFormat::F32 => {
@@ -741,6 +838,19 @@ fn sample_format_priority(sample_format: cpal_backend::SampleFormat) -> u8 {
741838
/// - Higher-quality sample formats are preferred (`f32` > `i16` > `u16`).
742839
/// - When priorities tie, the configuration with sample rate closest to
743840
/// `48_000` Hz is preferred.
841+
///
842+
/// # Arguments
843+
/// - `supported_configs`: The device-provided supported config ranges.
844+
/// - `requested_sample_rate`: Optional exact sample rate request.
845+
/// - `requested_channels`: Optional exact channel count request.
846+
///
847+
/// # Returns
848+
/// A supported stream configuration selected from `supported_configs`.
849+
///
850+
/// # Errors
851+
/// Returns [`AudioError::UnsupportedConfig`] when no configuration satisfies
852+
/// the request. Returns [`AudioError::UnsupportedSampleFormat`] when the device
853+
/// does not expose any supported sample format among `f32`, `i16`, and `u16`.
744854
fn select_output_stream_config(
745855
supported_configs: &[cpal_backend::SupportedStreamConfigRange],
746856
requested_sample_rate: Option<u32>,
@@ -834,6 +944,13 @@ fn select_output_stream_config(
834944
}
835945

836946
/// Enumerate available audio output devices.
947+
///
948+
/// # Returns
949+
/// A list of available output devices with stable metadata.
950+
///
951+
/// # Errors
952+
/// Returns an error if the platform host cannot enumerate output devices or if
953+
/// device metadata cannot be retrieved.
837954
pub fn enumerate_devices() -> Result<Vec<AudioDeviceInfo>, AudioError> {
838955
let host = cpal_backend::default_host();
839956

0 commit comments

Comments
 (0)