use std::sync::atomic::{AtomicBool, Ordering};
use super::seq_cell::SeqCell;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OutputLevels {
pub window: u32,
pub left: f32,
pub right: f32,
}
pub struct OutputTap {
enabled: AtomicBool,
window: SeqCell<3>,
}
impl Default for OutputTap {
fn default() -> Self {
Self::new()
}
}
impl OutputTap {
pub fn new() -> Self {
Self {
enabled: AtomicBool::new(false),
window: SeqCell::new([0; 3]),
}
}
pub fn set_enabled(&self, enabled: bool) {
self.enabled.store(enabled, Ordering::Relaxed);
}
pub fn is_enabled(&self) -> bool {
self.enabled.load(Ordering::Relaxed)
}
pub fn levels(&self) -> Option<OutputLevels> {
let [window, left, right] = self.window.read()?;
(window != 0).then(|| OutputLevels {
window,
left: f32::from_bits(left),
right: f32::from_bits(right),
})
}
pub fn publish(&self, left: f32, right: f32) {
let window = self.window.read().map_or(0, |[window, _, _]| window);
self.window.write([
window.wrapping_add(1).max(1),
left.to_bits(),
right.to_bits(),
]);
}
}