Clean up alsa stragglers and typos

This commit is contained in:
ashthespy 2018-10-16 16:46:26 +02:00
parent 0e1147077c
commit a80bf86a2b
2 changed files with 35 additions and 54 deletions

View file

@ -10,11 +10,13 @@ use std::process::exit;
pub struct AlsaSink(Option<PCM>, String);
fn list_outputs() {
for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] {
for t in &["pcm", "ctl", "hwdep"] {
println!("{} devices:", t);
let i = HintIter::new(None, &*CString::new(*t).unwrap()).unwrap();
for a in i {
println!(" {:?}", a)
if let Some(Direction::Playback) = a.direction {
println!("{:#?}", a)
}
}
}
}
@ -25,7 +27,8 @@ fn open_device(dev_name: &str) -> Result<(PCM), Box<Error>> {
// latency = period_size * periods / (rate * bytes_per_frame)
// For 16 Bit stereo data, one frame has a length of four bytes.
// 500ms = buffer_size / (44100 * 4)
// buffer_size = 0.5 * 44100 = 22050 frames
// buffer_size_bytes = 0.5 * 44100 / 4
// buffer_size_frames = 0.5 * 44100 = 22050
{
// Set hardware parameters: 44100 Hz / Stereo / 16 bit
let hwp = HwParams::any(&pcm)?;
@ -34,37 +37,16 @@ fn open_device(dev_name: &str) -> Result<(PCM), Box<Error>> {
hwp.set_format(Format::s16())?;
hwp.set_rate(44100, ValueOr::Nearest)?;
hwp.set_channels(2)?;
// hwp.set_period_size_near(256, ValueOr::Nearest)?;
hwp.set_buffer_size_near(11025 * 2)?; // ~ 0.25 x 2 s latency
hwp.set_buffer_size_near(22050)?; // ~ 0.5s latency
pcm.hw_params(&hwp)?;
}
// Additional software paramters + check
if env::var("LIBRESPOT_DEBUG").is_ok() {
let hwp = pcm.hw_params_current()?;
let swp = pcm.sw_params_current()?;
let (bufsize, periodsize) = (hwp.get_buffer_size()?, hwp.get_period_size()?);
let periods = hwp.get_periods()?;
info!(
"periods: {:?} buffer_size: {:?} period_size {:?}",
periods, bufsize, periodsize
);
// Not required now that buffer size is set properly
// swp.set_start_threshold(bufsize - periodsize)?;
// swp.set_avail_min(periodsize)?;
// pcm.sw_params(&swp).unwrap();
info!(
"Opened audio output {:?} with parameters: {:?}, {:?}",
dev_name, hwp, swp
);
}
Ok(pcm)
}
impl Open for AlsaSink {
fn open(device: Option<String>) -> AlsaSink {
fn open(device: Option<String>) -> AlsaSink {
info!("Using alsa sink");
let name = match device.as_ref().map(AsRef::as_ref) {
@ -116,7 +98,6 @@ impl Sink for AlsaSink {
match io.writei(&data) {
Ok(_) => (),
Err(err) => pcm.try_recover(err, false).unwrap(),
// Err(err) => println!("{:?}",err),
}
Ok(())

View file

@ -10,29 +10,31 @@ pub struct AlsaMixer {
}
impl AlsaMixer {
fn map_volume(&self, set_volume: Option<u16>) -> Result<(u16), Box<Error>> {
let mixer = alsa::mixer::Mixer::new(&self.config.card, false)?;
let sid = alsa::mixer::SelemId::new(&*self.config.mixer, self.config.index);
fn map_volume(&self, set_volume:Option<u16>) -> Result<(u16),Box<Error>> {
let mixer = alsa::mixer::Mixer::new(&self.config.card, false)?;
let sid = alsa::mixer::SelemId::new(&*self.config.mixer, self.config.index);
let selem = mixer.find_selem(&sid).expect("Coundn't find SelemId");
let selem = mixer.find_selem(&sid).expect("Couldn't find SelemId");
let (min, max) = selem.get_playback_volume_range();
let cur_vol = selem.get_playback_volume(alsa::mixer::SelemChannelId::mono()).expect("Couldn't get current volume");
let cur_vol = selem
.get_playback_volume(alsa::mixer::SelemChannelId::mono())
.expect("Couldn't get current volume");
let range = (max - min) as f64;
let new_vol:u16;
let new_vol: u16;
if let Some(vol) = set_volume {
let alsa_volume:i64 = ((vol as f64 / 0xFFFF as f64) * range) as i64 + min;
debug!("Maping volume {:?} [u16] ->> Alsa {:?} [i64]",vol,alsa_volume);
selem.set_playback_volume_all(alsa_volume).expect("Couldn't set alsa volume");
new_vol = vol; // Meh
let alsa_volume: i64 = ((vol as f64 / 0xFFFF as f64) * range) as i64 + min;
debug!("Mapping volume {:?} [u16] ->> alsa {:?} [i64]", vol, alsa_volume);
selem
.set_playback_volume_all(alsa_volume)
.expect("Couldn't set alsa volume");
new_vol = vol;
} else {
new_vol = (((cur_vol - min) as f64 / range) * 0xFFFF as f64) as u16;
debug!("Maping volume {:?} [u16] <<- Alsa {:?} [i64]",new_vol, cur_vol);
new_vol = (((cur_vol - min) as f64 / range) * 0xFFFF as f64) as u16;
debug!("Mapping volume {:?} [u16] <<- alsa {:?} [i64]", new_vol, cur_vol);
}
Ok(new_vol)
}
}
@ -47,26 +49,24 @@ impl Mixer for AlsaMixer {
AlsaMixer { config: config }
}
fn start(&self) {
}
fn start(&self) {}
fn stop(&self) {
}
fn stop(&self) {}
fn volume(&self) -> u16 {
match self.map_volume(None){
Ok(vol) => vol,
Err(e) => {
error!("Error getting volume for <{}>, {:?}",self.config.card, e);
0 }
match self.map_volume(None) {
Ok(vol) => vol,
Err(e) => {
error!("Error getting volume for <{}>, {:?}", self.config.card, e);
0
}
}
}
fn set_volume(&self, volume: u16) {
match self.map_volume(Some(volume)){
Ok(_) => (),
Err(e) => error!("Error setting volume for <{}>, {:?}",self.config.card, e),
match self.map_volume(Some(volume)) {
Ok(_) => (),
Err(e) => error!("Error setting volume for <{}>, {:?}", self.config.card, e),
}
}