Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

7.5 KB
Raw Blame
use cap_recording::feeds::microphone::MicrophoneSamples;
use cpal::{SampleFormat, StreamInstant};
use keyed_priority_queue::KeyedPriorityQueue;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::VecDeque;
use std::time::Duration;
use tauri::AppHandle;
use tauri_specta::Event;

const MAX_AMPLITUDE_F32: f64 = (u16::MAX / 2) as f64; // i16 max value
const ZERO_AMPLITUDE: u16 = 0;
const MIN_DB: f64 = -96.0;

#[derive(Deserialize, specta::Type, Serialize, tauri_specta::Event, Debug, Clone)]
pub struct AudioInputLevelChange(f64);

pub fn spawn_event_emitter(
    app_handle: AppHandle,
    audio_input_rx: flume::Receiver<MicrophoneSamples>,
) {
    let mut time_window = VolumeMeter::new(0.2);
    tokio::spawn(async move {
        while let Ok(samples) = audio_input_rx.recv_async().await {
            let floats = samples_to_f64(&samples);

            let db = db_fs(floats);

            time_window.push(samples.info.timestamp().capture, db);

            let max = time_window.max();

            AudioInputLevelChange(max).emit(&app_handle).ok();
        }
    });
}

// https://github.com/cgbur/meter/blob/master/src/time_window.rs
struct VolumeMeter {
    keep_duration: Duration, // secs
    maxes: KeyedPriorityQueue<StreamInstant, MinNonNan>,
    times: VecDeque<StreamInstant>,
}

impl VolumeMeter {
    pub fn new(keep_secs: f32) -> Self {
        Self {
            keep_duration: Duration::from_secs_f32(keep_secs),
            maxes: KeyedPriorityQueue::new(),
            times: Default::default(),
        }
    }

    pub fn push(&mut self, time: StreamInstant, value: f64) {
        let value = MinNonNan(-value);
        self.maxes.push(time, value);
        self.times.push_back(time);

        // The duration-window eviction below stalls permanently when a
        // device's capture clock is frozen or non-monotonic (duration_since
        // yields Zero forever or None), and this meter lives for the whole
        // process; without a hard cap it grows one entry per audio callback
        // until quit.
        const MAX_WINDOW_ENTRIES: usize = 512;
        while self.times.len() > MAX_WINDOW_ENTRIES {
            // A frozen clock produces duplicate keys that share one `maxes`
            // entry; only drop that entry once no duplicate remains queued.
            if let Some(front) = self.times.pop_front()
                && !self.times.contains(&front)
            {
                self.maxes.remove(&front);
            }
        }

        while let Some(time) = self
            .times
            .back()
            .unwrap()
            .duration_since(self.times.front().unwrap())
        {
            if time > self.keep_duration {
                self.maxes.remove(self.times.front().unwrap());
                self.times.pop_front();
            } else {
                break;
            }

            if self.times.len() <= 1 {
                break;
            }
        }
    }

    pub fn max(&self) -> f64 {
        -self.maxes.peek().map(|(_, db)| db.0).unwrap_or(-MIN_DB)
    }
}

#[derive(PartialEq)]
struct MinNonNan(f64);

impl Eq for MinNonNan {}

#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for MinNonNan {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl Ord for MinNonNan {
    fn cmp(&self, other: &MinNonNan) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}

use cpal::Sample;

fn db_fs(data: impl Iterator<Item = f64>) -> f64 {
    let max = data
        .map(|f| f.to_sample::<i16>().unsigned_abs())
        .max()
        .unwrap_or(ZERO_AMPLITUDE);

    (20.0 * (max as f64 / MAX_AMPLITUDE_F32).log10()).clamp(MIN_DB, 0.0)
}

fn samples_to_f64(samples: &MicrophoneSamples) -> impl Iterator<Item = f64> + use<'_> {
    samples
        .data
        .chunks(samples.format.sample_size())
        .map(|data| sample_to_f64(data, samples.format))
}

