2017-02-01 20:45:31 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2018-04-25 17:29:50 +00:00
|
|
|
use std::sync::Arc;
|
2017-01-20 19:39:05 +00:00
|
|
|
|
2017-02-01 20:32:14 +00:00
|
|
|
use super::AudioFilter;
|
2018-09-11 16:53:18 +00:00
|
|
|
use super::{Mixer, MixerConfig};
|
2017-02-01 20:32:14 +00:00
|
|
|
|
2017-02-22 04:17:04 +00:00
|
|
|
#[derive(Clone)]
|
2017-01-20 19:39:05 +00:00
|
|
|
pub struct SoftMixer {
|
2018-02-26 01:50:41 +00:00
|
|
|
volume: Arc<AtomicUsize>,
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 04:17:04 +00:00
|
|
|
impl Mixer for SoftMixer {
|
2018-09-11 16:53:18 +00:00
|
|
|
fn open(_: Option<MixerConfig>) -> SoftMixer {
|
2017-01-20 19:39:05 +00:00
|
|
|
SoftMixer {
|
2018-02-26 01:50:41 +00:00
|
|
|
volume: Arc::new(AtomicUsize::new(0xFFFF)),
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-26 01:50:41 +00:00
|
|
|
fn start(&self) {}
|
|
|
|
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
|
|
|
}
|
2019-10-08 09:31:18 +00:00
|
|
|
fn get_audio_filter(&self) -> Option<Box<dyn AudioFilter + Send>> {
|
2018-02-26 01:50:41 +00:00
|
|
|
Some(Box::new(SoftVolumeApplier {
|
|
|
|
volume: self.volume.clone(),
|
|
|
|
}))
|
2017-01-25 21:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SoftVolumeApplier {
|
2018-02-26 01:50:41 +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 {
|
2021-02-24 20:39:42 +00:00
|
|
|
fn modify_stream(&self, data: &mut [f32]) {
|
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 {
|
2021-02-24 20:39:42 +00:00
|
|
|
let volume_factor = volume as f64 / 0xFFFF as f64;
|
2017-02-01 21:16:28 +00:00
|
|
|
for x in data.iter_mut() {
|
2021-02-24 20:39:42 +00:00
|
|
|
*x = (*x as f64 * volume_factor) as f32;
|
2017-02-01 21:16:28 +00:00
|
|
|
}
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 04:17:04 +00:00
|
|
|
}
|