Skip to content

Commit 6176710

Browse files
committed
New filter coeff's and only have one quality setting.
1 parent e31293c commit 6176710

File tree

5 files changed

+1428
-1346
lines changed

5 files changed

+1428
-1346
lines changed

playback/src/config.rs

Lines changed: 65 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ pub use crate::dither::{mk_ditherer, DithererBuilder, TriangularDitherer};
44

55
use crate::{
66
convert::i24,
7-
filter_coefficients::{
8-
HZ48000_HIGH, HZ48000_LOW, HZ88200_HIGH, HZ88200_LOW, HZ96000_HIGH, HZ96000_LOW,
9-
},
7+
filter_coefficients::{HZ48000_COEFFICIENTS, HZ88200_COEFFICIENTS, HZ96000_COEFFICIENTS},
108
RESAMPLER_INPUT_SIZE, SAMPLE_RATE,
119
};
1210

@@ -33,69 +31,6 @@ const HZ88200_INTERPOLATION_OUTPUT_SIZE: usize =
3331
const HZ96000_INTERPOLATION_OUTPUT_SIZE: usize =
3432
(RESAMPLER_INPUT_SIZE as f64 * (1.0 / HZ96000_RESAMPLE_FACTOR_RECIPROCAL)) as usize;
3533

36-
#[derive(Clone, Copy, Debug, Default)]
37-
pub enum InterpolationQuality {
38-
Low,
39-
#[default]
40-
High,
41-
}
42-
43-
impl FromStr for InterpolationQuality {
44-
type Err = ();
45-
46-
fn from_str(s: &str) -> Result<Self, Self::Err> {
47-
use InterpolationQuality::*;
48-
49-
match s.to_lowercase().as_ref() {
50-
"low" => Ok(Low),
51-
"high" => Ok(High),
52-
_ => Err(()),
53-
}
54-
}
55-
}
56-
57-
impl std::fmt::Display for InterpolationQuality {
58-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59-
use InterpolationQuality::*;
60-
61-
match self {
62-
Low => write!(f, "Low"),
63-
High => write!(f, "High"),
64-
}
65-
}
66-
}
67-
68-
impl InterpolationQuality {
69-
pub fn get_interpolation_coefficients(
70-
&self,
71-
mut coefficients: Vec<f64>,
72-
resample_factor_reciprocal: f64,
73-
) -> Vec<f64> {
74-
let mut coefficient_sum = 0.0;
75-
76-
for (index, coefficient) in coefficients.iter_mut().enumerate() {
77-
*coefficient *= Self::sinc((index as f64 * resample_factor_reciprocal).fract());
78-
79-
coefficient_sum += *coefficient;
80-
}
81-
82-
coefficients
83-
.iter_mut()
84-
.for_each(|coefficient| *coefficient /= coefficient_sum);
85-
86-
coefficients
87-
}
88-
89-
fn sinc(x: f64) -> f64 {
90-
if x.abs() < f64::EPSILON {
91-
1.0
92-
} else {
93-
let pi_x = std::f64::consts::PI * x;
94-
pi_x.sin() / pi_x
95-
}
96-
}
97-
}
98-
9934
#[derive(Clone, Copy, Debug, Default)]
10035
pub enum SampleRate {
10136
#[default]
@@ -152,14 +87,6 @@ impl std::fmt::Display for SampleRate {
15287
}
15388
}
15489

