Skip to content

Commit

Permalink
Code clean up, renames and documenting
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidauminh committed Jul 3, 2024
1 parent f281961 commit e03dacd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 36 deletions.
24 changes: 21 additions & 3 deletions include/SampleThumbnail.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ constexpr unsigned long long THUMBNAIL_SIZE_DIVISOR = 32;

#include <QPainter>

#include <limits>

#include "Sample.h"
#include "SampleBuffer.h"

Expand All @@ -44,18 +46,34 @@ namespace lmms {

struct SampleThumbnailVisualizeParameters
{
// Assign to this field when we need to render using the
// original sample.
const Sample* originalSample = nullptr;

const float amplification;
const bool reversed;

const float sampleStartPercent = 0.0;
const float sampleEndPercent = 1.0;

// Sample clips in the song editor don't need to use these 2 fields.
// [0..1] Range
const float sampleStart = 0.0;
const float sampleEnd = 1.0;

// All the fields below are in pixel unit
const long x;
const long y;

// The length of the rendered sample is proportional to the
// value of this field in the song editor.
//
// Must be fixed regardless of clip length because this
// carries the zoom level information.
const long width;
const long height;

// Clips shorter than the sample length (measuring from the start
// of the sample) can specify this field so rendering cuts
// off early.
const long clipWidthSinceSampleStart = std::numeric_limits<long>::max();
};

struct SampleThumbnailBit
Expand Down
4 changes: 2 additions & 2 deletions plugins/AudioFileProcessor/AudioFileProcessorWaveView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ void AudioFileProcessorWaveView::updateGraph()
.amplification = m_sample->amplification(),
.reversed = m_sample->reversed(),

.sampleStartPercent = static_cast<float>(m_from) / m_sample->sampleSize(),
.sampleEndPercent = static_cast<float>(m_to ) / m_sample->sampleSize(),
.sampleStart = static_cast<float>(m_from) / m_sample->sampleSize(),
.sampleEnd = static_cast<float>(m_to ) / m_sample->sampleSize(),

.x = 0,
.y = 0,
Expand Down
4 changes: 2 additions & 2 deletions plugins/SlicerT/SlicerTWaveform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ void SlicerTWaveform::drawEditorWaveform()
.amplification = sample.amplification(),
.reversed = sample.reversed(),

.sampleStartPercent = static_cast<float>(startFrame) / sample.sampleSize(),
.sampleEndPercent = static_cast<float>( endFrame) / sample.sampleSize(),
.sampleStart = static_cast<float>(startFrame) / sample.sampleSize(),
.sampleEnd = static_cast<float>( endFrame) / sample.sampleSize(),

.x = 0,
.y = zoomOffset,
Expand Down
46 changes: 25 additions & 21 deletions src/gui/SampleThumbnail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ SampleThumbnailBit::SampleThumbnailBit(const SampleFrame& frame):

if (l > r)
{
this->max = l;
this->min = r;
max = l;
min = r;
}
else
{
this->max = r;
this->min = l;
max = r;
min = l;
}
}

Expand All @@ -59,21 +59,21 @@ SampleThumbnailBit::SampleThumbnailBit():

void SampleThumbnailBit::merge(const SampleThumbnailBit& other)
{
this->min = std::min(this->min, other.min);
this->max = std::max(this->max, other.max);
min = std::min(min, other.min);
max = std::max(max, other.max);

this->minRMS = std::min(this->minRMS, other.minRMS);
this->maxRMS = std::max(this->maxRMS, other.maxRMS);
minRMS = std::min(minRMS, other.minRMS);
maxRMS = std::max(maxRMS, other.maxRMS);

this->maxRMS = std::clamp(this->maxRMS, this->min, this->max);
this->minRMS = std::clamp(this->minRMS, this->min, this->max);
//maxRMS = std::clamp(maxRMS, min, max);
//minRMS = std::clamp(minRMS, min, max);
}

void SampleThumbnailBit::mergeFrame(const SampleFrame& frame)
{
const auto other = SampleThumbnailBit(frame);
this->min = std::min(this->min, other.min);
this->max = std::max(this->max, other.max);
min = std::min(min, other.min);
max = std::max(max, other.max);
}

