Skip to content

Commit 8aaab0a

Browse files
committed
More touch ups
1 parent a331729 commit 8aaab0a

5 files changed

Lines changed: 1497 additions & 1463 deletions

File tree

playback/src/config.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ use crate::{
88
RESAMPLER_INPUT_SIZE, SAMPLE_RATE,
99
};
1010

11-
// Reciprocals allow us to multiply instead of divide during interpolation.
12-
const HZ48000_RESAMPLE_FACTOR_RECIPROCAL: f64 = SAMPLE_RATE as f64 / 48_000.0;
13-
const HZ88200_RESAMPLE_FACTOR_RECIPROCAL: f64 = SAMPLE_RATE as f64 / 88_200.0;
14-
const HZ96000_RESAMPLE_FACTOR_RECIPROCAL: f64 = SAMPLE_RATE as f64 / 96_000.0;
11+
const HZ48000_RESAMPLE_FACTOR: f64 = 48_000.0 / (SAMPLE_RATE as f64);
12+
const HZ88200_RESAMPLE_FACTOR: f64 = 88_200.0 / (SAMPLE_RATE as f64);
13+
const HZ96000_RESAMPLE_FACTOR: f64 = 96_000.0 / (SAMPLE_RATE as f64);
14+
15+
// Reciprocals allow us to multiply instead of divide during normal interpolation.
16+
const HZ48000_RESAMPLE_FACTOR_RECIPROCAL: f64 = 1.0 / HZ48000_RESAMPLE_FACTOR;
17+
const HZ88200_RESAMPLE_FACTOR_RECIPROCAL: f64 = 1.0 / HZ88200_RESAMPLE_FACTOR;
18+
const HZ96000_RESAMPLE_FACTOR_RECIPROCAL: f64 = 1.0 / HZ96000_RESAMPLE_FACTOR;
1519

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

2832
const HZ88200_INTERPOLATION_OUTPUT_SIZE: usize =
29-
(RESAMPLER_INPUT_SIZE as f64 * (1.0 / HZ88200_RESAMPLE_FACTOR_RECIPROCAL)) as usize;
33+
(RESAMPLER_INPUT_SIZE as f64 * HZ88200_RESAMPLE_FACTOR) as usize;
3034

3135
const HZ96000_INTERPOLATION_OUTPUT_SIZE: usize =
32-
(RESAMPLER_INPUT_SIZE as f64 * (1.0 / HZ96000_RESAMPLE_FACTOR_RECIPROCAL)) as usize;
36+
(RESAMPLER_INPUT_SIZE as f64 * HZ96000_RESAMPLE_FACTOR) as usize;
3337

3438
#[derive(Clone, Copy, Debug, Default)]
3539
pub enum SampleRate {
@@ -134,6 +138,17 @@ impl SampleRate {
134138
}
135139
}
136140

141+
pub fn get_resample_factor(&self) -> Option<f64> {
142+
use SampleRate::*;
143+
144+
match self {
145+
Hz44100 => None,
146+
Hz48000 => Some(HZ48000_RESAMPLE_FACTOR),
147+
Hz88200 => Some(HZ88200_RESAMPLE_FACTOR),
148+
Hz96000 => Some(HZ96000_RESAMPLE_FACTOR),
149+
}
150+
}
151+
137152
pub fn get_resample_factor_reciprocal(&self) -> Option<f64> {
138153
use SampleRate::*;
139154

0 commit comments

Comments
 (0)