Skip to content

Commit

Permalink
Add mixer config option.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonTeixidor committed Jan 14, 2018
1 parent acbec88 commit f59fa78
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ username = USER
password = PASS
backend = alsa
device = alsa_audio_device # Given by `aplay -L`
mixer = PCM
volume-control = alsa # or softvol
onstart = command_run_on_playback_start
onstop = command_run_on_playback_stop
Expand Down
14 changes: 7 additions & 7 deletions src/alsa_mixer.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use librespot::mixer::{Mixer, AudioFilter};
use alsa;

pub struct AlsaMixer(pub String);
pub struct AlsaMixer {pub device: String, pub mixer: String}

impl Mixer for AlsaMixer {
fn open() -> AlsaMixer {
AlsaMixer("default".to_string())
AlsaMixer { device: "default".to_string(), mixer: "Master".to_string() }
}
fn start(&self) {}
fn stop(&self) {}

fn volume(&self) -> u16 {
let selem_id = alsa::mixer::SelemId::new("Master", 0);
match alsa::mixer::Mixer::new(&self.0, false)
let selem_id = alsa::mixer::SelemId::new(&*self.mixer, 0);
match alsa::mixer::Mixer::new(&self.device, false)
.ok()
.as_ref()
.and_then(|ref mixer| mixer.find_selem(&selem_id))
Expand All @@ -29,17 +29,17 @@ impl Mixer for AlsaMixer {
_ => {
error!(
"Couldn't read volume from alsa device with name \"{}\".",
self.0
self.device
);
0
}
}
}

fn set_volume(&self, volume: u16) {
match alsa::mixer::Mixer::new(&self.0, false).ok().and_then(
match alsa::mixer::Mixer::new(&self.device, false).ok().and_then(
|mixer| {
let selem_id = alsa::mixer::SelemId::new("Master", 0);
let selem_id = alsa::mixer::SelemId::new(&*self.mixer, 0);
mixer.find_selem(&selem_id).and_then(|elem| {
let (min, max) = elem.get_playback_volume_range();

Expand Down
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn command_line_argument_options() -> Options {
opts.optopt("u", "username", "Spotify user name.", "USERNAME");
opts.optopt("p", "password", "Spotify password.", "PASSWORD");
opts.optopt("", "device", "Audio device, given by aplay -L.", "DEVICE");
opts.optopt("", "mixer", "Audio mixer", "DEVICE");
opts.optopt("", "bitrate", "Any of 96, 160, and 320.", "DEVICE");
opts.optopt("", "pid", "Path to PID file.", "PID-FILE");
opts.optopt("", "device_name", "Name of this Spotify device.", "DEVICE");
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct SpotifydConfig {
pub cache: Option<Cache>,
pub backend: Option<String>,
pub audio_device: Option<String>,
pub mixer: Option<String>,
pub volume_controller: VolumeController,
pub device_name: String,
pub player_config: PlayerConfig,
Expand All @@ -54,6 +55,7 @@ impl Default for SpotifydConfig {
cache: None,
backend: None,
audio_device: None,
mixer: None,
volume_controller: VolumeController::SoftVol,
device_name: "Spotifyd".to_string(),
player_config: PlayerConfig {
Expand Down Expand Up @@ -137,6 +139,7 @@ pub fn get_config<P: AsRef<Path>>(config_path: Option<P>, matches: &Matches) ->
config.password = lookup("password");
config.backend = lookup("backend");
config.audio_device = lookup("device");
config.mixer = lookup("mixer");
update(
&mut config.volume_controller,
lookup("volume-control").and_then(|s| VolumeController::from_str(&*s).ok()),
Expand Down
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ fn main() {
let player_config = config.player_config;
let session_config = config.session_config;
let backend = config.backend.clone();
let audio_device = config.audio_device.clone();
let device_id = session_config.device_id.clone();
let discovery_stream = discovery(
&handle,
Expand Down Expand Up @@ -270,14 +269,16 @@ fn main() {
Box<futures::Future<Item = Session, Error = io::Error>>
};

let local_audio_device = audio_device.clone();
let local_audio_device = config.audio_device.clone();
let local_mixer = config.mixer.clone();
let mixer = match config.volume_controller {
config::VolumeController::Alsa => {
info!("Using alsa volume controller.");
Box::new(move || {
Box::new(alsa_mixer::AlsaMixer(
local_audio_device.clone().unwrap_or("default".to_string()),
)) as Box<Mixer>
Box::new( alsa_mixer::AlsaMixer{
device : local_audio_device.clone().unwrap_or("default".to_string()),
mixer : local_mixer.clone().unwrap_or("Master".to_string()),
}) as Box<Mixer>
}) as Box<FnMut() -> Box<Mixer>>
}
config::VolumeController::SoftVol => {
Expand All @@ -293,7 +294,7 @@ fn main() {
connection,
mixer,
backend,
audio_device,
config.audio_device.clone(),
ctrl_c(&handle).flatten_stream().boxed(),
discovery_stream,
cache,
Expand Down

0 comments on commit f59fa78

Please sign in to comment.