Pcm

Struct Pcm 

Source
pub struct Pcm { /* private fields */ }
Available on crate feature alsa only.
Expand description

An opened PCM device.

Implementations§

Source§

impl Pcm

Source

pub fn open(name: &CStr, stream: Stream) -> Result<Self>

Open the given pcm device identified by name.

§Examples
use audio_device::alsa;
use std::ffi::CStr;

let name = CStr::from_bytes_with_nul(b"hw:0\0")?;

let pcm = alsa::Pcm::open(name, alsa::Stream::Playback)?;
Source

pub fn open_default(stream: Stream) -> Result<Self>

Open the default pcm device.

§Examples
use audio_device::alsa;
use std::ffi::CStr;

let pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
Source

pub fn open_nonblocking(name: &CStr, stream: Stream) -> Result<Self>

Open the given pcm device identified by name in a nonblocking manner.

§Examples
use audio_device::alsa;
use std::ffi::CStr;

let name = CStr::from_bytes_with_nul(b"hw:0\0")?;

let pcm = alsa::Pcm::open_nonblocking(name, alsa::Stream::Playback)?;
Source

pub fn open_default_nonblocking(stream: Stream) -> Result<Self>

Open the default pcm device in a nonblocking mode.

§Examples
use audio_device::alsa;
use std::ffi::CStr;

let pcm = alsa::Pcm::open_default_nonblocking(alsa::Stream::Playback)?;
Source

pub fn state(&self) -> State

Get the state of the PCM.

§Examples
use audio_device::alsa;

let pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
dbg!(pcm.state());
Source

pub fn configure<T>(&mut self) -> Configurator<'_, T>
where T: Sample,

Construct a simple stream Configurator.

It will be initialized with a set of default parameters which are usually suitable for simple playback or recording for the given sample type T.

See Configurator.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
let config = pcm.configure::<i16>().install()?;
Source

pub fn start(&mut self) -> Result<()>

Start a PCM.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
pcm.start()?;
Source

pub fn pause(&mut self) -> Result<()>

Pause a PCM.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
pcm.pause()?;
Source

pub fn resume(&mut self) -> Result<()>

Resume a PCM.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
pcm.resume()?;
Source

pub fn hardware_parameters_any(&mut self) -> Result<HardwareParametersMut<'_>>

Open all available hardware parameters for the current handle.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
let mut hw = pcm.hardware_parameters_any()?;
hw.set_rate_last()?;
hw.install()?;
Source

pub fn hardware_parameters_mut(&mut self) -> Result<HardwareParametersMut<'_>>

Open current hardware parameters for the current handle for mutable access.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;

let mut hw = pcm.hardware_parameters_mut()?;
let actual_rate = hw.set_rate(44100, alsa::Direction::Nearest)?;
hw.install()?;

dbg!(actual_rate);
Source

pub fn hardware_parameters(&mut self) -> Result<HardwareParameters>

Open current hardware parameters for the current handle.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
let sw = pcm.hardware_parameters()?;
dbg!(sw.rate()?);
Source

pub fn software_parameters(&mut self) -> Result<SoftwareParameters>

Open current software parameters for the current handle.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
let sw = pcm.software_parameters()?;

dbg!(sw.boundary()?);
Source

pub fn software_parameters_mut(&mut self) -> Result<SoftwareParametersMut<'_>>

Open current software parameters for the current handle for mutable access.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
let mut sw = pcm.software_parameters_mut()?;

sw.set_timestamp_mode(alsa::Timestamp::Enable)?;
sw.install()?;
Source

pub fn poll_descriptors_count(&mut self) -> usize

Get count of poll descriptors for PCM handle.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
let count = pcm.poll_descriptors_count();
dbg!(count);
Source

pub fn poll_descriptors_vec(&mut self, fds: &mut Vec<pollfd>) -> Result<()>

Get poll descriptors.

This function fills the given poll descriptor structs for the specified PCM handle. The poll desctiptor array should have the size returned by poll_descriptors_count() function.

The result is intended for direct use with the poll() syscall.

For reading the returned events of poll descriptor after poll() system call, use ::snd_pcm_poll_descriptors_revents() function. The field values in pollfd structs may be bogus regarding the stream direction from the application perspective (POLLIN might not imply read direction and POLLOUT might not imply write), but the poll_descriptors_revents() function does the right “demangling”.

You can use output from this function as arguments for the select() syscall, too. Do not forget to translate POLLIN and POLLOUT events to corresponding FD_SET arrays and demangle events using poll_descriptors_revents().

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;

let mut fds = Vec::with_capacity(pcm.poll_descriptors_count());
pcm.poll_descriptors_vec(&mut fds)?;
Source

pub fn poll_descriptors_revents( &mut self, fds: &mut [pollfd], ) -> Result<PollFlags>

Get returned events from poll descriptors.

This function does “demangling” of the revents mask returned from the poll() syscall to correct semantics (PollFlags::POLLIN = read, PollFlags::POLLOUT = write).

Note: The null event also exists. Even if poll() or select() syscall returned that some events are waiting, this function might return empty set of events. In this case, application should do next event waiting using poll() or select().

Note: Even if multiple poll descriptors are used (i.e. fds.len() > 1), this function returns only a single event.

Source

pub unsafe fn write_interleaved_unchecked( &mut self, buf: *const c_void, len: c_ulong, ) -> Result<c_long>

Write unchecked interleaved frames to a PCM.

Note: that the len must be the number of frames in the buf which does not account for the number of channels. So if len is 100, and the number of configured channels is 2, the buf must contain at least 200 bytes.

See HardwareParameters::channels.

Source

pub fn writer<T>(&mut self) -> Result<Writer<'_, T>>
where T: Sample,

Construct a checked safe writer with the given number of channels and the specified sample type.

This will error if the type T is not appropriate for this device, or if the number of channels does not match the number of configured channels.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
let config = pcm.configure::<i16>().install()?;

let mut writer = pcm.writer::<i16>()?;
// use writer with the resulting config.
Source

pub fn async_writer<T>(&mut self) -> Result<AsyncWriter<'_, T>>
where T: Sample,

Available on crate feature poll-driver only.

Construct a checked safe writer with the given number of channels and the specified sample type.

This will error if the type T is not appropriate for this device, or if the number of channels does not match the number of configured channels.

§Panics

Panics if the audio runtime is not available.

See Runtime for more.

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;
let config = pcm.configure::<i16>().install()?;

let mut writer = pcm.writer::<i16>()?;
// use writer with the resulting config.
Source

pub fn available_update(&mut self) -> Result<usize>

Return number of frames ready to be read (capture) / written (playback).

§Examples
use audio_device::alsa;

let mut pcm = alsa::Pcm::open_default(alsa::Stream::Playback)?;

let avail = pcm.available_update()?;
dbg!(avail);

Trait Implementations§

Source§

impl Drop for Pcm

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Pcm

Auto Trait Implementations§

§

impl Freeze for Pcm

§

impl RefUnwindSafe for Pcm

§

impl !Sync for Pcm

§

impl Unpin for Pcm

§

impl UnwindSafe for Pcm

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<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, 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.