2017-01-20 19:39:05 +00:00
|
|
|
use super::Mixer;
|
2017-01-25 21:22:53 +00:00
|
|
|
use super::StreamEditor;
|
2017-01-20 19:39:05 +00:00
|
|
|
use std::borrow::Cow;
|
2017-01-25 21:22:53 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2017-01-20 19:39:05 +00:00
|
|
|
|
|
|
|
pub struct SoftMixer {
|
2017-01-25 21:22:53 +00:00
|
|
|
volume: Arc<RwLock<u16>>
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SoftMixer {
|
|
|
|
pub fn new() -> SoftMixer {
|
|
|
|
SoftMixer {
|
2017-01-25 21:22:53 +00:00
|
|
|
volume: Arc::new(RwLock::new(0xFFFF))
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mixer for SoftMixer {
|
2017-01-25 21:22:53 +00:00
|
|
|
fn init(&self) {
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
2017-01-25 21:22:53 +00:00
|
|
|
fn start(&self) {
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
2017-01-25 21:22:53 +00:00
|
|
|
fn stop(&self) {
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
fn volume(&self) -> u16 {
|
2017-01-25 21:22:53 +00:00
|
|
|
*self.volume.read().unwrap()
|
|
|
|
}
|
|
|
|
fn set_volume(&self, volume: u16) {
|
|
|
|
*self.volume.write().unwrap() = volume;
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
2017-01-25 21:56:06 +00:00
|
|
|
fn get_stream_editor(&self) -> Option<Box<StreamEditor + Send>> {
|
2017-01-25 21:22:53 +00:00
|
|
|
let vol = self.volume.clone();
|
|
|
|
Some(Box::new(SoftVolumeApplier { get_volume: Box::new(move || *vol.read().unwrap() ) }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SoftVolumeApplier {
|
2017-01-25 21:56:06 +00:00
|
|
|
get_volume: Box<Fn() -> u16 + Send>
|
2017-01-25 21:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StreamEditor for SoftVolumeApplier {
|
|
|
|
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]> {
|
|
|
|
let volume = (self.get_volume)();
|
|
|
|
if volume == 0xFFFF {
|
2017-01-20 19:39:05 +00:00
|
|
|
Cow::Borrowed(data)
|
|
|
|
} else {
|
|
|
|
Cow::Owned(data.iter()
|
|
|
|
.map(|&x| {
|
|
|
|
(x as i32
|
2017-01-25 21:22:53 +00:00
|
|
|
* volume as i32
|
2017-01-20 19:39:05 +00:00
|
|
|
/ 0xFFFF) as i16
|
|
|
|
})
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|