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