fn sample_to_f64(data: &[u8], format: SampleFormat) -> f64 {
    match format {
        SampleFormat::I8 => i8::from_ne_bytes([data[0]]) as f64 / i8::MAX as f64,
        SampleFormat::U8 => u8::from_ne_bytes([data[0]]).to_sample::<f64>(),
        SampleFormat::I16 => i16::from_ne_bytes([data[0], data[1]]) as f64 / i16::MAX as f64,
        SampleFormat::U16 => u16::from_ne_bytes([data[0], data[1]]).to_sample::<f64>(),
        SampleFormat::I32 => {
            i32::from_ne_bytes([data[0], data[1], data[2], data[3]]) as f64 / i32::MAX as f64
        }
        SampleFormat::U32 => {
            u32::from_ne_bytes([data[0], data[1], data[2], data[3]]).to_sample::<f64>()
        }
        SampleFormat::U64 => u64::from_ne_bytes([
            data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
        ])
        .to_sample::<f64>(),
        SampleFormat::I64 => {
            i64::from_ne_bytes([
                data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
            ]) as f64
                / i64::MAX as f64
        }
        SampleFormat::F32 => f32::from_ne_bytes([data[0], data[1], data[2], data[3]]) as f64,
        SampleFormat::F64 => f64::from_ne_bytes([
            data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
        ]),
        _ => 0.0,
    }
}

#[cfg(test)]
mod sample_tests {
    use super::*;

    #[test]
    fn unsigned_equilibrium_is_silent() {
        let cases = [
            (SampleFormat::U8, 128u8.to_ne_bytes().to_vec()),
            (SampleFormat::U16, 32768u16.to_ne_bytes().to_vec()),
            (SampleFormat::U32, (1u32 << 31).to_ne_bytes().to_vec()),
            (SampleFormat::U64, (1u64 << 63).to_ne_bytes().to_vec()),
        ];
        for (format, bytes) in cases {
            assert_eq!(sample_to_f64(&bytes, format), 0.0, "{format:?}");
        }
    }

    #[test]
    fn unsigned_negative_peaks_are_full_scale() {
        for format in [
            SampleFormat::U8,
            SampleFormat::U16,
            SampleFormat::U32,
            SampleFormat::U64,
        ] {
            assert_eq!(sample_to_f64(&[0; 8], format), -1.0, "{format:?}");
        }
    }

    #[test]
    fn unsigned_samples_preserve_both_sides_of_equilibrium() {
        assert_eq!(sample_to_f64(&[64], SampleFormat::U8), -0.5);
        assert_eq!(sample_to_f64(&[192], SampleFormat::U8), 0.5);
    }

    #[test]
    fn unsigned_microphone_silence_has_minimum_level() {
        assert_eq!(
            db_fs(std::iter::once(sample_to_f64(&[128], SampleFormat::U8))),
            MIN_DB
        );
        assert_eq!(
            db_fs(std::iter::once(sample_to_f64(&[0], SampleFormat::U8))),
            0.0
        );
    }

    #[test]
    fn signed_and_float_samples_keep_existing_levels() {
        let cases = [
            (
                SampleFormat::I8,
                63i8.to_ne_bytes().to_vec(),
                63.0 / i8::MAX as f64,
            ),
            (
                SampleFormat::I16,
                16383i16.to_ne_bytes().to_vec(),
                16383.0 / i16::MAX as f64,
            ),
            (
                SampleFormat::I32,
                123456i32.to_ne_bytes().to_vec(),
                123456.0 / i32::MAX as f64,
            ),
            (
                SampleFormat::I64,
                123456i64.to_ne_bytes().to_vec(),
                123456.0 / i64::MAX as f64,
            ),
            (SampleFormat::F32, 0.5f32.to_ne_bytes().to_vec(), 0.5),
            (SampleFormat::F64, 0.5f64.to_ne_bytes().to_vec(), 0.5),
        ];
        for (format, bytes, expected) in cases {
            assert_eq!(sample_to_f64(&bytes, format), expected, "{format:?}");
        }
    }
}