Skip to content

Commit

Permalink
Put it all together
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLG1979 committed Jun 22, 2023
1 parent e1ea400 commit efec96b
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 314 deletions.
4 changes: 1 addition & 3 deletions playback/src/audio_backend/gstreamer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use std::sync::Arc;

use super::{Open, Sink, SinkAsBytes, SinkError, SinkResult};

use crate::{
config::AudioFormat, convert::Converter, decoder::AudioPacket, NUM_CHANNELS,
};
use crate::{config::AudioFormat, convert::Converter, decoder::AudioPacket, NUM_CHANNELS};

pub struct GstreamerSink {
appsrc: gst_app::AppSrc,
Expand Down
5 changes: 4 additions & 1 deletion playback/src/audio_backend/jackaudio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ impl Open for JackSink {
if format != AudioFormat::F32 {
warn!("JACK currently does not support {format:?} output");
}
info!("Using JACK sink with format {:?}, sample rate: {sample_rate}", AudioFormat::F32);
info!(
"Using JACK sink with format {:?}, sample rate: {sample_rate}",
AudioFormat::F32
);

let client_name = client_name.unwrap_or_else(|| "librespot".to_string());
let (client, _status) =
Expand Down
12 changes: 9 additions & 3 deletions playback/src/audio_backend/portaudio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ impl<'a> Sink for PortAudioSink<'a> {
}

match self {
Self::F32(stream, parameters, sample_rate) => start_sink!(ref mut stream, ref parameters, ref sample_rate),
Self::S32(stream, parameters, sample_rate) => start_sink!(ref mut stream, ref parameters, ref sample_rate),
Self::S16(stream, parameters, sample_rate) => start_sink!(ref mut stream, ref parameters, ref sample_rate),
Self::F32(stream, parameters, sample_rate) => {
start_sink!(ref mut stream, ref parameters, ref sample_rate)
}
Self::S32(stream, parameters, sample_rate) => {
start_sink!(ref mut stream, ref parameters, ref sample_rate)
}
Self::S16(stream, parameters, sample_rate) => {
start_sink!(ref mut stream, ref parameters, ref sample_rate)
}
};

Ok(())
Expand Down
13 changes: 11 additions & 2 deletions playback/src/audio_backend/rodio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ pub fn mk_rodio(device: Option<String>, format: AudioFormat, sample_rate: u32) -
}

#[cfg(feature = "rodiojack-backend")]
pub fn mk_rodiojack(device: Option<String>, format: AudioFormat, sample_rate: u32) -> Box<dyn Sink> {
pub fn mk_rodiojack(
device: Option<String>,
format: AudioFormat,
sample_rate: u32,
) -> Box<dyn Sink> {
Box::new(open(
cpal::host_from_id(cpal::HostId::Jack).unwrap(),
device,
Expand Down Expand Up @@ -166,7 +170,12 @@ fn create_sink(
Ok((sink, stream))
}

pub fn open(host: cpal::Host, device: Option<String>, format: AudioFormat, sample_rate: u32) -> RodioSink {
pub fn open(
host: cpal::Host,
device: Option<String>,
format: AudioFormat,
sample_rate: u32,
) -> RodioSink {
info!(
"Using Rodio sink with format {format:?} and cpal host: {}",
host.id().name()
Expand Down
18 changes: 14 additions & 4 deletions playback/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{mem, str::FromStr, time::Duration};

pub use crate::dither::{mk_ditherer, DithererBuilder, TriangularDitherer};
use crate::{convert::i24, player::duration_to_coefficient, RESAMPLER_INPUT_SIZE, SAMPLE_RATE};
use crate::{convert::i24, 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;
Expand Down Expand Up @@ -152,10 +152,12 @@ impl FromStr for SampleRate {
fn from_str(s: &str) -> Result<Self, Self::Err> {
use SampleRate::*;

let lowercase_input = s.to_lowercase();

// Match against both the actual
// stringified value and how most
// humans would write a sample rate.
match s.to_uppercase().as_ref() {
match lowercase_input.as_str() {
"hz44100" | "44100hz" | "44100" | "44.1khz" => Ok(Hz44100),
"hz48000" | "48000hz" | "48000" | "48khz" => Ok(Hz48000),
"hz88200" | "88200hz" | "88200" | "88.2khz" => Ok(Hz88200),
Expand Down Expand Up @@ -348,6 +350,9 @@ pub struct PlayerConfig {
pub gapless: bool,
pub passthrough: bool,

pub interpolation_quality: InterpolationQuality,
pub sample_rate: SampleRate,

pub normalisation: bool,
pub normalisation_type: NormalisationType,
pub normalisation_method: NormalisationMethod,
Expand All @@ -368,12 +373,17 @@ impl Default for PlayerConfig {
bitrate: Bitrate::default(),
gapless: true,
normalisation: false,
interpolation_quality: InterpolationQuality::default(),
sample_rate: SampleRate::default(),
normalisation_type: NormalisationType::default(),
normalisation_method: NormalisationMethod::default(),
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)),
// Dummy value. We can't use the default because
// no matter what it's dependent on the sample rate.
normalisation_attack_cf: 0.0,
// Same with release.
normalisation_release_cf: 0.0,
normalisation_knee_db: 5.0,
passthrough: false,
ditherer: Some(mk_ditherer::<TriangularDitherer>),
Expand Down
2 changes: 1 addition & 1 deletion playback/src/mixer/alsamixer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::player::{db_to_ratio, ratio_to_db};
use crate::{db_to_ratio, ratio_to_db};

use super::mappings::{LogMapping, MappedCtrl, VolumeMapping};
use super::{Mixer, MixerConfig, VolumeCtrl};
Expand Down
2 changes: 1 addition & 1 deletion playback/src/mixer/mappings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::VolumeCtrl;
use crate::player::db_to_ratio;
use crate::db_to_ratio;

pub trait MappedCtrl {
fn to_mapped(&self, volume: u16) -> f64;
Expand Down
4 changes: 2 additions & 2 deletions playback/src/mixer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub trait Mixer: Send {
fn set_volume(&self, volume: u16);
fn volume(&self) -> u16;

fn get_soft_volume(&self) -> Box<dyn VolumeGetter + Send> {
fn get_soft_volume(&self) -> Box<dyn VolumeGetter> {
Box::new(NoOpVolume)
}
}

pub trait VolumeGetter {
pub trait VolumeGetter: Send {
fn attenuation_factor(&self) -> f64;
}

Expand Down
2 changes: 1 addition & 1 deletion playback/src/mixer/softmixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Mixer for SoftMixer {
.store(mapped_volume.to_bits(), Ordering::Relaxed)
}

fn get_soft_volume(&self) -> Box<dyn VolumeGetter + Send> {
fn get_soft_volume(&self) -> Box<dyn VolumeGetter> {
Box::new(SoftVolume(self.volume.clone()))
}
}
Expand Down
Loading

0 comments on commit efec96b

Please sign in to comment.