Skip to content

Commit

Permalink
More touch ups
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLG1979 committed Sep 3, 2023
1 parent a331729 commit 8aaab0a
Show file tree
Hide file tree
Showing 5 changed files with 1,497 additions and 1,463 deletions.
29 changes: 22 additions & 7 deletions playback/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ use crate::{
RESAMPLER_INPUT_SIZE, SAMPLE_RATE,
};

// Reciprocals allow us to multiply instead of divide during interpolation.
const HZ48000_RESAMPLE_FACTOR_RECIPROCAL: f64 = SAMPLE_RATE as f64 / 48_000.0;
const HZ88200_RESAMPLE_FACTOR_RECIPROCAL: f64 = SAMPLE_RATE as f64 / 88_200.0;
const HZ96000_RESAMPLE_FACTOR_RECIPROCAL: f64 = SAMPLE_RATE as f64 / 96_000.0;
const HZ48000_RESAMPLE_FACTOR: f64 = 48_000.0 / (SAMPLE_RATE as f64);
const HZ88200_RESAMPLE_FACTOR: f64 = 88_200.0 / (SAMPLE_RATE as f64);
const HZ96000_RESAMPLE_FACTOR: f64 = 96_000.0 / (SAMPLE_RATE as f64);

// Reciprocals allow us to multiply instead of divide during normal interpolation.
const HZ48000_RESAMPLE_FACTOR_RECIPROCAL: f64 = 1.0 / HZ48000_RESAMPLE_FACTOR;
const HZ88200_RESAMPLE_FACTOR_RECIPROCAL: f64 = 1.0 / HZ88200_RESAMPLE_FACTOR;
const HZ96000_RESAMPLE_FACTOR_RECIPROCAL: f64 = 1.0 / HZ96000_RESAMPLE_FACTOR;

// sample rate * channels
const HZ44100_SAMPLES_PER_SECOND: f64 = 44_100.0 * 2.0;
Expand All @@ -23,13 +27,13 @@ const HZ96000_SAMPLES_PER_SECOND: f64 = 96_000.0 * 2.0;
// to be integers, which is a very good thing. That means no fractional samples
// which translates to much better interpolation.
const HZ48000_INTERPOLATION_OUTPUT_SIZE: usize =
(RESAMPLER_INPUT_SIZE as f64 * (1.0 / HZ48000_RESAMPLE_FACTOR_RECIPROCAL)) as usize;
(RESAMPLER_INPUT_SIZE as f64 * HZ48000_RESAMPLE_FACTOR) as usize;

const HZ88200_INTERPOLATION_OUTPUT_SIZE: usize =
(RESAMPLER_INPUT_SIZE as f64 * (1.0 / HZ88200_RESAMPLE_FACTOR_RECIPROCAL)) as usize;
(RESAMPLER_INPUT_SIZE as f64 * HZ88200_RESAMPLE_FACTOR) as usize;

const HZ96000_INTERPOLATION_OUTPUT_SIZE: usize =
(RESAMPLER_INPUT_SIZE as f64 * (1.0 / HZ96000_RESAMPLE_FACTOR_RECIPROCAL)) as usize;
(RESAMPLER_INPUT_SIZE as f64 * HZ96000_RESAMPLE_FACTOR) as usize;

#[derive(Clone, Copy, Debug, Default)]
pub enum SampleRate {
Expand Down Expand Up @@ -134,6 +138,17 @@ impl SampleRate {
}
}

pub fn get_resample_factor(&self) -> Option<f64> {
use SampleRate::*;

match self {
Hz44100 => None,
Hz48000 => Some(HZ48000_RESAMPLE_FACTOR),
Hz88200 => Some(HZ88200_RESAMPLE_FACTOR),
Hz96000 => Some(HZ96000_RESAMPLE_FACTOR),
}
}

pub fn get_resample_factor_reciprocal(&self) -> Option<f64> {
use SampleRate::*;

Expand Down
Loading

0 comments on commit 8aaab0a

Please sign in to comment.