SoundPlayerWidgetAdvancedView.cpp 11 KB

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