SamplerWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SamplerWidget.h"
  7. #include "TrackManager.h"
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/Button.h>
  10. #include <LibGUI/FilePicker.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGUI/MessageBox.h>
  13. #include <LibGUI/Painter.h>
  14. WaveEditor::WaveEditor(TrackManager& track_manager)
  15. : m_track_manager(track_manager)
  16. {
  17. }
  18. WaveEditor::~WaveEditor()
  19. {
  20. }
  21. int WaveEditor::sample_to_y(double percentage) const
  22. {
  23. double portion_of_half_height = percentage * ((frame_inner_rect().height() - 1) / 2.0);
  24. double y = (frame_inner_rect().height() / 2.0) + portion_of_half_height;
  25. return y;
  26. }
  27. void WaveEditor::paint_event(GUI::PaintEvent& event)
  28. {
  29. GUI::Frame::paint_event(event);
  30. GUI::Painter painter(*this);
  31. painter.fill_rect(frame_inner_rect(), Color::Black);
  32. auto recorded_sample = m_track_manager.current_track().recorded_sample();
  33. if (recorded_sample.is_empty())
  34. return;
  35. double width_scale = static_cast<double>(frame_inner_rect().width()) / recorded_sample.size();
  36. painter.translate(frame_thickness(), frame_thickness());
  37. int prev_x = 0;
  38. int left_prev_y = sample_to_y(recorded_sample[0].left);
  39. int right_prev_y = sample_to_y(recorded_sample[0].right);
  40. painter.set_pixel({ prev_x, left_prev_y }, left_wave_colors[RecordedSample]);
  41. painter.set_pixel({ prev_x, right_prev_y }, right_wave_colors[RecordedSample]);
  42. for (size_t x = 1; x < recorded_sample.size(); ++x) {
  43. int left_y = sample_to_y(recorded_sample[x].left);
  44. int right_y = sample_to_y(recorded_sample[x].right);
  45. Gfx::IntPoint left_point1(prev_x * width_scale, left_prev_y);
  46. Gfx::IntPoint left_point2(x * width_scale, left_y);
  47. painter.draw_line(left_point1, left_point2, left_wave_colors[RecordedSample]);
  48. Gfx::IntPoint right_point1(prev_x * width_scale, right_prev_y);
  49. Gfx::IntPoint right_point2(x * width_scale, right_y);
  50. painter.draw_line(right_point1, right_point2, right_wave_colors[RecordedSample]);
  51. prev_x = x;
  52. left_prev_y = left_y;
  53. right_prev_y = right_y;
  54. }
  55. }
  56. SamplerWidget::SamplerWidget(TrackManager& track_manager)
  57. : m_track_manager(track_manager)
  58. {
  59. set_layout<GUI::VerticalBoxLayout>();
  60. layout()->set_margins(10);
  61. layout()->set_spacing(10);
  62. set_fill_with_background_color(true);
  63. m_open_button_and_recorded_sample_name_container = add<GUI::Widget>();
  64. m_open_button_and_recorded_sample_name_container->set_layout<GUI::HorizontalBoxLayout>();
  65. m_open_button_and_recorded_sample_name_container->layout()->set_spacing(10);
  66. m_open_button_and_recorded_sample_name_container->set_fixed_height(24);
  67. m_open_button = m_open_button_and_recorded_sample_name_container->add<GUI::Button>();
  68. m_open_button->set_fixed_size(24, 24);
  69. m_open_button->set_focus_policy(GUI::FocusPolicy::TabFocus);
  70. m_open_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/open.png"));
  71. m_open_button->on_click = [this](auto) {
  72. Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
  73. if (!open_path.has_value())
  74. return;
  75. String error_string = m_track_manager.current_track().set_recorded_sample(open_path.value());
  76. if (!error_string.is_empty()) {
  77. GUI::MessageBox::show(window(), String::formatted("Failed to load WAV file: {}", error_string.characters()), "Error", GUI::MessageBox::Type::Error);
  78. return;
  79. }
  80. m_recorded_sample_name->set_text(open_path.value());
  81. m_wave_editor->update();
  82. };
  83. m_recorded_sample_name = m_open_button_and_recorded_sample_name_container->add<GUI::Label>("No sample loaded");
  84. m_recorded_sample_name->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  85. m_wave_editor = add<WaveEditor>(m_track_manager);
  86. m_wave_editor->set_fixed_height(100);
  87. }
  88. SamplerWidget::~SamplerWidget()
  89. {
  90. }