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 super::Mixer;
|
|
|
|
use super::AudioFilter;
|
|
|
|
|
2017-01-20 19:39:05 +00:00
|
|
|
pub struct SoftMixer {
|
2017-02-03 16:08:35 +00:00
|
|
|
volume: Arc<AtomicUsize>
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SoftMixer {
|
|
|
|
pub fn new() -> SoftMixer {
|
|
|
|
SoftMixer {
|
2017-02-03 16:08:35 +00:00
|
|
|
volume: Arc::new(AtomicUsize::new(0xFFFF))
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mixer for SoftMixer {
|
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-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-02-01 21:16:28 +00:00
|
|
|
fn modify_stream(&self, data: &mut [i16]) {
|
2017-02-01 20:45:31 +00:00
|
|
|
let volume = self.volume.load(Ordering::Relaxed) as u16;
|
2017-02-01 21:16:28 +00:00
|
|
|
if volume != 0xFFFF {
|
|
|
|
for x in data.iter_mut() {
|
2017-02-03 13:19:30 +00:00
|
|
|
*x = (*x as i32 * volume as i32 / 0xFFFF) as i16;
|
2017-02-01 21:16:28 +00:00
|
|
|
}
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|