Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimize sample waveform rendering time #7210

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 45 additions & 35 deletions src/gui/SampleWaveform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,66 @@
namespace lmms::gui {

void SampleWaveform::visualize(Parameters parameters, QPainter& painter, const QRect& rect)
{
{
khoidauminh marked this conversation as resolved.
Show resolved Hide resolved
const auto x = rect.x();
const auto height = rect.height();
const auto width = rect.width();
const auto centerY = rect.center().y();

const auto halfHeight = height / 2;

const auto color = painter.pen().color();
const auto rmsColor = color.lighter(123);

const auto framesPerPixel = std::max<size_t>(1, parameters.size / width);

constexpr auto maxFramesPerPixel = 512;
const auto resolution = std::max<size_t>(1, framesPerPixel / maxFramesPerPixel);
const auto framesPerResolution = framesPerPixel / resolution;

const auto numPixels = std::min<size_t>(parameters.size, width);
auto min = std::vector<float>(numPixels, 1);
auto max = std::vector<float>(numPixels, -1);
auto squared = std::vector<float>(numPixels);

const auto maxFrames = numPixels * framesPerPixel;
for (int i = 0; i < maxFrames; i += resolution)
{
const auto pixelIndex = i / framesPerPixel;
const auto frameIndex = !parameters.reversed ? i : maxFrames - i;

const auto& frame = parameters.buffer[frameIndex];
const auto value = std::accumulate(frame.begin(), frame.end(), 0.0f) / frame.size();
const auto scalingFactor =
halfHeight * parameters.amplification
/ static_cast<float>(parameters.buffer[0].size())
;

if (value > max[pixelIndex]) { max[pixelIndex] = value; }
if (value < min[pixelIndex]) { min[pixelIndex] = value; }

squared[pixelIndex] += value * value;
}

for (int i = 0; i < numPixels; i++)
const auto maxFrames = numPixels * framesPerPixel;

const auto color = painter.pen().color();
const auto rmsColor = color.lighter(123);

constexpr size_t maxFramesPerPixel = 24;

const size_t frameStepSize = std::max<size_t>(framesPerPixel / maxFramesPerPixel, 1);

for (size_t pixelIndex = 0; pixelIndex < numPixels; pixelIndex++)
{
const auto lineY1 = centerY - max[i] * halfHeight * parameters.amplification;
const auto lineY2 = centerY - min[i] * halfHeight * parameters.amplification;
const auto lineX = i + x;
const auto i = pixelIndex * maxFrames / numPixels;
size_t frameIndex = !parameters.reversed ? i : maxFrames - i;
const auto frameIndexBound = std::min(maxFrames, frameIndex + framesPerPixel);

float max = -100.0;
float min = 100.0;
float squared = 0.0;

while (frameIndex < frameIndexBound)
{
const auto& frame = parameters.buffer[frameIndex];
const auto value = std::accumulate(frame.begin(), frame.end(), 0.0f);

if (max < value) max = value;
if (min > value) min = value;
khoidauminh marked this conversation as resolved.
Show resolved Hide resolved

squared += value * value;

frameIndex += frameStepSize;
}

const auto lineY1 = centerY - max * scalingFactor;
const auto lineY2 = centerY - min * scalingFactor;
const auto lineX = pixelIndex + x;
painter.drawLine(lineX, lineY1, lineX, lineY2);

const auto rms = std::sqrt(squared / maxFramesPerPixel);
const auto maxRMS = std::clamp(rms, min, max);
const auto minRMS = std::clamp(-rms, min, max);

const auto rms = std::sqrt(squared[i] / framesPerResolution);
const auto maxRMS = std::clamp(rms, min[i], max[i]);
const auto minRMS = std::clamp(-rms, min[i], max[i]);

const auto rmsLineY1 = centerY - maxRMS * halfHeight * parameters.amplification;
const auto rmsLineY2 = centerY - minRMS * halfHeight * parameters.amplification;
const auto rmsLineY1 = centerY - maxRMS * scalingFactor;
const auto rmsLineY2 = centerY - minRMS * scalingFactor;

painter.setPen(rmsColor);
painter.drawLine(lineX, rmsLineY1, lineX, rmsLineY2);
Expand Down
Loading