From 62c3cb77cd31b22b90c3772a128f3f963551a58e Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sun, 27 Nov 2011 17:29:33 +0000 Subject: [PATCH] Add settings for spotify bitrate and volume normalisation --- spotifyblob/blob/spotifyclient.cpp | 32 +++++++++--- spotifyblob/blob/spotifyclient.h | 3 +- spotifyblob/common/spotifymessages.proto | 14 +++++ src/internet/spotifyserver.cpp | 15 +++++- src/internet/spotifyserver.h | 4 +- src/internet/spotifyservice.cpp | 24 +++++++-- src/internet/spotifyservice.h | 4 +- src/internet/spotifysettingspage.cpp | 15 +++++- src/internet/spotifysettingspage.ui | 30 ++++++++++- src/translations/translations.pot | 65 ++++++++++++++---------- 10 files changed, 159 insertions(+), 47 deletions(-) diff --git a/spotifyblob/blob/spotifyclient.cpp b/spotifyblob/blob/spotifyclient.cpp index 088936c28..dfebe6c41 100644 --- a/spotifyblob/blob/spotifyclient.cpp +++ b/spotifyblob/blob/spotifyclient.cpp @@ -266,8 +266,7 @@ void SpotifyClient::SendSearchResponse(sp_search* result) { void SpotifyClient::HandleMessage(const spotify_pb::SpotifyMessage& message) { if (message.has_login_request()) { - const spotify_pb::LoginRequest& r = message.login_request(); - Login(QStringFromStdString(r.username()), QStringFromStdString(r.password())); + Login(message.login_request()); } else if (message.has_load_playlist_request()) { LoadPlaylist(message.load_playlist_request()); } else if (message.has_playback_request()) { @@ -282,10 +281,28 @@ void SpotifyClient::HandleMessage(const spotify_pb::SpotifyMessage& message) { SyncPlaylist(message.sync_playlist_request()); } else if (message.has_browse_album_request()) { BrowseAlbum(QStringFromStdString(message.browse_album_request().uri())); + } else if (message.has_set_playback_settings_request()) { + SetPlaybackSettings(message.set_playback_settings_request()); } } -void SpotifyClient::Login(const QString& username, const QString& password) { +void SpotifyClient::SetPlaybackSettings(const spotify_pb::PlaybackSettings& req) { + sp_bitrate bitrate = SP_BITRATE_320k; + switch (req.bitrate()) { + case spotify_pb::Bitrate96k: bitrate = SP_BITRATE_96k; break; + case spotify_pb::Bitrate160k: bitrate = SP_BITRATE_160k; break; + case spotify_pb::Bitrate320k: bitrate = SP_BITRATE_320k; break; + } + + qLog(Debug) << "Setting playback settings: bitrate" + << bitrate << "normalisation" << req.volume_normalisation(); + + sp_session_preferred_bitrate(session_, bitrate); + sp_session_preferred_offline_bitrate(session_, bitrate, false); + sp_session_set_volume_normalization(session_, req.volume_normalisation()); +} + +void SpotifyClient::Login(const spotify_pb::LoginRequest& req) { sp_error error = sp_session_create(&spotify_config_, &session_); if (error != SP_ERROR_OK) { qLog(Warning) << "Failed to create session" << sp_error_message(error); @@ -293,10 +310,9 @@ void SpotifyClient::Login(const QString& username, const QString& password) { return; } - sp_session_preferred_bitrate(session_, SP_BITRATE_320k); - sp_session_preferred_offline_bitrate(session_, SP_BITRATE_320k, false); + SetPlaybackSettings(req.playback_settings()); - if (password.isEmpty()) { + if (req.password().empty()) { sp_error error = sp_session_relogin(session_); if (error != SP_ERROR_OK) { qLog(Warning) << "Tried to relogin but no stored credentials"; @@ -305,8 +321,8 @@ void SpotifyClient::Login(const QString& username, const QString& password) { } } else { sp_session_login(session_, - username.toUtf8().constData(), - password.toUtf8().constData(), + req.username().c_str(), + req.password().c_str(), true); // Remember the password. } } diff --git a/spotifyblob/blob/spotifyclient.h b/spotifyblob/blob/spotifyclient.h index 295496b7b..bc4f67505 100644 --- a/spotifyblob/blob/spotifyclient.h +++ b/spotifyblob/blob/spotifyclient.h @@ -106,7 +106,7 @@ private: static void SP_CALLCONV AlbumBrowseComplete(sp_albumbrowse* result, void* userdata); // Request handlers. - void Login(const QString& username, const QString& password); + void Login(const spotify_pb::LoginRequest& req); void Search(const spotify_pb::SearchRequest& req); void LoadPlaylist(const spotify_pb::LoadPlaylistRequest& req); void SyncPlaylist(const spotify_pb::SyncPlaylistRequest& req); @@ -114,6 +114,7 @@ private: void Seek(qint64 offset_bytes); void LoadImage(const QString& id_b64); void BrowseAlbum(const QString& uri); + void SetPlaybackSettings(const spotify_pb::PlaybackSettings& req); void SendPlaylistList(); diff --git a/spotifyblob/common/spotifymessages.proto b/spotifyblob/common/spotifymessages.proto index d358fcb36..db46edff6 100644 --- a/spotifyblob/common/spotifymessages.proto +++ b/spotifyblob/common/spotifymessages.proto @@ -27,6 +27,8 @@ option optimize_for = LITE_RUNTIME; message LoginRequest { required string username = 1; optional string password = 2; + + optional PlaybackSettings playback_settings = 3; } message LoginResponse { @@ -148,6 +150,17 @@ message SeekRequest { optional int64 offset_bytes = 1; } +enum Bitrate { + Bitrate96k = 1; + Bitrate160k = 2; + Bitrate320k = 3; +} + +message PlaybackSettings { + optional Bitrate bitrate = 1 [default = Bitrate320k]; + optional bool volume_normalisation = 2 [default = false]; +} + message SpotifyMessage { optional LoginRequest login_request = 1; optional LoginResponse login_response = 2; @@ -165,4 +178,5 @@ message SpotifyMessage { optional BrowseAlbumRequest browse_album_request = 14; optional BrowseAlbumResponse browse_album_response = 15; optional SeekRequest seek_request = 16; + optional PlaybackSettings set_playback_settings_request = 17; } diff --git a/src/internet/spotifyserver.cpp b/src/internet/spotifyserver.cpp index c522bbd89..739f20155 100644 --- a/src/internet/spotifyserver.cpp +++ b/src/internet/spotifyserver.cpp @@ -75,7 +75,8 @@ void SpotifyServer::SendMessage(const spotify_pb::SpotifyMessage& message) { } } -void SpotifyServer::Login(const QString& username, const QString& password) { +void SpotifyServer::Login(const QString& username, const QString& password, + spotify_pb::Bitrate bitrate, bool volume_normalisation) { spotify_pb::SpotifyMessage message; spotify_pb::LoginRequest* request = message.mutable_login_request(); @@ -83,6 +84,18 @@ void SpotifyServer::Login(const QString& username, const QString& password) { if (!password.isEmpty()) { request->set_password(DataCommaSizeFromQString(password)); } + request->mutable_playback_settings()->set_bitrate(bitrate); + request->mutable_playback_settings()->set_volume_normalisation(volume_normalisation); + + SendMessage(message); +} + +void SpotifyServer::SetPlaybackSettings(spotify_pb::Bitrate bitrate, bool volume_normalisation) { + spotify_pb::SpotifyMessage message; + + spotify_pb::PlaybackSettings* request = message.mutable_set_playback_settings_request(); + request->set_bitrate(bitrate); + request->set_volume_normalisation(volume_normalisation); SendMessage(message); } diff --git a/src/internet/spotifyserver.h b/src/internet/spotifyserver.h index 74c60aafc..e0c753c09 100644 --- a/src/internet/spotifyserver.h +++ b/src/internet/spotifyserver.h @@ -35,7 +35,8 @@ public: SpotifyServer(QObject* parent = 0); void Init(); - void Login(const QString& username, const QString& password); + void Login(const QString& username, const QString& password, + spotify_pb::Bitrate bitrate, bool volume_normalisation); void LoadStarred(); void SyncStarred(); @@ -47,6 +48,7 @@ public: void Search(const QString& text, int limit, int limit_album = 0); void LoadImage(const QString& id); void AlbumBrowse(const QString& uri); + void SetPlaybackSettings(spotify_pb::Bitrate bitrate, bool volume_normalisation); int server_port() const; diff --git a/src/internet/spotifyservice.cpp b/src/internet/spotifyservice.cpp index 24dec2b47..735ad027f 100644 --- a/src/internet/spotifyservice.cpp +++ b/src/internet/spotifyservice.cpp @@ -47,7 +47,10 @@ SpotifyService::SpotifyService(InternetModel* parent) pending_search_playlist_(NULL), context_menu_(NULL), search_delay_(new QTimer(this)), - login_state_(LoginState_OtherError) { + login_state_(LoginState_OtherError), + bitrate_(spotify_pb::Bitrate320k), + volume_normalisation_(false) +{ // Build the search path for the binary blob. // Look for one distributed alongside clementine first, then check in the // user's home directory for any that have been downloaded. @@ -182,6 +185,13 @@ void SpotifyService::ReloadSettings() { s.beginGroup(kSettingsGroup); login_state_ = LoginState(s.value("login_state", LoginState_OtherError).toInt()); + bitrate_ = static_cast( + s.value("bitrate", spotify_pb::Bitrate320k).toInt()); + volume_normalisation_ = s.value("volume_normalisation", false).toBool(); + + if (server_ && blob_process_) { + server_->SetPlaybackSettings(bitrate_, volume_normalisation_); + } } void SpotifyService::EnsureServerCreated(const QString& username, @@ -216,15 +226,19 @@ void SpotifyService::EnsureServerCreated(const QString& username, login_task_id_ = model()->task_manager()->StartTask(tr("Connecting to Spotify")); + QString login_username = username; + QString login_password = password; + if (username.isEmpty()) { QSettings s; s.beginGroup(kSettingsGroup); - server_->Login(s.value("username").toString(), QString::null); - } else { - server_->Login(username, password); + login_username = s.value("username").toString(); + login_password = QString(); } + server_->Login(login_username, login_password, bitrate_, volume_normalisation_); + StartBlobProcess(); } @@ -349,7 +363,7 @@ void SpotifyService::PlaylistsUpdated(const spotify_pb::Playlists& response) { } } -bool SpotifyService::DoPlaylistsDiffer(const spotify_pb::Playlists& response) { +bool SpotifyService::DoPlaylistsDiffer(const spotify_pb::Playlists& response) const { if (playlists_.count() != response.playlist_size()) { return true; } diff --git a/src/internet/spotifyservice.h b/src/internet/spotifyservice.h index d287beeec..cf6b761be 100644 --- a/src/internet/spotifyservice.h +++ b/src/internet/spotifyservice.h @@ -88,7 +88,7 @@ private: void EnsureMenuCreated(); QStandardItem* PlaylistBySpotifyIndex(int index) const; - bool DoPlaylistsDiffer(const spotify_pb::Playlists& response); + bool DoPlaylistsDiffer(const spotify_pb::Playlists& response) const; private slots: void EnsureServerCreated(const QString& username = QString(), @@ -140,6 +140,8 @@ private: QMap playlist_sync_ids_; LoginState login_state_; + spotify_pb::Bitrate bitrate_; + bool volume_normalisation_; }; #endif diff --git a/src/internet/spotifysettingspage.cpp b/src/internet/spotifysettingspage.cpp index 6fb682ca6..e4a918525 100644 --- a/src/internet/spotifysettingspage.cpp +++ b/src/internet/spotifysettingspage.cpp @@ -17,10 +17,11 @@ #include "spotifysettingspage.h" -#include "core/network.h" #include "spotifyservice.h" #include "internetmodel.h" #include "ui_spotifysettingspage.h" +#include "core/network.h" +#include "spotifyblob/common/spotifymessages.pb.h" #include "ui/iconloader.h" #include @@ -56,6 +57,10 @@ SpotifySettingsPage::SpotifySettingsPage(SettingsDialog* dialog) ui_->login_state->AddCredentialField(ui_->password); ui_->login_state->AddCredentialGroup(ui_->account_group); + ui_->bitrate->addItem("96 " + tr("kbps"), spotify_pb::Bitrate96k); + ui_->bitrate->addItem("160 " + tr("kbps"), spotify_pb::Bitrate160k); + ui_->bitrate->addItem("320 " + tr("kbps"), spotify_pb::Bitrate320k); + BlobStateChanged(); } @@ -104,6 +109,11 @@ void SpotifySettingsPage::Load() { ui_->username->setText(original_username_); validated_ = false; + ui_->bitrate->setCurrentIndex(ui_->bitrate->findData( + s.value("bitrate", spotify_pb::Bitrate320k).toInt())); + ui_->volume_normalisation->setChecked( + s.value("volume_normalisation", false).toBool()); + UpdateLoginState(); } @@ -113,6 +123,9 @@ void SpotifySettingsPage::Save() { s.setValue("username", ui_->username->text()); s.setValue("password", ui_->password->text()); + + s.setValue("bitrate", ui_->bitrate->itemData(ui_->bitrate->currentIndex()).toInt()); + s.setValue("volume_normalisation", ui_->volume_normalisation->isChecked()); } void SpotifySettingsPage::LoginFinished(bool success) { diff --git a/src/internet/spotifysettingspage.ui b/src/internet/spotifysettingspage.ui index 5876d2d6c..48fb822af 100644 --- a/src/internet/spotifysettingspage.ui +++ b/src/internet/spotifysettingspage.ui @@ -6,8 +6,8 @@ 0 0 - 480 - 380 + 545 + 458 @@ -119,6 +119,32 @@ + + + + Preferences + + + + + + Preferred bitrate + + + + + + + + + + Use volume normalisation + + + + + + diff --git a/src/translations/translations.pot b/src/translations/translations.pot index 5cce59dff..b32801f79 100644 --- a/src/translations/translations.pot +++ b/src/translations/translations.pot @@ -244,7 +244,7 @@ msgstr "" msgid "A Grooveshark Anywhere account is required." msgstr "" -#: internet/spotifysettingspage.cpp:145 +#: internet/spotifysettingspage.cpp:158 msgid "A Spotify Premium account is required." msgstr "" @@ -302,7 +302,7 @@ msgstr "" #: ../bin/src/ui_groovesharksettingspage.h:113 #: ../bin/src/ui_magnatunesettingspage.h:151 -#: ../bin/src/ui_spotifysettingspage.h:179 +#: ../bin/src/ui_spotifysettingspage.h:209 #: ../bin/src/ui_remotesettingspage.h:203 #: ../bin/src/ui_lastfmsettingspage.h:145 msgid "Account details" @@ -1008,7 +1008,7 @@ msgstr "" msgid "Configure Shortcuts" msgstr "" -#: internet/spotifyservice.cpp:458 +#: internet/spotifyservice.cpp:472 msgid "Configure Spotify..." msgstr "" @@ -1029,7 +1029,7 @@ msgstr "" msgid "Connect device" msgstr "" -#: internet/spotifyservice.cpp:217 +#: internet/spotifyservice.cpp:227 msgid "Connecting to Spotify" msgstr "" @@ -1442,7 +1442,7 @@ msgstr "" msgid "Download this album..." msgstr "" -#: ../bin/src/ui_spotifysettingspage.h:186 +#: ../bin/src/ui_spotifysettingspage.h:216 msgid "Download..." msgstr "" @@ -1795,7 +1795,7 @@ msgstr "" msgid "Font size" msgstr "" -#: ../bin/src/ui_spotifysettingspage.h:184 +#: ../bin/src/ui_spotifysettingspage.h:214 msgid "For licensing reasons Spotify support is in a separate plugin." msgstr "" @@ -2057,7 +2057,7 @@ msgid "" "time a song finishes." msgstr "" -#: internet/spotifyservice.cpp:314 +#: internet/spotifyservice.cpp:328 msgid "Inbox" msgstr "" @@ -2089,7 +2089,7 @@ msgstr "" msgid "Insert..." msgstr "" -#: internet/spotifysettingspage.cpp:70 +#: internet/spotifysettingspage.cpp:75 msgid "Installed" msgstr "" @@ -2390,7 +2390,7 @@ msgstr "" #: ../bin/src/ui_digitallyimportedsettingspage.h:163 #: ../bin/src/ui_groovesharksettingspage.h:116 #: ../bin/src/ui_magnatunesettingspage.h:160 -#: ../bin/src/ui_spotifysettingspage.h:182 +#: ../bin/src/ui_spotifysettingspage.h:212 #: ../bin/src/ui_remotesettingspage.h:205 #: ../bin/src/ui_lastfmsettingspage.h:147 msgid "Login" @@ -2466,7 +2466,7 @@ msgstr "" msgid "Main profile (MAIN)" msgstr "" -#: internet/spotifyservice.cpp:463 +#: internet/spotifyservice.cpp:477 msgid "Make playlist available offline" msgstr "" @@ -2726,7 +2726,7 @@ msgstr "" msgid "Not enough neighbors" msgstr "" -#: internet/spotifysettingspage.cpp:70 +#: internet/spotifysettingspage.cpp:75 msgid "Not installed" msgstr "" @@ -2864,7 +2864,7 @@ msgstr "" #: ../bin/src/ui_groovesharksettingspage.h:115 #: ../bin/src/ui_magnatunesettingspage.h:161 -#: ../bin/src/ui_spotifysettingspage.h:181 +#: ../bin/src/ui_spotifysettingspage.h:211 #: ../bin/src/ui_networkproxysettingspage.h:169 msgid "Password" msgstr "" @@ -2971,7 +2971,7 @@ msgstr "" msgid "Playlists" msgstr "" -#: ../bin/src/ui_spotifysettingspage.h:185 +#: ../bin/src/ui_spotifysettingspage.h:215 msgid "Plugin status:" msgstr "" @@ -3005,7 +3005,8 @@ msgstr "" #: ../bin/src/ui_digitallyimportedsettingspage.h:166 #: ../bin/src/ui_magnatunesettingspage.h:162 -#: ../bin/src/ui_settingsdialog.h:115 ../bin/src/ui_lastfmsettingspage.h:149 +#: ../bin/src/ui_spotifysettingspage.h:217 ../bin/src/ui_settingsdialog.h:115 +#: ../bin/src/ui_lastfmsettingspage.h:149 msgid "Preferences" msgstr "" @@ -3021,6 +3022,10 @@ msgstr "" msgid "Preferred audio format" msgstr "" +#: ../bin/src/ui_spotifysettingspage.h:218 +msgid "Preferred bitrate" +msgstr "" + #: ../bin/src/ui_deviceproperties.h:380 msgid "Preferred format" msgstr "" @@ -3398,15 +3403,15 @@ msgstr "" msgid "Search Magnatune" msgstr "" -#: internet/spotifysearchplaylisttype.cpp:32 internet/spotifyservice.cpp:571 +#: internet/spotifysearchplaylisttype.cpp:32 internet/spotifyservice.cpp:585 msgid "Search Spotify" msgstr "" -#: internet/spotifyservice.cpp:305 +#: internet/spotifyservice.cpp:319 msgid "Search Spotify (opens a new tab)" msgstr "" -#: internet/spotifyservice.cpp:456 +#: internet/spotifyservice.cpp:470 msgid "Search Spotify (opens a new tab)..." msgstr "" @@ -3734,15 +3739,15 @@ msgstr "" msgid "Speex" msgstr "" -#: ../bin/src/ui_spotifysettingspage.h:178 +#: ../bin/src/ui_spotifysettingspage.h:208 msgid "Spotify" msgstr "" -#: internet/spotifyservice.cpp:143 +#: internet/spotifyservice.cpp:146 msgid "Spotify login error" msgstr "" -#: ../bin/src/ui_spotifysettingspage.h:183 +#: ../bin/src/ui_spotifysettingspage.h:213 msgid "Spotify plugin" msgstr "" @@ -3754,7 +3759,7 @@ msgstr "" msgid "Standard" msgstr "" -#: internet/spotifyservice.cpp:310 +#: internet/spotifyservice.cpp:324 msgid "Starred" msgstr "" @@ -3849,15 +3854,15 @@ msgstr "" msgid "Switch provider" msgstr "" -#: internet/spotifyservice.cpp:482 +#: internet/spotifyservice.cpp:496 msgid "Syncing Spotify inbox" msgstr "" -#: internet/spotifyservice.cpp:477 +#: internet/spotifyservice.cpp:491 msgid "Syncing Spotify playlist" msgstr "" -#: internet/spotifyservice.cpp:486 +#: internet/spotifyservice.cpp:500 msgid "Syncing Spotify starred tracks" msgstr "" @@ -4212,6 +4217,10 @@ msgstr "" msgid "Use the system proxy settings" msgstr "" +#: ../bin/src/ui_spotifysettingspage.h:219 +msgid "Use volume normalisation" +msgstr "" + #: widgets/freespacebar.cpp:47 msgid "Used" msgstr "" @@ -4227,7 +4236,7 @@ msgstr "" #: ../bin/src/ui_groovesharksettingspage.h:114 #: ../bin/src/ui_magnatunesettingspage.h:159 -#: ../bin/src/ui_spotifysettingspage.h:180 +#: ../bin/src/ui_spotifysettingspage.h:210 #: ../bin/src/ui_networkproxysettingspage.h:168 msgid "Username" msgstr "" @@ -4451,7 +4460,7 @@ msgstr "" msgid "You do not have a Grooveshark Anywhere account." msgstr "" -#: internet/spotifysettingspage.cpp:136 +#: internet/spotifysettingspage.cpp:149 msgid "You do not have a Spotify Premium account." msgstr "" @@ -4514,7 +4523,7 @@ msgid "Your scrobbles: %1" msgstr "" #: internet/groovesharksettingspage.cpp:109 -#: internet/spotifysettingspage.cpp:141 +#: internet/spotifysettingspage.cpp:154 msgid "Your username or password was incorrect." msgstr "" @@ -4603,6 +4612,8 @@ msgid "in the last" msgstr "" #: playlist/playlistview.cpp:163 ui/edittagdialog.cpp:421 +#: internet/spotifysettingspage.cpp:60 internet/spotifysettingspage.cpp:61 +#: internet/spotifysettingspage.cpp:62 msgid "kbps" msgstr ""