2021-05-30 18:09:39 +00:00
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
2018-04-25 17:29:50 +00:00
|
|
|
use std::sync::Arc;
|
2017-01-20 19:39:05 +00:00
|
|
|
|
2022-05-19 20:23:14 +00:00
|
|
|
use super::VolumeGetter;
|
2021-05-24 13:53:32 +00:00
|
|
|
use super::{MappedCtrl, VolumeCtrl};
|
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 {
|
2021-05-30 18:09:39 +00:00
|
|
|
// There is no AtomicF64, so we store the f64 as bits in a u64 field.
|
|
|
|
// It's much faster than a Mutex<f64>.
|
|
|
|
volume: Arc<AtomicU64>,
|
2021-05-24 13:53:32 +00:00
|
|
|
volume_ctrl: VolumeCtrl,
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 04:17:04 +00:00
|
|
|
impl Mixer for SoftMixer {
|
2021-05-24 13:53:32 +00:00
|
|
|
fn open(config: MixerConfig) -> Self {
|
|
|
|
let volume_ctrl = config.volume_ctrl;
|
|
|
|
info!("Mixing with softvol and volume control: {:?}", volume_ctrl);
|
|
|
|
|
|
|
|
Self {
|
2021-05-30 18:09:39 +00:00
|
|
|
volume: Arc::new(AtomicU64::new(f64::to_bits(0.5))),
|
2021-05-24 13:53:32 +00:00
|
|
|
volume_ctrl,
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-24 13:53:32 +00:00
|
|
|
|
2017-01-20 19:39:05 +00:00
|
|
|
fn volume(&self) -> u16 {
|
2021-05-30 18:09:39 +00:00
|
|
|
let mapped_volume = f64::from_bits(self.volume.load(Ordering::Relaxed));
|
2022-04-07 20:32:43 +00:00
|
|
|
self.volume_ctrl.as_unmapped(mapped_volume)
|
2017-01-25 21:22:53 +00:00
|
|
|
}
|
2021-05-24 13:53:32 +00:00
|
|
|
|
2017-01-25 21:22:53 +00:00
|
|
|
fn set_volume(&self, volume: u16) {
|
2021-05-24 13:53:32 +00:00
|
|
|
let mapped_volume = self.volume_ctrl.to_mapped(volume);
|
|
|
|
self.volume
|
|
|
|
.store(mapped_volume.to_bits(), Ordering::Relaxed)
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
2021-05-24 13:53:32 +00:00
|
|
|
|
2022-05-19 20:23:14 +00:00
|
|
|
fn get_soft_volume(&self) -> Box<dyn VolumeGetter + Send> {
|
|
|
|
Box::new(SoftVolume(self.volume.clone()))
|
2017-01-25 21:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 20:32:39 +00:00
|
|
|
impl SoftMixer {
|
2021-07-09 20:17:29 +00:00
|
|
|
pub const NAME: &'static str = "softvol";
|
2021-05-31 20:32:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 20:23:14 +00:00
|
|
|
struct SoftVolume(Arc<AtomicU64>);
|
2017-01-25 21:22:53 +00:00
|
|
|
|
2022-05-19 20:23:14 +00:00
|
|
|
impl VolumeGetter for SoftVolume {
|
|
|
|
fn attenuation_factor(&self) -> f64 {
|
|
|
|
f64::from_bits(self.0.load(Ordering::Relaxed))
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
2017-02-22 04:17:04 +00:00
|
|
|
}
|