mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Split mixer into Mixer and StreamEditor
This commit is contained in:
parent
59398b3cee
commit
464e85e285
2 changed files with 37 additions and 22 deletions
|
@ -3,12 +3,17 @@ use std::borrow::Cow;
|
||||||
pub mod softmixer;
|
pub mod softmixer;
|
||||||
|
|
||||||
pub trait Mixer {
|
pub trait Mixer {
|
||||||
fn init(&mut self);
|
fn init(&self);
|
||||||
fn inuse(&mut self);
|
fn start(&self);
|
||||||
fn release(&mut self);
|
fn stop(&self);
|
||||||
fn set_volume(&mut self, volume: u16);
|
fn set_volume(&self, volume: u16);
|
||||||
fn volume(&self) -> u16;
|
fn volume(&self) -> u16;
|
||||||
fn apply_volume<'a>(&mut self, data: &'a [i16]) -> Cow<'a, [i16]> {
|
fn get_stream_editor(&self) -> Option<Box<StreamEditor>>
|
||||||
Cow::Borrowed(data)
|
{
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait StreamEditor {
|
||||||
|
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]>;
|
||||||
}
|
}
|
|
@ -1,43 +1,53 @@
|
||||||
use super::Mixer;
|
use super::Mixer;
|
||||||
|
use super::StreamEditor;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
pub struct SoftMixer {
|
pub struct SoftMixer {
|
||||||
volume: u16,
|
volume: Arc<RwLock<u16>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SoftMixer {
|
impl SoftMixer {
|
||||||
pub fn new() -> SoftMixer {
|
pub fn new() -> SoftMixer {
|
||||||
SoftMixer {
|
SoftMixer {
|
||||||
volume: 0xFFFF
|
volume: Arc::new(RwLock::new(0xFFFF))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mixer for SoftMixer {
|
impl Mixer for SoftMixer {
|
||||||
fn init(&mut self) {
|
fn init(&self) {
|
||||||
}
|
}
|
||||||
|
fn start(&self) {
|
||||||
fn inuse(&mut self) {
|
|
||||||
}
|
}
|
||||||
|
fn stop(&self) {
|
||||||
fn release(&mut self) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_volume(&mut self, volume: u16) {
|
|
||||||
self.volume = volume;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn volume(&self) -> u16 {
|
fn volume(&self) -> u16 {
|
||||||
self.volume
|
*self.volume.read().unwrap()
|
||||||
}
|
}
|
||||||
fn apply_volume<'a>(&mut self, data: &'a [i16]) -> Cow<'a, [i16]> {
|
fn set_volume(&self, volume: u16) {
|
||||||
if self.volume == 0xFFFF {
|
*self.volume.write().unwrap() = volume;
|
||||||
|
}
|
||||||
|
fn get_stream_editor(&self) -> Option<Box<StreamEditor>> {
|
||||||
|
let vol = self.volume.clone();
|
||||||
|
Some(Box::new(SoftVolumeApplier { get_volume: Box::new(move || *vol.read().unwrap() ) }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SoftVolumeApplier {
|
||||||
|
get_volume: Box<Fn() -> u16>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StreamEditor for SoftVolumeApplier {
|
||||||
|
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]> {
|
||||||
|
let volume = (self.get_volume)();
|
||||||
|
if volume == 0xFFFF {
|
||||||
Cow::Borrowed(data)
|
Cow::Borrowed(data)
|
||||||
} else {
|
} else {
|
||||||
Cow::Owned(data.iter()
|
Cow::Owned(data.iter()
|
||||||
.map(|&x| {
|
.map(|&x| {
|
||||||
(x as i32
|
(x as i32
|
||||||
* self.volume as i32
|
* volume as i32
|
||||||
/ 0xFFFF) as i16
|
/ 0xFFFF) as i16
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
|
|
Loading…
Reference in a new issue