Changes code review

This commit is contained in:
Daniel Romero 2017-01-27 14:20:31 +01:00
parent 636de3fe71
commit e547a0c3da
4 changed files with 19 additions and 20 deletions

View file

@ -122,14 +122,13 @@ fn setup(args: &[String]) -> (Session, Player, Box<Mixer + Send>) {
let credentials = get_credentials(&session, matches.opt_str("username"), let credentials = get_credentials(&session, matches.opt_str("username"),
matches.opt_str("password")); matches.opt_str("password"));
session.login(credentials).unwrap(); session.login(credentials).unwrap();
let mixer_name = matches.opt_str("mixer");
let mixer_name = matches.opt_str("mixer").unwrap_or("SoftMixer".to_owned()); let mixer = mixer::find(mixer_name.as_ref()).expect("Invalid mixer");
let audio_filter = mixer.get_audio_filter();
let mixer = mixer::find(&mixer_name).unwrap();
let device_name = matches.opt_str("device"); let device_name = matches.opt_str("device");
let player = Player::new(session.clone(), mixer.get_stream_editor(), move || { let player = Player::new(session.clone(), audio_filter, move || {
(backend)(device_name.as_ref().map(AsRef::as_ref)) (backend)(device_name.as_ref().map(AsRef::as_ref))
}); });

View file

@ -1,4 +1,5 @@
use std::borrow::Cow; use std::borrow::Cow;
use self::softmixer::SoftMixer;
pub mod softmixer; pub mod softmixer;
@ -8,19 +9,17 @@ pub trait Mixer {
fn stop(&self); fn stop(&self);
fn set_volume(&self, volume: u16); fn set_volume(&self, volume: u16);
fn volume(&self) -> u16; fn volume(&self) -> u16;
fn get_stream_editor(&self) -> Option<Box<StreamEditor + Send>> fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> {
{
None None
} }
} }
pub trait StreamEditor { pub trait AudioFilter {
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]>; fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]>;
} }
pub fn find(s: &str) -> Option<Box<Mixer + Send>> { pub fn find<T: AsRef<str>>(name: Option<T>) -> Option<Box<Mixer + Send>> {
match s { match name {
"SoftMixer" => Some(Box::new(softmixer::SoftMixer::new())), _ => Some(Box::new(SoftMixer::new())),
_ => None,
} }
} }

View file

@ -1,5 +1,5 @@
use super::Mixer; use super::Mixer;
use super::StreamEditor; use super::AudioFilter;
use std::borrow::Cow; use std::borrow::Cow;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -28,9 +28,10 @@ impl Mixer for SoftMixer {
fn set_volume(&self, volume: u16) { fn set_volume(&self, volume: u16) {
*self.volume.write().unwrap() = volume; *self.volume.write().unwrap() = volume;
} }
fn get_stream_editor(&self) -> Option<Box<StreamEditor + Send>> { fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> {
let vol = self.volume.clone(); let vol = self.volume.clone();
Some(Box::new(SoftVolumeApplier { get_volume: Box::new(move || *vol.read().unwrap() ) })) let get_volume = Box::new(move || *vol.read().unwrap());
Some(Box::new(SoftVolumeApplier { get_volume: get_volume }))
} }
} }
@ -38,7 +39,7 @@ struct SoftVolumeApplier {
get_volume: Box<Fn() -> u16 + Send> get_volume: Box<Fn() -> u16 + Send>
} }
impl StreamEditor for SoftVolumeApplier { impl AudioFilter for SoftVolumeApplier {
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]> { fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]> {
let volume = (self.get_volume)(); let volume = (self.get_volume)();
if volume == 0xFFFF { if volume == 0xFFFF {

View file

@ -9,7 +9,7 @@ use audio_decrypt::AudioDecrypt;
use audio_backend::Sink; use audio_backend::Sink;
use metadata::{FileFormat, Track, TrackRef}; use metadata::{FileFormat, Track, TrackRef};
use session::{Bitrate, Session}; use session::{Bitrate, Session};
use mixer::StreamEditor; use mixer::AudioFilter;
use util::{self, ReadSeek, SpotifyId, Subfile}; use util::{self, ReadSeek, SpotifyId, Subfile};
pub use spirc::PlayStatus; pub use spirc::PlayStatus;
@ -72,7 +72,7 @@ enum PlayerCommand {
} }
impl Player { impl Player {
pub fn new<F>(session: Session, stream_editor: Option<Box<StreamEditor + Send>>, sink_builder: F) -> Player pub fn new<F>(session: Session, stream_editor: Option<Box<AudioFilter + Send>>, sink_builder: F) -> Player
where F: FnOnce() -> Box<Sink> + Send + 'static { where F: FnOnce() -> Box<Sink> + Send + 'static {
let (cmd_tx, cmd_rx) = mpsc::channel(); let (cmd_tx, cmd_rx) = mpsc::channel();
@ -210,7 +210,7 @@ fn run_onstop(session: &Session) {
} }
impl PlayerInternal { impl PlayerInternal {
fn run(self, mut sink: Box<Sink>, stream_editor: Option<Box<StreamEditor + Send>>) { fn run(self, mut sink: Box<Sink>, stream_editor: Option<Box<AudioFilter + Send>>) {
let mut decoder = None; let mut decoder = None;
loop { loop {