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
|
|
|
|
2017-02-01 20:32:14 +00:00
|
|
|
use super::AudioFilter;
|
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));
|
2021-05-24 13:53:32 +00:00
|
|
|
self.volume_ctrl.from_mapped(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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 20:32:39 +00:00
|
|
|
impl SoftMixer {
|
|
|
|
pub const NAME: &'static str = "softmixer";
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:22:53 +00:00
|
|
|
struct SoftVolumeApplier {
|
2021-05-30 18:09:39 +00:00
|
|
|
volume: Arc<AtomicU64>,
|
2017-01-25 21:22:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 13:20:31 +00:00
|
|
|
impl AudioFilter for SoftVolumeApplier {
|
2021-05-30 18:09:39 +00:00
|
|
|
fn modify_stream(&self, data: &mut [f64]) {
|
|
|
|
let volume = f64::from_bits(self.volume.load(Ordering::Relaxed));
|
2021-05-24 13:53:32 +00:00
|
|
|
if volume < 1.0 {
|
2017-02-01 21:16:28 +00:00
|
|
|
for x in data.iter_mut() {
|
2021-05-30 18:09:39 +00:00
|
|
|
*x *= volume;
|
2017-02-01 21:16:28 +00:00
|
|
|
}
|
2017-01-20 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 04:17:04 +00:00
|
|
|
}
|