From 46151e09cc8fe7bff77559c73e28b75c9128e144 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Tue, 22 Sep 2015 15:20:11 +0200 Subject: [PATCH] Use the correct format in ALSA --- src/alsa/mod.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/alsa/mod.rs b/src/alsa/mod.rs index 7dc6cc8..0a30bb3 100644 --- a/src/alsa/mod.rs +++ b/src/alsa/mod.rs @@ -34,6 +34,7 @@ impl Endpoint { let hw_params = HwParams::alloc(); check_errors(alsa::snd_pcm_hw_params_any(playback_handle, hw_params.0)).unwrap(); + // TODO: check endianess const FORMATS: [(SampleFormat, alsa::snd_pcm_format_t); 3] = [ //SND_PCM_FORMAT_S8, //SND_PCM_FORMAT_U8, @@ -192,7 +193,7 @@ impl Drop for HwParams { } impl Voice { - pub fn new(endpoint: &Endpoint, _format: &Format) -> Result { + pub fn new(endpoint: &Endpoint, format: &Format) -> Result { unsafe { let name = ffi::CString::new(endpoint.0.clone()).unwrap(); @@ -201,12 +202,19 @@ impl Voice { alsa::SND_PCM_STREAM_PLAYBACK, alsa::SND_PCM_NONBLOCK)).unwrap(); + // TODO: check endianess + let data_type = match format.data_type { + SampleFormat::I16 => alsa::SND_PCM_FORMAT_S16_LE, + SampleFormat::U16 => alsa::SND_PCM_FORMAT_U16_LE, + SampleFormat::F32 => alsa::SND_PCM_FORMAT_FLOAT_LE, + }; + let hw_params = HwParams::alloc(); check_errors(alsa::snd_pcm_hw_params_any(playback_handle, hw_params.0)).unwrap(); check_errors(alsa::snd_pcm_hw_params_set_access(playback_handle, hw_params.0, alsa::SND_PCM_ACCESS_RW_INTERLEAVED)).unwrap(); - check_errors(alsa::snd_pcm_hw_params_set_format(playback_handle, hw_params.0, alsa::SND_PCM_FORMAT_S16_LE)).unwrap(); // TODO: check endianess - check_errors(alsa::snd_pcm_hw_params_set_rate(playback_handle, hw_params.0, 44100, 0)).unwrap(); - check_errors(alsa::snd_pcm_hw_params_set_channels(playback_handle, hw_params.0, 2)).unwrap(); + check_errors(alsa::snd_pcm_hw_params_set_format(playback_handle, hw_params.0, data_type)).unwrap(); + check_errors(alsa::snd_pcm_hw_params_set_rate(playback_handle, hw_params.0, format.samples_rate.0 as libc::c_uint, 0)).unwrap(); + check_errors(alsa::snd_pcm_hw_params_set_channels(playback_handle, hw_params.0, format.channels.len() as libc::c_uint)).unwrap(); check_errors(alsa::snd_pcm_hw_params(playback_handle, hw_params.0)).unwrap(); @@ -214,7 +222,7 @@ impl Voice { Ok(Voice { channel: Mutex::new(playback_handle), - num_channels: 2, + num_channels: format.channels.len() as u16, }) } }