SoundPlayerWidget.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 "SoundPlayerWidget.h"
  27. #include "Common.h"
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/MimeData.h>
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/MessageBox.h>
  34. SoundPlayerWidget::SoundPlayerWidget(GUI::Window& window, PlayerState& state)
  35. : Player(state)
  36. , m_window(window)
  37. {
  38. window.set_resizable(false);
  39. window.resize(350, 140);
  40. set_fill_with_background_color(true);
  41. set_layout<GUI::VerticalBoxLayout>();
  42. layout()->set_margins({ 2, 2, 2, 2 });
  43. auto& status_widget = add<GUI::Widget>();
  44. status_widget.set_fill_with_background_color(true);
  45. status_widget.set_layout<GUI::HorizontalBoxLayout>();
  46. m_elapsed = status_widget.add<GUI::Label>();
  47. m_elapsed->set_frame_shape(Gfx::FrameShape::Container);
  48. m_elapsed->set_frame_shadow(Gfx::FrameShadow::Sunken);
  49. m_elapsed->set_frame_thickness(2);
  50. m_elapsed->set_fixed_width(80);
  51. auto& sample_widget_container = status_widget.add<GUI::Widget>();
  52. sample_widget_container.set_layout<GUI::HorizontalBoxLayout>();
  53. m_sample_widget = sample_widget_container.add<SampleWidget>();
  54. m_remaining = status_widget.add<GUI::Label>();
  55. m_remaining->set_frame_shape(Gfx::FrameShape::Container);
  56. m_remaining->set_frame_shadow(Gfx::FrameShadow::Sunken);
  57. m_remaining->set_frame_thickness(2);
  58. m_remaining->set_fixed_width(80);
  59. m_slider = add<Slider>(Orientation::Horizontal);
  60. m_slider->set_min(0);
  61. m_slider->set_enabled(has_loaded_file());
  62. m_slider->on_knob_released = [&](int value) { manager().seek(denormalize_rate(value)); };
  63. auto& control_widget = add<GUI::Widget>();
  64. control_widget.set_fill_with_background_color(true);
  65. control_widget.set_layout<GUI::HorizontalBoxLayout>();
  66. control_widget.set_fixed_height(30);
  67. control_widget.layout()->set_margins({ 10, 2, 10, 2 });
  68. control_widget.layout()->set_spacing(10);
  69. m_play = control_widget.add<GUI::Button>();
  70. m_play->set_icon(has_loaded_file() ? *m_play_icon : *m_pause_icon);
  71. m_play->set_enabled(has_loaded_file());
  72. m_play->on_click = [this](auto) {
  73. bool paused = manager().toggle_pause();
  74. set_paused(paused);
  75. m_play->set_icon(paused ? *m_play_icon : *m_pause_icon);
  76. };
  77. m_stop = control_widget.add<GUI::Button>();
  78. m_stop->set_enabled(has_loaded_file());
  79. m_stop->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png"));
  80. m_stop->on_click = [this](auto) {
  81. manager().stop();
  82. set_stopped(true);
  83. };
  84. m_status = add<GUI::Label>();
  85. m_status->set_frame_shape(Gfx::FrameShape::Box);
  86. m_status->set_frame_shadow(Gfx::FrameShadow::Raised);
  87. m_status->set_frame_thickness(4);
  88. m_status->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  89. m_status->set_fixed_height(18);
  90. m_status->set_text(has_loaded_file() ? loaded_filename() : "No file open!");
  91. update_position(0);
  92. manager().on_update = [&]() { update_ui(); };
  93. }
  94. SoundPlayerWidget::~SoundPlayerWidget()
  95. {
  96. }
  97. void SoundPlayerWidget::open_file(StringView path)
  98. {
  99. NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(path);
  100. if (loader->has_error() || !loader->sample_rate()) {
  101. const String error_string = loader->error_string();
  102. GUI::MessageBox::show(window(),
  103. String::formatted("Failed to load audio file: {} ({})", path, error_string.is_null() ? "Unknown error" : error_string),
  104. "Filetype error", GUI::MessageBox::Type::Error);
  105. return;
  106. }
  107. m_sample_ratio = PLAYBACK_MANAGER_RATE / static_cast<float>(loader->sample_rate());
  108. m_slider->set_max(normalize_rate(static_cast<int>(loader->total_samples())));
  109. m_slider->set_enabled(true);
  110. m_play->set_enabled(true);
  111. m_stop->set_enabled(true);
  112. m_window.set_title(String::formatted("{} - SoundPlayer", loader->file()->filename()));
  113. m_status->set_text(String::formatted(
  114. "Sample rate {}Hz, {} channel(s), {} bits per sample",
  115. loader->sample_rate(),
  116. loader->num_channels(),
  117. loader->bits_per_sample()));
  118. manager().set_loader(move(loader));
  119. update_position(0);
  120. set_has_loaded_file(true);
  121. set_loaded_filename(path);
  122. }
  123. void SoundPlayerWidget::drop_event(GUI::DropEvent& event)
  124. {
  125. event.accept();
  126. window()->move_to_front();
  127. if (event.mime_data().has_urls()) {
  128. auto urls = event.mime_data().urls();
  129. if (urls.is_empty())
  130. return;
  131. open_file(urls.first().path());
  132. }
  133. }
  134. int SoundPlayerWidget::normalize_rate(int rate) const
  135. {
  136. return static_cast<int>(rate * m_sample_ratio);
  137. }
  138. int SoundPlayerWidget::denormalize_rate(int rate) const
  139. {
  140. return static_cast<int>(rate / m_sample_ratio);
  141. }
  142. void SoundPlayerWidget::update_ui()
  143. {
  144. m_sample_widget->set_buffer(manager().current_buffer());
  145. m_play->set_icon(manager().is_paused() ? *m_play_icon : *m_pause_icon);
  146. update_position(manager().connection()->get_played_samples());
  147. }
  148. void SoundPlayerWidget::update_position(const int position)
  149. {
  150. int total_norm_samples = position + normalize_rate(manager().last_seek());
  151. float seconds = (total_norm_samples / static_cast<float>(PLAYBACK_MANAGER_RATE));
  152. float remaining_seconds = manager().total_length() - seconds;
  153. m_elapsed->set_text(String::formatted(
  154. "Elapsed:\n{}:{:02}.{:02}",
  155. static_cast<int>(seconds / 60),
  156. static_cast<int>(seconds) % 60,
  157. static_cast<int>(seconds * 100) % 100));
  158. m_remaining->set_text(String::formatted(
  159. "Remaining:\n{}:{:02}.{:02}",
  160. static_cast<int>(remaining_seconds / 60),
  161. static_cast<int>(remaining_seconds) % 60,
  162. static_cast<int>(remaining_seconds * 100) % 100));
  163. m_slider->set_value(total_norm_samples);
  164. }
  165. void SoundPlayerWidget::hide_scope(bool hide)
  166. {
  167. m_sample_widget->set_visible(!hide);
  168. }
  169. void SoundPlayerWidget::play()
  170. {
  171. manager().play();
  172. set_paused(false);
  173. set_stopped(false);
  174. }