Struct OutputStreamBuilder

Source
pub struct OutputStreamBuilder<E = fn(StreamError)>
where E: FnMut(StreamError) + Send + 'static,
{ /* private fields */ }
Expand description

Convenience builder for audio output stream. It provides methods to configure several parameters of the audio output and opening default device. See examples for use-cases.

When the OutputStream is dropped playback will end, and the associated output stream will be disposed

Implementations§

Source§

impl OutputStreamBuilder

Source

pub fn from_device(device: Device) -> Result<OutputStreamBuilder, StreamError>

Sets output device and its default parameters.

Source

pub fn from_default_device() -> Result<OutputStreamBuilder, StreamError>

Sets default output stream parameters for default output audio device.

Source

pub fn open_default_stream() -> Result<OutputStream, StreamError>

Try to open a new output stream for the default output device with its default configuration. Failing that attempt to open output stream with alternative configuration and/or non default output devices. Returns stream for first of the tried configurations that succeeds. If all attempts fail return the initial error.

Source§

impl<E> OutputStreamBuilder<E>
where E: FnMut(StreamError) + Send + 'static,

Source

pub fn with_device(self, device: Device) -> OutputStreamBuilder<E>

Sets output audio device keeping all existing stream parameters intact. This method is useful if you want to set other parameters yourself. To also set parameters that are appropriate for the device use Self::from_device() instead.

Source

pub fn with_channels( self, channel_count: ChannelCount, ) -> OutputStreamBuilder<E>

Sets number of output stream’s channels.

Source

pub fn with_sample_rate(self, sample_rate: SampleRate) -> OutputStreamBuilder<E>

Sets output stream’s sample rate.

Source

pub fn with_buffer_size(self, buffer_size: BufferSize) -> OutputStreamBuilder<E>

Sets preferred output buffer size.

To play sound without any glitches the audio card may never receive a sample to late. Some samples might take longer to generate then others. For example because:

  • The OS preempts the thread creating the samples. This happens more often if the computer is under high load.
  • The decoder needs to read more data from disk.
  • Rodio code takes longer to run for some samples then others
  • The OS can only send audio samples in groups to the DAC.

The OS solves this by buffering samples. The larger that buffer the smaller the impact of variable sample generation time. On the other hand Rodio controls audio by changing the value of samples. We can not change a sample already in the OS buffer. That means there is a minimum delay (latency) of <buffer size>/<sample_rate*channel_count> seconds before a change made through rodio takes effect.

§Large vs Small buffer
  • A larger buffer size results in high latency. Changes made trough Rodio (volume/skip/effects etc) takes longer before they can be heard.
  • A small buffer might cause:
    • Higher CPU usage
    • Playback interruptions such as buffer underruns.
    • Rodio to log errors like: alsa::poll() returned POLLERR
§Recommendation

If low latency is important to you consider offering the user a method to find the minimum buffer size that works well on their system under expected conditions. A good example of this approach can be seen in mumble (specifically the Output Delay & Jitter buffer.

These are some typical values that are a good starting point. They may also break audio completely, it depends on the system.

  • Low-latency (audio production, live monitoring): 512-1024
  • General use (games, media playback): 1024-2048
  • Stability-focused (background music, non-interactive): 2048-4096
Source

pub fn with_sample_format( self, sample_format: SampleFormat, ) -> OutputStreamBuilder<E>

Select scalar type that will carry a sample.

Source

pub fn with_supported_config( self, config: &SupportedStreamConfig, ) -> OutputStreamBuilder<E>

Set available parameters from a CPAL supported config. You can get a list of such configurations for an output device using crate::stream::supported_output_configs()

Source

pub fn with_config(self, config: &StreamConfig) -> OutputStreamBuilder<E>

Set all output stream parameters at once from CPAL stream config.

Source

pub fn with_error_callback<F>(self, callback: F) -> OutputStreamBuilder<F>
where F: FnMut(StreamError) + Send + 'static,

Set a callback that will be called when an error occurs with the stream

Source

pub fn open_stream(self) -> Result<OutputStream, StreamError>

Open output stream using parameters configured so far.

Source

pub fn open_stream_or_fallback(&self) -> Result<OutputStream, StreamError>
where E: Clone,

Try opening a new output stream with the builder’s current stream configuration. Failing that attempt to open stream with other available configurations supported by the device. If all attempts fail returns initial error.

Trait Implementations§

Source§

impl Debug for OutputStreamBuilder

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for OutputStreamBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<E> Freeze for OutputStreamBuilder<E>
where E: Freeze,

§

impl<E> RefUnwindSafe for OutputStreamBuilder<E>
where E: RefUnwindSafe,

§

impl<E> Send for OutputStreamBuilder<E>

§

impl<E> Sync for OutputStreamBuilder<E>
where E: Sync,

§

impl<E> Unpin for OutputStreamBuilder<E>
where E: Unpin,

§

impl<E> UnwindSafe for OutputStreamBuilder<E>
where E: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,