SampleThumbnailListManager::SampleThumbnailListManager()
Expand Down Expand Up @@ -259,7 +259,7 @@ void SampleThumbnailListManager::visualize(
QPainter& painter
) const {

const float sampleViewPercent = parameters.sampleEndPercent - parameters.sampleStartPercent;
const float sampleView = parameters.sampleEnd - parameters.sampleStart;

// We specify that the existence of the original sample
// means we may need the sample to be drawn
Expand All @@ -268,11 +268,11 @@ void SampleThumbnailListManager::visualize(
// For AFP and SlicerT where the sample isn't drawn a whole lot
// of times and waveform is required to be crisp.
//
// However when the sample is small enough, we can still use thumbnails.
// However when the sample too large, we still use thumbnails.
if (parameters.originalSample)
{
const float sampleSize = static_cast<float>(parameters.originalSample->sampleSize());
const long sampleViewSize = static_cast<long>(sampleSize * sampleViewPercent);
const long sampleViewSize = static_cast<long>(sampleSize * sampleView);

if (sampleViewSize / parameters.width < 882)
{
Expand Down Expand Up @@ -302,7 +302,7 @@ void SampleThumbnailListManager::visualize(
auto list = this->list->end()-1;
const auto begin = this->list->begin();

const long widthSelect = static_cast<long>(1.0f * width / sampleViewPercent);
const long widthSelect = static_cast<long>(1.0f * width / sampleView);
//qDebug("%ld", widthSelect);

while (list != begin && list->size() < widthSelect)
Expand All @@ -313,11 +313,11 @@ void SampleThumbnailListManager::visualize(
const auto& thumbnail = *list;

const long thumbnailSize = thumbnail.size();
const long thumbnailLastSample = std::max<long>(parameters.sampleEndPercent*thumbnailSize, 1) - 1;
const long thumbnailLastSample = std::max<long>(parameters.sampleEnd*thumbnailSize, 1) - 1;

//qDebug("Using thumbnail of size %ld", thumbnailSize);

const long tStart = static_cast<long>(parameters.sampleStartPercent * thumbnailSize);
const long tStart = static_cast<long>(parameters.sampleStart * thumbnailSize);

const long thumbnailViewSize = thumbnailLastSample + 1 - tStart;

Expand All @@ -326,9 +326,13 @@ void SampleThumbnailListManager::visualize(

const long tChunk = (thumbnailSize + width) / width;

const long pixelBound = std::min(width, parameters.clipWidthSinceSampleStart);

// Don't draw out of bounds.
long pixelIndex = absXOr0;

//qDebug("%ld", width);

do
{
tIndex = tStart + pixelIndex * thumbnailViewSize / width;
Expand All @@ -353,7 +357,7 @@ void SampleThumbnailListManager::visualize(

pixelIndex++;
}
while (pixelIndex <= width && tIndex < tLast);
while (pixelIndex <= pixelBound && tIndex < tLast);

}

Expand Down Expand Up @@ -387,9 +391,9 @@ void SampleThumbnailListManager::visualize_original(
const auto originalSampleBuffer = parameters.originalSample->data();
const long originalSampleSize = parameters.originalSample->sampleSize();

const long sampleStartFrame = static_cast<long>(parameters.sampleStartPercent * originalSampleSize);
const long sampleStartFrame = static_cast<long>(parameters.sampleStart * originalSampleSize);
const long sampleEndFrame = std::min<long>(
parameters.sampleEndPercent * originalSampleSize,
parameters.sampleEnd * originalSampleSize,
originalSampleSize
);

Expand Down
19 changes: 11 additions & 8 deletions src/gui/clips/SampleClipView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ void SampleClipView::paintEvent( QPaintEvent * pe )
float den = Engine::getSong()->getTimeSigModel().getDenominator();
float ticksPerBar = DefaultTicksPerBar * nom / den;

float offset_start = m_clip->startTimeOffset() / ticksPerBar * pixelsPerBar();
float offset_end = m_clip->sampleLength() * ppb / ticksPerBar;
float length = m_clip->length() * ppb / ticksPerBar;
float offsetStart = m_clip->startTimeOffset() / ticksPerBar * pixelsPerBar();
float sampleLength = m_clip->sampleLength() * ppb / ticksPerBar;
float clipLength = m_clip->length() * ppb / ticksPerBar;
// qDebug("%d %d", (int) offset_start, (int) length);

const auto& sample = m_clip->m_sample;
Expand All @@ -283,15 +283,18 @@ void SampleClipView::paintEvent( QPaintEvent * pe )

// qDebug("parent size %d %d", parentWidget()->width(), parentWidget()->height());
//~ qDebug("ratio %f", ratio);


//qDebug("%f %f", offset_end, length);

const auto parameters = SampleThumbnailVisualizeParameters{
.amplification = sample.amplification(),
.reversed = sample.reversed(),

.x = static_cast<long>(offset_start),
.x = static_cast<long>(offsetStart),
.y = spacing,
.width = std::max(static_cast<int>(offset_end), 1),
.height = rect().bottom() - 2 * spacing
.width = std::max<long>(static_cast<long>(sampleLength), 1),
.height = rect().bottom() - 2 * spacing,
.clipWidthSinceSampleStart = static_cast<long>(clipLength - offsetStart)
};

m_thumbnaillist.visualize(parameters, p);
Expand Down

0 comments on commit e03dacd

Please sign in to comment.