audio_lab/lib.rs
1//! # audio-lab
2//!
3//! `audio-lab` is a Rust library for offline audio research and algorithm testing. It provides a compact set of tools for generating signals, analyzing and manipulating them, plotting results, playing them back, and saving them to audio files or NumPy arrays. It is designed for experiments, validation, and measurement workflows, not for real-time processing.
4//!
5//! ## Features
6//!
7//! - Generate test signals such as sweeps, sine waves, impulses, noise, and pulsed noise
8//! - Work with time-domain signals, frequency-domain signals, and spectrograms
9//! - Manipulate signals with trimming, gain, shifting, resampling, convolution, and deconvolution
10//! - Plot waveforms, spectra, and spectrograms in native windows
11//! - Play signals back for quick listening checks
12//! - Save and load audio files and NumPy arrays
13//!
14//! ## Installation
15//!
16//! Add `audio-lab` to your `Cargo.toml`:
17//!
18//! ```toml
19//! [dependencies]
20//! audio-lab = "0.1.0"
21//! ```
22//!
23//! ## Usage
24//!
25//! ### Generating a test signal
26//!
27//! ```rust
28//! use audio_lab::signal;
29//!
30//! fn main() -> Result<(), Box<dyn std::error::Error>> {
31//! let sweep = signal::generate_sweep(
32//! 48_000,
33//! 20.0..20_000.0,
34//! signal::SweepConfig::default(),
35//! )?;
36//!
37//! let noise = signal::generate_noise(
38//! 48_000,
39//! signal::NoiseConfig {
40//! spectrum: signal::Spectrum::Pink,
41//! amplitude: 0.2,
42//! ..Default::default()
43//! },
44//! )?;
45//!
46//! let test_signal = audio_lab::signal::mix_time!(sweep, noise)?;
47//! Ok(())
48//! }
49//! ```
50//!
51//! ### Plotting
52//!
53//! ```rust,no_run
54//! use audio_lab::{plot, signal};
55//!
56//! fn main() -> Result<(), Box<dyn std::error::Error>> {
57//! let signal = signal::generate_sine(48_000, 440.0, signal::SineConfig::default())?;
58//!
59//! plot::show_time("time", &[&signal])?;
60//! plot::show_freq("spectrum", &[&signal.into_freq()])?;
61//! Ok(())
62//! }
63//! ```
64//!
65//! ### Playback
66//!
67//! ```rust,no_run
68//! use audio_lab::signal::{self, playback};
69//!
70//! fn main() -> Result<(), Box<dyn std::error::Error>> {
71//! let signal = signal::generate_sine(48_000, 440.0, signal::SineConfig::default())?;
72//! playback::play(&signal)?;
73//! Ok(())
74//! }
75//! ```
76//!
77//! ### Saving to audio or NumPy
78//!
79//! ```rust,no_run
80//! use audio_lab::signal::{
81//! self,
82//! fs::{audio, npy},
83//! };
84//!
85//! fn main() -> Result<(), Box<dyn std::error::Error>> {
86//! let signal = signal::generate_sweep(
87//! 48_000,
88//! 20.0..20_000.0,
89//! signal::SweepConfig::default(),
90//! )?;
91//!
92//! audio::write_audio(&signal, "test.wav", audio::SampleFormat::Float32)?;
93//! npy::write_npy_time(&signal, "test.npy")?;
94//! Ok(())
95//! }
96//! ```
97//!
98//! ### Computing a spectrogram
99//!
100//! ```rust,no_run
101//! use audio_lab::{dsp, plot, signal};
102//!
103//! fn main() -> Result<(), Box<dyn std::error::Error>> {
104//! let signal = signal::generate_sweep(
105//! 48_000,
106//! 20.0..20_000.0,
107//! signal::SweepConfig::default(),
108//! )?;
109//!
110//! let spec = dsp::stft(&signal, &dsp::StftConfig::default())?;
111//! plot::show_spectrogram("spectrogram", &spec, Default::default())?;
112//! Ok(())
113//! }
114//! ```
115//!
116//! ### Estimating an impulse response
117//!
118//! ```rust
119//! use audio_lab::{dsp, signal};
120//!
121//! fn main() -> Result<(), Box<dyn std::error::Error>> {
122//! let excitation = signal::generate_sweep(
123//! 48_000,
124//! 20.0..20_000.0,
125//! signal::SweepConfig::default(),
126//! )?;
127//!
128//! let recorded_output = excitation.clone();
129//!
130//! let impulse_response = dsp::deconvolve(
131//! &recorded_output,
132//! &excitation,
133//! &dsp::DeconvolutionConfig::default(),
134//! )?;
135//!
136//! println!("{}", impulse_response);
137//! Ok(())
138//! }
139//! ```
140//!
141//! ## Modules
142//!
143//! - `signal`: signal types, generators, I/O, playback
144//! - `dsp`: DSP and measurement primitives
145//! - `plot`: plotting and visualization
146//!
147//! ## Cargo features
148//!
149//! All of these are enabled by default, except `serde`:
150//!
151//! - `plot`: the `plot` module. Pulls in egui, so turn it off if you don't need windows.
152//! - `playback`: `signal::playback`, plays signals on the default audio device.
153//! - `audio-file`: `signal::fs::audio`, reads and writes audio files.
154//! - `numpy`: `signal::fs::npy`, reads and writes NumPy `.npy` files.
155//! - `serde`: `Serialize` and `Deserialize` for the signal types.
156
157pub mod dsp;
158#[cfg(feature = "plot")]
159pub mod plot;
160pub mod signal;