SoundPlayerWidgetAdvancedView.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "SoundPlayerWidgetAdvancedView.h"
  8. #include "BarsVisualizationWidget.h"
  9. #include "Common.h"
  10. #include "M3UParser.h"
  11. #include "PlaybackManager.h"
  12. #include <AK/LexicalPath.h>
  13. #include <AK/SIMD.h>
  14. #include <LibGUI/Action.h>
  15. #include <LibGUI/BoxLayout.h>
  16. #include <LibGUI/Button.h>
  17. #include <LibGUI/Label.h>
  18. #include <LibGUI/MessageBox.h>
  19. #include <LibGUI/Slider.h>
  20. #include <LibGUI/Splitter.h>
  21. #include <LibGUI/Toolbar.h>
  22. #include <LibGUI/ToolbarContainer.h>
  23. #include <LibGUI/Window.h>
  24. #include <LibGfx/Bitmap.h>
  25. SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window, Audio::ConnectionToServer& connection)
  26. : Player(connection)
  27. , m_window(window)
  28. {
  29. window.resize(455, 350);
  30. window.set_resizable(true);
  31. set_fill_with_background_color(true);
  32. set_layout<GUI::VerticalBoxLayout>();
  33. m_splitter = add<GUI::HorizontalSplitter>();
  34. m_player_view = m_splitter->add<GUI::Widget>();
  35. m_playlist_widget = PlaylistWidget::construct();
  36. m_playlist_widget->set_data_model(playlist().model());
  37. m_playlist_widget->set_fixed_width(150);
  38. m_player_view->set_layout<GUI::VerticalBoxLayout>();
  39. m_play_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/play.png"sv).release_value_but_fixme_should_propagate_errors();
  40. m_pause_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/pause.png"sv).release_value_but_fixme_should_propagate_errors();
  41. m_stop_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/stop.png"sv).release_value_but_fixme_should_propagate_errors();
  42. m_back_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-back.png"sv).release_value_but_fixme_should_propagate_errors();
  43. m_next_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png"sv).release_value_but_fixme_should_propagate_errors();
  44. m_visualization = m_player_view->add<BarsVisualizationWidget>();
  45. m_playback_progress_slider = m_player_view->add<AutoSlider>(Orientation::Horizontal);
  46. m_playback_progress_slider->set_fixed_height(20);
  47. m_playback_progress_slider->set_jump_to_cursor(true);
  48. m_playback_progress_slider->set_min(0);
  49. m_playback_progress_slider->on_knob_released = [&](int value) {
  50. seek(value);
  51. };
  52. auto& toolbar_container = m_player_view->add<GUI::ToolbarContainer>();
  53. auto& menubar = toolbar_container.add<GUI::Toolbar>();
  54. m_play_action = GUI::Action::create("Play", { Key_Space }, m_play_icon, [&](auto&) {
  55. toggle_pause();
  56. });
  57. m_play_action->set_enabled(false);
  58. menubar.add_action(*m_play_action);
  59. m_stop_action = GUI::Action::create("Stop", { Key_S }, m_stop_icon, [&](auto&) {
  60. stop();
  61. });
  62. m_stop_action->set_enabled(false);
  63. menubar.add_action(*m_stop_action);
  64. menubar.add_separator();
  65. m_timestamp_label = menubar.add<GUI::Label>();
  66. m_timestamp_label->set_fixed_width(110);
  67. // Filler label
  68. menubar.add<GUI::Label>();
  69. m_back_action = GUI::Action::create("Back", m_back_icon, [&](auto&) {
  70. play_file_path(playlist().previous());
  71. });
  72. m_back_action->set_enabled(false);
  73. menubar.add_action(*m_back_action);
  74. m_next_action = GUI::Action::create("Next", m_next_icon, [&](auto&) {
  75. play_file_path(playlist().next());
  76. });
  77. m_next_action->set_enabled(false);
  78. menubar.add_action(*m_next_action);
  79. menubar.add_separator();
  80. m_volume_label = &menubar.add<GUI::Label>();
  81. m_volume_label->set_fixed_width(30);
  82. m_volume_slider = &menubar.add<GUI::HorizontalSlider>();
  83. m_volume_slider->set_fixed_width(95);
  84. m_volume_slider->set_min(0);
  85. m_volume_slider->set_max(150);
  86. m_volume_slider->set_value(100);
  87. m_volume_slider->on_change = [&](int value) {
  88. double volume = m_nonlinear_volume_slider ? (double)(value * value) / (100 * 100) : value / 100.;
  89. set_volume(volume);
  90. };
  91. set_nonlinear_volume_slider(false);
  92. done_initializing();
  93. }
  94. void SoundPlayerWidgetAdvancedView::set_nonlinear_volume_slider(bool nonlinear)
  95. {
  96. m_nonlinear_volume_slider = nonlinear;
  97. }
  98. void SoundPlayerWidgetAdvancedView::drop_event(GUI::DropEvent& event)
  99. {
  100. event.accept();
  101. if (event.mime_data().has_urls()) {
  102. auto urls = event.mime_data().urls();
  103. if (urls.is_empty())
  104. return;
  105. window()->move_to_front();
  106. // FIXME: Add all paths from drop event to the playlist
  107. play_file_path(urls.first().path());
  108. }
  109. }
  110. void SoundPlayerWidgetAdvancedView::keydown_event(GUI::KeyEvent& event)
  111. {
  112. if (event.key() == Key_M)
  113. toggle_mute();
  114. if (event.key() == Key_Up)
  115. m_volume_slider->increase_slider_by_page_steps(1);
  116. if (event.key() == Key_Down)
  117. m_volume_slider->decrease_slider_by_page_steps(1);
  118. GUI::Widget::keydown_event(event);
  119. }
  120. void SoundPlayerWidgetAdvancedView::set_playlist_visible(bool visible)
  121. {
  122. if (!visible) {
  123. m_playlist_widget->remove_from_parent();
  124. m_player_view->set_max_width(window()->width());
  125. } else if (!m_playlist_widget->parent()) {
  126. m_player_view->parent_widget()->add_child(*m_playlist_widget);
  127. }
  128. }
  129. void SoundPlayerWidgetAdvancedView::play_state_changed(Player::PlayState state)
  130. {
  131. sync_previous_next_actions();
  132. m_play_action->set_enabled(state != PlayState::NoFileLoaded);
  133. m_play_action->set_icon(state == PlayState::Playing ? m_pause_icon : m_play_icon);
  134. m_stop_action->set_enabled(state != PlayState::Stopped && state != PlayState::NoFileLoaded);
  135. m_playback_progress_slider->set_enabled(state != PlayState::NoFileLoaded);
  136. }
  137. void SoundPlayerWidgetAdvancedView::loop_mode_changed(Player::LoopMode)
  138. {
  139. }
  140. void SoundPlayerWidgetAdvancedView::mute_changed(bool)
  141. {
  142. // FIXME: Update the volume slider when player is muted
  143. }
  144. void SoundPlayerWidgetAdvancedView::sync_previous_next_actions()
  145. {
  146. m_back_action->set_enabled(playlist().size() > 1 && !playlist().shuffling());
  147. m_next_action->set_enabled(playlist().size() > 1);
  148. }
  149. void SoundPlayerWidgetAdvancedView::shuffle_mode_changed(Player::ShuffleMode)
  150. {
  151. sync_previous_next_actions();
  152. }
  153. void SoundPlayerWidgetAdvancedView::time_elapsed(int seconds)
  154. {
  155. m_timestamp_label->set_text(String::formatted("Elapsed: {:02}:{:02}:{:02}", seconds / 3600, seconds / 60, seconds % 60));
  156. }
  157. void SoundPlayerWidgetAdvancedView::file_name_changed(StringView name)
  158. {
  159. m_visualization->start_new_file(name);
  160. m_window.set_title(String::formatted("{} - Sound Player", name));
  161. }
  162. void SoundPlayerWidgetAdvancedView::total_samples_changed(int total_samples)
  163. {
  164. m_playback_progress_slider->set_max(total_samples);
  165. m_playback_progress_slider->set_page_step(total_samples / 10);
  166. }
  167. void SoundPlayerWidgetAdvancedView::sound_buffer_played(FixedArray<Audio::Sample> const& buffer, int sample_rate, int samples_played)
  168. {
  169. m_visualization->set_buffer(buffer);
  170. m_visualization->set_samplerate(sample_rate);
  171. // If the user is currently dragging the slider, don't interfere.
  172. if (!m_playback_progress_slider->mouse_is_down())
  173. m_playback_progress_slider->set_value(samples_played);
  174. }
  175. void SoundPlayerWidgetAdvancedView::volume_changed(double volume)
  176. {
  177. m_volume_label->set_text(String::formatted("{}%", static_cast<int>(volume * 100)));
  178. }
  179. void SoundPlayerWidgetAdvancedView::playlist_loaded(StringView path, bool loaded)
  180. {
  181. if (!loaded) {
  182. GUI::MessageBox::show(&m_window, String::formatted("Could not load playlist at \"{}\".", path), "Error opening playlist"sv, GUI::MessageBox::Type::Error);
  183. return;
  184. }
  185. set_playlist_visible(true);
  186. play_file_path(playlist().next());
  187. }
  188. void SoundPlayerWidgetAdvancedView::audio_load_error(StringView path, StringView error_string)
  189. {
  190. GUI::MessageBox::show(&m_window, String::formatted("Failed to load audio file: {} ({})", path, error_string.is_null() ? "Unknown error"sv : error_string),
  191. "Filetype error"sv, GUI::MessageBox::Type::Error);
  192. }