diff options
author | Leandro Pereira <leandro@hardinfo.org> | 2021-09-29 21:55:42 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-25 23:37:18 +0200 |
commit | 3126b78903a4cf8bdb91a0d4a2551558bf1cb77f (patch) | |
tree | 6a7e40356d875c5658b28df6c5037557099f6b23 /Userland/Applications/SoundPlayer/Player.h | |
parent | 73924f94164c2bccf5097d68d1597abb5621f036 (diff) | |
download | serenity-3126b78903a4cf8bdb91a0d4a2551558bf1cb77f.zip |
SoundPlayer: Fix inconsistencies and code duplication
This is a first pass at refactoring SoundPlayer so that the View widget
is decoupled from the player itself.
In doing so, this fixed a couple of issues, including possibly
inconsistent states (e.g. player could be paused and stopped at the
same time).
With the change, Player actually controls the show, and calls methods
overriden by its subclasses to perform actions, such as update the Seek
bar; the hard work of massaging the raw data is done by the Player
class, so subclasses don't need to reimplement any of these things.
This also removes some copies of playlist management code that happened
to be copied+pasted inside callbacks of buttons -- it now lives inside
a neatly packaged Playlist class, and the Player only asks for the next
song to play.
In addition, the menu bar has been slightly rearranged.
Diffstat (limited to 'Userland/Applications/SoundPlayer/Player.h')
-rw-r--r-- | Userland/Applications/SoundPlayer/Player.h | 107 |
1 files changed, 58 insertions, 49 deletions
diff --git a/Userland/Applications/SoundPlayer/Player.h b/Userland/Applications/SoundPlayer/Player.h index 23b4fdfebe..9846fc2177 100644 --- a/Userland/Applications/SoundPlayer/Player.h +++ b/Userland/Applications/SoundPlayer/Player.h @@ -7,65 +7,74 @@ #pragma once #include "PlaybackManager.h" +#include "Playlist.h" #include "PlaylistWidget.h" -#include "VisualizationBase.h" #include <AK/RefPtr.h> -struct PlayerState { - bool is_paused; - bool is_stopped; - bool has_loaded_file; - bool is_looping_file; - bool is_looping_playlist; - int loaded_file_samplerate; - double volume; - Audio::ClientConnection& connection; - PlaybackManager& manager; - String loaded_filename; -}; - class Player { public: - explicit Player(PlayerState& state) - : m_player_state(state) {}; - virtual void open_file(StringView path) = 0; - virtual void play() = 0; + enum class PlayState { + NoFileLoaded, + Paused, + Stopped, + Playing, + }; + enum class LoopMode { + None, + File, + Playlist, + }; - PlayerState& get_player_state() { return m_player_state; } - bool is_stopped() const { return m_player_state.is_stopped; } - bool is_paused() const { return m_player_state.is_paused; } - bool has_loaded_file() const { return m_player_state.has_loaded_file; } - double volume() const { return m_player_state.volume; } - bool looping() const { return m_player_state.is_looping_file; } - bool looping_playlist() const { return m_player_state.is_looping_playlist; } - const String& loaded_filename() { return m_player_state.loaded_filename; } - int loaded_file_samplerate() { return m_player_state.loaded_file_samplerate; } + explicit Player(Audio::ClientConnection& audio_client_connection); + virtual ~Player() { } - virtual void set_stopped(bool stopped) { m_player_state.is_stopped = stopped; } - virtual void set_paused(bool paused) { m_player_state.is_paused = paused; } - virtual void set_has_loaded_file(bool loaded) { m_player_state.has_loaded_file = loaded; } - virtual void set_volume(double volume) - { - m_player_state.volume = volume; - client_connection().set_self_volume(volume); - } - virtual void set_loaded_file_samplerate(int samplerate) { m_player_state.loaded_file_samplerate = samplerate; } - virtual void set_looping_file(bool loop) - { - m_player_state.is_looping_file = loop; - } - virtual void set_looping_playlist(bool loop) + void play_file_path(StringView path); + + Playlist& playlist() { return m_playlist; } + StringView loaded_filename() const { return m_loaded_filename; } + + PlayState play_state() const { return m_play_state; } + void set_play_state(PlayState state); + + LoopMode loop_mode() const { return m_loop_mode; } + void set_loop_mode(LoopMode mode); + + double volume() const { return m_volume; } + void set_volume(double value); + + void play(); + void pause(); + void toggle_pause(); + void stop(); + void seek(int sample); + + virtual void play_state_changed(PlayState) = 0; + virtual void loop_mode_changed(LoopMode) = 0; + virtual void time_elapsed(int) = 0; + virtual void file_name_changed(StringView) = 0; + virtual void playlist_loaded(StringView, bool) { } + virtual void audio_load_error(StringView, StringView) { } + virtual void volume_changed(double) { } + virtual void total_samples_changed(int) { } + virtual void sound_buffer_played(RefPtr<Audio::Buffer>, [[maybe_unused]] int sample_rate, [[maybe_unused]] int samples_played) { } + +protected: + void done_initializing() { - m_player_state.is_looping_playlist = loop; + set_play_state(PlayState::NoFileLoaded); + set_loop_mode(LoopMode::None); + time_elapsed(0); + set_volume(1.); } - virtual void set_loaded_filename(StringView& filename) { m_player_state.loaded_filename = filename; } - Audio::ClientConnection& client_connection() { return m_player_state.connection; } - PlaybackManager& manager() { return m_player_state.manager; } +private: + Playlist m_playlist; + PlayState m_play_state; + LoopMode m_loop_mode; -protected: - virtual ~Player() = default; + Audio::ClientConnection& m_audio_client_connection; + PlaybackManager m_playback_manager; - PlayerState m_player_state; - RefPtr<PlaylistModel> m_playlist_model; + StringView m_loaded_filename; + double m_volume { 0 }; }; |