2022-01-14 22:31:29 +00:00
|
|
|
use std::{mem, str::FromStr, time::Duration};
|
2021-05-16 20:30:35 +00:00
|
|
|
|
2022-01-14 22:31:29 +00:00
|
|
|
pub use crate::dither::{mk_ditherer, DithererBuilder, TriangularDitherer};
|
|
|
|
use crate::{convert::i24, player::duration_to_coefficient};
|
2018-02-11 15:13:42 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
|
|
|
|
pub enum Bitrate {
|
|
|
|
Bitrate96,
|
|
|
|
Bitrate160,
|
|
|
|
Bitrate320,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Bitrate {
|
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
2021-03-18 21:06:43 +00:00
|
|
|
"96" => Ok(Self::Bitrate96),
|
|
|
|
"160" => Ok(Self::Bitrate160),
|
|
|
|
"320" => Ok(Self::Bitrate320),
|
2018-02-11 15:13:42 +00:00
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Bitrate {
|
2021-03-18 21:06:43 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::Bitrate160
|
2018-02-11 15:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 22:05:38 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
|
|
|
|
pub enum AudioFormat {
|
2021-05-30 18:09:39 +00:00
|
|
|
F64,
|
2021-03-12 22:05:38 +00:00
|
|
|
F32,
|
2021-03-13 22:43:24 +00:00
|
|
|
S32,
|
2021-03-16 23:00:27 +00:00
|
|
|
S24,
|
|
|
|
S24_3,
|
2021-03-12 22:05:38 +00:00
|
|
|
S16,
|
|
|
|
}
|
|
|
|
|
2021-05-31 20:32:39 +00:00
|
|
|
impl FromStr for AudioFormat {
|
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s.to_uppercase().as_ref() {
|
2021-05-30 18:09:39 +00:00
|
|
|
"F64" => Ok(Self::F64),
|
2021-03-16 23:00:27 +00:00
|
|
|
"F32" => Ok(Self::F32),
|
|
|
|
"S32" => Ok(Self::S32),
|
|
|
|
"S24" => Ok(Self::S24),
|
|
|
|
"S24_3" => Ok(Self::S24_3),
|
|
|
|
"S16" => Ok(Self::S16),
|
|
|
|
_ => Err(()),
|
2021-03-12 22:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AudioFormat {
|
2021-03-18 21:06:43 +00:00
|
|
|
fn default() -> Self {
|
2021-03-16 23:00:27 +00:00
|
|
|
Self::S16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioFormat {
|
|
|
|
// not used by all backends
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn size(&self) -> usize {
|
|
|
|
match self {
|
2021-05-30 18:09:39 +00:00
|
|
|
Self::F64 => mem::size_of::<f64>(),
|
|
|
|
Self::F32 => mem::size_of::<f32>(),
|
2021-03-16 23:00:27 +00:00
|
|
|
Self::S24_3 => mem::size_of::<i24>(),
|
|
|
|
Self::S16 => mem::size_of::<i16>(),
|
2021-03-18 21:06:43 +00:00
|
|
|
_ => mem::size_of::<i32>(), // S32 and S24 are both stored in i32
|
2021-03-16 23:00:27 +00:00
|
|
|
}
|
2021-03-12 22:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 20:38:19 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2021-01-21 19:16:05 +00:00
|
|
|
pub enum NormalisationType {
|
|
|
|
Album,
|
|
|
|
Track,
|
2021-09-20 17:22:02 +00:00
|
|
|
Auto,
|
2021-01-21 19:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for NormalisationType {
|
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2021-05-24 13:53:32 +00:00
|
|
|
match s.to_lowercase().as_ref() {
|
2021-03-18 21:06:43 +00:00
|
|
|
"album" => Ok(Self::Album),
|
|
|
|
"track" => Ok(Self::Track),
|
2021-09-20 17:22:02 +00:00
|
|
|
"auto" => Ok(Self::Auto),
|
2021-01-21 19:16:05 +00:00
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for NormalisationType {
|
2021-03-18 21:06:43 +00:00
|
|
|
fn default() -> Self {
|
2021-09-20 17:22:02 +00:00
|
|
|
Self::Auto
|
2021-01-21 19:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 20:38:19 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2021-02-24 20:39:42 +00:00
|
|
|
pub enum NormalisationMethod {
|
|
|
|
Basic,
|
|
|
|
Dynamic,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for NormalisationMethod {
|
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2021-05-24 13:53:32 +00:00
|
|
|
match s.to_lowercase().as_ref() {
|
2021-03-18 21:06:43 +00:00
|
|
|
"basic" => Ok(Self::Basic),
|
|
|
|
"dynamic" => Ok(Self::Dynamic),
|
2021-02-24 20:39:42 +00:00
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for NormalisationMethod {
|
2021-03-18 21:06:43 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::Dynamic
|
2021-02-24 20:39:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Implement dithering (#694)
Dithering lowers digital-to-analog conversion ("requantization") error, linearizing output, lowering distortion and replacing it with a constant, fixed noise level, which is more pleasant to the ear than the distortion.
Guidance:
- On S24, S24_3 and S24, the default is to use triangular dithering. Depending on personal preference you may use Gaussian dithering instead; it's not as good objectively, but it may be preferred subjectively if you are looking for a more "analog" sound akin to tape hiss.
- Advanced users who know that they have a DAC without noise shaping have a third option: high-passed dithering, which is like triangular dithering except that it moves dithering noise up in frequency where it is less audible. Note: 99% of DACs are of delta-sigma design with noise shaping, so unless you have a multibit / R2R DAC, or otherwise know what you are doing, this is not for you.
- Don't dither or shape noise on S32 or F32. On F32 it's not supported anyway (there are no integer conversions and so no rounding errors) and on S32 the noise level is so far down that it is simply inaudible even after volume normalisation and control.
New command line option:
--dither DITHER Specify the dither algorithm to use - [none, gpdf,
tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16
S24, S24_3 and 'none' for other formats.
Notes:
This PR also features some opportunistic improvements. Worthy of mention are:
- matching reference Vorbis sample conversion techniques for lower noise
- a cleanup of the convert API
2021-05-26 19:19:17 +00:00
|
|
|
#[derive(Clone)]
|
2018-02-11 15:13:42 +00:00
|
|
|
pub struct PlayerConfig {
|
|
|
|
pub bitrate: Bitrate,
|
2021-05-24 13:53:32 +00:00
|
|
|
pub gapless: bool,
|
Implement dithering (#694)
Dithering lowers digital-to-analog conversion ("requantization") error, linearizing output, lowering distortion and replacing it with a constant, fixed noise level, which is more pleasant to the ear than the distortion.
Guidance:
- On S24, S24_3 and S24, the default is to use triangular dithering. Depending on personal preference you may use Gaussian dithering instead; it's not as good objectively, but it may be preferred subjectively if you are looking for a more "analog" sound akin to tape hiss.
- Advanced users who know that they have a DAC without noise shaping have a third option: high-passed dithering, which is like triangular dithering except that it moves dithering noise up in frequency where it is less audible. Note: 99% of DACs are of delta-sigma design with noise shaping, so unless you have a multibit / R2R DAC, or otherwise know what you are doing, this is not for you.
- Don't dither or shape noise on S32 or F32. On F32 it's not supported anyway (there are no integer conversions and so no rounding errors) and on S32 the noise level is so far down that it is simply inaudible even after volume normalisation and control.
New command line option:
--dither DITHER Specify the dither algorithm to use - [none, gpdf,
tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16
S24, S24_3 and 'none' for other formats.
Notes:
This PR also features some opportunistic improvements. Worthy of mention are:
- matching reference Vorbis sample conversion techniques for lower noise
- a cleanup of the convert API
2021-05-26 19:19:17 +00:00
|
|
|
pub passthrough: bool,
|
|
|
|
|
2018-02-23 19:08:20 +00:00
|
|
|
pub normalisation: bool,
|
2021-01-21 19:16:05 +00:00
|
|
|
pub normalisation_type: NormalisationType,
|
2021-02-24 20:39:42 +00:00
|
|
|
pub normalisation_method: NormalisationMethod,
|
2022-01-17 21:57:30 +00:00
|
|
|
pub normalisation_pregain_db: f64,
|
2022-01-14 22:31:29 +00:00
|
|
|
pub normalisation_threshold_dbfs: f64,
|
|
|
|
pub normalisation_attack_cf: f64,
|
|
|
|
pub normalisation_release_cf: f64,
|
|
|
|
pub normalisation_knee_db: f64,
|
Implement dithering (#694)
Dithering lowers digital-to-analog conversion ("requantization") error, linearizing output, lowering distortion and replacing it with a constant, fixed noise level, which is more pleasant to the ear than the distortion.
Guidance:
- On S24, S24_3 and S24, the default is to use triangular dithering. Depending on personal preference you may use Gaussian dithering instead; it's not as good objectively, but it may be preferred subjectively if you are looking for a more "analog" sound akin to tape hiss.
- Advanced users who know that they have a DAC without noise shaping have a third option: high-passed dithering, which is like triangular dithering except that it moves dithering noise up in frequency where it is less audible. Note: 99% of DACs are of delta-sigma design with noise shaping, so unless you have a multibit / R2R DAC, or otherwise know what you are doing, this is not for you.
- Don't dither or shape noise on S32 or F32. On F32 it's not supported anyway (there are no integer conversions and so no rounding errors) and on S32 the noise level is so far down that it is simply inaudible even after volume normalisation and control.
New command line option:
--dither DITHER Specify the dither algorithm to use - [none, gpdf,
tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16
S24, S24_3 and 'none' for other formats.
Notes:
This PR also features some opportunistic improvements. Worthy of mention are:
- matching reference Vorbis sample conversion techniques for lower noise
- a cleanup of the convert API
2021-05-26 19:19:17 +00:00
|
|
|
|
|
|
|
// pass function pointers so they can be lazily instantiated *after* spawning a thread
|
|
|
|
// (thereby circumventing Send bounds that they might not satisfy)
|
|
|
|
pub ditherer: Option<DithererBuilder>,
|
2018-02-11 15:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PlayerConfig {
|
Implement dithering (#694)
Dithering lowers digital-to-analog conversion ("requantization") error, linearizing output, lowering distortion and replacing it with a constant, fixed noise level, which is more pleasant to the ear than the distortion.
Guidance:
- On S24, S24_3 and S24, the default is to use triangular dithering. Depending on personal preference you may use Gaussian dithering instead; it's not as good objectively, but it may be preferred subjectively if you are looking for a more "analog" sound akin to tape hiss.
- Advanced users who know that they have a DAC without noise shaping have a third option: high-passed dithering, which is like triangular dithering except that it moves dithering noise up in frequency where it is less audible. Note: 99% of DACs are of delta-sigma design with noise shaping, so unless you have a multibit / R2R DAC, or otherwise know what you are doing, this is not for you.
- Don't dither or shape noise on S32 or F32. On F32 it's not supported anyway (there are no integer conversions and so no rounding errors) and on S32 the noise level is so far down that it is simply inaudible even after volume normalisation and control.
New command line option:
--dither DITHER Specify the dither algorithm to use - [none, gpdf,
tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16
S24, S24_3 and 'none' for other formats.
Notes:
This PR also features some opportunistic improvements. Worthy of mention are:
- matching reference Vorbis sample conversion techniques for lower noise
- a cleanup of the convert API
2021-05-26 19:19:17 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2018-02-11 15:13:42 +00:00
|
|
|
bitrate: Bitrate::default(),
|
2021-05-24 13:53:32 +00:00
|
|
|
gapless: true,
|
2018-02-23 19:08:20 +00:00
|
|
|
normalisation: false,
|
2021-01-21 19:16:05 +00:00
|
|
|
normalisation_type: NormalisationType::default(),
|
2021-02-24 20:39:42 +00:00
|
|
|
normalisation_method: NormalisationMethod::default(),
|
2022-01-14 22:31:29 +00:00
|
|
|
normalisation_pregain_db: 0.0,
|
|
|
|
normalisation_threshold_dbfs: -2.0,
|
|
|
|
normalisation_attack_cf: duration_to_coefficient(Duration::from_millis(5)),
|
|
|
|
normalisation_release_cf: duration_to_coefficient(Duration::from_millis(100)),
|
|
|
|
normalisation_knee_db: 5.0,
|
2021-01-07 06:42:38 +00:00
|
|
|
passthrough: false,
|
Implement dithering (#694)
Dithering lowers digital-to-analog conversion ("requantization") error, linearizing output, lowering distortion and replacing it with a constant, fixed noise level, which is more pleasant to the ear than the distortion.
Guidance:
- On S24, S24_3 and S24, the default is to use triangular dithering. Depending on personal preference you may use Gaussian dithering instead; it's not as good objectively, but it may be preferred subjectively if you are looking for a more "analog" sound akin to tape hiss.
- Advanced users who know that they have a DAC without noise shaping have a third option: high-passed dithering, which is like triangular dithering except that it moves dithering noise up in frequency where it is less audible. Note: 99% of DACs are of delta-sigma design with noise shaping, so unless you have a multibit / R2R DAC, or otherwise know what you are doing, this is not for you.
- Don't dither or shape noise on S32 or F32. On F32 it's not supported anyway (there are no integer conversions and so no rounding errors) and on S32 the noise level is so far down that it is simply inaudible even after volume normalisation and control.
New command line option:
--dither DITHER Specify the dither algorithm to use - [none, gpdf,
tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16
S24, S24_3 and 'none' for other formats.
Notes:
This PR also features some opportunistic improvements. Worthy of mention are:
- matching reference Vorbis sample conversion techniques for lower noise
- a cleanup of the convert API
2021-05-26 19:19:17 +00:00
|
|
|
ditherer: Some(mk_ditherer::<TriangularDitherer>),
|
2018-02-11 15:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-26 01:50:41 +00:00
|
|
|
}
|
2021-05-24 13:53:32 +00:00
|
|
|
|
|
|
|
// fields are intended for volume control range in dB
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum VolumeCtrl {
|
2021-05-30 18:09:39 +00:00
|
|
|
Cubic(f64),
|
2021-05-24 13:53:32 +00:00
|
|
|
Fixed,
|
|
|
|
Linear,
|
2021-05-30 18:09:39 +00:00
|
|
|
Log(f64),
|
2021-05-24 13:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for VolumeCtrl {
|
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Self::from_str_with_range(s, Self::DEFAULT_DB_RANGE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for VolumeCtrl {
|
|
|
|
fn default() -> VolumeCtrl {
|
|
|
|
VolumeCtrl::Log(Self::DEFAULT_DB_RANGE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VolumeCtrl {
|
2021-05-31 20:32:39 +00:00
|
|
|
pub const MAX_VOLUME: u16 = u16::MAX;
|
2021-05-24 13:53:32 +00:00
|
|
|
|
|
|
|
// Taken from: https://www.dr-lex.be/info-stuff/volumecontrols.html
|
2021-05-30 18:09:39 +00:00
|
|
|
pub const DEFAULT_DB_RANGE: f64 = 60.0;
|
2021-05-24 13:53:32 +00:00
|
|
|
|
2021-05-30 18:09:39 +00:00
|
|
|
pub fn from_str_with_range(s: &str, db_range: f64) -> Result<Self, <Self as FromStr>::Err> {
|
2021-05-24 13:53:32 +00:00
|
|
|
use self::VolumeCtrl::*;
|
|
|
|
match s.to_lowercase().as_ref() {
|
|
|
|
"cubic" => Ok(Cubic(db_range)),
|
|
|
|
"fixed" => Ok(Fixed),
|
|
|
|
"linear" => Ok(Linear),
|
|
|
|
"log" => Ok(Log(db_range)),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|