From 585e9960987b9f1aacb201586f79efe68877d4ba Mon Sep 17 00:00:00 2001 From: casey langen Date: Tue, 29 Dec 2020 23:36:55 -0800 Subject: [PATCH] PlaybackService warning cleanup / syntax modernization. --- src/musikcore/audio/PlaybackService.cpp | 146 ++++++++++++------------ src/musikcore/audio/PlaybackService.h | 83 +++++++------- 2 files changed, 119 insertions(+), 110 deletions(-) diff --git a/src/musikcore/audio/PlaybackService.cpp b/src/musikcore/audio/PlaybackService.cpp index d6b0ea4a8..2b6fcb2dc 100755 --- a/src/musikcore/audio/PlaybackService.cpp +++ b/src/musikcore/audio/PlaybackService.cpp @@ -86,11 +86,13 @@ class StreamMessage: public Message { this->uri = uri; } - virtual ~StreamMessage() { + std::string GetUri() { + return this->uri; } - std::string GetUri() { return this->uri; } - int GetEventType() { return (int) this->UserData1(); } + int GetEventType() { + return static_cast(this->UserData1()); + } private: std::string uri; @@ -108,30 +110,28 @@ class StreamMessage: public Message { this->messageQueue.Post( \ musik::core::runtime::IMessagePtr(new StreamMessage(instance, eventType, uri))); -static inline void loadPreferences( - std::shared_ptr transport, - IPlaybackService& playback, - std::shared_ptr prefs) -{ - double volume = prefs->GetDouble(keys::Volume, 1.0f); +static inline void loadPreferences(ITransport& transport, IPlaybackService& playback, Preferences& prefs) { + double volume = prefs.GetDouble(keys::Volume, 1.0f); volume = std::max(0.0f, std::min(1.0f, (float)volume)); - transport->SetVolume(volume); + transport.SetVolume(volume); + + auto repeatMode = static_cast( + prefs.GetInt(keys::RepeatMode, static_cast(RepeatMode::None))); - auto repeatMode = (RepeatMode) prefs->GetInt(keys::RepeatMode, (int) RepeatMode::None); repeatMode = (repeatMode > RepeatMode::List || repeatMode < RepeatMode::None) ? RepeatMode::None : repeatMode; + playback.SetRepeatMode(repeatMode); - auto timeChangeMode = (TimeChangeMode) prefs->GetInt(keys::TimeChangeMode, (int) TimeChangeMode::Scrub); + const auto timeChangeMode = static_cast( + prefs.GetInt(keys::TimeChangeMode, static_cast(TimeChangeMode::Scrub))); + playback.SetTimeChangeMode(timeChangeMode); } -static inline void savePreferences( - IPlaybackService& playback, - std::shared_ptr prefs) -{ - prefs->SetDouble(keys::Volume, playback.GetVolume()); - prefs->SetInt(keys::RepeatMode, (int) playback.GetRepeatMode()); - prefs->SetInt(keys::TimeChangeMode, (int) playback.GetTimeChangeMode()); +static inline void savePreferences(IPlaybackService& playback, Preferences& prefs) { + prefs.SetDouble(keys::Volume, playback.GetVolume()); + prefs.SetInt(keys::RepeatMode, static_cast(playback.GetRepeatMode())); + prefs.SetInt(keys::TimeChangeMode, static_cast(playback.GetTimeChangeMode())); } /* internally PlaybackService leverages a message queue for synchronization; @@ -175,7 +175,7 @@ PlaybackService::PlaybackService( PlaybackService::~PlaybackService() { playback::SavePlaybackContext(library, *this); this->messageQueue.Unregister(this); - savePreferences(*this, playbackPrefs); + savePreferences(*this, *playbackPrefs); this->Stop(); this->ResetRemotes(); } @@ -186,14 +186,14 @@ void PlaybackService::InitRemotes() { this->remotes = PluginFactory::Instance() .QueryInterface("GetPlaybackRemote"); - for (auto it = remotes.begin(); it != remotes.end(); it++) { - (*it)->SetPlaybackService(this); + for (auto it : remotes) { + it->SetPlaybackService(this); } } void PlaybackService::ResetRemotes() { - for (auto it = remotes.begin(); it != remotes.end(); it++) { - (*it)->SetPlaybackService(nullptr); + for (auto it : remotes) { + it->SetPlaybackService(nullptr); } } @@ -255,11 +255,11 @@ void PlaybackService::SetRepeatMode(RepeatMode mode) { } } -musik::core::sdk::TimeChangeMode PlaybackService::GetTimeChangeMode() { +musik::core::sdk::TimeChangeMode PlaybackService::GetTimeChangeMode() noexcept { return this->timeChangeMode; } -void PlaybackService::SetTimeChangeMode(TimeChangeMode mode) { +void PlaybackService::SetTimeChangeMode(TimeChangeMode mode) noexcept { this->timeChangeMode = mode; } @@ -289,7 +289,7 @@ void PlaybackService::ToggleShuffle() { /* find the new playback index and prefetch the next track */ if (id != -1) { - int index = this->playlist.IndexOf(id); + const int index = this->playlist.IndexOf(id); if (index != -1) { this->index = index; POST(this, MESSAGE_PREPARE_NEXT_TRACK, NO_POSITION, 0); @@ -301,9 +301,9 @@ void PlaybackService::ToggleShuffle() { } void PlaybackService::ProcessMessage(IMessage &message) { - int type = message.Type(); + const int type = message.Type(); if (type == MESSAGE_LOAD_PLAYBACK_CONTEXT) { - loadPreferences(this->transport, *this, playbackPrefs); + loadPreferences(*this->transport, *this, *playbackPrefs); playback::LoadPlaybackContext(library, *this); this->InitRemotes(); } @@ -311,8 +311,8 @@ void PlaybackService::ProcessMessage(IMessage &message) { this->MarkTrackAsPlayed(message.UserData1()); /* UserData1 is a trackId */ } else if (type == MESSAGE_STREAM_EVENT) { - StreamMessage* streamMessage = static_cast(&message); - StreamState eventType = (StreamState) streamMessage->GetEventType(); + StreamMessage* streamMessage = dynamic_cast(&message); + const StreamState eventType = static_cast(streamMessage->GetEventType()); if (eventType == StreamState::Buffering || eventType == StreamState::Buffered || eventType == StreamState::Playing) { TrackPtr track; @@ -366,7 +366,7 @@ void PlaybackService::ProcessMessage(IMessage &message) { } } else if (type == MESSAGE_PLAYBACK_EVENT) { - PlaybackState eventType = (PlaybackState) message.UserData1(); + const PlaybackState eventType = static_cast(message.UserData1()); if (eventType == PlaybackState::Stopped) { this->OnTrackChanged(NO_POSITION, TrackPtr()); @@ -385,15 +385,15 @@ void PlaybackService::ProcessMessage(IMessage &message) { } } - for (auto it = remotes.begin(); it != remotes.end(); it++) { - (*it)->OnPlaybackStateChanged((PlaybackState) eventType); + for (auto it : remotes) { + it->OnPlaybackStateChanged(static_cast(eventType)); } - this->PlaybackStateChanged((PlaybackState) eventType); + this->PlaybackStateChanged(static_cast(eventType)); } else if (type == MESSAGE_PREPARE_NEXT_TRACK) { if (transport->GetPlaybackState() != PlaybackState::Stopped) { - size_t updatedIndex = (size_t)message.UserData1(); + const size_t updatedIndex = static_cast(message.UserData1()); if (updatedIndex != NO_POSITION) { this->index = updatedIndex; @@ -404,9 +404,9 @@ void PlaybackService::ProcessMessage(IMessage &message) { } } else if (type == MESSAGE_VOLUME_CHANGED) { - double volume = transport->Volume(); - for (auto it = remotes.begin(); it != remotes.end(); it++) { - (*it)->OnVolumeChanged(volume); + const double volume = transport->Volume(); + for (auto it : remotes) { + it->OnVolumeChanged(volume); } this->VolumeChanged(); } @@ -420,7 +420,7 @@ void PlaybackService::ProcessMessage(IMessage &message) { } else if (type == MESSAGE_TIME_CHANGED) { this->TimeChanged(transport->Position()); - double volume = transport->Volume(); + const double volume = transport->Volume(); for (auto remote : this->remotes) { remote->OnPlaybackTimeChanged(transport->Position()); } @@ -428,8 +428,8 @@ void PlaybackService::ProcessMessage(IMessage &message) { else if (type == MESSAGE_NOTIFY_EDITED || type == MESSAGE_NOTIFY_RESET) { - for (auto it = remotes.begin(); it != remotes.end(); it++) { - (*it)->OnPlayQueueChanged(); + for (auto it : remotes) { + it->OnPlayQueueChanged(); } this->QueueEdited(); @@ -441,9 +441,9 @@ void PlaybackService::ProcessMessage(IMessage &message) { } } else if (type == MESSAGE_RELOAD_OUTPUT) { - auto state = this->GetPlaybackState(); - auto index = this->GetIndex(); - double time = this->GetPosition(); + const auto state = this->GetPlaybackState(); + const auto index = this->GetIndex(); + const double time = this->GetPosition(); /* we generally have a MasterTransport available, but apps may choose to implement their own transport and use it instead, so @@ -452,9 +452,10 @@ void PlaybackService::ProcessMessage(IMessage &message) { dynamic_cast(this->transport.get()); if (masterTransport) { - TransportType transportType = (TransportType) - playbackPrefs->GetInt(keys::Transport.c_str(), - (int)TransportType::Gapless); + const TransportType transportType = static_cast( + playbackPrefs->GetInt( + keys::Transport.c_str()), + static_cast(TransportType::Gapless)); if (masterTransport->GetType() != transportType) { masterTransport->SwitchTo(transportType); @@ -466,8 +467,10 @@ void PlaybackService::ProcessMessage(IMessage &message) { this->transport->ReloadOutput(); if (index != NO_POSITION) { - auto startMode = (state != PlaybackState::Paused && state != PlaybackState::Prepared) - ? ITransport::StartMode::Immediate : ITransport::StartMode::Wait; + const auto startMode = + (state != PlaybackState::Paused && state != PlaybackState::Prepared) + ? ITransport::StartMode::Immediate + : ITransport::StartMode::Wait; this->PlayAt(index, startMode); if (time > 0.0f) { @@ -486,10 +489,10 @@ void PlaybackService::ProcessMessage(IMessage &message) { } void PlaybackService::NotifyRemotesModeChanged() { - RepeatMode mode = this->repeatMode; - bool shuffled = this->IsShuffled(); - for (auto it = remotes.begin(); it != remotes.end(); it++) { - (*it)->OnModeChanged(repeatMode, shuffled); + const RepeatMode mode = this->repeatMode; + const bool shuffled = this->IsShuffled(); + for (auto it : remotes) { + it->OnModeChanged(repeatMode, shuffled); } } @@ -506,7 +509,7 @@ void PlaybackService::OnTrackChanged(size_t pos, TrackPtr track) { /* we consider a track to be played if (1) it enters the playing state and it's less than 10 seconds long, or (2) it enters the playing state, and remains playing for > 10 seconds */ - double duration = this->transport->GetDuration(); + const double duration = this->transport->GetDuration(); if (duration > 0 && duration < 10.0) { this->MarkTrackAsPlayed(track->GetId()); } @@ -515,8 +518,8 @@ void PlaybackService::OnTrackChanged(size_t pos, TrackPtr track) { } } - for (auto it = remotes.begin(); it != remotes.end(); it++) { - (*it)->OnTrackChanged(track.get()); + for (auto it : remotes) { + it->OnTrackChanged(track.get()); } } @@ -578,7 +581,7 @@ size_t PlaybackService::Count() { } void PlaybackService::ToggleRepeatMode() { - RepeatMode mode = GetRepeatMode(); + const RepeatMode mode = GetRepeatMode(); switch (mode) { case RepeatMode::None: SetRepeatMode(RepeatMode::List); break; case RepeatMode::List: SetRepeatMode(RepeatMode::Track); break; @@ -602,7 +605,7 @@ bool PlaybackService::HotSwap(const TrackList& tracks, size_t index) { bool found = false; auto playingTrack = this->GetPlaying(); if (playingTrack && tracks.Count() > index) { - auto supplantId = tracks.GetId(index); + const auto supplantId = tracks.GetId(index); const auto playingId = playingTrack->GetId(); /* look at the index hint, see if we can find a matching track without @@ -744,7 +747,7 @@ void PlaybackService::PlayAt(size_t index, ITransport::StartMode mode) { index = std::min(this->Count(), index); std::string uri = this->UriAtIndex(index); - auto gain = this->GainAtIndex(index); + const auto gain = this->GainAtIndex(index); if (uri.size()) { transport->Start(uri, gain, mode); @@ -764,7 +767,7 @@ void PlaybackService::Prepare(size_t index, double position) { } } -size_t PlaybackService::GetIndex() { +size_t PlaybackService::GetIndex() noexcept { return this->index; } @@ -822,7 +825,7 @@ void PlaybackService::SetPosition(double seconds) { double PlaybackService::GetDuration() { TrackPtr track; - double duration = this->transport->GetDuration(); + const double duration = this->transport->GetDuration(); if (duration > 0) { return duration; @@ -830,7 +833,7 @@ double PlaybackService::GetDuration() { { std::unique_lock lock(this->playlistMutex); - size_t index = this->index; + const size_t index = this->index; if (index < this->playlist.Count()) { track = TrackAtIndexWithTimeout(index); } @@ -916,7 +919,7 @@ ITrackListEditor* PlaybackService::EditPlaylist() { virtual ~SdkTrackListEditor() { } - virtual void Release() { + virtual void Release() noexcept { delete this; } }; @@ -1107,7 +1110,7 @@ void PlaybackService::Editor::Shuffle() { this->edited = true; } -void PlaybackService::Editor::Release() { +void PlaybackService::Editor::Release() noexcept { /* nothing! */ } @@ -1133,18 +1136,21 @@ ITransport::Gain PlaybackService::GainAtIndex(size_t index) { ITransport::Gain result; - float preampDb = (float)playbackPrefs->GetDouble(keys::PreampDecibels.c_str(), 0.0f); + const float preampDb = static_cast( + playbackPrefs->GetDouble(keys::PreampDecibels.c_str(), 0.0f)); + result.preamp = powf(10.0f, (preampDb / 20.0f)); result.peakValid = false; - Mode mode = (Mode)playbackPrefs->GetInt(keys::ReplayGainMode.c_str(), (int) Mode::Disabled); + const Mode mode = static_cast(playbackPrefs->GetInt( + keys::ReplayGainMode.c_str(), (int) Mode::Disabled)); if (mode != Mode::Disabled && index < this->playlist.Count()) { auto track = TrackAtIndexWithTimeout(index); if (track) { - auto rg = track->GetReplayGain(); - float gain = (mode == Mode::Album) ? rg.albumGain : rg.trackGain; - float peak = (mode == Mode::Album) ? rg.albumPeak : rg.trackPeak; + const auto rg = track->GetReplayGain(); + const float gain = (mode == Mode::Album) ? rg.albumGain : rg.trackGain; + const float peak = (mode == Mode::Album) ? rg.albumPeak : rg.trackPeak; if (gain != 1.0f) { /* http://wiki.hydrogenaud.io/index.php?title=ReplayGain_2.0_specification#Reduced_gain */ result.gain = powf(10.0f, (gain / 20.0f)); diff --git a/src/musikcore/audio/PlaybackService.h b/src/musikcore/audio/PlaybackService.h index df3e247a1..0952dbcdb 100755 --- a/src/musikcore/audio/PlaybackService.h +++ b/src/musikcore/audio/PlaybackService.h @@ -80,52 +80,55 @@ namespace musik { namespace core { namespace audio { virtual ~PlaybackService(); /* IMessageTarget */ - virtual void ProcessMessage(musik::core::runtime::IMessage &message) override; + void ProcessMessage(musik::core::runtime::IMessage &message) override; /* IPlaybackService */ - virtual void Play(size_t index) override; - virtual bool Next() override; - virtual bool Previous() override; - virtual void Stop() override { transport->Stop(); } - virtual musik::core::sdk::RepeatMode GetRepeatMode() override { return this->repeatMode; } - virtual void SetRepeatMode(musik::core::sdk::RepeatMode mode) override; - virtual void ToggleRepeatMode() override; - virtual musik::core::sdk::PlaybackState GetPlaybackState() override; - virtual bool IsShuffled() override; - virtual void ToggleShuffle() override; - virtual size_t GetIndex() override; - virtual size_t Count() override; - virtual double GetVolume() override; - virtual void SetVolume(double vol) override; - virtual void PauseOrResume() override; - virtual bool IsMuted() override; - virtual void ToggleMute() override; - virtual double GetPosition() override; - virtual void SetPosition(double seconds) override; - virtual double GetDuration() override; - virtual musik::core::sdk::ITrack* GetTrack(size_t index) override; - virtual musik::core::sdk::ITrack* GetPlayingTrack() override; - virtual void CopyFrom(const musik::core::sdk::ITrackList* source) override; - virtual void Play(const musik::core::sdk::ITrackList* source, size_t index) override; - virtual musik::core::sdk::ITrackListEditor* EditPlaylist() override; - virtual musik::core::sdk::TimeChangeMode GetTimeChangeMode() override; - virtual void SetTimeChangeMode(musik::core::sdk::TimeChangeMode) override; - virtual void ReloadOutput() override; - virtual musik::core::sdk::ITrackList* Clone() override; + void Play(size_t index) override; + bool Next() override; + bool Previous() override; + void Stop() override { transport->Stop(); } + musik::core::sdk::RepeatMode GetRepeatMode() override { return this->repeatMode; } + void SetRepeatMode(musik::core::sdk::RepeatMode mode) override; + void ToggleRepeatMode() override; + musik::core::sdk::PlaybackState GetPlaybackState() override; + bool IsShuffled() override; + void ToggleShuffle() override; + size_t GetIndex() noexcept override; + size_t Count() override; + double GetVolume() override; + void SetVolume(double vol) override; + void PauseOrResume() override; + bool IsMuted() override; + void ToggleMute() override; + double GetPosition() override; + void SetPosition(double seconds) override; + double GetDuration() override; + musik::core::sdk::ITrack* GetTrack(size_t index) override; + musik::core::sdk::ITrack* GetPlayingTrack() override; + void CopyFrom(const musik::core::sdk::ITrackList* source) override; + void Play(const musik::core::sdk::ITrackList* source, size_t index) override; + musik::core::sdk::ITrackListEditor* EditPlaylist() override; + musik::core::sdk::TimeChangeMode GetTimeChangeMode() noexcept override; + void SetTimeChangeMode(musik::core::sdk::TimeChangeMode) noexcept override; + void ReloadOutput() override; + musik::core::sdk::ITrackList* Clone() override; /* TODO: include in SDK? */ virtual bool HotSwap(const TrackList& source, size_t index = 0); /* app-specific implementation. similar to some SDK methods, but use concrete data types with known optimizations */ - musik::core::audio::ITransport& GetTransport() { return *this->transport.get(); } void Play(const musik::core::TrackList& tracks, size_t index); void Prepare(size_t index, double position = 0.0f); void CopyTo(musik::core::TrackList& target); void CopyFrom(const musik::core::TrackList& source); musik::core::TrackPtr GetPlaying(); - std::shared_ptr GetTrackList() { + musik::core::audio::ITransport& GetTransport() noexcept { + return *this->transport.get(); + } + + std::shared_ptr GetTrackList() noexcept { return std::shared_ptr( &this->playlist, [](const musik::core::TrackList*) {}); } @@ -141,14 +144,14 @@ namespace musik { namespace core { namespace audio { virtual ~Editor(); /* ITrackListEditor */ - virtual bool Insert(int64_t id, size_t index) override; - virtual bool Swap(size_t index1, size_t index2) override; - virtual bool Move(size_t from, size_t to) override; - virtual bool Delete(size_t index) override; - virtual void Add(const int64_t id) override; - virtual void Clear() override; - virtual void Shuffle() override; - virtual void Release() override; + bool Insert(int64_t id, size_t index) override; + bool Swap(size_t index1, size_t index2) override; + bool Move(size_t from, size_t to) override; + bool Delete(size_t index) override; + void Add(const int64_t id) override; + void Clear() override; + void Shuffle() override; + void Release() noexcept override; private: friend class PlaybackService;