Skip to content

Commit

Permalink
Revisit the initialization for local variables (#7143)
Browse files Browse the repository at this point in the history
* clang-tidy: Apply cppcoreguidelines-init-variables everywhere (treating NaNs as zeros)

* Initialize msec and tick outside switch

* Update plugins/Vestige/Vestige.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/Vestige/Vestige.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/Vestige/Vestige.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/VstEffect/VstEffectControls.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/VstEffect/VstEffectControls.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/VstEffect/VstEffectControls.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Use initialization with =

* Use tabs

* Use static_cast

* Update DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Do not use tabs for alignment in src/core/DrumSynth.cpp

Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>

* Move x variable inside loop

* Use ternary operator for b variable

* Revert "Use tabs"

This reverts commit 07afd8a.

* Remove unnecessary variables in XpressiveView

* Simplify initialization in Plugin

* Combine declaration and initialization in EqCurve

* Combine declaration and initialization in Song

* Combine declaration and initialization in AudioAlsa

* Combine declaration and initialization in EqCurve (again)

* Missed some

* Undo changes made to non-LMMS files

* Undo indentation changes in SidInstrument.cpp

* Combine declaration with assignment in IoHelper

* Combine declaration with assignment using auto in Carla

* Combine declaration with assignment

* Combine declaration with assignment in BasicFilters

* Simplify assignments in AudioFileProcessorWaveView::zoom

* Simplify out sample variable in BitInvader

* Remove sampleLength variable in DelayEffect

* Move gain variable in DynamicsProcessor

* Combine peak variable declaration with assignment in EqSpectrumView

* Move left/right lfo variables in for loop in FlangerEffect

* Use ternary operator for group variable in LadspaControlDialog

* Combine declaration with assignment in Lb302

* Combine declaration with assignment in MidiExport

* Combine declaration with assignment in MidiFile

* Combine declaration with assignment in MidiImport

* Use ternary operator for vel_adjusted variable in OpulenZ

* Move tmpL and dcblkL variables in for loop in ReverbSC

* Combine declaration with initialization in SlicerT

* Combine declaration with assignment in SaSpectrumView

* Combine declaration with assignment in SaWaterfallView

* Combine declaration with assignment in StereoEnhancerEffect

* Combine declaration with assignment in VibratingString

* Combine declaration with assignment in VstEffectControls

* Combine declaration with assignment in Xpressive

* Combine declaration with assignment in AutomatableModel

* Combine declaration with assignment in AutomationClip

* Move sample variable in for loop in BandLimitedWave

* Combine declaration with assignment in DataFile

* Combine declaration with assignment in DrumSynth

* Combine declaration with assignment in Effect

* Remove redundant assignment to nphsLeft in InstrumentPlayHandle

* Combine declaration with assignment in LadspaManager

* Combine declaration with assignment in LinkedModelGroups

* Combine declaration with assignment in MemoryHelper

* Combine declaration with assignment in AudioAlsa

* Combine declaration with assignment in AudioFileOgg

* Combine declaration with assignment in AudioPortAudio

* Combine declaration with assignment in AudioSoundIo

* Combine declaration with assignment in Lv2Evbuf

* Combine declaration with assignment in Lv2Proc

* Combine declaration with assignment in main

* Combine declaration with assignment in MidiAlsaRaw

* Combine declaration with assignment in MidiAlsaSeq

* Combine declaration with assignment in MidiController

* Combine declaration with assignment in MidiJack

* Combine declaration with assignment in MidiSndio

* Combine declaration with assignment in ControlLayout

* Combine declaration with assignment in MainWindow

* Combine declaration with assignment in ProjectNotes

* Use ternary operator for nextValue variable in AutomationClipView

* Combine declaration with assignment in AutomationEditor

* Move length variable in for-loop in PianoRoll

* Combine declaration with assignment in ControllerConnectionDialog

* Combine declaration with assignment in Graph

* Combine declaration with assignment in LcdFloatSpinBox

* Combine declaration with assignment in TimeDisplayWidget

* Remove currentNote variable in InstrumentTrack

* Combine declaration with assignment in DrumSynth (again)

* Use ternary operator for factor variable in BitInvader

* Use ternary operator for highestBandwich variable in EqCurve

Bandwich?

* Move sum variable into for loop in Graph

* Fix format in MidiSndio

* Fixup a few more

* Cleanup error variables

* Use ternary operators and combine declaration with initialization

* Combine declaration with initialization

* Update plugins/LadspaEffect/LadspaControlDialog.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/OpulenZ/OpulenZ.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/SpectrumAnalyzer/SaProcessor.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/midi/MidiAlsaRaw.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/MainWindow.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/clips/AutomationClipView.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/editors/AutomationEditor.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/widgets/Fader.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Move static_cast conversion into separate variable

* Use real index when interpolating

* Remove empty line

* Make helpBtn a private member

* Move controller type into separate variable

* Fix format of DrumSynth::waveform function

* Use tabs and static_cast

* Remove redundant if branch

* Refactor using static_cast/reinterpret_cast

* Add std namespace prefix

* Store repeated conditional into boolean variable

* Cast to int before assigning to m_currentLength

* Rename note_frames to noteFrames

* Update src/core/Controller.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/widgets/Graph.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Revert changes that initialized variables redudantly

For situations where the initialization is
more complex or passed into a function
by a pointer, we dont need to do
initialization ourselves since it is
already done for us, just in a different way.

* Remove redundant err variable

* Remove explicit check of err variable

* Clean up changes and address review

* Do not initialize to 0/nullptr when not needed

* Wrap condition in parentheses for readability

---------

Co-authored-by: Kevin Zander <veratil@gmail.com>
Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>
  • Loading branch information
3 people committed Mar 28, 2024
1 parent 6f5f2c2 commit b2f2fc4
Show file tree
Hide file tree
Showing 72 changed files with 507 additions and 825 deletions.
41 changes: 18 additions & 23 deletions include/BasicFilters.h
Expand Up @@ -340,7 +340,7 @@ class BasicFilters

inline sample_t update( sample_t _in0, ch_cnt_t _chnl )
{
sample_t out;
sample_t out = 0.0f;
switch( m_type )
{
case FilterType::Moog:
Expand Down Expand Up @@ -375,7 +375,6 @@ class BasicFilters
// input signal is linear-interpolated after oversampling, output signal is averaged from oversampled outputs
case FilterType::Tripole:
{
out = 0.0f;
float ip = 0.0f;
for( int i = 0; i < 4; ++i )
{
Expand Down Expand Up @@ -431,7 +430,6 @@ class BasicFilters
case FilterType::Highpass_SV:
{
float hp;

for( int i = 0; i < 2; ++i ) // 2x oversample
{
m_delay2[_chnl] = m_delay2[_chnl] + m_svf1 * m_delay1[_chnl];
Expand All @@ -444,16 +442,15 @@ class BasicFilters

case FilterType::Notch_SV:
{
float hp1, hp2;

float hp1;
for( int i = 0; i < 2; ++i ) // 2x oversample
{
m_delay2[_chnl] = m_delay2[_chnl] + m_svf1 * m_delay1[_chnl]; /* delay2/4 = lowpass output */
hp1 = _in0 - m_delay2[_chnl] - m_svq * m_delay1[_chnl];
m_delay1[_chnl] = m_svf1 * hp1 + m_delay1[_chnl]; /* delay1/3 = bandpass output */

m_delay4[_chnl] = m_delay4[_chnl] + m_svf2 * m_delay3[_chnl];
hp2 = m_delay2[_chnl] - m_delay4[_chnl] - m_svq * m_delay3[_chnl];
float hp2 = m_delay2[_chnl] - m_delay4[_chnl] - m_svq * m_delay3[_chnl];
m_delay3[_chnl] = m_svf2 * hp2 + m_delay3[_chnl];
}

Expand All @@ -469,19 +466,19 @@ class BasicFilters

case FilterType::Lowpass_RC12:
{
sample_t lp, bp, hp, in;
sample_t lp = 0.0f;
for( int n = 4; n != 0; --n )
{
in = _in0 + m_rcbp0[_chnl] * m_rcq;
sample_t in = _in0 + m_rcbp0[_chnl] * m_rcq;
in = std::clamp(in, -1.0f, 1.0f);

lp = in * m_rcb + m_rclp0[_chnl] * m_rca;
lp = std::clamp(lp, -1.0f, 1.0f);

hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
sample_t hp = m_rcc * (m_rchp0[_chnl] + in - m_rclast0[_chnl]);
hp = std::clamp(hp, -1.0f, 1.0f);

bp = hp * m_rcb + m_rcbp0[_chnl] * m_rca;
sample_t bp = hp * m_rcb + m_rcbp0[_chnl] * m_rca;
bp = std::clamp(bp, -1.0f, 1.0f);

m_rclast0[_chnl] = in;
Expand All @@ -494,10 +491,10 @@ class BasicFilters
case FilterType::Highpass_RC12:
case FilterType::Bandpass_RC12:
{
sample_t hp, bp, in;
sample_t hp, bp;
for( int n = 4; n != 0; --n )
{
in = _in0 + m_rcbp0[_chnl] * m_rcq;
sample_t in = _in0 + m_rcbp0[_chnl] * m_rcq;
in = std::clamp(in, -1.0f, 1.0f);

hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
Expand All @@ -515,20 +512,20 @@ class BasicFilters

case FilterType::Lowpass_RC24:
{
sample_t lp, bp, hp, in;
sample_t lp;
for( int n = 4; n != 0; --n )
{
// first stage is as for the 12dB case...
in = _in0 + m_rcbp0[_chnl] * m_rcq;
sample_t in = _in0 + m_rcbp0[_chnl] * m_rcq;
in = std::clamp(in, -1.0f, 1.0f);

lp = in * m_rcb + m_rclp0[_chnl] * m_rca;
lp = std::clamp(lp, -1.0f, 1.0f);

hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
sample_t hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
hp = std::clamp(hp, -1.0f, 1.0f);

bp = hp * m_rcb + m_rcbp0[_chnl] * m_rca;
sample_t bp = hp * m_rcb + m_rcbp0[_chnl] * m_rca;
bp = std::clamp(bp, -1.0f, 1.0f);

m_rclast0[_chnl] = in;
Expand Down Expand Up @@ -559,11 +556,11 @@ class BasicFilters
case FilterType::Highpass_RC24:
case FilterType::Bandpass_RC24:
{
sample_t hp, bp, in;
sample_t hp, bp;
for( int n = 4; n != 0; --n )
{
// first stage is as for the 12dB case...
in = _in0 + m_rcbp0[_chnl] * m_rcq;
sample_t in = _in0 + m_rcbp0[_chnl] * m_rcq;
in = std::clamp(in, -1.0f, 1.0f);

hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
Expand Down Expand Up @@ -600,20 +597,18 @@ class BasicFilters
case FilterType::FastFormant:
{
if (std::abs(_in0) < 1.0e-10f && std::abs(m_vflast[0][_chnl]) < 1.0e-10f) { return 0.0f; } // performance hack - skip processing when the numbers get too small
sample_t hp, bp, in;

out = 0;
const int os = m_type == FilterType::FastFormant ? 1 : 4; // no oversampling for fast formant
for( int o = 0; o < os; ++o )
{
// first formant
in = _in0 + m_vfbp[0][_chnl] * m_vfq;
sample_t in = _in0 + m_vfbp[0][_chnl] * m_vfq;
in = std::clamp(in, -1.0f, 1.0f);

hp = m_vfc[0] * ( m_vfhp[0][_chnl] + in - m_vflast[0][_chnl] );
sample_t hp = m_vfc[0] * ( m_vfhp[0][_chnl] + in - m_vflast[0][_chnl] );
hp = std::clamp(hp, -1.0f, 1.0f);

bp = hp * m_vfb[0] + m_vfbp[0][_chnl] * m_vfa[0];
sample_t bp = hp * m_vfb[0] + m_vfbp[0][_chnl] * m_vfa[0];
bp = std::clamp(bp, -1.0f, 1.0f);

m_vflast[0][_chnl] = in;
Expand Down
5 changes: 2 additions & 3 deletions include/IoHelper.h
Expand Up @@ -75,13 +75,12 @@ inline FILE* F_OPEN_UTF8(std::string const& fname, const char* mode){

inline int fileToDescriptor(FILE* f, bool closeFile = true)
{
int fh;
if (f == nullptr) {return -1;}

#ifdef LMMS_BUILD_WIN32
fh = _dup(_fileno(f));
int fh = _dup(_fileno(f));
#else
fh = dup(fileno(f));
int fh = dup(fileno(f));
#endif

if (closeFile) {fclose(f);}
Expand Down
3 changes: 1 addition & 2 deletions include/LocaleHelper.h
Expand Up @@ -39,10 +39,9 @@ namespace lmms::LocaleHelper
inline double toDouble(QString str, bool* ok = nullptr)
{
bool isOkay;
double value;
QLocale c(QLocale::C);
c.setNumberOptions(QLocale::RejectGroupSeparator);
value = c.toDouble(str, &isOkay);
double value = c.toDouble(str, &isOkay);
if (!isOkay)
{
QLocale german(QLocale::German);
Expand Down
35 changes: 12 additions & 23 deletions plugins/AudioFileProcessor/AudioFileProcessorWaveView.cpp
Expand Up @@ -354,32 +354,21 @@ void AudioFileProcessorWaveView::zoom(const bool out)
const double comp_ratio = double(qMin(d_from, d_to))
/ qMax(1, qMax(d_from, d_to));

f_cnt_t new_from;
f_cnt_t new_to;
const auto boundedFrom = std::clamp(m_from + step_from, 0, start);
const auto boundedTo = std::clamp(m_to + step_to, end, frames);

if ((out && d_from < d_to) || (! out && d_to < d_from))
{
new_from = qBound(0, m_from + step_from, start);
new_to = qBound(
end,
m_to + f_cnt_t(step_to * (new_from == m_from ? 1 : comp_ratio)),
frames
);
}
else
{
new_to = qBound(end, m_to + step_to, frames);
new_from = qBound(
0,
m_from + f_cnt_t(step_from * (new_to == m_to ? 1 : comp_ratio)),
start
);
}
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 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;

if (static_cast<double>(new_to - new_from) / m_sample->sampleRate() > 0.05)
if (static_cast<double>(newTo - newFrom) / m_sample->sampleRate() > 0.05)
{
setFrom(new_from);
setTo(new_to);
setFrom(newFrom);
setTo(newTo);
}
}

Expand Down
45 changes: 10 additions & 35 deletions plugins/BitInvader/BitInvader.cpp
Expand Up @@ -110,34 +110,18 @@ sample_t BSynth::nextStringSample( float sample_length )
sample_realindex -= sample_length;
}

sample_t sample;

if (interpolation) {
const auto currentRealIndex = sample_realindex;
const auto currentIndex = static_cast<int>(sample_realindex);
sample_realindex += sample_step;

// find position in shape
int a = static_cast<int>(sample_realindex);
int b;
if (a < (sample_length-1)) {
b = static_cast<int>(sample_realindex+1);
} else {
b = 0;
}

// Nachkommaanteil
const float frac = fraction( sample_realindex );

sample = linearInterpolate( sample_shape[a], sample_shape[b], frac );

} else {
// No interpolation
sample_index = static_cast<int>(sample_realindex);
sample = sample_shape[sample_index];
if (!interpolation)
{
sample_index = currentIndex;
return sample_shape[sample_index];
}

// progress in shape
sample_realindex += sample_step;

return sample;
const auto nextIndex = currentIndex < sample_length - 1 ? currentIndex + 1 : 0;
return linearInterpolate(sample_shape[currentIndex], sample_shape[nextIndex], fraction(currentRealIndex));
}

/***********************************************************************
Expand Down Expand Up @@ -276,16 +260,7 @@ void BitInvader::playNote( NotePlayHandle * _n,
{
if (!_n->m_pluginData)
{
float factor;
if( !m_normalize.value() )
{
factor = defaultNormalizationFactor;
}
else
{
factor = m_normalizeFactor;
}

float factor = !m_normalize.value() ? defaultNormalizationFactor : m_normalizeFactor;
_n->m_pluginData = new BSynth(
const_cast<float*>( m_graph.samples() ),
_n,
Expand Down
5 changes: 2 additions & 3 deletions plugins/CarlaBase/Carla.cpp
Expand Up @@ -1116,16 +1116,15 @@ void CarlaParamsView::clearKnobs()
}

// Remove spacers
QLayoutItem* item;
for (int16_t i=m_inputScrollAreaLayout->count() - 1; i > 0; i--)
{
item = m_inputScrollAreaLayout->takeAt(i);
auto item = m_inputScrollAreaLayout->takeAt(i);
if (item->widget()) {continue;}
delete item;
}
for (int16_t i=m_outputScrollAreaLayout->count() - 1; i > 0; i--)
{
item = m_outputScrollAreaLayout->takeAt(i);
auto item = m_outputScrollAreaLayout->takeAt(i);
if (item->widget()) {continue;}
delete item;
}
Expand Down
5 changes: 2 additions & 3 deletions plugins/Delay/DelayEffect.cpp
Expand Up @@ -115,16 +115,15 @@ bool DelayEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
{
m_outGain = dbfsToAmp( m_delayControls.m_outGainModel.value() );
}
int sampleLength;

for( fpp_t f = 0; f < frames; ++f )
{
dryS[0] = buf[f][0];
dryS[1] = buf[f][1];

m_delay->setFeedback( *feedbackPtr );
m_lfo->setFrequency( *lfoTimePtr );
sampleLength = *lengthPtr * Engine::audioEngine()->processingSampleRate();
m_currentLength = sampleLength;
m_currentLength = static_cast<int>(*lengthPtr * Engine::audioEngine()->processingSampleRate());
m_delay->setLength( m_currentLength + ( *amplitudePtr * ( float )m_lfo->tick() ) );
m_delay->tick( buf[f] );

Expand Down
19 changes: 4 additions & 15 deletions plugins/DynamicsProcessor/DynamicsProcessor.cpp
Expand Up @@ -106,7 +106,6 @@ bool DynProcEffect::processAudioBuffer( sampleFrame * _buf,
int i = 0;

auto sm_peak = std::array{0.0f, 0.0f};
float gain;

double out_sum = 0.0;
const float d = dryLevel();
Expand Down Expand Up @@ -196,20 +195,10 @@ bool DynProcEffect::processAudioBuffer( sampleFrame * _buf,

if( sm_peak[i] > DYN_NOISE_FLOOR )
{
if ( lookup < 1 )
{
gain = frac * samples[0];
}
else
if ( lookup < 200 )
{
gain = linearInterpolate( samples[ lookup - 1 ],
samples[ lookup ], frac );
}
else
{
gain = samples[199];
};
float gain;
if (lookup < 1) { gain = frac * samples[0]; }
else if (lookup < 200) { gain = linearInterpolate(samples[lookup - 1], samples[lookup], frac); }
else { gain = samples[199]; }

s[i] *= gain;
s[i] /= sm_peak[i];
Expand Down

0 comments on commit b2f2fc4

Please sign in to comment.