SoundPlayerWidgetAdvancedView.cpp 8.4 KB

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