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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! This module is not really part of Babycat's pubic API, but is made public
//! to make benchmarking Babycat internals easier.
//!
//! If you want to use Babycat to resample audio, you should decode
//! the audio into a [`FloatWaveform`][crate::FloatWaveform]
//! and then use the [`FloatWaveform.resample()`][crate::FloatWaveform#method.resample] method.

use crate::backend::errors::Error;

pub fn get<T: Copy>(v: &[T], frame: usize, channel_idx: usize, num_channels: usize) -> T {
    v[frame * num_channels + channel_idx]
}

pub fn validate_args(
    input_frame_rate_hz: u32,
    output_frame_rate_hz: u32,
    num_channels: u32,
) -> Result<(), Error> {
    if input_frame_rate_hz == 0 || output_frame_rate_hz == 0 {
        return Err(Error::WrongFrameRate(
            input_frame_rate_hz,
            output_frame_rate_hz,
        ));
    }
    if num_channels == 0 {
        return Err(Error::ResamplingError);
    }
    if (input_frame_rate_hz > output_frame_rate_hz)
        && (input_frame_rate_hz as f64 / output_frame_rate_hz as f64 > 256.0)
    {
        return Err(Error::WrongFrameRateRatio(
            input_frame_rate_hz,
            output_frame_rate_hz,
        ));
    }
    if output_frame_rate_hz as f64 / input_frame_rate_hz as f64 > 256.0 {
        return Err(Error::WrongFrameRateRatio(
            input_frame_rate_hz,
            output_frame_rate_hz,
        ));
    }
    Ok(())
}

pub fn get_num_output_frames(
    input_audio: &[f32],
    input_frame_rate_hz: u32,
    output_frame_rate_hz: u32,
    num_channels: u32,
) -> usize {
    ((input_audio.len() as f64 * output_frame_rate_hz as f64 / input_frame_rate_hz as f64).ceil()
        / num_channels as f64)
        .ceil() as usize
}