Skip to main content

audio_visualizer/
lib.rs

1/*
2MIT License
3
4Copyright (c) 2026 Philipp Schuster
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24//! Audio visualization library for developers: quickly check audio samples
25//! visually, e.g. while working on audio algorithms.
26//!
27//! All functionality works on mono `f32` samples, typically amplitudes in
28//! `[-1.0, 1.0]`. Split interleaved stereo data with [`deinterleave_stereo`]
29//! first.
30//!
31//! - **Static images**: [`WaveformVisualizer`] renders samples as PNG file
32//!   or SVG string; [`SpectrumVisualizer`] does the same for frequency
33//!   spectra.
34//! - **Live visualization**: [`live`] records audio from an input device
35//!   and shows the waveform plus a custom transformation (e.g. lowpass filter
36//!   or spectrum) in a real-time GUI window.
37//!
38//! Waveforms show the min/max amplitude per pixel column rather than
39//! individual samples; [`WaveformVisualizer`] explains why.
40
41#![deny(
42    clippy::all,
43    clippy::cargo,
44    clippy::nursery,
45    // clippy::restriction,
46    // clippy::pedantic
47)]
48// now allow a few rules which are denied by the above statement
49// --> they are ridiculous and not necessary
50#![allow(
51    clippy::suboptimal_flops,
52    clippy::redundant_pub_crate,
53    clippy::fallible_impl_from,
54    clippy::multiple_crate_versions
55)]
56#![deny(missing_debug_implementations)]
57#![deny(rustdoc::all)]
58
59pub mod live;
60mod spectrum;
61mod waveform;
62
63mod chart;
64mod error;
65#[cfg(test)]
66mod tests;
67
68pub use error::Error;
69pub use spectrum::Spectrum as SpectrumVisualizer;
70pub use waveform::Waveform as WaveformVisualizer;
71
72/// Splits interleaved stereo samples (left, right, left, right, ...) into a
73/// left and a right channel vector.
74///
75/// # Panics
76/// Panics if the number of samples is odd.
77pub fn deinterleave_stereo(samples: &[f32]) -> (Vec<f32>, Vec<f32>) {
78    let (pairs, rest) = samples.as_chunks::<2>();
79    assert!(
80        rest.is_empty(),
81        "stereo data must have an even number of samples"
82    );
83    pairs.iter().map(|[l, r]| (*l, *r)).unzip()
84}