2017-01-20 19:39:05 +00:00
|
|
|
use std::borrow::Cow;
|
2017-02-01 20:45:31 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2017-01-20 19:39:05 +00:00
|
|
|
|
2017-02-01 20:32:14 +00:00
|
|
|
use spirc::UpdateMessageSender;
|
|
|
|
use spirc::UpdateMessage;
|
|
|
|
|
|
|
|
use super::Mixer;
|
|
|
|
use super::AudioFilter;
|
|
|
|
|
2017-01-20 19:39:05 +00:00
|
|
|
pub struct SoftMixer {
|
2017-02-01 20:45:31 +00:00
|
|
|
volume: Arc<AtomicUsize>,
|
2017-02-01 20:32:14 +00:00
|
|
|
tx: Option<UpdateMessageSender>
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SoftMixer {
|
|
|
|
pub fn new() -> SoftMixer {
|
|
|
|
SoftMixer {
|
2017-02-01 20:45:31 +00:00
|
|
|
volume: Arc::new(AtomicUsize::new(0xFFFF)),
|
2017-02-01 20:32:14 +00:00
|
|
|
tx: None
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mixer for SoftMixer {
|
2017-02-01 20:32:14 +00:00
|
|
|
fn init(&mut self, tx: UpdateMessageSender) {
|
|
|
|
self.tx = Some(tx);
|
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-02-01 20:45:31 +00:00
|
|
|
self.volume.load(Ordering::Relaxed) as u16
|
2017-01-25 21:22:53 +00:00
|
|
|
}
|
|
|
|
fn set_volume(&self, volume: u16) {
|
2017-02-01 20:45:31 +00:00
|
|
|
self.volume.store(volume as usize, Ordering::Relaxed);
|
2017-02-01 20:32:14 +00:00
|
|
|
let tx = self.tx.as_ref().expect("SoftMixer not initialized");
|
|
|
|
tx.send(UpdateMessage).unwrap();
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
2017-01-27 13:20:31 +00:00
|
|
|
fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> {
|
2017-02-01 20:45:31 +00:00
|
|
|
Some(Box::new(SoftVolumeApplier { volume: self.volume.clone() }))
|
2017-01-25 21:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SoftVolumeApplier {
|
2017-02-01 20:45:31 +00:00
|
|
|
volume: Arc<AtomicUsize>
|
2017-01-25 21:22:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 13:20:31 +00:00
|
|
|
impl AudioFilter for SoftVolumeApplier {
|
2017-01-25 21:22:53 +00:00
|
|
|
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]> {
|
2017-02-01 20:45:31 +00:00
|
|
|
let volume = self.volume.load(Ordering::Relaxed) as u16;
|
2017-01-25 21:22:53 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|