2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-09-04 15:18:41 -03:00
|
|
|
#include "SampleWidget.h"
|
2021-07-17 13:29:28 -03:00
|
|
|
#include <AK/Math.h>
|
2020-02-06 11:18:03 -03:00
|
|
|
#include <LibAudio/Buffer.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Painter.h>
|
2019-09-04 15:18:41 -03:00
|
|
|
|
2020-02-23 06:57:42 -03:00
|
|
|
SampleWidget::SampleWidget()
|
2019-09-04 15:18:41 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SampleWidget::~SampleWidget()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void SampleWidget::paint_event(GUI::PaintEvent& event)
|
2019-09-04 15:18:41 -03:00
|
|
|
{
|
2020-02-02 11:07:41 -03:00
|
|
|
GUI::Frame::paint_event(event);
|
|
|
|
|
GUI::Painter painter(*this);
|
2019-09-04 15:18:41 -03:00
|
|
|
|
2019-10-29 13:31:49 -03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2019-09-04 15:18:41 -03:00
|
|
|
painter.fill_rect(frame_inner_rect(), Color::Black);
|
|
|
|
|
|
2019-10-29 13:31:49 -03:00
|
|
|
float sample_max = 0;
|
|
|
|
|
int count = 0;
|
|
|
|
|
int x_offset = frame_inner_rect().x();
|
|
|
|
|
int x = x_offset;
|
|
|
|
|
int y_offset = frame_inner_rect().center().y();
|
|
|
|
|
|
2019-11-04 15:45:19 -03:00
|
|
|
if (m_buffer) {
|
|
|
|
|
int samples_per_pixel = m_buffer->sample_count() / frame_inner_rect().width();
|
|
|
|
|
for (int sample_index = 0; sample_index < m_buffer->sample_count() && (x - x_offset) < frame_inner_rect().width(); ++sample_index) {
|
2021-07-17 13:29:28 -03:00
|
|
|
float sample = AK::fabs((float)m_buffer->samples()[sample_index].left);
|
2019-10-29 13:31:49 -03:00
|
|
|
|
2019-11-04 15:45:19 -03:00
|
|
|
sample_max = max(sample, sample_max);
|
|
|
|
|
++count;
|
2019-10-29 13:31:49 -03:00
|
|
|
|
2019-11-04 15:45:19 -03:00
|
|
|
if (count >= samples_per_pixel) {
|
2020-06-10 05:57:59 -03:00
|
|
|
Gfx::IntPoint min_point = { x, y_offset + static_cast<int>(-sample_max * frame_inner_rect().height() / 2) };
|
|
|
|
|
Gfx::IntPoint max_point = { x++, y_offset + static_cast<int>(sample_max * frame_inner_rect().height() / 2) };
|
2019-11-04 15:45:19 -03:00
|
|
|
painter.draw_line(min_point, max_point, Color::Green);
|
2019-10-29 13:31:49 -03:00
|
|
|
|
2019-11-04 15:45:19 -03:00
|
|
|
count = 0;
|
|
|
|
|
sample_max = 0;
|
|
|
|
|
}
|
2019-10-29 13:31:49 -03:00
|
|
|
}
|
2019-11-04 15:45:19 -03:00
|
|
|
} else {
|
|
|
|
|
painter.draw_line({ x, y_offset }, { frame_inner_rect().width(), y_offset }, Color::Green);
|
2019-09-04 15:18:41 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-21 08:58:00 -03:00
|
|
|
void SampleWidget::set_buffer(RefPtr<Audio::Buffer> buffer)
|
2019-09-04 15:18:41 -03:00
|
|
|
{
|
|
|
|
|
if (m_buffer == buffer)
|
|
|
|
|
return;
|
|
|
|
|
m_buffer = buffer;
|
|
|
|
|
update();
|
|
|
|
|
}
|