Skip to content

Commit

Permalink
Upgrade librespot to 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Icelk committed Mar 15, 2023
1 parent e640aca commit a323123
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 245 deletions.
296 changes: 187 additions & 109 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ tokio-compat-02 = "0.2.0"
tokio-stream = "0.1.7"
url = "2.2.2"
xdg = "2.2"
librespot-audio = { version = "0.2.0", default-features=false, features = ["with-tremor"] }
librespot-playback = { version = "0.2.0", default-features=false }
librespot-core = { version = "0.2.0"}
librespot-connect = { version = "0.2.0"}
librespot-audio = { version = "0.4", default-features = false }
librespot-playback = { version = "0.4", default-features = false }
librespot-core = { version = "0.4" }
librespot-discovery = { version = "0.4" }
librespot-connect = { version = "0.4" }
toml = "0.5.8"
color-eyre = "0.5"

Expand Down
13 changes: 3 additions & 10 deletions src/alsa_mixer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use librespot_playback::mixer::{AudioFilter, Mixer, MixerConfig};
use librespot_playback::mixer::{Mixer, MixerConfig};
use log::error;
use std::error::Error;

#[derive(Clone)]
pub struct AlsaMixer {
pub device: String,
pub mixer: String,
Expand Down Expand Up @@ -35,18 +36,14 @@ impl AlsaMixer {
}

impl Mixer for AlsaMixer {
fn open(_: Option<MixerConfig>) -> AlsaMixer {
fn open(_: MixerConfig) -> AlsaMixer {
AlsaMixer {
device: "default".to_string(),
mixer: "Master".to_string(),
linear_scaling: false,
}
}

fn start(&self) {}

fn stop(&self) {}

fn volume(&self) -> u16 {
let selem_id = alsa::mixer::SelemId::new(&self.mixer, 0);
match alsa::mixer::Mixer::new(&self.device, false)
Expand Down Expand Up @@ -79,8 +76,4 @@ impl Mixer for AlsaMixer {
Err(e) => error!("Couldn't set volume: {:?}", e),
}
}

fn get_audio_filter(&self) -> Option<Box<dyn AudioFilter + Send>> {
None
}
}
18 changes: 11 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ pub struct SharedConfigValues {

/// A custom pregain applied before sending the audio to the output device
#[structopt(long, value_name = "number")]
normalisation_pregain: Option<f32>,
normalisation_pregain: Option<f64>,

/// The port used for the Spotify Connect discovery
#[structopt(long, value_name = "number")]
Expand Down Expand Up @@ -647,7 +647,14 @@ pub(crate) fn get_internal_config(config: CliConfig) -> SpotifydConfig {
let cache = config
.shared_config
.cache_path
.map(|path| Cache::new(Some(&path), audio_cache.then_some(&path), size_limit))
.map(|path| {
Cache::new(
Some(&path),
Some(&path),
audio_cache.then_some(&path),
size_limit,
)
})
.transpose()
.unwrap_or_else(|e| {
warn!("Cache couldn't be initialized: {e}");
Expand Down Expand Up @@ -692,7 +699,7 @@ pub(crate) fn get_internal_config(config: CliConfig) -> SpotifydConfig {

let device_id = device_id(&device_name);

let normalisation_pregain = config.shared_config.normalisation_pregain.unwrap_or(0.0f32);
let normalisation_pregain = config.shared_config.normalisation_pregain.unwrap_or(0.0);

let dbus_type = config.shared_config.dbus_type.unwrap_or(DBusType::Session);
let autoplay = config.shared_config.autoplay;
Expand Down Expand Up @@ -759,10 +766,7 @@ pub(crate) fn get_internal_config(config: CliConfig) -> SpotifydConfig {
let pc = PlayerConfig {
bitrate,
normalisation: config.shared_config.volume_normalisation,
normalisation_pregain,
// Sensible default; the "default" supplied by PlayerConfig::default() sets this to -1.0,
// which turns the output to garbage.
normalisation_threshold: 1.0,
normalisation_pregain_db: normalisation_pregain,
gapless: true,
..Default::default()
};
Expand Down
58 changes: 15 additions & 43 deletions src/dbus_mpris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,48 +349,21 @@ async fn create_dbus_server(
let mv_device_name = device_name.clone();
let sp_client = Arc::clone(&spotify_api_client);
b.method("OpenUri", ("uri",), (), move |_, _, (uri,): (String,)| {
struct AnyContextId(Box<dyn PlayContextId>);

impl Id for AnyContextId {
fn id(&self) -> &str {
self.0.id()
}

fn _type(&self) -> Type {
self.0._type()
}

fn _type_static() -> Type
where
Self: Sized,
{
unreachable!("never called");
}

unsafe fn from_id_unchecked(_id: &str) -> Self
where
Self: Sized,
{
unreachable!("never called");
}
}
impl PlayContextId for AnyContextId {}

enum Uri {
Playable(Box<dyn PlayableId>),
Context(AnyContextId),
Playable(PlayableId<'static>),
Context(PlayContextId<'static>),
}

impl Uri {
fn from_id(id_type: Type, id: &str) -> Result<Uri, IdError> {
use Uri::*;
let uri = match id_type {
Type::Track => Playable(Box::new(TrackId::from_id(id)?)),
Type::Episode => Playable(Box::new(EpisodeId::from_id(id)?)),
Type::Artist => Context(AnyContextId(Box::new(ArtistId::from_id(id)?))),
Type::Album => Context(AnyContextId(Box::new(AlbumId::from_id(id)?))),
Type::Playlist => Context(AnyContextId(Box::new(PlaylistId::from_id(id)?))),
Type::Show => Context(AnyContextId(Box::new(ShowId::from_id(id)?))),
Type::Track => Playable(TrackId::from_id(id)?.into_static().into()),
Type::Episode => Playable(EpisodeId::from_id(id)?.into_static().into()),
Type::Artist => Context(ArtistId::from_id(id)?.into_static().into()),
Type::Album => Context(AlbumId::from_id(id)?.into_static().into()),
Type::Playlist => Context(PlaylistId::from_id(id)?.into_static().into()),
Type::Show => Context(ShowId::from_id(id)?.into_static().into()),
Type::User | Type::Collection => return Err(IdError::InvalidType),
};
Ok(uri)
Expand Down Expand Up @@ -423,15 +396,15 @@ async fn create_dbus_server(
let _ = sp_client.start_uris_playback(
Some(id.as_ref()),
Some(&device_id),
Some(Offset::for_position(0)),
Some(Offset::Position(0)),
None,
);
}
Uri::Context(id) => {
let _ = sp_client.start_context_playback(
&id,
id,
Some(&device_id),
Some(Offset::for_position(0)),
Some(Offset::Position(0)),
None,
);
}
Expand Down Expand Up @@ -665,15 +638,14 @@ async fn create_dbus_server(
if let Some(track_id) = track_id {
let item = match track_id.audio_type {
SpotifyAudioType::Track => {
let track_id = TrackId::from_id(&track_id.to_base62()).unwrap();
let track =
spotify_api_client.track(&track_id).map(PlayableItem::Track);
let track_id = TrackId::from_id(track_id.to_base62().unwrap()).unwrap();
let track = spotify_api_client.track(track_id).map(PlayableItem::Track);
Some(track)
}
SpotifyAudioType::Podcast => {
let id = EpisodeId::from_id(&track_id.to_base62()).unwrap();
let id = EpisodeId::from_id(track_id.to_base62().unwrap()).unwrap();
let episode = spotify_api_client
.get_an_episode(&id, None)
.get_an_episode(id, None)
.map(PlayableItem::Episode);
Some(episode)
}
Expand Down
30 changes: 16 additions & 14 deletions src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use futures::{
stream::Peekable,
Future, FutureExt, StreamExt,
};
use librespot_connect::{discovery::DiscoveryStream, spirc::Spirc};
use librespot_connect::spirc::Spirc;
use librespot_core::{
authentication::Credentials,
cache::Cache,
config::{ConnectConfig, DeviceType, SessionConfig, VolumeCtrl},
config::{ConnectConfig, DeviceType, SessionConfig},
session::{Session, SessionError},
};
use librespot_discovery::Discovery;
use librespot_playback::{
audio_backend::Sink,
config::{AudioFormat, PlayerConfig},
Expand All @@ -38,28 +39,28 @@ pub struct SpotifydState {
}

pub(crate) enum CredentialsProvider {
DiscoveryStream(Peekable<DiscoveryStream>),
Discovery(Peekable<Discovery>),
SpotifyCredentials(Credentials),
}

impl From<DiscoveryStream> for CredentialsProvider {
fn from(stream: DiscoveryStream) -> Self {
CredentialsProvider::DiscoveryStream(stream.peekable())
impl From<Discovery> for CredentialsProvider {
fn from(stream: Discovery) -> Self {
CredentialsProvider::Discovery(stream.peekable())
}
}

impl CredentialsProvider {
async fn get_credentials(&mut self) -> Credentials {
match self {
CredentialsProvider::DiscoveryStream(stream) => stream.next().await.unwrap(),
CredentialsProvider::Discovery(stream) => stream.next().await.unwrap(),
CredentialsProvider::SpotifyCredentials(creds) => creds.clone(),
}
}

// wait for an incoming connection if the underlying provider is a discovery stream
async fn incoming_connection(&mut self) {
match self {
CredentialsProvider::DiscoveryStream(stream) => {
CredentialsProvider::Discovery(stream) => {
let peeked = Pin::new(stream).peek().await;
if peeked.is_none() {
future::pending().await
Expand All @@ -76,7 +77,7 @@ pub(crate) struct MainLoop {
pub(crate) player_config: PlayerConfig,
pub(crate) session_config: SessionConfig,
pub(crate) autoplay: bool,
pub(crate) volume_ctrl: VolumeCtrl,
pub(crate) has_volume_ctrl: bool,
pub(crate) initial_volume: Option<u16>,
pub(crate) shell: String,
pub(crate) device_type: DeviceType,
Expand All @@ -94,7 +95,9 @@ impl MainLoop {
let session_config = self.session_config.clone();
let cache = self.spotifyd_state.cache.clone();

Session::connect(session_config, creds, cache).await
Session::connect(session_config, creds, cache, false)
.await
.map(|(session, _creds)| session)
}

pub(crate) async fn run(&mut self) {
Expand All @@ -119,13 +122,12 @@ impl MainLoop {
);

let mixer = (self.audio_setup.mixer)();
let audio_filter = mixer.get_audio_filter();
let backend = self.audio_setup.backend;
let audio_device = self.audio_setup.audio_device.clone();
let (player, mut event_channel) = Player::new(
self.player_config.clone(),
session.clone(),
audio_filter,
mixer.get_soft_volume(),
// TODO: dunno how to work with AudioFormat yet, maybe dig further if this
// doesn't work for all configurations
move || (backend)(audio_device, AudioFormat::default()),
Expand All @@ -136,8 +138,8 @@ impl MainLoop {
autoplay: self.autoplay,
name: self.spotifyd_state.device_name.clone(),
device_type: self.device_type,
volume: self.initial_volume.unwrap_or_else(|| mixer.volume()),
volume_ctrl: self.volume_ctrl.clone(),
initial_volume: self.initial_volume,
has_volume_ctrl: self.has_volume_ctrl,
},
session.clone(),
player,
Expand Down
14 changes: 3 additions & 11 deletions src/no_mixer.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
use librespot_playback::mixer::{AudioFilter, Mixer, MixerConfig};
use librespot_playback::mixer::{Mixer, MixerConfig};

pub struct NoMixer {}
pub struct NoMixer;

impl Mixer for NoMixer {
fn open(_: Option<MixerConfig>) -> NoMixer {
fn open(_: MixerConfig) -> NoMixer {
NoMixer {}
}

fn start(&self) {}

fn stop(&self) {}

fn volume(&self) -> u16 {
u16::MAX
}

fn set_volume(&self, _volume: u16) {}

fn get_audio_filter(&self) -> Option<Box<dyn AudioFilter + Send>> {
None
}
}
Loading

0 comments on commit a323123

Please sign in to comment.