audiofox 1.0.2

Cross platform audio capture made easy!
Documentation
// SPDX-License-Identifier: GPL-2.0-or-later WITH Classpath-exception-2.0
// Audiofox, the audio capturer!
// Copyright (c) 2026 foxberries

use alloc::sync::Arc;
use core::fmt::Debug;
use core::ops::Deref;

use crate::common::chunk::Chunk;

/// Common callback struct for returning chunks in `link()` calls. This struct is required to
/// derive `Clone` to be passable to `StackBlock` on macOS.
#[derive(Clone)]
pub struct AudioCallback(Arc<dyn AudioCallbackFn>);

/// Used to call the inner callback during audio processing.
impl Deref for AudioCallback {
    type Target = dyn AudioCallbackFn;
    fn deref(&self) -> &Self::Target {
        self.0.deref()
    }
}

/// Required for the `Command` enum on Linux and Windows.
impl Debug for AudioCallback {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "AudioCallback(..)")
    }
}

/// Converts a generic closure into an `AudioCallback`.
impl<F> From<F> for AudioCallback
where
    F: AudioCallbackFn,
{
    fn from(f: F) -> Self {
        Self(Arc::new(f))
    }
}

/// Common callback trait for returning a [`Chunk`] to the caller. [`link()`](crate::AudioManager::link)
/// itself does not use this trait to prevent issues with the type-hinter.
pub trait AudioCallbackFn: Fn(Chunk) + Send + Sync + 'static {}

/// Makes `AudioCallbackFn` interchangeable with `Fn(Chunk) + Send + Sync + 'static`.
impl<F> AudioCallbackFn for F where F: Fn(Chunk) + Send + Sync + 'static {}