mkaudiolibrary 0.1.4

Modular audio processing library including MKAU plugin format based on Rust.
Documentation
use crate::buffer::{CircularBuffer, PushBuffer};

///Convolve input data into window, then returns into output. Barrier exists for multithreading.
pub fn convolution(input : & PushBuffer<f64>, output : & mut PushBuffer<f64>,
                   window : Box<[f64]>, state : & bool)
{
    let mut internal_buffer = PushBuffer::<f64>::new(window.len());
    let mut index = 0;
    internal_buffer.index = internal_buffer.len() - 1;

    while ! * state
    {
        (0..internal_buffer.len()).for_each(|x| output[index] = internal_buffer[x] * window[x]);
        if index < input.len() { index += 1; }
    }
}

///Set saturation character for one side.
pub struct Saturation
{
    ths : f64,
    lim : f64,
    gap : f64,
    rad_pow : f64,
    org : f64
}
impl Saturation
{
    ///New saturation data.
    pub fn new(ths : f64, lim : f64) -> Self
    {
        let gap = lim - ths;
        let side = ((gap * 2.0).powi(2) + gap.powi(2)).sqrt();
        let ang = 180.0 - 2.0 * (gap * 2.0 / side).asin();
        let rad = side / (2.0 * (ang/2.0).sin());
        let rad_pow = rad.powi(2);
        let org = lim - rad;

        return Self { ths, lim, gap, rad_pow, org }
    }
}
///Process each data for non-linear behavior.
pub fn saturation(data : f64, upper : Saturation, lower : Saturation) -> f64
{
    return if data > upper.lim + upper.gap { upper.lim }
    else if data > upper.ths { upper.org + (upper.rad_pow - (upper.lim - data).powi(2)).sqrt() }
    else if data < lower.lim - lower.gap { upper.lim }
    else if data < lower.ths { lower.org - (lower.rad_pow - (lower.lim - data).powi(2)).sqrt() }
    else { 0.0 }
}
///Read and write data from CircularBuffer I/O, process in PushBuffer. Next to pause by buffer size. State to stop.
pub fn process(input : & mut CircularBuffer<f64>, output : & mut CircularBuffer<f64>, next : & bool, state : & bool,
               process : fn(input : & PushBuffer<f64>, output : & mut PushBuffer<f64>, state : & bool))
{
    let mut input_buffer = PushBuffer::<f64>::new(input.len());
    let mut output_buffer = PushBuffer::<f64>::new(output.len());

    let mut index = 0;

    while ! * state
    {
        if * next
        {
            (0..input.len()).for_each(|_|
                {
                    input_buffer.push((* input).next());
                });
            process(& input_buffer, & mut output_buffer, state);
            (0..input.len()).for_each(|_|
                {
                    if index < output.len()
                    {
                        output.push(output_buffer[index]);
                        index += 1;
                    }
                    else { output.push(output_buffer[output.len()]); }
                });
        }
    }
}