Module alsa::direct::pcm

source ·
Expand description

This module bypasses alsa-lib and directly read and write into memory mapped kernel memory. In case of the sample memory, this is in many cases the DMA buffers that is transferred to the sound card.

The reasons for doing this are:

  • Minimum overhead where it matters most: let alsa-lib do the code heavy setup - then steal its file descriptor and deal with sample streaming from Rust.
  • RT-safety to the maximum extent possible. Creating/dropping any of these structs causes syscalls, but function calls on these are just read and write from memory. No syscalls, no memory allocations, not even loops (with the exception of MmapPlayback::write that loops over samples to write).
  • Possibility to allow Send + Sync for structs
  • It’s a fun experiment and an interesting deep dive into how alsa-lib does things.

Note: Not all sound card drivers support this direct method of communication; although almost all modern/common ones do. It only works with hardware devices though (such as “hw:xxx” device strings), don’t expect it to work with, e g, the PulseAudio plugin or so.

For an example of how to use this mode, look in the “synth-example” directory.

Structs

  • Dummy struct for better generics
  • Iterator over captured samples
  • Write PCM appl ptr directly, bypassing alsa-lib.
  • Struct containing direct I/O functions shared between playback and capture.
  • Dummy struct for better generics
  • A raw pointer to samples, and the amount of samples readable or writable.
  • Read PCM status directly from memory, bypassing alsa-lib.
  • Read PCM status via a simple kernel syscall, bypassing alsa-lib.

Traits

  • Dummy trait for better generics

Type Aliases