From 56cd8b4d179a9f7cebdc8d52a8a1c051aec06a17 Mon Sep 17 00:00:00 2001 From: Till Mayer Date: Sat, 9 Nov 2019 17:15:16 +0100 Subject: [PATCH] SoundPlayer: Let the user open a file from the GUI The user now can open a file without passing it as an argument --- Applications/SoundPlayer/PlaybackManager.cpp | 42 +++++++++--- Applications/SoundPlayer/PlaybackManager.h | 8 +-- .../SoundPlayer/SoundPlayerWidget.cpp | 67 ++++++++++++++----- Applications/SoundPlayer/SoundPlayerWidget.h | 7 +- Applications/SoundPlayer/main.cpp | 65 +++++++++--------- 5 files changed, 123 insertions(+), 66 deletions(-) diff --git a/Applications/SoundPlayer/PlaybackManager.cpp b/Applications/SoundPlayer/PlaybackManager.cpp index c83263ca8b0..1f336362beb 100644 --- a/Applications/SoundPlayer/PlaybackManager.cpp +++ b/Applications/SoundPlayer/PlaybackManager.cpp @@ -1,28 +1,45 @@ #include "PlaybackManager.h" -PlaybackManager::PlaybackManager(NonnullRefPtr connection, AWavLoader& loader) - : m_loader(loader) - , m_connection(connection) +PlaybackManager::PlaybackManager(NonnullRefPtr connection) + : m_connection(connection) { - m_total_length = loader.total_samples() / static_cast(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&& loader) +{ + stop(); + m_loader = move(loader); + if (m_loader) { + m_total_length = m_loader->total_samples() / static_cast(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; diff --git a/Applications/SoundPlayer/PlaybackManager.h b/Applications/SoundPlayer/PlaybackManager.h index c58334b6a23..cf4d73a6329 100644 --- a/Applications/SoundPlayer/PlaybackManager.h +++ b/Applications/SoundPlayer/PlaybackManager.h @@ -10,7 +10,7 @@ class PlaybackManager final { public: - PlaybackManager(NonnullRefPtr, AWavLoader&); + PlaybackManager(NonnullRefPtr); ~PlaybackManager(); void play(); @@ -18,6 +18,7 @@ public: void pause(); void seek(const int position); bool toggle_pause(); + void set_loader(OwnPtr&&); int last_seek() const { return m_last_seek; } bool is_paused() const { return m_paused; } @@ -25,7 +26,6 @@ public: RefPtr current_buffer() const { return m_current_buffer; } NonnullRefPtr connection() const { return m_connection; } - AWavLoader& loader() const { return m_loader; } Function 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 m_loader { nullptr }; NonnullRefPtr m_connection; RefPtr m_next_buffer; RefPtr m_current_buffer; diff --git a/Applications/SoundPlayer/SoundPlayerWidget.cpp b/Applications/SoundPlayer/SoundPlayerWidget.cpp index 07d0586bb9b..28776ba274d 100644 --- a/Applications/SoundPlayer/SoundPlayerWidget.cpp +++ b/Applications/SoundPlayer/SoundPlayerWidget.cpp @@ -3,19 +3,18 @@ #include #include #include +#include #include -SoundPlayerWidget::SoundPlayerWidget(GWindow& window, NonnullRefPtr connection, AWavLoader& loader) - : m_manager(PlaybackManager(connection, loader)) +SoundPlayerWidget::SoundPlayerWidget(GWindow& window, NonnullRefPtr 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(Orientation::Vertical)); layout()->set_margins({ 2, 2, 2, 2 }); - m_sample_ratio = PLAYBACK_MANAGER_RATE / static_cast(loader.sample_rate()); - auto status_widget = GWidget::construct(this); status_widget->set_fill_with_background_color(true); status_widget->set_layout(make(Orientation::Horizontal)); @@ -38,7 +37,7 @@ SoundPlayerWidget::SoundPlayerWidget(GWindow& window, NonnullRefPtrset_min(0); - m_slider->set_max(normalize_rate(static_cast(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, NonnullRefPtrset_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, NonnullRefPtrset_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 loader = make(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(loader->sample_rate()); + + m_slider->set_max(normalize_rate(static_cast(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(rate * m_sample_ratio); diff --git a/Applications/SoundPlayer/SoundPlayerWidget.h b/Applications/SoundPlayer/SoundPlayerWidget.h index caf8eb6b8e4..6f144a5af2e 100644 --- a/Applications/SoundPlayer/SoundPlayerWidget.h +++ b/Applications/SoundPlayer/SoundPlayerWidget.h @@ -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, AWavLoader&); + explicit SoundPlayerWidget(GWindow&, NonnullRefPtr); void update_position(const int position); void update_ui(); @@ -47,6 +49,8 @@ private: } }; + GWindow& m_window; + NonnullRefPtr m_connection; PlaybackManager m_manager; float m_sample_ratio; RefPtr m_status; @@ -57,4 +61,5 @@ private: RefPtr m_play_icon { GraphicsBitmap::load_from_file("/res/icons/16x16/play.png") }; RefPtr m_pause_icon { GraphicsBitmap::load_from_file("/res/icons/16x16/pause.png") }; RefPtr m_play; + RefPtr m_stop; }; diff --git a/Applications/SoundPlayer/main.cpp b/Applications/SoundPlayer/main.cpp index ae8f5e025f9..f2699b969c9 100644 --- a/Applications/SoundPlayer/main.cpp +++ b/Applications/SoundPlayer/main.cpp @@ -1,58 +1,59 @@ #include "SoundPlayerWidget.h" -#include -#include #include -#include -#include #include #include #include -#include -#include -#include +#include #include #include -#include -#include #include +#include #include int main(int argc, char** argv) { - if (argc != 2) { - printf("usage: %s \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("SoundPlayer"); - app_menu->add_action(GCommonActions::make_quit_action([&](auto&) { - app.quit(); - })); - - auto menubar = make(); - 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(); + auto app_menu = make("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 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("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();