shady_audio/lib.rs
1//! # Description
2//! A crate which simplifies the data management of audio sources to be easily able
3//! to retrieve the frequency powers of the source.
4//!
5//! ### [cpal]
6//!
7//! This crate also re-exports [cpal] so there's no need to add [cpal] exclusively
8//! to your dependency list.
9//!
10//! # Example
11//!
12//! ## Simple workflow
13//! A simple workflow can look like this:
14//! ```
15//! use shady_audio::{SampleProcessor, BarProcessor, BarProcessorConfig, fetcher::DummyFetcher};
16//!
17//! // A fetcher with 2 channels
18//! let mut sample_processor = SampleProcessor::new(DummyFetcher::new(2));
19//! // Note: The bar procesor is intended to only work with the given sample processor.
20//! let mut bar_processor = BarProcessor::new(
21//! &sample_processor,
22//! BarProcessorConfig {
23//! amount_bars: std::num::NonZero::new(30).unwrap(),
24//! ..Default::default()
25//! }
26//! );
27//!
28//! loop {
29//! // let the sample processor process the next batch of samples
30//! sample_processor.process_next_samples();
31//!
32//! // let the bar processor convert the samples into "bar-values"
33//! // which are tried to be set in the range of `[0, 1]`.
34//! let bars = bar_processor.process_bars(&sample_processor);
35//!
36//! // we have two channels, so there should be two arrays:
37//! assert_eq!(bars.len(), 2);
38//! // each channel has the same amount of bars as set in the config
39//! assert_eq!(bars[0].len(), 30);
40//! assert_eq!(bars[1].len(), 30);
41//!
42//! break;
43//! }
44//! ```
45//!
46//! ## Multiple bar processors
47//! You can also create multiple [BarProcessor]s with different configs.
48//!
49//! ```
50//! use std::num::NonZero;
51//! use shady_audio::{SampleProcessor, BarProcessor, BarProcessorConfig, fetcher::DummyFetcher};
52//!
53//! let mut sample_processor = SampleProcessor::new(DummyFetcher::new(2));
54//!
55//! let mut bar_processor = BarProcessor::new(
56//! &sample_processor,
57//! BarProcessorConfig {
58//! amount_bars: NonZero::new(20).unwrap(),
59//! ..Default::default()
60//! }
61//! );
62//! let mut bar_processor2 = BarProcessor::new(
63//! &sample_processor,
64//! BarProcessorConfig {
65//! amount_bars: NonZero::new(11).unwrap(),
66//! ..Default::default()
67//! }
68//! );
69//!
70//! loop {
71//! // the sample processor needs to compute the new samples only once
72//! // for both bar processors (to reduce computation)
73//! sample_processor.process_next_samples();
74//!
75//! let bars = bar_processor.process_bars(&sample_processor);
76//! let bars2 = bar_processor2.process_bars(&sample_processor);
77//!
78//! // the dummy fetcher has two channels, so each bar processor should return a 2D array
79//! assert_eq!(bars.len(), 2);
80//! assert_eq!(bars2.len(), 2);
81//!
82//! // each channel has the same amount of bars as set in the config ...
83//! assert_eq!(bars[0].len(), 20);
84//! assert_eq!(bars[1].len(), 20);
85//!
86//! // ... same here
87//! assert_eq!(bars2[0].len(), 11);
88//! assert_eq!(bars2[1].len(), 11);
89//!
90//! break;
91//! }
92//! ```
93pub mod fetcher;
94pub mod util;
95
96mod bar_processor;
97mod interpolation;
98mod sample_processor;
99
100pub use bar_processor::{BarProcessor, BarProcessorConfig, InterpolationVariant};
101pub use cpal;
102pub use sample_processor::SampleProcessor;
103
104use cpal::SampleRate;
105
106type Hz = u16;
107
108/// The minimal frequency which humans can here (roughly)
109/// See: <https://en.wikipedia.org/wiki/Hearing_range>
110pub const MIN_HUMAN_FREQUENCY: Hz = 20;
111
112/// The maximal frequency which humans can here (roughly)
113/// See: <https://en.wikipedia.org/wiki/Hearing_range>
114pub const MAX_HUMAN_FREQUENCY: Hz = 20_000;
115
116/// The default sample rate for a fetcher.
117/// Fetchers are allowed to use this for orientation.
118pub const DEFAULT_SAMPLE_RATE: SampleRate = SampleRate(44_100);