2021-05-24 13:53:32 +00:00
|
|
|
use super::player::db_to_ratio;
|
2021-04-13 08:29:34 +00:00
|
|
|
use crate::convert::i24;
|
2021-05-16 20:30:35 +00:00
|
|
|
|
2021-03-12 22:05:38 +00:00
|
|
|
use std::convert::TryFrom;
|
2021-03-16 23:00:27 +00:00
|
|
|
use std::mem;
|
2018-02-11 15:13:42 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
#[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 {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&String> for AudioFormat {
|
|
|
|
type Error = ();
|
|
|
|
fn try_from(s: &String) -> Result<Self, Self::Error> {
|
|
|
|
match s.to_uppercase().as_str() {
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 19:16:05 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum NormalisationType {
|
|
|
|
Album,
|
|
|
|
Track,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-21 19:16:05 +00:00
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for NormalisationType {
|
2021-03-18 21:06:43 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::Album
|
2021-01-21 19:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 20:39:42 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 15:13:42 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct PlayerConfig {
|
|
|
|
pub bitrate: Bitrate,
|
2021-05-24 13:53:32 +00:00
|
|
|
pub gapless: 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,
|
2018-02-23 19:08:20 +00:00
|
|
|
pub normalisation_pregain: f32,
|
2021-02-24 20:39:42 +00:00
|
|
|
pub normalisation_threshold: f32,
|
|
|
|
pub normalisation_attack: f32,
|
|
|
|
pub normalisation_release: f32,
|
2021-03-14 13:28:16 +00:00
|
|
|
pub normalisation_knee: f32,
|
2021-01-07 06:42:38 +00:00
|
|
|
pub passthrough: bool,
|
2018-02-11 15:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PlayerConfig {
|
|
|
|
fn default() -> PlayerConfig {
|
|
|
|
PlayerConfig {
|
|
|
|
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(),
|
2018-02-23 19:08:20 +00:00
|
|
|
normalisation_pregain: 0.0,
|
2021-05-24 13:53:32 +00:00
|
|
|
normalisation_threshold: db_to_ratio(-1.0),
|
2021-02-24 20:39:42 +00:00
|
|
|
normalisation_attack: 0.005,
|
|
|
|
normalisation_release: 0.1,
|
2021-03-14 13:28:16 +00:00
|
|
|
normalisation_knee: 1.0,
|
2021-01-07 06:42:38 +00:00
|
|
|
passthrough: false,
|
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 {
|
|
|
|
Cubic(f32),
|
|
|
|
Fixed,
|
|
|
|
Linear,
|
|
|
|
Log(f32),
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
pub const MAX_VOLUME: u16 = std::u16::MAX;
|
|
|
|
|
|
|
|
// Taken from: https://www.dr-lex.be/info-stuff/volumecontrols.html
|
|
|
|
pub const DEFAULT_DB_RANGE: f32 = 60.0;
|
|
|
|
|
|
|
|
pub fn from_str_with_range(s: &str, db_range: f32) -> Result<Self, <Self as FromStr>::Err> {
|
|
|
|
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(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|