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 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<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::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;
}
}
}
}

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>> {
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();