1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-10-30 09:02:35 +00:00
serenity/Userland/Applications/SoundPlayer/SampleWidget.cpp
kleines Filmröllchen ab49fcfb7c LibAudio+Userland: Remove Audio::LegacyBuffer
The file is now renamed to Queue.h, and the Resampler APIs with
LegacyBuffer are also removed. These changes look large because nobody
actually needs Buffer.h (or Queue.h). It was mostly transitive
dependencies on the massive list of includes in that header, which are
now almost all gone. Instead, we include common things like Sample.h
directly, which should give faster compile times as very few files
actually need Queue.h.
2022-05-03 23:09:20 +02:00

47 lines
1.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "SampleWidget.h"
#include <AK/Math.h>
#include <LibGUI/Painter.h>
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);
painter.add_clip_rect(event.rect());
painter.fill_rect(frame_inner_rect(), Color::Black);
int x_offset = frame_inner_rect().x();
int x = x_offset;
int y_offset = frame_inner_rect().center().y();
if (samples.size() > 0) {
float samples_per_pixel = samples.size() / static_cast<float>(frame_inner_rect().width());
float sample_index = 0;
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]));
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);
}
}