Rework alsa hw and mixer parameters

This commit is contained in:
ashthespy 2018-09-01 02:42:50 +02:00
parent 08cfb1516d
commit 99106c5ae3
2 changed files with 114 additions and 62 deletions

View file

@ -1,20 +1,66 @@
use super::{Open, Sink}; use super::{Open, Sink};
use alsa::{Direction, Error, ValueOr};
use alsa::device_name::HintIter; use alsa::device_name::HintIter;
use std::ffi::{CStr, CString};
use alsa::pcm::{Access, Format, HwParams, PCM}; use alsa::pcm::{Access, Format, HwParams, PCM};
use alsa::{Direction, Error, ValueOr};
use std::env;
use std::ffi::CString;
use std::io; use std::io;
use std::process::exit; use std::process::exit;
pub struct AlsaSink(Option<PCM>, String); pub struct AlsaSink(Option<PCM>, String);
fn list_outputs() { fn list_outputs() {
for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] { for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] {
println!("{} devices:", t); println!("{} devices:", t);
let i = HintIter::new(None, &*CString::new(*t).unwrap()).unwrap(); let i = HintIter::new(None, &*CString::new(*t).unwrap()).unwrap();
for a in i { println!(" {:?}", a) } for a in i {
} println!(" {:?}", a)
}
}
}
fn open_device(dev_name: &str) -> Result<(PCM), Box<Error>> {
let pcm = PCM::new(dev_name, Direction::Playback, false)?;
// http://www.linuxjournal.com/article/6735?page=0,1#N0x19ab2890.0x19ba78d8
// 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
{
// Set hardware parameters: 44100 Hz / Stereo / 16 bit
let hwp = HwParams::any(&pcm)?;
hwp.set_access(Access::RWInterleaved)?;
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
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 { impl Open for AlsaSink {
@ -23,6 +69,7 @@ impl Open for AlsaSink {
let name = match device.as_ref().map(AsRef::as_ref) { let name = match device.as_ref().map(AsRef::as_ref) {
Some("?") => { Some("?") => {
println!("Listing available alsa outputs");
list_outputs(); list_outputs();
exit(0) exit(0)
} }
@ -37,20 +84,17 @@ impl Open for AlsaSink {
impl Sink for AlsaSink { impl Sink for AlsaSink {
fn start(&mut self) -> io::Result<()> { fn start(&mut self) -> io::Result<()> {
if self.0.is_none() { if self.0.is_none() {
let pcm = PCM::new(&*self.1, Direction::Playback, false).unwrap(); let pcm = open_device(&self.1);
{ match pcm {
// Set hardware parameters: 44100 Hz / Stereo / 16 bit Ok(p) => self.0 = Some(p),
let hwp = HwParams::any(&pcm).unwrap(); Err(e) => {
hwp.set_channels(2).unwrap(); error!("Alsa error PCM open {}", e);
hwp.set_rate(44100, ValueOr::Nearest).unwrap(); return Err(io::Error::new(
hwp.set_format(Format::s16()).unwrap(); io::ErrorKind::Other,
hwp.set_access(Access::RWInterleaved).unwrap(); "Alsa error: PCM open failed",
pcm.hw_params(&hwp).unwrap(); ));
println!("PCM status: {:?}, {:?}", pcm.state(), pcm.hw_params_current().unwrap())
} }
PCM::prepare(&pcm).unwrap(); }
self.0 = Some(pcm);
} }
Ok(()) Ok(())
@ -58,7 +102,7 @@ impl Sink for AlsaSink {
fn stop(&mut self) -> io::Result<()> { fn stop(&mut self) -> io::Result<()> {
{ {
let pcm = self.0.as_mut().unwrap(); let pcm = self.0.as_ref().unwrap();
pcm.drain().unwrap(); pcm.drain().unwrap();
} }
self.0 = None; self.0 = None;

View file

@ -1,5 +1,7 @@
use super::Mixer;
use super::AudioFilter; use super::AudioFilter;
use super::Mixer;
use std::env;
use std::error::Error;
use alsa; use alsa;
@ -7,27 +9,50 @@ use alsa;
pub struct AlsaMixer { pub struct AlsaMixer {
card: String, card: String,
mixer: String, mixer: String,
index: u32,
} }
// Doesn't work - Selem is borrowed from Mixer impl AlsaMixer {
// impl AlsaMixer {
// fn get_selem(&self ) -> Result<(alsa::mixer::Selem), Box<Error>> { fn map_volume(&self, set_volume:Option<u16>) -> Result<(u16),Box<Error>> {
// let mixer = alsa::mixer::Mixer::new(&self.card, false)?;
// let selem_id = alsa::mixer::SelemId::new(self.mixer, 0); let sid = alsa::mixer::SelemId::new(&*self.mixer, self.index);
// let mixer = alsa::mixer::Mixer::new(self.card, false)?;
// let selem = mixer.find_selem(&selem_id).unwrap(); let selem = mixer.find_selem(&sid).expect("Coundn't find SelemId");
// let (min, max) = selem.get_playback_volume_range();
// Ok((selem)) 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;
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
} else {
new_vol = (((cur_vol - min) as f64 / range) * 0xFFFF as f64) as u16;
debug!("Maping volume {:?} [u16] <<- Alsa {:?} [i64]",new_vol, cur_vol);
}
Ok(new_vol)
}
}
impl Mixer for AlsaMixer { impl Mixer for AlsaMixer {
fn open(device: Option<String>) -> AlsaMixer { fn open(device: Option<String>) -> AlsaMixer {
let card = device.unwrap_or(String::from("default")); let card = env::var("LIBRESPOT_CARD").unwrap_or(device.unwrap_or(String::from("default")));
let mixer = String::from("PCM"); let mixer = env::var("LIBRESPOT_MIXER").unwrap_or(String::from("PCM"));
let index: u32 = 0;
info!(
"Setting up new mixer: card:{} mixer:{} index:{}",
card, mixer, index
);
AlsaMixer { AlsaMixer {
card: card, card: card,
mixer: mixer, mixer: mixer,
index: index,
} }
} }
@ -38,37 +63,20 @@ impl Mixer for AlsaMixer {
} }
fn volume(&self) -> u16 { fn volume(&self) -> u16 {
let mixer = alsa::mixer::Mixer::new(&self.card, false).unwrap();
let selem_id = alsa::mixer::SelemId::new(&self.mixer, 0);
let selem = mixer.find_selem(&selem_id).unwrap();
let (min, max) = selem.get_playback_volume_range();
let volume: i64 = selem.get_playback_volume(alsa::mixer::SelemChannelId::FrontLeft).unwrap();
// Spotify uses a volume range from 0 to 65535, but the ALSA mixers resolution might match self.map_volume(None){
// differ, e.g. most ALSA mixers uses a resolution of 256. Therefore, we have to calculate Ok(vol) => vol,
// the multiplier to use, to get the corresponding Spotify volume value from the ALSA Err(e) => {
// mixers volume. error!("Error getting volume for <{}>, {:?}",self.card, e);
let resolution = max - min + 1; 0 }
let multiplier: u16 = (((0xFFFF + 1) / resolution) - 1) as u16; }
volume as u16 * multiplier
} }
fn set_volume(&self, volume: u16) { fn set_volume(&self, volume: u16) {
let mixer = alsa::mixer::Mixer::new(&self.card, false).unwrap(); match self.map_volume(Some(volume)){
let selem_id = alsa::mixer::SelemId::new(&self.mixer, 0); Ok(_) => (),
let selem = mixer.find_selem(&selem_id).unwrap(); Err(e) => error!("Error setting volume for <{}>, {:?}",self.card, e),
let (min, max) = selem.get_playback_volume_range(); }
// Spotify uses a volume range from 0 to 65535, but the ALSA mixers resolution might
// differ, e.g. most ALSA mixers uses a resolution of 256. Therefore, we have to calculate
// the factor to use, to get the corresponding ALSA mixers volume value from the Spotify
// volume.
let resolution = max - min + 1;
let factor: u16 = (((0xFFFF + 1) / resolution) - 1) as u16;
let volume: i64 = (volume / factor) as i64;
info!("Setting volume: {:?}", volume);
selem.set_playback_volume_all(volume).unwrap();
} }
fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> { fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> {