SoundPlayerWidget.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <AK/StringBuilder.h>
  28. #include <LibGUI/GBoxLayout.h>
  29. #include <LibGUI/GButton.h>
  30. #include <LibGUI/GLabel.h>
  31. #include <LibGUI/GMessageBox.h>
  32. #include <LibM/math.h>
  33. SoundPlayerWidget::SoundPlayerWidget(GUI::Window& window, NonnullRefPtr<Audio::ClientConnection> connection)
  34. : m_window(window)
  35. , m_connection(connection)
  36. , m_manager(connection)
  37. {
  38. set_fill_with_background_color(true);
  39. set_layout(make<GUI::VBoxLayout>());
  40. layout()->set_margins({ 2, 2, 2, 2 });
  41. auto status_widget = GUI::Widget::construct(this);
  42. status_widget->set_fill_with_background_color(true);
  43. status_widget->set_layout(make<GUI::HBoxLayout>());
  44. m_elapsed = GUI::Label::construct(status_widget);
  45. m_elapsed->set_frame_shape(Gfx::FrameShape::Container);
  46. m_elapsed->set_frame_shadow(Gfx::FrameShadow::Sunken);
  47. m_elapsed->set_frame_thickness(2);
  48. m_elapsed->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  49. m_elapsed->set_preferred_size(80, 0);
  50. auto sample_widget_container = GUI::Widget::construct(status_widget.ptr());
  51. sample_widget_container->set_layout(make<GUI::HBoxLayout>());
  52. sample_widget_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  53. m_sample_widget = SampleWidget::construct(sample_widget_container);
  54. m_remaining = GUI::Label::construct(status_widget);
  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_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  59. m_remaining->set_preferred_size(80, 0);
  60. m_slider = Slider::construct(Orientation::Horizontal, this);
  61. m_slider->set_min(0);
  62. m_slider->set_enabled(false);
  63. m_slider->on_knob_released = [&](int value) { m_manager.seek(denormalize_rate(value)); };
  64. auto control_widget = GUI::Widget::construct(this);
  65. control_widget->set_fill_with_background_color(true);
  66. control_widget->set_layout(make<GUI::HBoxLayout>());
  67. control_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  68. control_widget->set_preferred_size(0, 30);
  69. control_widget->layout()->set_margins({ 10, 2, 10, 2 });
  70. control_widget->layout()->set_spacing(10);
  71. m_play = GUI::Button::construct(control_widget);
  72. m_play->set_icon(*m_pause_icon);
  73. m_play->set_enabled(false);
  74. m_play->on_click = [this](GUI::Button& button) {
  75. button.set_icon(m_manager.toggle_pause() ? *m_play_icon : *m_pause_icon);
  76. };
  77. m_stop = GUI::Button::construct(control_widget);
  78. m_stop->set_enabled(false);
  79. m_stop->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png"));
  80. m_stop->on_click = [&](GUI::Button&) { m_manager.stop(); };
  81. m_status = GUI::Label::construct(this);
  82. m_status->set_frame_shape(Gfx::FrameShape::Box);
  83. m_status->set_frame_shadow(Gfx::FrameShadow::Raised);
  84. m_status->set_frame_thickness(4);
  85. m_status->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  86. m_status->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  87. m_status->set_preferred_size(0, 18);
  88. m_status->set_text("No file open!");
  89. update_position(0);
  90. m_manager.on_update = [&]() { update_ui(); };
  91. }
  92. SoundPlayerWidget::~SoundPlayerWidget()
  93. {
  94. }
  95. SoundPlayerWidget::Slider::~Slider()
  96. {
  97. }
  98. void SoundPlayerWidget::hide_scope(bool hide)
  99. {
  100. m_sample_widget->set_visible(!hide);
  101. }
  102. void SoundPlayerWidget::open_file(String path)
  103. {
  104. if (!path.ends_with(".wav")) {
  105. GUI::MessageBox::show("Selected file is not a \".wav\" file!", "Filetype error", GUI::MessageBox::Type::Error);
  106. return;
  107. }
  108. OwnPtr<Audio::WavLoader> loader = make<Audio::WavLoader>(path);
  109. if (loader->has_error()) {
  110. GUI::MessageBox::show(
  111. String::format(
  112. "Failed to load WAV file: %s (%s)",
  113. path.characters(),
  114. loader->error_string()),
  115. "Filetype error", GUI::MessageBox::Type::Error);
  116. return;
  117. }
  118. m_sample_ratio = PLAYBACK_MANAGER_RATE / static_cast<float>(loader->sample_rate());
  119. m_slider->set_max(normalize_rate(static_cast<int>(loader->total_samples())));
  120. m_slider->set_enabled(true);
  121. m_play->set_enabled(true);
  122. m_stop->set_enabled(true);
  123. m_window.set_title(String::format("SoundPlayer - \"%s\"", loader->file()->filename().characters()));
  124. m_status->set_text(String::format(
  125. "Sample rate %uHz, %u %s, %u bits per sample",
  126. loader->sample_rate(),
  127. loader->num_channels(),
  128. (loader->num_channels() == 1) ? "channel" : "channels",
  129. loader->bits_per_sample()));
  130. m_manager.set_loader(move(loader));
  131. update_position(0);
  132. }
  133. int SoundPlayerWidget::normalize_rate(int rate) const
  134. {
  135. return static_cast<int>(rate * m_sample_ratio);
  136. }
  137. int SoundPlayerWidget::denormalize_rate(int rate) const
  138. {
  139. return static_cast<int>(rate / m_sample_ratio);
  140. }
  141. void SoundPlayerWidget::update_ui()
  142. {
  143. m_sample_widget->set_buffer(m_manager.current_buffer());
  144. m_play->set_icon(m_manager.is_paused() ? *m_play_icon : *m_pause_icon);
  145. update_position(m_manager.connection()->get_played_samples());
  146. }
  147. void SoundPlayerWidget::update_position(const int position)
  148. {
  149. int total_norm_samples = position + normalize_rate(m_manager.last_seek());
  150. float seconds = (total_norm_samples / static_cast<float>(PLAYBACK_MANAGER_RATE));
  151. float remaining_seconds = m_manager.total_length() - seconds;
  152. m_elapsed->set_text(String::format(
  153. "Elapsed:\n%u:%02u.%02u",
  154. static_cast<int>(seconds / 60),
  155. static_cast<int>(seconds) % 60,
  156. static_cast<int>(seconds * 100) % 100));
  157. m_remaining->set_text(String::format(
  158. "Remaining:\n%u:%02u.%02u",
  159. static_cast<int>(remaining_seconds / 60),
  160. static_cast<int>(remaining_seconds) % 60,
  161. static_cast<int>(remaining_seconds * 100) % 100));
  162. m_slider->set_value(total_norm_samples);
  163. }