Skip to content

Commit 1540636

Browse files
authored
Merge pull request #821 from roderickvd/fix-alsa-mixer
Fix Alsa mixer and rename options
2 parents 7f6386a + c67e268 commit 1540636

5 files changed

Lines changed: 93 additions & 41 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929

3030
### Deprecated
3131
- [connect] The `discovery` module was deprecated in favor of the `librespot-discovery` crate
32+
- [playback] `alsamixer`: renamed `mixer-card` to `alsa-mixer-device`
33+
- [playback] `alsamixer`: renamed `mixer-name` to `alsa-mixer-control`
34+
- [playback] `alsamixer`: renamed `mixer-index` to `alsa-mixer-index`
3235

3336
### Removed
3437
- [connect] Removed no-op mixer started/stopped logic (breaking)
3538
- [playback] Removed `with-vorbis` and `with-tremor` features
36-
- [playback] `alsamixer`: removed `--mixer-linear-volume` option; use `--volume-ctrl linear` instead
39+
- [playback] `alsamixer`: removed `--mixer-linear-volume` option, now that `--volume-ctrl {linear|log}` work as expected on Alsa
3740

3841
### Fixed
3942
- [connect] Fix step size on volume up/down events

playback/src/mixer/alsamixer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ const ZERO_DB: MilliBel = MilliBel(0);
3131
impl Mixer for AlsaMixer {
3232
fn open(config: MixerConfig) -> Self {
3333
info!(
34-
"Mixing with alsa and volume control: {:?} for card: {} with mixer control: {},{}",
35-
config.volume_ctrl, config.card, config.control, config.index,
34+
"Mixing with Alsa and volume control: {:?} for device: {} with mixer control: {},{}",
35+
config.volume_ctrl, config.device, config.control, config.index,
3636
);
3737

3838
let mut config = config; // clone
3939

4040
let mixer =
41-
alsa::mixer::Mixer::new(&config.card, false).expect("Could not open Alsa mixer");
41+
alsa::mixer::Mixer::new(&config.device, false).expect("Could not open Alsa mixer");
4242
let simple_element = mixer
4343
.find_selem(&SelemId::new(&config.control, config.index))
4444
.expect("Could not find Alsa mixer control");
@@ -56,8 +56,8 @@ impl Mixer for AlsaMixer {
5656
// Query dB volume range -- note that Alsa exposes a different
5757
// API for hardware and software mixers
5858
let (min_millibel, max_millibel) = if is_softvol {
59-
let control =
60-
Ctl::new(&config.card, false).expect("Could not open Alsa softvol with that card");
59+
let control = Ctl::new(&config.device, false)
60+
.expect("Could not open Alsa softvol with that device");
6161
let mut element_id = ElemId::new(ElemIface::Mixer);
6262
element_id.set_name(
6363
&CString::new(config.control.as_str())
@@ -144,7 +144,7 @@ impl Mixer for AlsaMixer {
144144

145145
fn volume(&self) -> u16 {
146146
let mixer =
147-
alsa::mixer::Mixer::new(&self.config.card, false).expect("Could not open Alsa mixer");
147+
alsa::mixer::Mixer::new(&self.config.device, false).expect("Could not open Alsa mixer");
148148
let simple_element = mixer
149149
.find_selem(&SelemId::new(&self.config.control, self.config.index))
150150
.expect("Could not find Alsa mixer control");
@@ -184,7 +184,7 @@ impl Mixer for AlsaMixer {
184184

185185
fn set_volume(&self, volume: u16) {
186186
let mixer =
187-
alsa::mixer::Mixer::new(&self.config.card, false).expect("Could not open Alsa mixer");
187+
alsa::mixer::Mixer::new(&self.config.device, false).expect("Could not open Alsa mixer");
188188
let simple_element = mixer
189189
.find_selem(&SelemId::new(&self.config.control, self.config.index))
190190
.expect("Could not find Alsa mixer control");
@@ -249,7 +249,7 @@ impl AlsaMixer {
249249
}
250250

251251
let mixer =
252-
alsa::mixer::Mixer::new(&self.config.card, false).expect("Could not open Alsa mixer");
252+
alsa::mixer::Mixer::new(&self.config.device, false).expect("Could not open Alsa mixer");
253253
let simple_element = mixer
254254
.find_selem(&SelemId::new(&self.config.control, self.config.index))
255255
.expect("Could not find Alsa mixer control");

playback/src/mixer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use self::alsamixer::AlsaMixer;
3030

3131
#[derive(Debug, Clone)]
3232
pub struct MixerConfig {
33-
pub card: String,
33+
pub device: String,
3434
pub control: String,
3535
pub index: u32,
3636
pub volume_ctrl: VolumeCtrl,
@@ -39,7 +39,7 @@ pub struct MixerConfig {
3939
impl Default for MixerConfig {
4040
fn default() -> MixerConfig {
4141
MixerConfig {
42-
card: String::from("default"),
42+
device: String::from("default"),
4343
control: String::from("PCM"),
4444
index: 0,
4545
volume_ctrl: VolumeCtrl::default(),

playback/src/mixer/softmixer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Mixer for SoftMixer {
4343
}
4444

4545
impl SoftMixer {
46-
pub const NAME: &'static str = "softmixer";
46+
pub const NAME: &'static str = "softvol";
4747
}
4848

4949
struct SoftVolumeApplier {

src/main.rs

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ fn get_setup(args: &[String]) -> Setup {
205205
const FORMAT: &str = "format";
206206
const HELP: &str = "h";
207207
const INITIAL_VOLUME: &str = "initial-volume";
208-
const MIXER_CARD: &str = "mixer-card";
209-
const MIXER_INDEX: &str = "mixer-index";
210-
const MIXER_NAME: &str = "mixer-name";
208+
const MIXER_TYPE: &str = "mixer";
209+
const ALSA_MIXER_DEVICE: &str = "alsa-mixer-device";
210+
const ALSA_MIXER_INDEX: &str = "alsa-mixer-index";
211+
const ALSA_MIXER_CONTROL: &str = "alsa-mixer-control";
211212
const NAME: &str = "name";
212213
const NORMALISATION_ATTACK: &str = "normalisation-attack";
213214
const NORMALISATION_GAIN_TYPE: &str = "normalisation-gain-type";
@@ -295,24 +296,42 @@ fn get_setup(args: &[String]) -> Setup {
295296
"Specify the dither algorithm to use - [none, gpdf, tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16, S24, S24_3 and 'none' for other formats.",
296297
"DITHER",
297298
)
298-
.optopt("", "mixer", "Mixer to use {alsa|softvol}.", "MIXER")
299+
.optopt("m", MIXER_TYPE, "Mixer to use {alsa|softvol}.", "MIXER")
299300
.optopt(
300-
"m",
301-
MIXER_NAME,
301+
"",
302+
"mixer-name", // deprecated
303+
"",
304+
"",
305+
)
306+
.optopt(
307+
"",
308+
ALSA_MIXER_CONTROL,
302309
"Alsa mixer control, e.g. 'PCM' or 'Master'. Defaults to 'PCM'.",
303310
"NAME",
304311
)
305312
.optopt(
306313
"",
307-
MIXER_CARD,
308-
"Alsa mixer card, e.g 'hw:0' or similar from `aplay -l`. Defaults to DEVICE if specified, 'default' otherwise.",
309-
"MIXER_CARD",
314+
"mixer-card", // deprecated
315+
"",
316+
"",
317+
)
318+
.optopt(
319+
"",
320+
ALSA_MIXER_DEVICE,
321+
"Alsa mixer device, e.g 'hw:0' or similar from `aplay -l`. Defaults to `--device` if specified, 'default' otherwise.",
322+
"DEVICE",
323+
)
324+
.optopt(
325+
"",
326+
"mixer-index", // deprecated
327+
"",
328+
"",
310329
)
311330
.optopt(
312331
"",
313-
MIXER_INDEX,
332+
ALSA_MIXER_INDEX,
314333
"Alsa index of the cards mixer. Defaults to 0.",
315-
"INDEX",
334+
"NUMBER",
316335
)
317336
.optopt(
318337
"",
@@ -454,28 +473,58 @@ fn get_setup(args: &[String]) -> Setup {
454473
exit(0);
455474
}
456475

457-
let mixer_name = matches.opt_str(MIXER_NAME);
458-
let mixer = mixer::find(mixer_name.as_deref()).expect("Invalid mixer");
476+
let mixer_type = matches.opt_str(MIXER_TYPE);
477+
let mixer = mixer::find(mixer_type.as_deref()).expect("Invalid mixer");
459478

460479
let mixer_config = {
461-
let card = matches.opt_str(MIXER_CARD).unwrap_or_else(|| {
462-
if let Some(ref device_name) = device {
463-
device_name.to_string()
464-
} else {
465-
MixerConfig::default().card
480+
let mixer_device = match matches.opt_str("mixer-card") {
481+
Some(card) => {
482+
warn!("--mixer-card is deprecated and will be removed in a future release.");
483+
warn!("Please use --alsa-mixer-device instead.");
484+
card
466485
}
467-
});
468-
let index = matches
469-
.opt_str(MIXER_INDEX)
470-
.map(|index| index.parse::<u32>().unwrap())
471-
.unwrap_or(0);
472-
let control = matches
473-
.opt_str(MIXER_NAME)
474-
.unwrap_or_else(|| MixerConfig::default().control);
486+
None => matches.opt_str(ALSA_MIXER_DEVICE).unwrap_or_else(|| {
487+
if let Some(ref device_name) = device {
488+
device_name.to_string()
489+
} else {
490+
MixerConfig::default().device
491+
}
492+
}),
493+
};
494+
495+
let index = match matches.opt_str("mixer-index") {
496+
Some(index) => {
497+
warn!("--mixer-index is deprecated and will be removed in a future release.");
498+
warn!("Please use --alsa-mixer-index instead.");
499+
index
500+
.parse::<u32>()
501+
.expect("Mixer index is not a valid number")
502+
}
503+
None => matches
504+
.opt_str(ALSA_MIXER_INDEX)
505+
.map(|index| {
506+
index
507+
.parse::<u32>()
508+
.expect("Alsa mixer index is not a valid number")
509+
})
510+
.unwrap_or(0),
511+
};
512+
513+
let control = match matches.opt_str("mixer-name") {
514+
Some(name) => {
515+
warn!("--mixer-name is deprecated and will be removed in a future release.");
516+
warn!("Please use --alsa-mixer-control instead.");
517+
name
518+
}
519+
None => matches
520+
.opt_str(ALSA_MIXER_CONTROL)
521+
.unwrap_or_else(|| MixerConfig::default().control),
522+
};
523+
475524
let mut volume_range = matches
476525
.opt_str(VOLUME_RANGE)
477526
.map(|range| range.parse::<f64>().unwrap())
478-
.unwrap_or_else(|| match mixer_name.as_deref() {
527+
.unwrap_or_else(|| match mixer_type.as_deref() {
479528
#[cfg(feature = "alsa-backend")]
480529
Some(AlsaMixer::NAME) => 0.0, // let Alsa query the control
481530
_ => VolumeCtrl::DEFAULT_DB_RANGE,
@@ -502,7 +551,7 @@ fn get_setup(args: &[String]) -> Setup {
502551
});
503552

504553
MixerConfig {
505-
card,
554+
device: mixer_device,
506555
control,
507556
index,
508557
volume_ctrl,
@@ -563,7 +612,7 @@ fn get_setup(args: &[String]) -> Setup {
563612
}
564613
(volume as f32 / 100.0 * VolumeCtrl::MAX_VOLUME as f32) as u16
565614
})
566-
.or_else(|| match mixer_name.as_deref() {
615+
.or_else(|| match mixer_type.as_deref() {
567616
#[cfg(feature = "alsa-backend")]
568617
Some(AlsaMixer::NAME) => None,
569618
_ => cache.as_ref().and_then(Cache::volume),

0 commit comments

Comments
 (0)