SoundPlayerWidgetAdvancedView.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "SoundPlayerWidgetAdvancedView.h"
  27. #include "BarsVisualizationWidget.h"
  28. #include "PlaybackManager.h"
  29. #include "SoundPlayerWidget.h"
  30. #include <AK/SIMD.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/Button.h>
  34. #include <LibGUI/DragOperation.h>
  35. #include <LibGUI/Label.h>
  36. #include <LibGUI/MessageBox.h>
  37. #include <LibGUI/Slider.h>
  38. #include <LibGUI/ToolBar.h>
  39. #include <LibGUI/ToolBarContainer.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibGfx/Bitmap.h>
  42. SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window, PlayerState& state)
  43. : Player(state)
  44. , m_window(window)
  45. {
  46. window.resize(455, 350);
  47. window.set_minimum_size(440, 130);
  48. window.set_resizable(true);
  49. set_fill_with_background_color(true);
  50. set_layout<GUI::VerticalBoxLayout>();
  51. m_play_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/play.png");
  52. m_pause_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/pause.png");
  53. m_stop_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png");
  54. m_back_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png");
  55. m_next_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png");
  56. m_visualization = add<BarsVisualizationWidget>();
  57. m_playback_progress_slider = add<Slider>(Orientation::Horizontal);
  58. m_playback_progress_slider->set_fixed_height(20);
  59. m_playback_progress_slider->set_min(0);
  60. m_playback_progress_slider->set_max(this->manager().total_length() * 44100); //this value should be set when we load a new file
  61. m_playback_progress_slider->on_knob_released = [&](int value) {
  62. this->manager().seek(value);
  63. };
  64. auto& toolbar_container = add<GUI::ToolBarContainer>();
  65. auto& menubar = toolbar_container.add<GUI::ToolBar>();
  66. m_play_button = menubar.add<GUI::Button>();
  67. m_play_button->set_icon(is_paused() ? (!has_loaded_file() ? *m_play_icon : *m_pause_icon) : *m_pause_icon);
  68. m_play_button->set_fixed_width(50);
  69. m_play_button->set_enabled(has_loaded_file());
  70. m_play_button->on_click = [&](unsigned) {
  71. bool paused = this->manager().toggle_pause();
  72. set_paused(paused);
  73. m_play_button->set_icon(paused ? *m_play_icon : *m_pause_icon);
  74. };
  75. m_stop_button = menubar.add<GUI::Button>();
  76. m_stop_button->set_icon(*m_stop_icon);
  77. m_stop_button->set_fixed_width(50);
  78. m_stop_button->set_enabled(has_loaded_file());
  79. m_stop_button->on_click = [&](unsigned) {
  80. this->manager().stop();
  81. set_stopped(true);
  82. m_play_button->set_icon(*m_play_icon);
  83. m_stop_button->set_enabled(false);
  84. };
  85. auto& timestamp_label = menubar.add<GUI::Label>();
  86. timestamp_label.set_fixed_width(110);
  87. timestamp_label.set_text("Elapsed: 00:00:00");
  88. // filler_label
  89. menubar.add<GUI::Label>();
  90. m_back_button = menubar.add<GUI::Button>();
  91. m_back_button->set_fixed_width(50);
  92. m_back_button->set_icon(*m_back_icon);
  93. m_back_button->set_enabled(has_loaded_file());
  94. m_next_button = menubar.add<GUI::Button>();
  95. m_next_button->set_fixed_width(50);
  96. m_next_button->set_icon(*m_next_icon);
  97. m_next_button->set_enabled(has_loaded_file());
  98. m_volume_label = &menubar.add<GUI::Label>();
  99. m_volume_label->set_fixed_width(30);
  100. m_volume_label->set_text("100%");
  101. auto& volume_slider = menubar.add<GUI::HorizontalSlider>();
  102. volume_slider.set_fixed_width(95);
  103. volume_slider.set_min(0);
  104. volume_slider.set_max(150);
  105. volume_slider.set_value(100);
  106. volume_slider.on_change = [&](int value) {
  107. double volume = m_nonlinear_volume_slider ? (double)(value * value) / (100 * 100) : value / 100.;
  108. m_volume_label->set_text(String::formatted("{}%", (int)(volume * 100)));
  109. set_volume(volume);
  110. };
  111. set_volume(1.);
  112. set_nonlinear_volume_slider(false);
  113. manager().on_update = [&]() {
  114. //TODO: make this program support other sample rates
  115. int samples_played = client_connection().get_played_samples() + this->manager().last_seek();
  116. int current_second = samples_played / 44100;
  117. timestamp_label.set_text(String::formatted("Elapsed: {:02}:{:02}:{:02}", current_second / 3600, current_second / 60, current_second % 60));
  118. m_playback_progress_slider->set_value(samples_played);
  119. dynamic_cast<Visualization*>(m_visualization.ptr())->set_buffer(this->manager().current_buffer());
  120. };
  121. this->manager().on_load_sample_buffer = [&](Audio::Buffer& buffer) {
  122. if (volume() == 1.)
  123. return;
  124. auto sample_count = buffer.sample_count();
  125. if (sample_count % 4 == 0) {
  126. const int total_iter = sample_count / (sizeof(AK::SIMD::f64x4) / sizeof(double) / 2);
  127. AK::SIMD::f64x4* sample_ptr = const_cast<AK::SIMD::f64x4*>(reinterpret_cast<const AK::SIMD::f64x4*>((buffer.data())));
  128. for (int i = 0; i < total_iter; ++i) {
  129. sample_ptr[i] = sample_ptr[i] * volume();
  130. }
  131. } else {
  132. const int total_iter = sample_count / (sizeof(AK::SIMD::f64x2) / sizeof(double) / 2);
  133. AK::SIMD::f64x2* sample_ptr = const_cast<AK::SIMD::f64x2*>(reinterpret_cast<const AK::SIMD::f64x2*>((buffer.data())));
  134. for (int i = 0; i < total_iter; ++i) {
  135. sample_ptr[i] = sample_ptr[i] * volume();
  136. }
  137. }
  138. };
  139. }
  140. void SoundPlayerWidgetAdvancedView::open_file(StringView path)
  141. {
  142. NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(path);
  143. if (loader->has_error() || !loader->sample_rate()) {
  144. const String error_string = loader->error_string();
  145. GUI::MessageBox::show(&m_window, String::formatted("Failed to load audio file: {} ({})", path, error_string.is_null() ? "Unknown error" : error_string),
  146. "Filetype error", GUI::MessageBox::Type::Error);
  147. return;
  148. }
  149. m_window.set_title(String::formatted("{} - SoundPlayer", loader->file()->filename()));
  150. m_playback_progress_slider->set_max(loader->total_samples());
  151. m_playback_progress_slider->set_enabled(true);
  152. m_play_button->set_enabled(true);
  153. m_stop_button->set_enabled(true);
  154. manager().set_loader(move(loader));
  155. set_has_loaded_file(true);
  156. set_loaded_filename(path);
  157. }
  158. void SoundPlayerWidgetAdvancedView::set_nonlinear_volume_slider(bool nonlinear)
  159. {
  160. m_nonlinear_volume_slider = nonlinear;
  161. }
  162. void SoundPlayerWidgetAdvancedView::drop_event(GUI::DropEvent& event)
  163. {
  164. event.accept();
  165. window()->move_to_front();
  166. if (event.mime_data().has_urls()) {
  167. auto urls = event.mime_data().urls();
  168. if (urls.is_empty())
  169. return;
  170. open_file(urls.first().path());
  171. }
  172. }
  173. SoundPlayerWidgetAdvancedView::~SoundPlayerWidgetAdvancedView()
  174. {
  175. manager().on_load_sample_buffer = nullptr;
  176. manager().on_update = nullptr;
  177. }
  178. void SoundPlayerWidgetAdvancedView::play()
  179. {
  180. manager().play();
  181. set_paused(false);
  182. set_stopped(false);
  183. }