Skip to content

Commit

Permalink
Define fpp_t and f_cnt_t to be of size_t (#7363)
Browse files Browse the repository at this point in the history
Defines `fpp_t` and `f_cnt_t` to be of `size_t` within `lmms_basics.h`.  Most of the codebase used `fpp_t` to represent the size of an array of sample frames, which is incorrect, given that `fpp_t` was only 16 bits when sizes greater than 32767 are very possible. `f_cnt_t` was defined as `size_t` as well for consistency.

---------

Co-authored-by: Dominic Clark <mrdomclark@gmail.com>
  • Loading branch information
sakertooth and DomClark committed Jul 9, 2024
1 parent f2c815b commit 1420a1f
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 74 deletions.
7 changes: 4 additions & 3 deletions include/InstrumentFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
#ifndef LMMS_INSTRUMENT_FUNCTIONS_H
#define LMMS_INSTRUMENT_FUNCTIONS_H

#include "JournallingObject.h"
#include "lmms_basics.h"
#include <array>

#include "AutomatableModel.h"
#include "TempoSyncKnobModel.h"
#include "ComboBoxModel.h"
#include "JournallingObject.h"
#include "TempoSyncKnobModel.h"

namespace lmms
{
Expand Down
4 changes: 3 additions & 1 deletion include/Piano.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
#ifndef LMMS_PIANO_H
#define LMMS_PIANO_H

#include "Note.h"
#include <array>

#include "Model.h"
#include "Note.h"

namespace lmms
{
Expand Down
3 changes: 2 additions & 1 deletion include/SampleFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@

#include "lmms_basics.h"

#include <algorithm>
#include <array>
#include <cstddef>
#include <cmath>


namespace lmms
Expand Down
7 changes: 2 additions & 5 deletions include/lmms_basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
#include "lmmsconfig.h"

#include <cstdint>
#include <array>

#include <cmath>
#include <algorithm>


namespace lmms
Expand All @@ -49,8 +46,8 @@ using sample_t = float; // standard sample-type
using int_sample_t = int16_t; // 16-bit-int-sample

using sample_rate_t = uint32_t; // sample-rate
using fpp_t = int16_t; // frames per period (0-16384)
using f_cnt_t = int32_t; // standard frame-count
using fpp_t = size_t; // frames per period (0-16384)
using f_cnt_t = size_t; // standard frame-count
using ch_cnt_t = uint8_t; // channel-count (0-DEFAULT_CHANNELS)
using bpm_t = uint16_t; // tempo (MIN_BPM to MAX_BPM)
using bitrate_t = uint16_t; // bitrate in kbps
Expand Down
5 changes: 3 additions & 2 deletions plugins/AudioFileProcessor/AudioFileProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "SampleLoader.h"
#include "Song.h"

#include "lmms_basics.h"
#include "plugin_export.h"

#include <QDomElement>
Expand Down Expand Up @@ -276,7 +277,7 @@ QString AudioFileProcessor::nodeName() const



auto AudioFileProcessor::beatLen(NotePlayHandle* note) const -> int
auto AudioFileProcessor::beatLen(NotePlayHandle* note) const -> f_cnt_t
{
// If we can play indefinitely, use the default beat note duration
if (static_cast<Sample::Loop>(m_loopModel.value()) != Sample::Loop::Off) { return 0; }
Expand All @@ -292,7 +293,7 @@ auto AudioFileProcessor::beatLen(NotePlayHandle* note) const -> int
: m_nextPlayStartPoint;
const auto duration = m_sample.endFrame() - startFrame;

return static_cast<int>(std::floor(duration * freqFactor));
return static_cast<f_cnt_t>(std::floor(duration * freqFactor));
}


Expand Down
3 changes: 2 additions & 1 deletion plugins/AudioFileProcessor/AudioFileProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "Instrument.h"
#include "Sample.h"
#include "lmms_basics.h"


namespace lmms
Expand All @@ -54,7 +55,7 @@ class AudioFileProcessor : public Instrument

QString nodeName() const override;

auto beatLen(NotePlayHandle* note) const -> int override;
auto beatLen(NotePlayHandle* note) const -> f_cnt_t override;

float desiredReleaseTimeMs() const override
{
Expand Down
68 changes: 31 additions & 37 deletions plugins/AudioFileProcessor/AudioFileProcessorWaveView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@ void AudioFileProcessorWaveView::updateSampleRange()
{
if (m_sample->sampleSize() > 1)
{
const f_cnt_t marging = (m_sample->endFrame() - m_sample->startFrame()) * 0.1;
const auto marging = (m_sample->endFrame() - m_sample->startFrame()) * 0.1;
setFrom(m_sample->startFrame() - marging);
setTo(m_sample->endFrame() + marging);
}
}

void AudioFileProcessorWaveView::setTo(f_cnt_t to)
void AudioFileProcessorWaveView::setTo(int to)
{
m_to = std::min(to, static_cast<lmms::f_cnt_t>(m_sample->sampleSize()));
m_to = std::min(to, static_cast<int>(m_sample->sampleSize()));
}

void AudioFileProcessorWaveView::setFrom(f_cnt_t from)
void AudioFileProcessorWaveView::setFrom(int from)
{
m_from = std::max(from, 0);
}

f_cnt_t AudioFileProcessorWaveView::range() const
int AudioFileProcessorWaveView::range() const
{
return m_to - m_from;
}
Expand Down Expand Up @@ -196,7 +196,7 @@ void AudioFileProcessorWaveView::paintEvent(QPaintEvent * pe)
p.drawPixmap(s_padding, s_padding, m_graph);

const QRect graph_rect(s_padding, s_padding, width() - 2 * s_padding, height() - 2 * s_padding);
const f_cnt_t frames = range();
const auto frames = range();
m_startFrameX = graph_rect.x() + (m_sample->startFrame() - m_from) *
double(graph_rect.width()) / frames;
m_endFrameX = graph_rect.x() + (m_sample->endFrame() - m_from) *
Expand Down Expand Up @@ -341,31 +341,32 @@ void AudioFileProcessorWaveView::updateGraph()

void AudioFileProcessorWaveView::zoom(const bool out)
{
const f_cnt_t start = m_sample->startFrame();
const f_cnt_t end = m_sample->endFrame();
const f_cnt_t frames = m_sample->sampleSize();
const f_cnt_t d_from = start - m_from;
const f_cnt_t d_to = m_to - end;
const auto start = m_sample->startFrame();
const auto end = m_sample->endFrame();
const auto frames = m_sample->sampleSize();

const f_cnt_t step = qMax(1, qMax(d_from, d_to) / 10);
const f_cnt_t step_from = (out ? - step : step);
const f_cnt_t step_to = (out ? step : - step);
const auto dFrom = start - m_from;
const auto dTo = m_to - end;

const double comp_ratio = double(qMin(d_from, d_to))
/ qMax(1, qMax(d_from, d_to));
const auto step = std::max(1.0, std::max(dFrom, dTo) / 10.0);
const auto stepFrom = out ? -step : step;
const auto stepTo = out ? step : -step;

const auto boundedFrom = std::clamp(m_from + step_from, 0, start);
const auto boundedTo = std::clamp(m_to + step_to, end, frames);
const auto boundedFrom = std::clamp(m_from + stepFrom, 0.0, static_cast<double>(start));
const auto boundedTo = std::clamp(m_to + stepTo, static_cast<double>(end), static_cast<double>(frames));

const auto toStep = static_cast<f_cnt_t>(step_from * (boundedTo == m_to ? 1 : comp_ratio));
const auto newFrom
= (out && d_from < d_to) || (!out && d_to < d_from) ? boundedFrom : std::clamp(m_from + toStep, 0, start);
const auto compRatio = std::min(dFrom, dTo) / static_cast<double>(std::max(1, std::max(dFrom, dTo)));
const auto toStep = stepFrom * (boundedTo == m_to ? 1 : compRatio);
const auto newFrom = (out && dFrom < dTo) || (!out && dTo < dFrom)
? boundedFrom
: std::clamp(m_from + toStep, 0.0, static_cast<double>(start));

const auto fromStep = static_cast<f_cnt_t>(step_to * (boundedFrom == m_from ? 1 : comp_ratio));
const auto newTo
= (out && d_from < d_to) || (!out && d_to < d_from) ? std::clamp(m_to + fromStep, end, frames) : boundedTo;
const auto fromStep = stepTo * (boundedFrom == m_from ? 1 : compRatio);
const auto newTo = (out && dFrom < dTo) || (!out && dTo < dFrom)
? std::clamp(m_to + fromStep, static_cast<double>(end), static_cast<double>(frames))
: boundedTo;

if (static_cast<double>(newTo - newFrom) / m_sample->sampleRate() > 0.05)
if ((newTo - newFrom) / m_sample->sampleRate() > 0.05)
{
setFrom(newFrom);
setTo(newTo);
Expand All @@ -375,16 +376,11 @@ void AudioFileProcessorWaveView::zoom(const bool out)
void AudioFileProcessorWaveView::slide(int px)
{
const double fact = qAbs(double(px) / width());
f_cnt_t step = range() * fact;
if (px > 0)
{
step = -step;
}
auto step = range() * fact * (px > 0 ? -1 : 1);

f_cnt_t step_from = qBound<size_t>(0, m_from + step, m_sample->sampleSize()) - m_from;
f_cnt_t step_to = qBound<size_t>(m_from + 1, m_to + step, m_sample->sampleSize()) - m_to;

step = qAbs(step_from) < qAbs(step_to) ? step_from : step_to;
const auto stepFrom = std::clamp(m_from + step, 0.0, static_cast<double>(m_sample->sampleSize())) - m_from;
const auto stepTo = std::clamp(m_to + step, m_from + 1.0, static_cast<double>(m_sample->sampleSize())) - m_to;
step = std::abs(stepFrom) < std::abs(stepTo) ? stepFrom : stepTo;

setFrom(m_from + step);
setTo(m_to + step);
Expand Down Expand Up @@ -465,10 +461,8 @@ void AudioFileProcessorWaveView::reverse()
- m_sample->startFrame()
);

const f_cnt_t from = m_from;
setFrom(m_sample->sampleSize() - m_to);
setTo(m_sample->sampleSize() - from);

setTo(m_sample->sampleSize() - m_from);
m_reversed = ! m_reversed;
}

Expand Down
20 changes: 10 additions & 10 deletions plugins/AudioFileProcessor/AudioFileProcessorWaveView.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ public slots:

Sample const* m_sample;
QPixmap m_graph;
f_cnt_t m_from;
f_cnt_t m_to;
f_cnt_t m_last_from;
f_cnt_t m_last_to;
int m_from;
int m_to;
int m_last_from;
int m_last_to;
float m_last_amp;
knob* m_startKnob;
knob* m_endKnob;
knob* m_loopKnob;
f_cnt_t m_startFrameX;
f_cnt_t m_endFrameX;
f_cnt_t m_loopFrameX;
int m_startFrameX;
int m_endFrameX;
int m_loopFrameX;
bool m_isDragging;
QPoint m_draggingLastPoint;
DraggingType m_draggingType;
Expand All @@ -152,9 +152,9 @@ public slots:

void updateSampleRange();
private:
void setTo(f_cnt_t to);
void setFrom(f_cnt_t from);
f_cnt_t range() const;
void setTo(int to);
void setFrom(int from);
int range() const;
void zoom(const bool out = false);
void slide(int px);
void slideSamplePointByPx(Point point, int px);
Expand Down
4 changes: 2 additions & 2 deletions plugins/Lb302/Lb302.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,8 @@ void Lb302Synth::playNote( NotePlayHandle * _n, SampleFrame* _working_buffer )
m_notes.prepend( _n );
}
m_notesMutex.unlock();
release_frame = qMax( release_frame, _n->framesLeft() + _n->offset() );

release_frame = std::max(release_frame, static_cast<int>(_n->framesLeft()) + static_cast<int>(_n->offset()));
}


Expand Down
3 changes: 2 additions & 1 deletion plugins/Lv2Instrument/Lv2Instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
#define LV2_INSTRUMENT_H

#include <QString>
#include <array>

#include "Instrument.h"
#include "InstrumentView.h"
#include "Note.h"
#include "Lv2ControlBase.h"
#include "Lv2ViewBase.h"
#include "Note.h"

// whether to use MIDI vs playHandle
// currently only MIDI works
Expand Down
7 changes: 4 additions & 3 deletions plugins/MultitapEcho/MultitapEcho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "MultitapEcho.h"
#include "embed.h"
#include "lmms_basics.h"
#include "plugin_export.h"

namespace lmms
Expand Down Expand Up @@ -118,8 +119,8 @@ bool MultitapEchoEffect::processAudioBuffer( SampleFrame* buf, const fpp_t frame
}

// add dry buffer - never swap inputs for dry
m_buffer.writeAddingMultiplied( buf, 0, frames, dryGain );
m_buffer.writeAddingMultiplied(buf, f_cnt_t{0}, frames, dryGain);

// swapped inputs?
if( swapInputs )
{
Expand Down Expand Up @@ -176,4 +177,4 @@ PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
}


} // namespace lmms
} // namespace lmms
2 changes: 1 addition & 1 deletion plugins/Sf2Player/Sf2Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ void Sf2Instrument::renderFrames( f_cnt_t frames, SampleFrame* buf )
}
if( src_data.output_frames_gen > frames )
{
qCritical( "Sf2Instrument: not enough frames: %ld / %d", src_data.output_frames_gen, frames );
qCritical("Sf2Instrument: not enough frames: %ld / %zu", src_data.output_frames_gen, frames);
}
}
else
Expand Down
1 change: 1 addition & 0 deletions plugins/Sf2Player/Sf2Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef SF2_PLAYER_H
#define SF2_PLAYER_H

