Expand description
Audio playback and capture
Example
Playback a sine wave through the “default” device.
use alsa::{Direction, ValueOr};
use alsa::pcm::{PCM, HwParams, Format, Access, State};
// Open default playback device
let pcm = PCM::new("default", Direction::Playback, false).unwrap();
// Set hardware parameters: 44100 Hz / Mono / 16 bit
let hwp = HwParams::any(&pcm).unwrap();
hwp.set_channels(1).unwrap();
hwp.set_rate(44100, ValueOr::Nearest).unwrap();
hwp.set_format(Format::s16()).unwrap();
hwp.set_access(Access::RWInterleaved).unwrap();
pcm.hw_params(&hwp).unwrap();
let io = pcm.io_i16().unwrap();
// Make sure we don't start the stream too early
let hwp = pcm.hw_params_current().unwrap();
let swp = pcm.sw_params_current().unwrap();
swp.set_start_threshold(hwp.get_buffer_size().unwrap()).unwrap();
pcm.sw_params(&swp).unwrap();
// Make a sine wave
let mut buf = [0i16; 1024];
for (i, a) in buf.iter_mut().enumerate() {
*a = ((i as f32 * 2.0 * ::std::f32::consts::PI / 128.0).sin() * 8192.0) as i16
}
// Play it back for 2 seconds.
for _ in 0..2*44100/1024 {
assert_eq!(io.writei(&buf[..]).unwrap(), 1024);
}
// In case the buffer was larger than 2 seconds, start the stream manually.
if pcm.state() != State::Running { pcm.start().unwrap() };
// Wait for the stream to finish playback.
pcm.drain().unwrap();
Structs
- snd_pcm_chmap_t wrapper
- Iterator over available channel maps - see snd_pcm_chmap_query_t
- snd_pcm_hw_params_t wrapper
- Sample format dependent struct for reading from and writing data to a
PCM
. Also implementsstd::io::Read
andstd::io::Write
. - snd_pcm_info_t wrapper - PCM generic info container
- snd_pcm_t wrapper - start here for audio playback and recording
- snd_pcm_status_t wrapper
- Builder for
Status
. - snd_pcm_sw_params_t wrapper
Enums
- SND_PCM_ACCESS_xxx constants
- SND_PCM_AUDIO_TSTAMP_TYPE_xxx constants
- SND_CHMAP_xxx constants
- SND_CHMAP_TYPE_xxx constants
- SND_PCM_FORMAT_xxx constants
- SND_PCM_STATE_xxx constants
- SND_PCM_TSTAMP_TYPE_xxx constants