1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:38:12 +00:00

SoundPlayer: Auto refresh visualization widgets

Visualization widgets should only have to tell how many samples they
need per frame and have a render method which receives all data relevant
to draw the next frame.
This commit is contained in:
Arne Elster 2022-01-09 22:46:06 +01:00 committed by Andreas Kling
parent 02462b6068
commit 9edaa033e5
7 changed files with 115 additions and 107 deletions

View file

@ -10,7 +10,12 @@
#include <LibAudio/Buffer.h>
#include <LibGUI/Painter.h>
void SampleWidget::paint_event(GUI::PaintEvent& event)
SampleWidget::SampleWidget()
{
MUST(set_render_sample_count(512));
}
void SampleWidget::render(GUI::PaintEvent& event, FixedArray<double> const& samples)
{
GUI::Frame::paint_event(event);
GUI::Painter painter(*this);
@ -18,38 +23,26 @@ void SampleWidget::paint_event(GUI::PaintEvent& event)
painter.add_clip_rect(event.rect());
painter.fill_rect(frame_inner_rect(), Color::Black);
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();
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) {
float sample = AK::fabs((float)m_buffer->samples()[sample_index].left);
if (samples.size() > 0) {
float samples_per_pixel = samples.size() / static_cast<float>(frame_inner_rect().width());
float sample_index = 0;
sample_max = max(sample, sample_max);
++count;
while (sample_index < samples.size()) {
float sample = AK::abs(samples[sample_index]);
for (size_t i = 1; i < static_cast<size_t>(samples_per_pixel + 0.5f); i++)
sample = max(sample, AK::abs(samples[sample_index]));
if (count >= samples_per_pixel) {
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) };
painter.draw_line(min_point, max_point, Color::Green);
count = 0;
sample_max = 0;
}
Gfx::IntPoint min_point = { x, y_offset + static_cast<int>(-sample * frame_inner_rect().height() / 2) };
Gfx::IntPoint max_point = { x, y_offset + static_cast<int>(sample * frame_inner_rect().height() / 2) };
painter.draw_line(min_point, max_point, Color::Green);
sample_index += samples_per_pixel;
x++;
}
} else {
painter.draw_line({ x, y_offset }, { frame_inner_rect().width(), y_offset }, Color::Green);
}
}
void SampleWidget::set_buffer(RefPtr<Audio::Buffer> buffer)
{
if (m_buffer == buffer)
return;
m_buffer = buffer;
update();
}