Skip to main content

VolumeControl

Struct VolumeControl 

Source
pub struct VolumeControl { /* private fields */ }
Expand description

Handle to the DAC’s hardware volume control.

Cheap to clone. Calls serialize with each other and with the library’s own volume I/O. Writes are verified by reading the level back; a failed multi-channel write restores the channels already written.

Implementations§

Source§

impl VolumeControl

Source

pub fn nonblocking(&self) -> Result<AsyncVolumeControl>

Create a bounded, ordered worker for control I/O. Keep one controller per device; requests never block the submitting or async executor thread. Dropping a request does not cancel a write already accepted by the worker.

Examples found in repository?
examples/sdk_host_boundary.rs (line 27)
26async fn change(volume: VolumeControl) -> Result<()> {
27    let controls = volume.nonblocking()?;
28    let applied = controls.set_db(-45.0)?.await?;
29    assert!(applied.db <= 0.0);
30    Ok(())
31}
Source

pub fn range(&self) -> Result<VolumeRange>

The writable range, or Error::Control if the control disappeared (for example the active stream moved to another terminal).

Source

pub fn get(&self) -> Result<VolumeState>

Reads the live level from the device.

Examples found in repository?
examples/guide_player.rs (line 330)
326pub fn restore_level(volume: &VolumeControl, remembered_db: Option<f64>) -> Result<f64> {
327    let ceiling = volume.startup().limit_db;
328    match remembered_db {
329        Some(db) => volume.set_db(db.min(ceiling)).map(|state| state.db),
330        None => volume.get().map(|state| state.db),
331    }
332}
Source

pub fn set_db(&self, db: f64) -> Result<VolumeState>

Sets the level in dB (NEG_INFINITY selects the verified raw minimum). The value is clamped to the range and snapped to the nearest step the DAC supports (a tie goes to the quieter one). Returns the level read back.

Examples found in repository?
examples/guide_player.rs (line 329)
326pub fn restore_level(volume: &VolumeControl, remembered_db: Option<f64>) -> Result<f64> {
327    let ceiling = volume.startup().limit_db;
328    match remembered_db {
329        Some(db) => volume.set_db(db.min(ceiling)).map(|state| state.db),
330        None => volume.get().map(|state| state.db),
331    }
332}
Source

pub fn set_fraction(&self, fraction: f64) -> Result<VolumeState>

Sets the level on the 0..1 curve (see VolumeState::fraction). The exact inverse of the read-back mapping: feeding a read-back fraction back in reproduces the same step.

Source

pub fn step(&self, direction: i8, steps: u8) -> Result<VolumeState>

Moves the level up (direction > 0) or down (direction < 0) by 1/steps of the 0..1 curve, always by at least one DAC step. steps == 0 selects DEFAULT_VOLUME_STEPS. A direction of 0 only reads the level.

Examples found in repository?
examples/guide_player.rs (line 338)
336pub fn nudge(volume: &VolumeControl, up: bool) -> Result<f64> {
337    volume
338        .step(if up { 1 } else { -1 }, 0)
339        .map(|state| state.db)
340}
Source

pub fn set_mute(&self, muted: bool) -> Result<VolumeState>

Sets the master mute. Error::Control if the DAC has no writable mute or the readback disagrees.

Source

pub fn startup(&self) -> StartupVolumeReport

What the library did with the DAC’s level when it was opened, before any stream could start: the level each channel had, the level it has now and the limit a host should not restore above. See StartupVolumeReport.

On connect a host reads this, then restores the level it remembers for this DAC capped by limit_db, or leaves the attenuated level.

Examples found in repository?
examples/guide_player.rs (line 327)
326pub fn restore_level(volume: &VolumeControl, remembered_db: Option<f64>) -> Result<f64> {
327    let ceiling = volume.startup().limit_db;
328    match remembered_db {
329        Some(db) => volume.set_db(db.min(ceiling)).map(|state| state.db),
330        None => volume.get().map(|state| state.db),
331    }
332}
Source

pub fn original_db(&self) -> Option<f64>

The level the DAC had before the library first wrote to it, in dB, if it was recorded (all channels agreed).

Source

pub fn hand_back(&self) -> Result<bool>

Writes the original level back now and ends the record. Ok(false) if there was nothing to restore or it was already in place.

Trait Implementations§

Source§

impl Clone for VolumeControl

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for VolumeControl

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.