SampleWidget.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "SampleWidget.h"
  27. #include <LibAudio/ABuffer.h>
  28. #include <LibGUI/GPainter.h>
  29. #include <LibM/math.h>
  30. SampleWidget::SampleWidget(GUI::Widget* parent)
  31. : GUI::Frame(parent)
  32. {
  33. set_frame_shape(FrameShape::Container);
  34. set_frame_shadow(FrameShadow::Sunken);
  35. set_frame_thickness(2);
  36. }
  37. SampleWidget::~SampleWidget()
  38. {
  39. }
  40. void SampleWidget::paint_event(GUI::PaintEvent& event)
  41. {
  42. GUI::Frame::paint_event(event);
  43. GUI::Painter painter(*this);
  44. painter.add_clip_rect(event.rect());
  45. painter.fill_rect(frame_inner_rect(), Color::Black);
  46. float sample_max = 0;
  47. int count = 0;
  48. int x_offset = frame_inner_rect().x();
  49. int x = x_offset;
  50. int y_offset = frame_inner_rect().center().y();
  51. if (m_buffer) {
  52. int samples_per_pixel = m_buffer->sample_count() / frame_inner_rect().width();
  53. for (int sample_index = 0; sample_index < m_buffer->sample_count() && (x - x_offset) < frame_inner_rect().width(); ++sample_index) {
  54. float sample = fabsf(m_buffer->samples()[sample_index].left);
  55. sample_max = max(sample, sample_max);
  56. ++count;
  57. if (count >= samples_per_pixel) {
  58. Point min_point = { x, y_offset + static_cast<int>(-sample_max * frame_inner_rect().height() / 2) };
  59. Point max_point = { x++, y_offset + static_cast<int>(sample_max * frame_inner_rect().height() / 2) };
  60. painter.draw_line(min_point, max_point, Color::Green);
  61. count = 0;
  62. sample_max = 0;
  63. }
  64. }
  65. } else {
  66. painter.draw_line({ x, y_offset }, { frame_inner_rect().width(), y_offset }, Color::Green);
  67. }
  68. }
  69. void SampleWidget::set_buffer(Audio::Buffer* buffer)
  70. {
  71. if (m_buffer == buffer)
  72. return;
  73. m_buffer = buffer;
  74. update();
  75. }