mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:57:44 +00:00
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.
This commit is contained in:
parent
73924f9416
commit
3126b78903
14 changed files with 447 additions and 304 deletions
122
Userland/Applications/SoundPlayer/Player.cpp
Normal file
122
Userland/Applications/SoundPlayer/Player.cpp
Normal file
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
|
||||
* Copyright (c) 2021, Leandro A. F. Pereira <leandro@tia.mat.br>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "Player.h"
|
||||
|
||||
Player::Player(Audio::ClientConnection& audio_client_connection)
|
||||
: m_audio_client_connection(audio_client_connection)
|
||||
, m_playback_manager(audio_client_connection)
|
||||
{
|
||||
m_playback_manager.on_update = [&]() {
|
||||
auto samples_played = m_audio_client_connection.get_played_samples();
|
||||
auto sample_rate = m_playback_manager.loader()->sample_rate();
|
||||
float source_to_dest_ratio = static_cast<float>(sample_rate) / m_playback_manager.device_sample_rate();
|
||||
samples_played *= source_to_dest_ratio;
|
||||
samples_played += m_playback_manager.last_seek();
|
||||
|
||||
auto played_seconds = samples_played / sample_rate;
|
||||
time_elapsed(played_seconds);
|
||||
sound_buffer_played(m_playback_manager.current_buffer(), m_playback_manager.device_sample_rate(), samples_played);
|
||||
};
|
||||
m_playback_manager.on_finished_playing = [&]() {
|
||||
set_play_state(PlayState::Stopped);
|
||||
|
||||
switch (loop_mode()) {
|
||||
case LoopMode::File:
|
||||
play_file_path(loaded_filename());
|
||||
return;
|
||||
case LoopMode::Playlist:
|
||||
play_file_path(m_playlist.next());
|
||||
return;
|
||||
case LoopMode::None:
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void Player::play_file_path(StringView path)
|
||||
{
|
||||
if (path.is_null())
|
||||
return;
|
||||
|
||||
if (!Core::File::exists(path)) {
|
||||
audio_load_error(path, "File does not exist");
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.ends_with(".m3u", AK::CaseSensitivity::CaseInsensitive) || path.ends_with(".m3u8", AK::CaseSensitivity::CaseInsensitive)) {
|
||||
playlist_loaded(path, m_playlist.load(path));
|
||||
return;
|
||||
}
|
||||
|
||||
NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(path);
|
||||
if (loader->has_error()) {
|
||||
audio_load_error(path, loader->error_string());
|
||||
return;
|
||||
}
|
||||
|
||||
m_loaded_filename = path;
|
||||
|
||||
file_name_changed(path);
|
||||
total_samples_changed(loader->total_samples());
|
||||
m_playback_manager.set_loader(move(loader));
|
||||
|
||||
play();
|
||||
}
|
||||
|
||||
void Player::set_play_state(PlayState state)
|
||||
{
|
||||
if (m_play_state != state) {
|
||||
m_play_state = state;
|
||||
play_state_changed(state);
|
||||
}
|
||||
}
|
||||
|
||||
void Player::set_loop_mode(LoopMode mode)
|
||||
{
|
||||
if (m_loop_mode != mode) {
|
||||
m_loop_mode = mode;
|
||||
m_playlist.set_looping(mode == LoopMode::Playlist);
|
||||
loop_mode_changed(mode);
|
||||
}
|
||||
}
|
||||
|
||||
void Player::set_volume(double volume)
|
||||
{
|
||||
m_volume = clamp(volume, 0, 1.0);
|
||||
m_audio_client_connection.set_self_volume(m_volume);
|
||||
volume_changed(m_volume);
|
||||
}
|
||||
|
||||
void Player::play()
|
||||
{
|
||||
m_playback_manager.play();
|
||||
set_play_state(PlayState::Playing);
|
||||
}
|
||||
|
||||
void Player::pause()
|
||||
{
|
||||
m_playback_manager.pause();
|
||||
set_play_state(PlayState::Paused);
|
||||
}
|
||||
|
||||
void Player::toggle_pause()
|
||||
{
|
||||
bool paused = m_playback_manager.toggle_pause();
|
||||
set_play_state(paused ? PlayState::Paused : PlayState::Playing);
|
||||
}
|
||||
|
||||
void Player::stop()
|
||||
{
|
||||
m_playback_manager.stop();
|
||||
set_play_state(PlayState::Stopped);
|
||||
}
|
||||
|
||||
void Player::seek(int sample)
|
||||
{
|
||||
m_playback_manager.seek(sample);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue