SoundPlayer: Let the user open a file from the GUI

The user now can open a file without passing it as an argument
This commit is contained in:
Till Mayer 2019-11-09 17:15:16 +01:00 committed by Andreas Kling
parent b7efebe11c
commit 56cd8b4d17
Notes: sideshowbarker 2024-07-19 11:18:15 +09:00
5 changed files with 123 additions and 66 deletions

View File

@ -1,28 +1,45 @@
#include "PlaybackManager.h"
PlaybackManager::PlaybackManager(NonnullRefPtr<AClientConnection> connection, AWavLoader& loader)
: m_loader(loader)
, m_connection(connection)
PlaybackManager::PlaybackManager(NonnullRefPtr<AClientConnection> connection)
: m_connection(connection)
{
m_total_length = loader.total_samples() / static_cast<float>(loader.sample_rate());
m_timer = CTimer::construct(100, [&]() { next_buffer(); });
pause();
m_timer = CTimer::construct(100, [&]() {
if (!m_loader)
return;
next_buffer();
});
m_timer->stop();
}
PlaybackManager::~PlaybackManager()
{
}
void PlaybackManager::set_loader(OwnPtr<AWavLoader>&& loader)
{
stop();
m_loader = move(loader);
if (m_loader) {
m_total_length = m_loader->total_samples() / static_cast<float>(m_loader->sample_rate());
m_timer->start();
load_next_buffer();
} else {
m_timer->stop();
}
}
void PlaybackManager::stop()
{
set_paused(true);
m_connection->clear_buffer(true);
m_buffers.clear();
m_loader.reset();
m_last_seek = 0;
m_next_buffer = nullptr;
m_current_buffer = nullptr;
m_next_ptr = 0;
if (m_loader)
m_loader->reset();
}
void PlaybackManager::play()
@ -32,6 +49,9 @@ void PlaybackManager::play()
void PlaybackManager::seek(const int position)
{
if (!m_loader)
return;
m_last_seek = position;
bool paused_state = m_paused;
set_paused(true);
@ -41,7 +61,7 @@ void PlaybackManager::seek(const int position)
m_current_buffer = nullptr;
m_next_ptr = 0;
m_buffers.clear();
m_loader.seek(position);
m_loader->seek(position);
if (!paused_state)
set_paused(false);
@ -75,8 +95,8 @@ void PlaybackManager::remove_dead_buffers()
void PlaybackManager::load_next_buffer()
{
if (m_buffers.size() < 10) {
for (int i = 0; i < 20 && m_loader.loaded_samples() < m_loader.total_samples(); i++) {
auto buffer = m_loader.get_more_samples(PLAYBACK_MANAGER_BUFFER_SIZE);
for (int i = 0; i < 20 && m_loader->loaded_samples() < m_loader->total_samples(); i++) {
auto buffer = m_loader->get_more_samples(PLAYBACK_MANAGER_BUFFER_SIZE);
if (buffer)
m_buffers.append(buffer);
}
@ -91,7 +111,7 @@ void PlaybackManager::load_next_buffer()
void PlaybackManager::set_paused(bool paused)
{
if (!m_next_buffer)
if (!m_next_buffer && m_loader)
load_next_buffer();
m_paused = paused;

View File

@ -10,7 +10,7 @@
class PlaybackManager final {
public:
PlaybackManager(NonnullRefPtr<AClientConnection>, AWavLoader&);
PlaybackManager(NonnullRefPtr<AClientConnection>);
~PlaybackManager();
void play();
@ -18,6 +18,7 @@ public:
void pause();
void seek(const int position);
bool toggle_pause();
void set_loader(OwnPtr<AWavLoader>&&);
int last_seek() const { return m_last_seek; }
bool is_paused() const { return m_paused; }
@ -25,7 +26,6 @@ public:
RefPtr<ABuffer> current_buffer() const { return m_current_buffer; }
NonnullRefPtr<AClientConnection> connection() const { return m_connection; }
AWavLoader& loader() const { return m_loader; }
Function<void()> on_update;
@ -38,8 +38,8 @@ private:
bool m_paused { true };
int m_next_ptr { 0 };
int m_last_seek { 0 };
float m_total_length;
AWavLoader& m_loader;
float m_total_length { 0 };
OwnPtr<AWavLoader> m_loader { nullptr };
NonnullRefPtr<AClientConnection> m_connection;
RefPtr<ABuffer> m_next_buffer;
RefPtr<ABuffer> m_current_buffer;

View File

@ -3,19 +3,18 @@
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GMessageBox.h>
#include <LibM/math.h>
SoundPlayerWidget::SoundPlayerWidget(GWindow& window, NonnullRefPtr<AClientConnection> connection, AWavLoader& loader)
: m_manager(PlaybackManager(connection, loader))
SoundPlayerWidget::SoundPlayerWidget(GWindow& window, NonnullRefPtr<AClientConnection> connection)
: m_window(window)
, m_connection(connection)
, m_manager(connection)
{
window.set_title(String::format("SoundPlayer - \"%s\"", loader.file()->filename().characters()));
set_fill_with_background_color(true);
set_layout(make<GBoxLayout>(Orientation::Vertical));
layout()->set_margins({ 2, 2, 2, 2 });
m_sample_ratio = PLAYBACK_MANAGER_RATE / static_cast<float>(loader.sample_rate());
auto status_widget = GWidget::construct(this);
status_widget->set_fill_with_background_color(true);
status_widget->set_layout(make<GBoxLayout>(Orientation::Horizontal));
@ -38,7 +37,7 @@ SoundPlayerWidget::SoundPlayerWidget(GWindow& window, NonnullRefPtr<AClientConne
m_slider = Slider::construct(Orientation::Horizontal, this);
m_slider->set_min(0);
m_slider->set_max(normalize_rate(static_cast<int>(loader.total_samples())));
m_slider->set_enabled(false);
m_slider->on_knob_released = [&](int value) { m_manager.seek(denormalize_rate(value)); };
auto control_widget = GWidget::construct(this);
@ -51,13 +50,15 @@ SoundPlayerWidget::SoundPlayerWidget(GWindow& window, NonnullRefPtr<AClientConne
m_play = GButton::construct(control_widget);
m_play->set_icon(*m_pause_icon);
m_play->set_enabled(false);
m_play->on_click = [this](GButton& button) {
button.set_icon(m_manager.toggle_pause() ? *m_play_icon : *m_pause_icon);
};
auto stop = GButton::construct(control_widget);
stop->set_icon(GraphicsBitmap::load_from_file("/res/icons/16x16/stop.png"));
stop->on_click = [&](GButton&) { m_manager.stop(); };
m_stop = GButton::construct(control_widget);
m_stop->set_enabled(false);
m_stop->set_icon(GraphicsBitmap::load_from_file("/res/icons/16x16/stop.png"));
m_stop->on_click = [&](GButton&) { m_manager.stop(); };
m_status = GLabel::construct(this);
m_status->set_frame_shape(FrameShape::Box);
@ -66,18 +67,11 @@ SoundPlayerWidget::SoundPlayerWidget(GWindow& window, NonnullRefPtr<AClientConne
m_status->set_text_alignment(TextAlignment::CenterLeft);
m_status->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
m_status->set_preferred_size(0, 18);
m_status->set_text(String::format(
"Sample rate %uHz, %u %s, %u bits per sample",
loader.sample_rate(),
loader.num_channels(),
(loader.num_channels() == 1) ? "channel" : "channels",
loader.bits_per_sample()));
m_status->set_text("No file open!");
update_position(0);
m_manager.on_update = [&]() { update_ui(); };
m_manager.play();
}
SoundPlayerWidget::~SoundPlayerWidget()
@ -88,6 +82,43 @@ SoundPlayerWidget::Slider::~Slider()
{
}
void SoundPlayerWidget::open_file(String path)
{
if (!path.ends_with(".wav")) {
GMessageBox::show("Selected file is not a \".wav\" file!", "Filetype error", GMessageBox::Type::Error);
return;
}
OwnPtr<AWavLoader> loader = make<AWavLoader>(path);
if (loader->has_error()) {
GMessageBox::show(
String::format(
"Failed to load WAV file: %s (%s)",
path.characters(),
loader->error_string()),
"Filetype error", GMessageBox::Type::Error);
return;
}
m_sample_ratio = PLAYBACK_MANAGER_RATE / static_cast<float>(loader->sample_rate());
m_slider->set_max(normalize_rate(static_cast<int>(loader->total_samples())));
m_slider->set_enabled(true);
m_play->set_enabled(true);
m_stop->set_enabled(true);
m_window.set_title(String::format("SoundPlayer - \"%s\"", loader->file()->filename().characters()));
m_status->set_text(String::format(
"Sample rate %uHz, %u %s, %u bits per sample",
loader->sample_rate(),
loader->num_channels(),
(loader->num_channels() == 1) ? "channel" : "channels",
loader->bits_per_sample()));
m_manager.set_loader(move(loader));
update_position(0);
}
int SoundPlayerWidget::normalize_rate(int rate) const
{
return static_cast<int>(rate * m_sample_ratio);

View File

@ -12,9 +12,11 @@ class SoundPlayerWidget final : public GWidget {
C_OBJECT(SoundPlayerWidget)
public:
virtual ~SoundPlayerWidget() override;
void open_file(String path);
PlaybackManager& manager() { return m_manager; }
private:
explicit SoundPlayerWidget(GWindow&, NonnullRefPtr<AClientConnection>, AWavLoader&);
explicit SoundPlayerWidget(GWindow&, NonnullRefPtr<AClientConnection>);
void update_position(const int position);
void update_ui();
@ -47,6 +49,8 @@ private:
}
};
GWindow& m_window;
NonnullRefPtr<AClientConnection> m_connection;
PlaybackManager m_manager;
float m_sample_ratio;
RefPtr<GLabel> m_status;
@ -57,4 +61,5 @@ private:
RefPtr<GraphicsBitmap> m_play_icon { GraphicsBitmap::load_from_file("/res/icons/16x16/play.png") };
RefPtr<GraphicsBitmap> m_pause_icon { GraphicsBitmap::load_from_file("/res/icons/16x16/pause.png") };
RefPtr<GButton> m_play;
RefPtr<GButton> m_stop;
};

View File

@ -1,58 +1,59 @@
#include "SoundPlayerWidget.h"
#include <AK/StringBuilder.h>
#include <LibAudio/ABuffer.h>
#include <LibAudio/AClientConnection.h>
#include <LibAudio/AWavLoader.h>
#include <LibCore/CTimer.h>
#include <LibDraw/CharacterBitmap.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GFilePicker.h>
#include <LibGUI/GMenu.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GSlider.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GAboutDialog.h>
#include <stdio.h>
int main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s <wav-file>\n", argv[0]);
return 0;
}
GApplication app(argc, argv);
String path = argv[1];
AWavLoader loader(path);
if (loader.has_error()) {
fprintf(stderr, "Failed to load WAV file: %s (%s)\n", path.characters(), loader.error_string());
return 1;
}
auto audio_client = AClientConnection::construct();
audio_client->handshake();
auto app_menu = make<GMenu>("SoundPlayer");
app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
app.quit();
}));
auto menubar = make<GMenuBar>();
menubar->add_menu(move(app_menu));
app.set_menubar(move(menubar));
auto window = GWindow::construct();
window->set_title("SoundPlayer");
window->set_resizable(false);
window->set_rect(300, 300, 350, 140);
window->set_icon(GraphicsBitmap::load_from_file("/res/icons/16x16/app-sound-player.png"));
auto player = SoundPlayerWidget::construct(window, audio_client, loader);
auto menubar = make<GMenuBar>();
auto app_menu = make<GMenu>("SoundPlayer");
auto player = SoundPlayerWidget::construct(window, audio_client);
if (argc > 1) {
String path = argv[1];
player->open_file(path);
player->manager().play();
}
app_menu->add_action(GCommonActions::make_open_action([&](auto&) {
Optional<String> path = GFilePicker::get_open_filepath("Open wav file...");
if (path.has_value()) {
player->open_file(path.value());
}
}));
app_menu->add_separator();
app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
app.quit();
}));
menubar->add_menu(move(app_menu));
auto help_menu = make<GMenu>("Help");
help_menu->add_action(GAction::create("About", [](auto&) {
GAboutDialog::show("SoundPlayer", GraphicsBitmap::load_from_file("/res/icons/32x32/app-sound-player.png"));
}));
menubar->add_menu(move(help_menu));
app.set_menubar(move(menubar));
window->set_main_widget(player);
window->show();