summaryrefslogtreecommitdiff
path: root/benches/transforms.rs
blob: 8771492c118231238f2d62f3a7f44aba285e2c8d (plain)
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
#[macro_use]
extern crate bencher;

extern crate rusty_microphone;

use bencher::Bencher;

use std::f32::consts::PI;

const SAMPLE_RATE: f32 = 44100.0;
const FRAMES: u16 = 512;


fn sin_arg(f: f32, t: f32, phase: f32) -> f32 {
    2.0 as f32 * PI * f * t + phase
}

fn sample_sinusoud(amplitude: f32, frequency: f32, phase: f32) -> Vec<f32> {
    (0..FRAMES)
        .map(|x| {
            let t = f32::from(x) / SAMPLE_RATE;
            sin_arg(frequency, t, phase).sin() * amplitude
        }).collect()
}

fn bench_correlation_on_sine_wave(b: &mut Bencher) {
    let frequency = 440.0f32; //concert A
    let samples = sample_sinusoud(1.0, frequency, 0.0);
    
    b.iter(|| {
        rusty_microphone::transforms::find_fundamental_frequency(&samples, SAMPLE_RATE)
    })
}
benchmark_group!(transforms, bench_correlation_on_sine_wave);
benchmark_main!(transforms);