AudioFilter: Modify fn modify_stream(&self, data: &mut [i16]);

This commit is contained in:
Daniel Romero 2017-02-01 22:16:28 +01:00
parent 2de5d10a2f
commit da537b57f4
3 changed files with 11 additions and 23 deletions

View file

@ -1,5 +1,3 @@
use std::borrow::Cow;
use spirc::UpdateMessageSender; use spirc::UpdateMessageSender;
use self::softmixer::SoftMixer; use self::softmixer::SoftMixer;
@ -18,7 +16,7 @@ pub trait Mixer {
} }
pub trait AudioFilter { pub trait AudioFilter {
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]>; fn modify_stream(&self, data: &mut [i16]);
} }
pub fn find<T: AsRef<str>>(name: Option<T>) -> Option<Box<Mixer + Send>> { pub fn find<T: AsRef<str>>(name: Option<T>) -> Option<Box<Mixer + Send>> {

View file

@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
@ -48,18 +47,13 @@ struct SoftVolumeApplier {
} }
impl AudioFilter for 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; let volume = self.volume.load(Ordering::Relaxed) as u16;
if volume == 0xFFFF { if volume != 0xFFFF {
Cow::Borrowed(data) let factor = volume as i32 / 0xFFFF;
} else { for x in data.iter_mut() {
Cow::Owned(data.iter() *x = (*x as i32 * factor) as i16;
.map(|&x| { }
(x as i32
* volume as i32
/ 0xFFFF) as i16
})
.collect())
} }
} }
} }

View file

@ -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<Cow<'a, Track>> { fn find_available_alternative<'a>(session: &Session, track: &'a Track) -> Option<Cow<'a, Track>> {
if track.available { if track.available {
Some(Cow::Borrowed(track)) Some(Cow::Borrowed(track))
@ -345,10 +341,10 @@ impl PlayerInternal {
match packet { match packet {
Some(Ok(packet)) => { Some(Ok(packet)) => {
let buffer = if let Some(ref editor) = stream_editor { let mut buffer = packet.data.to_vec();
editor.modify_stream(&packet.data)
} else { if let Some(ref editor) = stream_editor {
borrow_data(&packet.data) editor.modify_stream(&mut buffer)
}; };
sink.write(&buffer).unwrap(); sink.write(&buffer).unwrap();