Module alsa::pcm [] [src]

Audio playback and capture

Example

Playback a sine wave through the "default" device.

use std::ffi::CString;
use alsa::{Direction, ValueOr};
use alsa::pcm::{PCM, HwParams, Format, Access, State};

// Open default playback device
let pcm = PCM::open(&*CString::new("default").unwrap(), 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 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

Chmap

snd_pcm_chmap_t wrapper

ChmapsQuery

Iterator over available channel maps - see snd_pcm_chmap_query_t

HwParams

snd_pcm_hw_params_t wrapper

IO

Sample format dependent struct for reading from and writing data to a PCM. Also implements std::io::Read and std::io::Write.

Info
PCM

snd_pcm_t wrapper - start here for audio playback and recording

Status

snd_pcm_status_t wrapper

SwParams

snd_pcm_sw_params_t wrapper

Enums

Access

SND_PCM_ACCESS_xxx constants

ChmapPosition

SND_CHMAP_xxx constants

ChmapType

SND_CHMAP_TYPE_xxx constants

Format

SND_PCM_FORMAT_xxx constants

State

SND_PCM_STATE_xxx constants

Type Definitions

Frames

snd_pcm_sframes_t