diff --git a/src/mixer/mod.rs b/src/mixer/mod.rs index a0e991e1..fdadf4fe 100644 --- a/src/mixer/mod.rs +++ b/src/mixer/mod.rs @@ -1,5 +1,3 @@ -use std::borrow::Cow; - use spirc::UpdateMessageSender; use self::softmixer::SoftMixer; @@ -18,7 +16,7 @@ pub trait Mixer { } pub trait AudioFilter { - fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]>; + fn modify_stream(&self, data: &mut [i16]); } pub fn find>(name: Option) -> Option> { diff --git a/src/mixer/softmixer.rs b/src/mixer/softmixer.rs index d2b75f6e..94b03f48 100644 --- a/src/mixer/softmixer.rs +++ b/src/mixer/softmixer.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -48,18 +47,13 @@ struct SoftVolumeApplier { } impl AudioFilter for SoftVolumeApplier { - fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]> { + fn modify_stream(&self, data: &mut [i16]) { let volume = self.volume.load(Ordering::Relaxed) as u16; - if volume == 0xFFFF { - Cow::Borrowed(data) - } else { - Cow::Owned(data.iter() - .map(|&x| { - (x as i32 - * volume as i32 - / 0xFFFF) as i16 - }) - .collect()) + if volume != 0xFFFF { + let factor = volume as i32 / 0xFFFF; + for x in data.iter_mut() { + *x = (*x as i32 * factor) as i16; + } } } } \ No newline at end of file diff --git a/src/player.rs b/src/player.rs index f03a592b..5a4f0c98 100644 --- a/src/player.rs +++ b/src/player.rs @@ -139,10 +139,6 @@ impl Player { } } -fn borrow_data<'a>(data: &'a [i16]) -> Cow<'a, [i16]> { - Cow::Borrowed(&data) -} - fn find_available_alternative<'a>(session: &Session, track: &'a Track) -> Option> { if track.available { Some(Cow::Borrowed(track)) @@ -345,10 +341,10 @@ impl PlayerInternal { match packet { Some(Ok(packet)) => { - let buffer = if let Some(ref editor) = stream_editor { - editor.modify_stream(&packet.data) - } else { - borrow_data(&packet.data) + let mut buffer = packet.data.to_vec(); + + if let Some(ref editor) = stream_editor { + editor.modify_stream(&mut buffer) }; sink.write(&buffer).unwrap();