audio_visualizer/
lib.rs

1/*
2MIT License
3
4Copyright (c) 2021 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//! Super basic and simple audio visualization library which is especially useful for developers to
25//! visually check audio samples, e.g. by waveform or spectrum. (So far) this library is not
26//! capable of doing nice visualizations for end users. Contributions are welcome.
27
28#![deny(
29    clippy::all,
30    clippy::cargo,
31    clippy::nursery,
32    // clippy::restriction,
33    // clippy::pedantic
34)]
35// now allow a few rules which are denied by the above statement
36// --> they are ridiculous and not necessary
37#![allow(
38    clippy::suboptimal_flops,
39    clippy::redundant_pub_crate,
40    clippy::fallible_impl_from,
41    clippy::multiple_crate_versions
42)]
43#![deny(missing_debug_implementations)]
44#![deny(rustdoc::all)]
45
46pub mod spectrum;
47pub mod waveform;
48
49pub mod dynamic;
50#[cfg(test)]
51mod tests;
52pub mod util;
53
54/// Describes the interleavement of audio data if
55/// it is not mono but stereo.
56#[derive(Debug, Copy, Clone)]
57pub enum ChannelInterleavement {
58    /// Stereo samples of one vector of audio data are alternating: left, right, left, right
59    LRLR,
60    /// Stereo samples of one vector of audio data are ordered like: left, left, ..., right, right
61    /// In this case the length must be a multiple of 2.
62    LLRR,
63}
64
65impl ChannelInterleavement {
66    pub const fn is_lrlr(&self) -> bool {
67        matches!(self, Self::LRLR)
68    }
69    pub const fn is_lllrr(&self) -> bool {
70        matches!(self, Self::LLRR)
71    }
72    /// Transforms the interleaved data into two vectors.
73    /// Returns a tuple. First/left value is left channel, second/right value is right channel.
74    pub fn to_channel_data(&self, interleaved_data: &[i16]) -> (Vec<i16>, Vec<i16>) {
75        let mut left_data = vec![];
76        let mut right_data = vec![];
77
78        if self.is_lrlr() {
79            let mut is_left = true;
80            for sample in interleaved_data {
81                if is_left {
82                    left_data.push(*sample);
83                } else {
84                    right_data.push(*sample)
85                }
86                is_left = !is_left;
87            }
88        } else {
89            let n = interleaved_data.len();
90            for sample_i in interleaved_data.iter().take(n / 2).copied() {
91                left_data.push(sample_i);
92            }
93            for sample_i in interleaved_data.iter().skip(n / 2).copied() {
94                right_data.push(sample_i);
95            }
96        }
97
98        (left_data, right_data)
99    }
100}
101
102/// Describes the number of channels of an audio stream.
103#[derive(Debug, Copy, Clone)]
104pub enum Channels {
105    Mono,
106    Stereo(ChannelInterleavement),
107}
108
109impl Channels {
110    pub const fn is_mono(&self) -> bool {
111        matches!(self, Self::Mono)
112    }
113
114    pub const fn is_stereo(&self) -> bool {
115        matches!(self, Self::Stereo(_))
116    }
117
118    pub fn stereo_interleavement(&self) -> ChannelInterleavement {
119        match self {
120            Self::Stereo(interleavmement) => *interleavmement,
121            _ => panic!("Not stereo"),
122        }
123    }
124}