2018-03-20 15:32:43 +00:00
|
|
|
use super::AudioFilter;
|
2018-09-11 16:53:18 +00:00
|
|
|
use super::{Mixer, MixerConfig};
|
2018-09-17 15:28:54 +00:00
|
|
|
use std;
|
2018-09-01 00:42:50 +00:00
|
|
|
use std::error::Error;
|
2018-03-20 15:32:43 +00:00
|
|
|
|
|
|
|
use alsa;
|
|
|
|
|
2020-03-04 14:46:32 +00:00
|
|
|
const SND_CTL_TLV_DB_GAIN_MUTE: i64 = -9999999;
|
|
|
|
|
2018-09-17 15:28:54 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct AlsaMixerVolumeParams {
|
|
|
|
min: i64,
|
|
|
|
max: i64,
|
|
|
|
range: f64,
|
|
|
|
min_db: alsa::mixer::MilliBel,
|
|
|
|
max_db: alsa::mixer::MilliBel,
|
|
|
|
}
|
|
|
|
|
2018-03-20 15:32:43 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AlsaMixer {
|
2018-09-11 16:53:18 +00:00
|
|
|
config: MixerConfig,
|
2018-09-17 15:28:54 +00:00
|
|
|
params: AlsaMixerVolumeParams,
|
2018-03-20 15:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 00:42:50 +00:00
|
|
|
impl AlsaMixer {
|
2018-09-17 15:28:54 +00:00
|
|
|
fn pvol<T>(&self, vol: T, min: T, max: T) -> f64
|
|
|
|
where
|
|
|
|
T: std::ops::Sub + Copy,
|
|
|
|
f64: std::convert::From<<T as std::ops::Sub>::Output>,
|
|
|
|
{
|
|
|
|
f64::from(vol - min) / f64::from(max - min)
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:46:32 +00:00
|
|
|
fn init_mixer(mut config: MixerConfig) -> Result<AlsaMixer, Box<dyn Error>> {
|
2018-09-17 15:28:54 +00:00
|
|
|
let mixer = alsa::mixer::Mixer::new(&config.card, false)?;
|
|
|
|
let sid = alsa::mixer::SelemId::new(&config.mixer, config.index);
|
|
|
|
|
2020-03-04 14:46:32 +00:00
|
|
|
let selem = mixer.find_selem(&sid).expect(
|
|
|
|
format!(
|
|
|
|
"Couldn't find simple mixer control for {},{}",
|
|
|
|
&config.mixer, &config.index,
|
|
|
|
)
|
|
|
|
.as_str(),
|
|
|
|
);
|
2018-09-17 15:28:54 +00:00
|
|
|
let (min, max) = selem.get_playback_volume_range();
|
|
|
|
let (min_db, max_db) = selem.get_playback_db_range();
|
2018-09-22 15:56:13 +00:00
|
|
|
let hw_mix = selem
|
|
|
|
.get_playback_vol_db(alsa::mixer::SelemChannelId::mono())
|
|
|
|
.is_ok();
|
2018-09-17 15:28:54 +00:00
|
|
|
|
|
|
|
info!(
|
2020-03-04 14:46:32 +00:00
|
|
|
"Alsa Mixer info min: {} ({:?}[dB]) -- max: {} ({:?}[dB]) HW: {:?}",
|
2018-09-22 15:56:13 +00:00
|
|
|
min, min_db, max, max_db, hw_mix
|
2018-09-17 15:28:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if config.mapped_volume && (max_db - min_db <= alsa::mixer::MilliBel(24)) {
|
|
|
|
warn!(
|
|
|
|
"Switching to linear volume mapping, control range: {:?}",
|
|
|
|
max_db - min_db
|
|
|
|
);
|
|
|
|
config.mapped_volume = false;
|
2018-09-22 15:56:13 +00:00
|
|
|
} else if !config.mapped_volume {
|
|
|
|
info!("Using Alsa linear volume");
|
2018-09-17 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 14:46:32 +00:00
|
|
|
if min_db != alsa::mixer::MilliBel(SND_CTL_TLV_DB_GAIN_MUTE) {
|
|
|
|
debug!("Alsa min-db is not SND_CTL_TLV_DB_GAIN_MUTE!!");
|
|
|
|
}
|
|
|
|
|
2018-09-17 15:28:54 +00:00
|
|
|
Ok(AlsaMixer {
|
|
|
|
config: config,
|
|
|
|
params: AlsaMixerVolumeParams {
|
|
|
|
min: min,
|
|
|
|
max: max,
|
|
|
|
range: (max - min) as f64,
|
|
|
|
min_db: min_db,
|
|
|
|
max_db: max_db,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:46:32 +00:00
|
|
|
fn map_volume(&self, set_volume: Option<u16>) -> Result<u16, Box<dyn Error>> {
|
2018-10-16 14:46:26 +00:00
|
|
|
let mixer = alsa::mixer::Mixer::new(&self.config.card, false)?;
|
|
|
|
let sid = alsa::mixer::SelemId::new(&*self.config.mixer, self.config.index);
|
2018-09-01 00:42:50 +00:00
|
|
|
|
2020-03-04 14:46:32 +00:00
|
|
|
let selem = mixer.find_selem(&sid).unwrap();
|
2018-09-17 15:28:54 +00:00
|
|
|
let cur_vol = selem
|
|
|
|
.get_playback_volume(alsa::mixer::SelemChannelId::mono())
|
|
|
|
.expect("Couldn't get current volume");
|
|
|
|
let cur_vol_db = selem
|
|
|
|
.get_playback_vol_db(alsa::mixer::SelemChannelId::mono())
|
2020-03-04 14:46:32 +00:00
|
|
|
.unwrap_or(alsa::mixer::MilliBel(-SND_CTL_TLV_DB_GAIN_MUTE));
|
|
|
|
|
|
|
|
let mut new_vol: u16 = 0;
|
|
|
|
trace!("Current alsa volume: {}{:?}", cur_vol, cur_vol_db);
|
|
|
|
|
|
|
|
match set_volume {
|
|
|
|
Some(vol) => {
|
|
|
|
if self.config.mapped_volume {
|
|
|
|
// Cubic mapping ala alsamixer
|
|
|
|
// https://linux.die.net/man/1/alsamixer
|
|
|
|
// In alsamixer, the volume is mapped to a value that is more natural for a
|
|
|
|
// human ear. The mapping is designed so that the position in the interval is
|
|
|
|
// proportional to the volume as a human ear would perceive it, i.e. the
|
|
|
|
// position is the cubic root of the linear sample multiplication factor. For
|
|
|
|
// controls with a small range (24 dB or less), the mapping is linear in the dB
|
|
|
|
// values so that each step has the same size visually. TODO
|
|
|
|
// TODO: Check if min is not mute!
|
|
|
|
let vol_db = (self.pvol(vol, 0x0000, 0xFFFF).log10() * 6000.0).floor() as i64
|
|
|
|
+ self.params.max_db.0;
|
|
|
|
selem
|
|
|
|
.set_playback_db_all(alsa::mixer::MilliBel(vol_db), alsa::Round::Floor)
|
|
|
|
.expect("Couldn't set alsa dB volume");
|
|
|
|
debug!(
|
|
|
|
"Mapping volume [{:.3}%] {:?} [u16] ->> Alsa [{:.3}%] {:?} [dB] - {} [i64]",
|
|
|
|
self.pvol(vol, 0x0000, 0xFFFF) * 100.0,
|
|
|
|
vol,
|
|
|
|
self.pvol(
|
|
|
|
vol_db as f64,
|
|
|
|
self.params.min as f64,
|
|
|
|
self.params.max as f64
|
|
|
|
) * 100.0,
|
|
|
|
vol_db as f64 / 100.0,
|
|
|
|
vol_db
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Linear mapping
|
|
|
|
let alsa_volume =
|
|
|
|
((vol as f64 / 0xFFFF as f64) * self.params.range) as i64 + self.params.min;
|
|
|
|
selem
|
|
|
|
.set_playback_volume_all(alsa_volume)
|
|
|
|
.expect("Couldn't set alsa raw volume");
|
|
|
|
debug!(
|
|
|
|
"Mapping volume [{:.3}%] {:?} [u16] ->> Alsa [{:.3}%] {:?} [i64]",
|
|
|
|
self.pvol(vol, 0x0000, 0xFFFF) * 100.0,
|
|
|
|
vol,
|
|
|
|
self.pvol(
|
|
|
|
alsa_volume as f64,
|
|
|
|
self.params.min as f64,
|
|
|
|
self.params.max as f64
|
|
|
|
) * 100.0,
|
|
|
|
alsa_volume
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
new_vol = (((cur_vol - self.params.min) as f64 / self.params.range) * 0xFFFF as f64)
|
|
|
|
as u16;
|
|
|
|
debug!(
|
|
|
|
"Mapping volume [{:.3}%] {:?} [u16] <<- Alsa [{:.3}%] {:?} [i64]",
|
|
|
|
self.pvol(new_vol, 0x0000, 0xFFFF),
|
|
|
|
new_vol,
|
2018-09-22 15:56:13 +00:00
|
|
|
self.pvol(
|
2020-03-04 14:46:32 +00:00
|
|
|
cur_vol as f64,
|
2018-09-22 15:56:13 +00:00
|
|
|
self.params.min as f64,
|
|
|
|
self.params.max as f64
|
2020-03-04 14:46:32 +00:00
|
|
|
),
|
|
|
|
cur_vol
|
2018-09-22 15:56:13 +00:00
|
|
|
);
|
2020-03-04 14:46:32 +00:00
|
|
|
}
|
2018-09-01 00:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(new_vol)
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 21:18:37 +00:00
|
|
|
|
2018-03-20 15:32:43 +00:00
|
|
|
impl Mixer for AlsaMixer {
|
2018-09-11 16:53:18 +00:00
|
|
|
fn open(config: Option<MixerConfig>) -> AlsaMixer {
|
|
|
|
let config = config.unwrap_or_default();
|
2018-09-01 00:42:50 +00:00
|
|
|
info!(
|
|
|
|
"Setting up new mixer: card:{} mixer:{} index:{}",
|
2018-09-11 16:53:18 +00:00
|
|
|
config.card, config.mixer, config.index
|
2018-09-01 00:42:50 +00:00
|
|
|
);
|
2018-09-17 15:28:54 +00:00
|
|
|
AlsaMixer::init_mixer(config).expect("Error setting up mixer!")
|
2018-03-20 15:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 14:46:26 +00:00
|
|
|
fn start(&self) {}
|
2018-03-20 15:32:43 +00:00
|
|
|
|
2018-10-16 14:46:26 +00:00
|
|
|
fn stop(&self) {}
|
2018-03-20 15:32:43 +00:00
|
|
|
|
|
|
|
fn volume(&self) -> u16 {
|
2018-10-16 14:46:26 +00:00
|
|
|
match self.map_volume(None) {
|
|
|
|
Ok(vol) => vol,
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error getting volume for <{}>, {:?}", self.config.card, e);
|
|
|
|
0
|
|
|
|
}
|
2018-09-01 00:42:50 +00:00
|
|
|
}
|
2018-03-20 15:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_volume(&self, volume: u16) {
|
2018-10-16 14:46:26 +00:00
|
|
|
match self.map_volume(Some(volume)) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => error!("Error setting volume for <{}>, {:?}", self.config.card, e),
|
2018-09-01 00:42:50 +00:00
|
|
|
}
|
2018-03-20 15:32:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
fn get_audio_filter(&self) -> Option<Box<dyn AudioFilter + Send>> {
|
2018-03-20 15:32:43 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|