mkaudiolibrary/lib.rs
1//! # mkaudiolibrary
2//!
3//! A Rust library for real-time audio signal processing, featuring analog modeling
4//! through numeric functions and circuit simulation via Modified Nodal Analysis (MNA).
5//!
6//! Every audio-sample-carrying API in this crate - [`dsp`], [`processor`],
7//! [`audiofile`], [`host`], [`realtime`] - operates on plain `f32` (matching
8//! VST3/AU/MKAP plugin hosting's native sample format, and [`sim`]'s own
9//! `f32` circuit models), passed as plain `&[f32]`/`&mut [f32]` slices or
10//! the unlocked [`buffer::Buffer<f32>`] wrapper. None of this crate's own
11//! processors share buffers across threads internally, so nothing here
12//! pays for locking that has no reader on the other end; if you do need to
13//! hand a buffer to another thread (e.g. an audio thread feeding a UI
14//! meter), wrap it yourself with whatever synchronization actually fits.
15//!
16//! ## Features
17//!
18//! - **Analog modeling** - asymmetric log-curve saturation, and (via the
19//! `sim` feature) physically-modeled vacuum tube saturation
20//! - **Circuit simulation** - real-time MNA solver for reactive circuits
21//! ([`dsp::Circuit`]), plus a full tube/diode/transistor + Wave Digital
22//! Filter circuit modeling toolkit (see [`sim`], `sim` feature)
23//! - **DSP primitives** - convolution, IIR (biquad/Butterworth) and FIR
24//! (windowed-sinc) filtering, compression/limiting/gating, delay, integer
25//! oversampling, and FFT-based sample-rate conversion - all with
26//! pre-allocated scratch buffers so steady-state processing never allocates
27//! - **SIMD** - AVX2+FMA/SSE2 (`x86_64`) or NEON (`aarch64`) hot loops (optional `simd` feature)
28//! - **Time-frequency analysis** - DFT, FFT, DCT, STFT, CWT, CQT, mel spectrograms (see [`tf`])
29//! - **Audio file I/O** - WAV, BWF, and AIFF format support with Buffer integration
30//! - **Plugin hosting** - load and run MKAP, VST3, and AUv2 (macOS) plugins through one trait (see [`host`])
31//! - **MKAP plugin system** - native format for building your own modular processing chains
32//! - **Real-time streaming** - RTAudio-style API with real CoreAudio/WASAPI/ALSA backends (optional `realtime` feature)
33//!
34//! ## Quick Start
35//!
36//! ```ignore
37//! use mkaudiolibrary::audiofile::{AudioFile, FileFormat};
38//! use mkaudiolibrary::dsp::Compression;
39//!
40//! // Load an audio file
41//! let mut audio = AudioFile::default();
42//! audio.load("input.wav");
43//!
44//! // Convert to buffers for processing
45//! let mut buffers = audio.to_buffers();
46//!
47//! // Apply compression
48//! let mut comp = Compression::new(audio.sample_rate());
49//! comp.threshold = -12.0;
50//! comp.ratio = 4.0;
51//! for buffer in &mut buffers {
52//! let mut output = mkaudiolibrary::buffer::Buffer::new(buffer.len());
53//! comp.run(buffer, &mut output);
54//! // ... use processed output
55//! }
56//!
57//! // Save result
58//! audio.save("output.wav", FileFormat::Wav);
59//! ```
60//!
61//! ## Modules
62//!
63//! - [`buffer`] - plain (unlocked) audio sample containers (`Buffer`, `PushBuffer`, `CircularBuffer`)
64//! - [`dsp`] - digital signal processing components
65//! - [`simd`] - SIMD-accelerated primitives used by `dsp` and `tf`'s hot loops
66//! - [`tf`] - time-frequency analysis (DFT, FFT, DCT, STFT, CWT, CQT, mel spectrograms)
67//! - [`sim`] - analog circuit simulation: tubes, diodes, transistors, WDF networks (`sim` feature)
68//! - [`audiofile`] - WAV/BWF/AIFF file loading and saving
69//! - [`processor`] - MKAP plugin format and dynamic loading
70//! - [`host`] - unified plugin hosting for MKAP, VST3 (`vst3` feature), and AUv2 (`au` feature)
71//! - [`realtime`] - real-time audio streaming I/O (requires `realtime` feature)
72//!
73//! ## DSP Processing Examples
74//!
75//! ### Saturation (Analog Modeling)
76//!
77//! ```ignore
78//! use mkaudiolibrary::dsp::Saturation;
79//! use mkaudiolibrary::buffer::Buffer;
80//!
81//! let sat = Saturation::new(10.0, 10.0, 1.0, 1.0, 0.0, false);
82//! let input = Buffer::from_slice(&[0.0, 0.5, 1.0, -0.5, -1.0]);
83//! let mut output = Buffer::new(5);
84//! sat.run(&input, &mut output);
85//! ```
86//!
87//! ### Circuit Simulation
88//!
89//! ```ignore
90//! use mkaudiolibrary::dsp::{Circuit, Resistor, Capacitor};
91//!
92//! // RC lowpass filter: R=1kΩ, C=1µF, fc ≈ 159Hz
93//! let mut circuit = Circuit::new(44100.0, 2);
94//! circuit.add_component(Box::new(Resistor::new(1, 2, 1000.0)));
95//! circuit.add_component(Box::new(Capacitor::new(2, 0, 1e-6)));
96//! circuit.preprocess(10.0);
97//!
98//! let output = circuit.process(1.0, 2); // Input 1V, probe node 2
99//! ```
100//!
101//! ### Dynamics Processing
102//!
103//! ```ignore
104//! use mkaudiolibrary::dsp::{Compression, Limit, Gate};
105//!
106//! let mut compressor = Compression::new(44100.0);
107//! compressor.threshold = -20.0; // dB
108//! compressor.ratio = 4.0; // 4:1
109//!
110//! let mut limiter = Limit::new(44100.0);
111//! limiter.ceiling = -0.1; // dB
112//!
113//! let mut gate = Gate::new(44100.0);
114//! gate.threshold = -40.0; // dB
115//! ```
116//!
117//! ### IIR/FIR Filtering
118//!
119//! ```ignore
120//! use mkaudiolibrary::dsp::iir::{Biquad, BiquadType};
121//! use mkaudiolibrary::dsp::fir::FirFilter;
122//!
123//! let mut lowpass = Biquad::new(BiquadType::LowPass, 44100.0, 1000.0, 0.707, 0.0);
124//! let y = lowpass.process(0.5);
125//!
126//! let mut fir_lp = FirFilter::lowpass(101, 44100.0, 1000.0);
127//! let y2 = fir_lp.process(0.5);
128//! ```
129//!
130//! ## License
131//!
132//! MIT License.
133
134/// Plain (unlocked) audio sample containers for real-time processing.
135///
136/// Provides `Buffer`, `PushBuffer`, and `CircularBuffer` types with no
137/// internal locking - see the module docs for why.
138pub mod buffer;
139
140/// Digital signal processing components for real-time audio.
141///
142/// Includes convolution, IIR/FIR filtering, saturation, circuit simulation,
143/// compression/limiting/gating, delay, oversampling, and resampling.
144pub mod dsp;
145
146/// MKAU plugin format for modular audio processing chains.
147///
148/// Provides the `Processor` trait and dynamic plugin loading.
149pub mod processor;
150
151/// Audio file loading and saving for WAV and AIFF formats.
152///
153/// Supports 8/16/24/32-bit audio with normalized f32 sample representation.
154pub mod audiofile;
155
156/// Real-time audio streaming I/O inspired by RTAudio.
157///
158/// Provides cross-platform audio input/output with a callback-based API.
159/// Enable with the `realtime` feature flag.
160#[cfg(feature = "realtime")]
161pub mod realtime;
162
163#[cfg(all(target_os = "macos", any(feature = "realtime", feature = "au")))]
164mod macos_util;
165
166/// SIMD-accelerated primitives for hot per-sample DSP/TF loops.
167///
168/// Falls back to scalar loops unless the `simd` feature is enabled.
169pub mod simd;
170
171/// Plugin hosting for MKAP, VST3, and AUv2 formats.
172///
173/// Provides a unified `HostedPlugin` trait and scanning API on top of the
174/// native MKAP loader (always available), VST3 hosting (`vst3` feature),
175/// and AUv2 hosting on macOS (`au` feature).
176pub mod host;
177
178/// Time-frequency analysis: DFT, FFT, DCT, STFT/multi-resolution STFT,
179/// CWT, CQT, and mel spectrograms.
180pub mod tf;
181
182/// Real-time analog circuit simulation (tubes, diodes, transistors, WDF
183/// networks), merged in from [libmksim](https://github.com/mkaudio-company/libmksim).
184///
185/// Enable with the `sim` feature flag; `sim-avx2`/`sim-avx512`/`sim-neon`
186/// additionally enable SIMD backends for its internal math.
187#[cfg(feature = "sim")]
188pub mod sim;