155-
#[derive(Debug, Default)]
156-
pub struct ResampleSpec {
157-
pub resample_factor_reciprocal: f64,
158-
pub interpolation_output_size: usize,
159-
pub high_coefficients: Vec<f64>,
160-
pub low_coefficients: Vec<f64>,
161-
}
162-
16390
impl SampleRate {
16491
pub fn as_u32(&self) -> u32 {
16592
use SampleRate::*;
@@ -207,41 +134,73 @@ impl SampleRate {
207134
}
208135
}
209136

210-
pub fn get_resample_spec(&self) -> ResampleSpec {
137+
pub fn get_resample_factor_reciprocal(&self) -> Option<f64> {
138+
use SampleRate::*;
139+
140+
match self {
141+
Hz44100 => None,
142+
Hz48000 => Some(HZ48000_RESAMPLE_FACTOR_RECIPROCAL),
143+
Hz88200 => Some(HZ88200_RESAMPLE_FACTOR_RECIPROCAL),
144+
Hz96000 => Some(HZ96000_RESAMPLE_FACTOR_RECIPROCAL),
145+
}
146+
}
147+
148+
pub fn get_interpolation_output_size(&self) -> Option<usize> {
211149
use SampleRate::*;
212150

213151
match self {
214-
// Dummy values to satisfy
215-
// the match statement.
216-
// 44.1kHz will be bypassed.
217-
Hz44100 => {
218-
warn!("Resampling 44.1kHz to 44.1kHz is just a really CPU intensive no-op, you should not be doing it");
219-
220-
ResampleSpec {
221-
resample_factor_reciprocal: 1.0,
222-
interpolation_output_size: RESAMPLER_INPUT_SIZE,
223-
high_coefficients: vec![],
224-
low_coefficients: vec![],
225-
}
226-
}
227-
Hz48000 => ResampleSpec {
228-
resample_factor_reciprocal: HZ48000_RESAMPLE_FACTOR_RECIPROCAL,
229-
interpolation_output_size: HZ48000_INTERPOLATION_OUTPUT_SIZE,
230-
high_coefficients: HZ48000_HIGH.to_vec(),
231-
low_coefficients: HZ48000_LOW.to_vec(),
232-
},
233-
Hz88200 => ResampleSpec {
234-
resample_factor_reciprocal: HZ88200_RESAMPLE_FACTOR_RECIPROCAL,
235-
interpolation_output_size: HZ88200_INTERPOLATION_OUTPUT_SIZE,
236-
high_coefficients: HZ88200_HIGH.to_vec(),
237-
low_coefficients: HZ88200_LOW.to_vec(),
238-
},
239-
Hz96000 => ResampleSpec {
240-
resample_factor_reciprocal: HZ96000_RESAMPLE_FACTOR_RECIPROCAL,
241-
interpolation_output_size: HZ96000_INTERPOLATION_OUTPUT_SIZE,
242-
high_coefficients: HZ96000_HIGH.to_vec(),
243-
low_coefficients: HZ96000_LOW.to_vec(),
244-
},
152+
Hz44100 => None,
153+
Hz48000 => Some(HZ48000_INTERPOLATION_OUTPUT_SIZE),
154+
Hz88200 => Some(HZ88200_INTERPOLATION_OUTPUT_SIZE),
155+
Hz96000 => Some(HZ96000_INTERPOLATION_OUTPUT_SIZE),
156+
}
157+
}
158+
159+
pub fn get_interpolation_coefficients(&self) -> Option<Vec<f64>> {
160+
use SampleRate::*;
161+
162+
match self {
163+
Hz44100 => None,
164+
Hz48000 => Some(Self::calculate_interpolation_coefficients(
165+
HZ48000_COEFFICIENTS.to_vec(),
166+
HZ48000_RESAMPLE_FACTOR_RECIPROCAL,
167+
)),
168+
Hz88200 => Some(Self::calculate_interpolation_coefficients(
169+
HZ88200_COEFFICIENTS.to_vec(),
170+
HZ88200_RESAMPLE_FACTOR_RECIPROCAL,
171+
)),
172+
Hz96000 => Some(Self::calculate_interpolation_coefficients(
173+
HZ96000_COEFFICIENTS.to_vec(),
174+
HZ96000_RESAMPLE_FACTOR_RECIPROCAL,
175+
)),
176+
}
177+
}
178+
179+
fn calculate_interpolation_coefficients(
180+
mut coefficients: Vec<f64>,
181+
resample_factor_reciprocal: f64,
182+
) -> Vec<f64> {
183+
let mut coefficient_sum = 0.0;
184+
185+
for (index, coefficient) in coefficients.iter_mut().enumerate() {
186+
*coefficient *= Self::sinc((index as f64 * resample_factor_reciprocal).fract());
187+
188+
coefficient_sum += *coefficient;
189+
}
190+
191+
coefficients
192+
.iter_mut()
193+
.for_each(|coefficient| *coefficient /= coefficient_sum);
194+
195+
coefficients
196+
}
197+
198+
fn sinc(x: f64) -> f64 {
199+
if x.abs() < f64::EPSILON {
200+
1.0
201+
} else {
202+
let pi_x = std::f64::consts::PI * x;
203+
pi_x.sin() / pi_x
245204
}
246205
}
247206
}
@@ -361,7 +320,6 @@ pub struct PlayerConfig {
361320
pub gapless: bool,
362321
pub passthrough: bool,
363322

364-
pub interpolation_quality: InterpolationQuality,
365323
pub sample_rate: SampleRate,
366324

367325
pub normalisation: bool,
@@ -384,7 +342,6 @@ impl Default for PlayerConfig {
384342
bitrate: Bitrate::default(),
385343
gapless: true,
386344
normalisation: false,
387-
interpolation_quality: InterpolationQuality::default(),
388345
sample_rate: SampleRate::default(),
389346
normalisation_type: NormalisationType::default(),
390347
normalisation_method: NormalisationMethod::default(),

0 commit comments

Comments
 (0)