lasprs 0.14.2

Library for Acoustic Signal Processing (Rust edition, with optional Python bindings via pyo3)
use super::{daqconfig::DaqChannel, *};
use crate::{StrictlyPositive, config::Flt};
use anyhow::Result;

/// Stream metadata. All information required for properly interpreting the raw
/// data that is coming from the stream.
#[cfg_attr(
    feature = "python-bindings",
    gen_stub_pyclass,
    pyclass(get_all, from_py_object)
)]
#[derive(Clone, Debug)]
pub struct StreamMetaData {
    /// Information for each channel in the stream
    pub channelInfo: Vec<DaqChannel>,

    /// The data type of the device [Number / voltage / Acoustic pressure / ...]
    pub rawDatatype: DataType,

    /// Sample rate in \[Hz\]
    pub samplerate: StrictlyPositive,

    /// The number of frames per block of data that comes in. Multiplied by
    /// channelInfo.len() we get the total number of samples that come in at
    /// each callback.
    pub framesPerBlock: usize,

    /// The quantity of input / output
    pub physicalIOQty: Qty,
}

impl StreamMetaData {
    /// Create new metadata object.
    /// # Args
    ///
    /// - `channelInfo`: DaqChannel configuraion for each channel in the stream
    /// - `rawdtype`: Datatype of raw stream data
    /// - `samplerate`: Sampling frequency \[Hz\]
    /// - `framesPerBlock`: Number of frames per callback
    /// - `ioqty` - Physical quantity of i/o
    pub fn new<'a, T>(
        channelInfo: T,
        rawdtype: DataType,
        samplerate: StrictlyPositive,
        framesPerBlock: usize,
        ioqty: Qty,
    ) -> StreamMetaData
    where
        T: IntoIterator<Item = &'a DaqChannel>,
    {
        let channelInfo = channelInfo
            .into_iter()
            .inspect(|ch| {
                assert!(
                    ch.enabled,
                    "Only enabled channels should be given as input to StreamMetaData"
                );
            })
            .cloned()
            .collect();
        StreamMetaData {
            channelInfo,
            rawDatatype: rawdtype,
            samplerate,
            framesPerBlock,
            physicalIOQty: ioqty,
        }
    }

    /// Returns the number of channels in the stream metadata.
    #[inline]
    pub fn nchannels(&self) -> usize {
        self.channelInfo.len()
    }

    /// Returns the names of all channels in the stream metadata.
    pub fn channelNames(&self) -> Vec<String> {
        self.channelInfo.iter().map(|ch| ch.name.clone()).collect()
    }
    /// Returns the sensitivities of all channels in the stream metadata.
    pub fn sensitivities(&self) -> Vec<f64> {
        self.channelInfo.iter().map(|ch| ch.sensitivity).collect()
    }
    /// Returns the quantities of all channels in the stream metadata.
    pub fn quantities(&self) -> Vec<Qty> {
        self.channelInfo.iter().map(|ch| ch.qty).collect()
    }
}

/// Simple getters for all sub-attributes of the stream metadata.
#[cfg(feature = "python-bindings")]
#[cfg_attr(feature = "python-bindings", pymethods)]
impl StreamMetaData {
    fn __repr__(&self) -> String {
        format!("{self:#?}")
    }
}