#include <array>
#include <fluidsynth/types.h>
#include <QMutex>
#include <samplerate.h>
Expand Down
2 changes: 2 additions & 0 deletions plugins/Sfxr/Sfxr.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#ifndef SFXR_H
#define SFXR_H

#include <array>

#include "AutomatableModel.h"
#include "Instrument.h"
#include "InstrumentView.h"
Expand Down
3 changes: 2 additions & 1 deletion plugins/Vibed/VibratingString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "interpolation.h"
#include "AudioEngine.h"
#include "Engine.h"
#include "lmms_basics.h"

#include <algorithm>
#include <cstdlib>
Expand Down Expand Up @@ -99,7 +100,7 @@ void VibratingString::resample(const float* src, f_cnt_t srcFrames, f_cnt_t dstF
{
const float srcFrameFloat = frame * static_cast<float>(srcFrames) / dstFrames;
const float fracPos = srcFrameFloat - static_cast<f_cnt_t>(srcFrameFloat);
const f_cnt_t srcFrame = std::clamp(static_cast<f_cnt_t>(srcFrameFloat), 1, srcFrames - 3);
const f_cnt_t srcFrame = std::clamp(static_cast<f_cnt_t>(srcFrameFloat), f_cnt_t{1}, srcFrames - 3);
m_impulse[frame] = cubicInterpolate(
src[srcFrame - 1],
src[srcFrame + 0],
Expand Down
2 changes: 2 additions & 0 deletions plugins/ZynAddSubFx/LocalZynAddSubFx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef LOCAL_ZYNADDSUBFX_H
#define LOCAL_ZYNADDSUBFX_H

#include <array>

#include "Note.h"

class Master;
Expand Down
2 changes: 1 addition & 1 deletion src/core/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void AudioEngine::pushInputFrames( SampleFrame* _ab, const f_cnt_t _frames )
requestChangeInModel();

f_cnt_t frames = m_inputBufferFrames[ m_inputBufferWrite ];
int size = m_inputBufferSize[ m_inputBufferWrite ];
auto size = m_inputBufferSize[m_inputBufferWrite];
SampleFrame* buf = m_inputBuffer[ m_inputBufferWrite ];

if( frames + _frames > size )
Expand Down
3 changes: 2 additions & 1 deletion src/core/Instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "DummyInstrument.h"
#include "InstrumentTrack.h"
#include "lmms_basics.h"
#include "lmms_constants.h"


Expand Down Expand Up @@ -185,7 +186,7 @@ void Instrument::applyRelease( SampleFrame* buf, const NotePlayHandle * _n )
const auto releaseFrames = desiredReleaseFrames();

const auto endFrame = _n->framesLeft();
const auto startFrame = std::max(0, endFrame - releaseFrames);
const auto startFrame = endFrame - std::min(endFrame, releaseFrames);

for (auto f = startFrame; f < endFrame && f < fpp; f++)
{
Expand Down
Loading

0 comments on commit 1420a1f

Please sign in to comment.