SoundPlayerWidgetAdvancedView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SoundPlayerWidgetAdvancedView.h"
  7. #include "BarsVisualizationWidget.h"
  8. #include "Common.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, PlayerState& state)
  25. : Player(state)
  26. , m_window(window)
  27. {
  28. window.resize(455, 350);
  29. window.set_minimum_size(600, 130);
  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_model = adopt_ref(*new PlaylistModel());
  36. m_player_view->set_layout<GUI::VerticalBoxLayout>();
  37. m_play_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/play.png");
  38. m_pause_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/pause.png");
  39. m_stop_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png");
  40. m_back_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png");
  41. m_next_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png");
  42. m_visualization = m_player_view->add<BarsVisualizationWidget>();
  43. m_playback_progress_slider = m_player_view->add<AutoSlider>(Orientation::Horizontal);
  44. m_playback_progress_slider->set_fixed_height(20);
  45. m_playback_progress_slider->set_min(0);
  46. m_playback_progress_slider->set_max(this->manager().total_length() * 44100); //this value should be set when we load a new file
  47. m_playback_progress_slider->on_knob_released = [&](int value) {
  48. this->manager().seek(value);
  49. };
  50. auto& toolbar_container = m_player_view->add<GUI::ToolbarContainer>();
  51. auto& menubar = toolbar_container.add<GUI::Toolbar>();
  52. m_play_button = menubar.add<GUI::Button>();
  53. m_play_button->set_icon(is_paused() ? (!has_loaded_file() ? *m_play_icon : *m_pause_icon) : *m_pause_icon);
  54. m_play_button->set_fixed_width(50);
  55. m_play_button->set_enabled(has_loaded_file());
  56. m_play_button->on_click = [&](unsigned) {
  57. bool paused = this->manager().toggle_pause();
  58. set_paused(paused);
  59. m_play_button->set_icon(paused ? *m_play_icon : *m_pause_icon);
  60. };
  61. m_stop_button = menubar.add<GUI::Button>();
  62. m_stop_button->set_icon(*m_stop_icon);
  63. m_stop_button->set_fixed_width(50);
  64. m_stop_button->set_enabled(has_loaded_file());
  65. m_stop_button->on_click = [&](unsigned) {
  66. this->manager().stop();
  67. set_stopped(true);
  68. m_play_button->set_icon(*m_play_icon);
  69. m_stop_button->set_enabled(false);
  70. };
  71. auto& timestamp_label = menubar.add<GUI::Label>();
  72. timestamp_label.set_fixed_width(110);
  73. timestamp_label.set_text("Elapsed: 00:00:00");
  74. // filler_label
  75. menubar.add<GUI::Label>();
  76. m_back_button = menubar.add<GUI::Button>();
  77. m_back_button->set_fixed_width(50);
  78. m_back_button->set_icon(*m_back_icon);
  79. m_back_button->set_enabled(has_loaded_file());
  80. m_back_button->on_click = [&](unsigned) {
  81. if (!m_playlist_model.is_null()) {
  82. auto it = m_playlist_model->items().find_if([&](const M3UEntry& e) { return e.path == loaded_filename(); });
  83. if (it.index() == 0) {
  84. open_file(m_playlist_model->items().at(m_playlist_model->items().size() - 1).path);
  85. } else
  86. open_file((it - 1)->path);
  87. play();
  88. }
  89. };
  90. m_next_button = menubar.add<GUI::Button>();
  91. m_next_button->set_fixed_width(50);
  92. m_next_button->set_icon(*m_next_icon);
  93. m_next_button->set_enabled(has_loaded_file());
  94. m_next_button->on_click = [&](unsigned) {
  95. if (!m_playlist_model.is_null()) {
  96. auto it = m_playlist_model->items().find_if([&](const M3UEntry& e) { return e.path == loaded_filename(); });
  97. if (it.index() + 1 == m_playlist_model->items().size()) {
  98. open_file(m_playlist_model->items().at(0).path);
  99. } else
  100. open_file((it + 1)->path);
  101. play();
  102. }
  103. };
  104. m_volume_label = &menubar.add<GUI::Label>();
  105. m_volume_label->set_fixed_width(30);
  106. m_volume_label->set_text("100%");
  107. auto& volume_slider = menubar.add<GUI::HorizontalSlider>();
  108. volume_slider.set_fixed_width(95);
  109. volume_slider.set_min(0);
  110. volume_slider.set_max(150);
  111. volume_slider.set_value(100);
  112. volume_slider.on_change = [&](int value) {
  113. double volume = m_nonlinear_volume_slider ? (double)(value * value) / (100 * 100) : value / 100.;
  114. m_volume_label->set_text(String::formatted("{}%", (int)(volume * 100)));
  115. set_volume(volume);
  116. };
  117. set_volume(1.);
  118. set_nonlinear_volume_slider(false);
  119. manager().on_update = [&]() {
  120. //TODO: make this program support other sample rates
  121. int samples_played = client_connection().get_played_samples() + this->manager().last_seek();
  122. int current_second = samples_played / 44100;
  123. timestamp_label.set_text(String::formatted("Elapsed: {:02}:{:02}:{:02}", current_second / 3600, current_second / 60, current_second % 60));
  124. m_playback_progress_slider->set_value(samples_played);
  125. dynamic_cast<Visualization*>(m_visualization.ptr())->set_buffer(this->manager().current_buffer());
  126. dynamic_cast<Visualization*>(m_visualization.ptr())->set_samplerate(loaded_file_samplerate());
  127. };
  128. manager().on_load_sample_buffer = [&](Audio::Buffer&) {
  129. //TODO: Implement an equalizer
  130. return;
  131. };
  132. manager().on_finished_playing = [&] {
  133. m_play_button->set_icon(*m_play_icon);
  134. if (looping()) {
  135. open_file(loaded_filename());
  136. return;
  137. }
  138. if (!m_playlist_model.is_null() && !m_playlist_model->items().is_empty()) {
  139. auto it = m_playlist_model->items().find_if([&](const M3UEntry& e) { return e.path == loaded_filename(); });
  140. if (it.index() + 1 == m_playlist_model->items().size()) {
  141. if (looping_playlist()) {
  142. open_file(m_playlist_model->items().at(0).path);
  143. return;
  144. }
  145. } else
  146. open_file((it + 1)->path);
  147. }
  148. };
  149. }
  150. void SoundPlayerWidgetAdvancedView::open_file(StringView path)
  151. {
  152. if (!Core::File::exists(path)) {
  153. GUI::MessageBox::show(window(), String::formatted("File \"{}\" does not exist", path), "Error opening file", GUI::MessageBox::Type::Error);
  154. return;
  155. }
  156. if (path.ends_with(".m3u", AK::CaseSensitivity::CaseInsensitive) || path.ends_with(".m3u8", AK::CaseSensitivity::CaseInsensitive)) {
  157. read_playlist(path);
  158. return;
  159. }
  160. NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(path);
  161. if (loader->has_error() || !loader->sample_rate()) {
  162. const String error_string = loader->error_string();
  163. GUI::MessageBox::show(&m_window, String::formatted("Failed to load audio file: {} ({})", path, error_string.is_null() ? "Unknown error" : error_string),
  164. "Filetype error", GUI::MessageBox::Type::Error);
  165. return;
  166. }
  167. m_window.set_title(String::formatted("{} - SoundPlayer", loader->file()->filename()));
  168. m_playback_progress_slider->set_max(loader->total_samples());
  169. m_playback_progress_slider->set_enabled(true);
  170. m_play_button->set_enabled(true);
  171. m_play_button->set_icon(*m_pause_icon);
  172. m_stop_button->set_enabled(true);
  173. m_playback_progress_slider->set_max(loader->total_samples());
  174. manager().set_loader(move(loader));
  175. set_has_loaded_file(true);
  176. set_loaded_file_samplerate(loader->sample_rate());
  177. set_loaded_filename(path);
  178. play();
  179. }
  180. void SoundPlayerWidgetAdvancedView::set_nonlinear_volume_slider(bool nonlinear)
  181. {
  182. m_nonlinear_volume_slider = nonlinear;
  183. }
  184. void SoundPlayerWidgetAdvancedView::drop_event(GUI::DropEvent& event)
  185. {
  186. event.accept();
  187. window()->move_to_front();
  188. if (event.mime_data().has_urls()) {
  189. auto urls = event.mime_data().urls();
  190. if (urls.is_empty())
  191. return;
  192. open_file(urls.first().path());
  193. }
  194. }
  195. SoundPlayerWidgetAdvancedView::~SoundPlayerWidgetAdvancedView()
  196. {
  197. manager().on_load_sample_buffer = nullptr;
  198. manager().on_update = nullptr;
  199. }
  200. void SoundPlayerWidgetAdvancedView::play()
  201. {
  202. manager().play();
  203. set_paused(false);
  204. set_stopped(false);
  205. }
  206. void SoundPlayerWidgetAdvancedView::read_playlist(StringView path)
  207. {
  208. auto parser = M3UParser::from_file(path);
  209. auto items = parser->parse(true);
  210. VERIFY(items->size() > 0);
  211. try_fill_missing_info(*items, path);
  212. for (auto& item : *items)
  213. m_playlist_model->items().append(item);
  214. set_playlist_visible(true);
  215. m_playlist_model->update();
  216. open_file(items->at(0).path);
  217. if (items->size() > 1) {
  218. m_back_button->set_enabled(true);
  219. m_next_button->set_enabled(true);
  220. } else {
  221. m_back_button->set_enabled(false);
  222. m_next_button->set_enabled(false);
  223. }
  224. }
  225. void SoundPlayerWidgetAdvancedView::set_playlist_visible(bool visible)
  226. {
  227. if (visible) {
  228. m_playlist_widget = m_player_view->parent_widget()->add<PlaylistWidget>();
  229. m_playlist_widget->set_data_model(m_playlist_model);
  230. m_playlist_widget->set_fixed_width(150);
  231. } else {
  232. m_playlist_widget->remove_from_parent();
  233. m_player_view->set_max_width(window()->width());
  234. }
  235. }
  236. void SoundPlayerWidgetAdvancedView::try_fill_missing_info(Vector<M3UEntry>& entries, StringView playlist_p)
  237. {
  238. LexicalPath playlist_path(playlist_p);
  239. Vector<M3UEntry*> to_delete;
  240. for (auto& entry : entries) {
  241. LexicalPath entry_path(entry.path);
  242. if (!entry_path.is_absolute()) {
  243. entry.path = String::formatted("{}/{}", playlist_path.dirname(), entry_path.basename());
  244. }
  245. if (!Core::File::exists(entry.path)) {
  246. GUI::MessageBox::show(window(), String::formatted("The file \"{}\" present in the playlist does not exist or was not found. This file will be ignored.", entry_path.basename()), "Error reading playlist", GUI::MessageBox::Type::Warning);
  247. to_delete.append(&entry);
  248. continue;
  249. }
  250. if (!entry.extended_info->track_display_title.has_value())
  251. entry.extended_info->track_display_title = LexicalPath(entry.path).title();
  252. if (!entry.extended_info->track_length_in_seconds.has_value()) {
  253. if (entry_path.has_extension("wav")) {
  254. auto wav_reader = Audio::Loader::create(entry.path);
  255. entry.extended_info->track_length_in_seconds = wav_reader->total_samples() / wav_reader->sample_rate();
  256. }
  257. //TODO: Implement embedded metadata extractor for other audio formats
  258. }
  259. //TODO: Implement a metadata parser for the uncomfortably numerous popular embedded metadata formats
  260. if (!entry.extended_info->file_size_in_bytes.has_value()) {
  261. FILE* f = fopen(entry.path.characters(), "r");
  262. VERIFY(f != nullptr);
  263. fseek(f, 0, SEEK_END);
  264. entry.extended_info->file_size_in_bytes = ftell(f);
  265. fclose(f);
  266. }
  267. }
  268. for (M3UEntry* entry : to_delete)
  269. entries.remove_first_matching([&](M3UEntry& e) { return &e == entry; });
  270. }