-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
519 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//! Benchmarks a few general audio transformations relevant in the field of this | ||
//! crate. Useful to run this on a host platform to see the roughly costs. | ||
//! | ||
//! To run bench these, run `$ cargo bench "convert samples"` | ||
|
||
use beat_detector::util::{f32_sample_to_i16, i16_sample_to_f32, stereo_to_mono}; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use itertools::Itertools; | ||
use std::hint::black_box; | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let typical_sampling_rate = 44100; | ||
let sample_count = typical_sampling_rate; | ||
let mut samples_f32 = vec![0.0; sample_count]; | ||
samples_f32.fill_with(rand::random::<f32>); | ||
let mut samples_i16 = vec![0; sample_count]; | ||
samples_i16.fill_with(rand::random::<i16>); | ||
|
||
assert_eq!(samples_f32.len(), sample_count); | ||
assert_eq!(samples_i16.len(), sample_count); | ||
|
||
c.bench_function( | ||
&format!("{sample_count} convert samples (i16 to f32)"), | ||
|b| { | ||
b.iter(|| { | ||
let _res = black_box( | ||
samples_i16 | ||
.iter() | ||
.copied() | ||
.map(|s| i16_sample_to_f32(black_box(s))) | ||
.collect::<Vec<_>>(), | ||
); | ||
}) | ||
}, | ||
); | ||
|
||
c.bench_function( | ||
&format!("{sample_count} convert samples (i16 to f32 (just cast))"), | ||
|b| { | ||
b.iter(|| { | ||
let _res = black_box( | ||
samples_i16 | ||
.iter() | ||
.copied() | ||
.map(|s| black_box(s as f32)) | ||
.collect::<Vec<_>>(), | ||
); | ||
}) | ||
}, | ||
); | ||
|
||
c.bench_function( | ||
&format!("{sample_count} convert samples (f32 to i16)"), | ||
|b| { | ||
b.iter(|| { | ||
let _res = black_box( | ||
samples_f32 | ||
.iter() | ||
.copied() | ||
.map(|s| f32_sample_to_i16(black_box(s)).unwrap()) | ||
.collect::<Vec<_>>(), | ||
); | ||
}) | ||
}, | ||
); | ||
|
||
c.bench_function( | ||
&format!("{sample_count} convert samples (i16 stereo to mono)"), | ||
|b| { | ||
b.iter(|| { | ||
let _res = black_box( | ||
samples_i16 | ||
.iter() | ||
.copied() | ||
// We pretend the data is interleaved (LRLR pattern). | ||
.chunks(2) | ||
.into_iter() | ||
.map(|mut lr| { | ||
let l = lr.next().unwrap(); | ||
let r = lr | ||
.next() | ||
.expect("should have an even number of LRLR samples"); | ||
stereo_to_mono(black_box(l), black_box(r)) | ||
}) | ||
.collect::<Vec<_>>(), | ||
); | ||
}) | ||
}, | ||
); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use cpal::traits::DeviceTrait; | ||
|
||
#[path = "_modules/example_utils.rs"] | ||
mod example_utils; | ||
|
||
/// Minimal example to explore the structure of the audio input samples we get | ||
/// from cpal. This example does nothing with the beat detection library. | ||
fn main() { | ||
let input_device = example_utils::select_audio_device(); | ||
let supported_configs = input_device | ||
.supported_input_configs() | ||
.unwrap() | ||
.collect::<Vec<_>>(); | ||
println!("Supported input configs:"); | ||
for cfg in supported_configs { | ||
println!( | ||
"channels: {:>2}, format: {format:>3}, min_rate: {:06?}, max_rate: {:06?}, buffer: {:?}", | ||
cfg.channels(), | ||
cfg.min_sample_rate(), | ||
cfg.max_sample_rate(), | ||
cfg.buffer_size(), | ||
format = format!("{:?}", cfg.sample_format(),) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; | ||
use cpal::{BufferSize, StreamConfig}; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::sync::Arc; | ||
|
||
#[path = "_modules/example_utils.rs"] | ||
mod example_utils; | ||
|
||
/// Minimal example to explore the structure of the audio input samples we get | ||
/// from cpal. This example does nothing with the beat detection library. | ||
fn main() { | ||
let host = cpal::default_host(); | ||
let dev = host.default_input_device().unwrap(); | ||
let x = dev.supported_input_configs().unwrap().collect::<Vec<_>>(); | ||
dbg!(x); | ||
let cfg = dev.default_input_config().unwrap(); | ||
let cfg = StreamConfig { | ||
channels: 1, | ||
sample_rate: cfg.sample_rate(), | ||
buffer_size: BufferSize::Default, | ||
}; | ||
|
||
let mut max = i16::MIN; | ||
let mut min = i16::MAX; | ||
|
||
let stream = dev | ||
.build_input_stream( | ||
&cfg, | ||
// cpal is powerful enough to let us specify the type of the | ||
// samples, such as `&[i16]` or `&[f32]`. For i16, the value is | ||
// between `i16::MIN..i16::MAX`, for f32, the value is between | ||
// `-1.0..1.0`. Supported formats are in enum `SampleFormat`. | ||
move |samples: &[i16], _info| { | ||
for &sample in samples { | ||
max = core::cmp::max(max, sample); | ||
min = core::cmp::min(min, sample); | ||
println!("{sample:>6}, max={max:>6}, min={min:>6}"); | ||
} | ||
}, | ||
|e| eprintln!("error: {e:?}"), | ||
None, | ||
) | ||
.unwrap(); | ||
|
||
let stop_recording = Arc::new(AtomicBool::new(false)); | ||
{ | ||
let stop_recording = stop_recording.clone(); | ||
ctrlc::set_handler(move || { | ||
stop_recording.store(true, Ordering::SeqCst); | ||
}) | ||
.unwrap(); | ||
} | ||
|
||
stream.play().unwrap(); | ||
while !stop_recording.load(Ordering::SeqCst) {} | ||
stream.pause().unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.