SoundPlayerWidgetAdvancedView.cpp 8.2 KB

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