Skip to content

Commit

Permalink
use size_t consistently for collection sizes and indices
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiapoirier committed Sep 2, 2022
1 parent 9b423e8 commit 66aaee2
Show file tree
Hide file tree
Showing 64 changed files with 720 additions and 629 deletions.
8 changes: 4 additions & 4 deletions bufferoverride/bufferoverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class BufferOverride final : public DfxPlugin
void cleanup() override;
void reset() override;

void processaudio(float const* const* inAudio, float* const* outAudio, unsigned long inNumFrames) override;
void processaudio(float const* const* inAudio, float* const* outAudio, size_t inNumFrames) override;
void processparameters() override;
void parameterChanged(long inParameterIndex) override;

long dfx_GetPropertyInfo(dfx::PropertyID inPropertyID, dfx::Scope inScope, unsigned int inItemIndex, size_t& outDataSize, dfx::PropertyFlags& outFlags) override;
long dfx_GetProperty(dfx::PropertyID, dfx::Scope inScope, unsigned int inItemIndex, void* outData) override;

private:
static constexpr long kNumPresets = 16;
static constexpr size_t kNumPresets = 16;
// the lowest parameter value before it behaves as one (no division)
static constexpr float kActiveDivisorMinimum = 2.f;
// We need this to get some maximum buffer size and allocate for that.
Expand All @@ -59,9 +59,9 @@ class BufferOverride final : public DfxPlugin

long ms2samples(double inSizeMS) const;
long beat2samples(double inBeatScalar, double inTempoBPS) const;
void updateBuffer(unsigned long samplePos, bool& ioViewDataChanged);
void updateBuffer(size_t samplePos, bool& ioViewDataChanged);

void heedMidiEvents(unsigned long samplePos);
void heedMidiEvents(size_t samplePos);
float getDivisorParameterFromNote(int currentNote);
float getDivisorParameterFromPitchbend(int valueLSB, int valueMSB);

Expand Down
2 changes: 1 addition & 1 deletion bufferoverride/bufferoverrideformalities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void BufferOverride::reset()
//-------------------------------------------------------------------------
void BufferOverride::initPresets()
{
long i = 1;
size_t i = 1;

setpresetname(i, "drum roll");
setpresetparameter_f(i, kDivisor, 4.0);
Expand Down
14 changes: 9 additions & 5 deletions bufferoverride/bufferoverridemidi.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*------------------------------------------------------------------------
Copyright (C) 2001-2021 Sophia Poirier
Copyright (C) 2001-2022 Sophia Poirier
This file is part of Buffer Override.
Expand Down Expand Up @@ -76,7 +76,7 @@ float BufferOverride::getDivisorParameterFromPitchbend(int valueLSB, int valueMS

//-----------------------------------------------------------------------------
// this function implements the changes that new MIDI events demand
void BufferOverride::heedMidiEvents(unsigned long samplePos)
void BufferOverride::heedMidiEvents(size_t samplePos)
{
auto& midiState = getmidistate();
// look at the events if we have any
Expand All @@ -87,7 +87,7 @@ void BufferOverride::heedMidiEvents(unsigned long samplePos)
bool foundNote = false;
int foundNoteOn = DfxMidi::kInvalidValue;
// search events from the beginning up until the current processing block position
for (long eventIndex = 0; eventIndex < midiState.getBlockEventCount(); eventIndex++)
for (size_t eventIndex = 0; eventIndex < midiState.getBlockEventCount(); eventIndex++)
{
// don't search past the current processing block position
if (midiState.getBlockEvent(eventIndex).mOffsetFrames > samplePos)
Expand Down Expand Up @@ -150,7 +150,7 @@ void BufferOverride::heedMidiEvents(unsigned long samplePos)

// SEARCH FOR PITCHBEND
// search events backwards again looking for the most recent valid pitchbend
for (long eventIndex = midiState.getBlockEventCount() - 1; eventIndex >= 0; eventIndex--)
for (size_t eventIndex = midiState.getBlockEventCount() - 1; eventIndex >= 0; eventIndex--)
{
// once we're below the current block position, pitchbend messages can be considered
if ((midiState.getBlockEvent(eventIndex).mOffsetFrames <= samplePos)
Expand All @@ -169,13 +169,17 @@ void BufferOverride::heedMidiEvents(unsigned long samplePos)
mLastPitchbendLSB = mLastPitchbendMSB = DfxMidi::kInvalidValue;

// invalidate this and all earlier pitchbend messages so that they are not found in a future search
while (eventIndex >= 0)
while (true)
{
if (midiState.getBlockEvent(eventIndex).mStatus == DfxMidi::kStatus_PitchBend)
{
// the note shouldn't be considered anymore
midiState.invalidateBlockEvent(eventIndex);
}
if (eventIndex == 0)
{
break;
}
eventIndex--;
}
}
Expand Down
25 changes: 14 additions & 11 deletions bufferoverride/bufferoverrideprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ To contact the author, use the contact form at https://destroyfx.org/
#include <cmath>
#include <numbers>

#include "dfxmath.h"



//-----------------------------------------------------------------------------
Expand All @@ -48,7 +50,7 @@ long BufferOverride::beat2samples(double inBeatScalar, double inTempoBPS) const
}

//-----------------------------------------------------------------------------
void BufferOverride::updateBuffer(unsigned long samplePos, bool& ioViewDataChanged)
void BufferOverride::updateBuffer(size_t samplePos, bool& ioViewDataChanged)
{
bool doSmoothing = true; // but in some situations, we shouldn't
bool barSync = false; // true if we need to sync up with the next bar start
Expand Down Expand Up @@ -152,12 +154,13 @@ void BufferOverride::updateBuffer(unsigned long samplePos, bool& ioViewDataChang
// this is a new forced buffer just beginning, act accordingly, do bar sync if necessary
else
{
auto const samplesPerBar = std::lround(static_cast<double>(gettimeinfo().mSamplesPerBeat) * gettimeinfo().mNumerator);
long samplesToBar = gettimeinfo().mSamplesToNextBar - samplePos;
auto const samplesPerBar = std::lround(gettimeinfo().mSamplesPerBeat * gettimeinfo().mNumerator);
long samplesToBar = std::lround(gettimeinfo().mSamplesToNextBar) - dfx::math::ToSigned(samplePos);
while ((samplesToBar < 0) && (samplesPerBar > 0))
{
samplesToBar += samplesPerBar;
}
samplesToBar = std::max(samplesToBar, 0L);
if (barSync)
{
// do beat sync for each LFO if it ought to be done
Expand Down Expand Up @@ -243,7 +246,7 @@ void BufferOverride::updateBuffer(unsigned long samplePos, bool& ioViewDataChang

//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
void BufferOverride::processaudio(float const* const* inAudio, float* const* outAudio, unsigned long inNumFrames)
void BufferOverride::processaudio(float const* const* inAudio, float* const* outAudio, size_t inNumFrames)
{
auto const numChannels = getnumoutputs();
auto const entryDivisor = mDivisor;
Expand Down Expand Up @@ -281,7 +284,7 @@ void BufferOverride::processaudio(float const* const* inAudio, float* const* out

//-----------------------AUDIO STUFF---------------------------
// here we begin the audio output loop, which has two checkpoints at the beginning
for (unsigned long sampleIndex = 0; sampleIndex < inNumFrames; sampleIndex++)
for (size_t sampleIndex = 0; sampleIndex < inNumFrames; sampleIndex++)
{
// check if it's the end of this minibuffer
if (mReadPos >= mMinibufferSize)
Expand All @@ -290,21 +293,21 @@ void BufferOverride::processaudio(float const* const* inAudio, float* const* out
}

// store the latest input samples into the buffers
for (unsigned long ch = 0; ch < numChannels; ch++)
for (size_t ch = 0; ch < numChannels; ch++)
{
mBuffers[ch][mWritePos] = inAudio[ch][sampleIndex];
}

// get the current output without any smoothing
for (unsigned long ch = 0; ch < numChannels; ch++)
for (size_t ch = 0; ch < numChannels; ch++)
{
mAudioOutputValues[ch] = mBuffers[ch][mReadPos];
}

// and if smoothing is taking place, get the smoothed audio output
if (mSmoothCount > 0)
{
for (unsigned long ch = 0; ch < numChannels; ch++)
for (size_t ch = 0; ch < numChannels; ch++)
{
// crossfade between the current input and its corresponding overlap sample
// mAudioOutputValues[ch] *= 1.0f - (mSmoothStep * static_cast<float>(mSmoothCount)); // current
Expand All @@ -325,7 +328,7 @@ void BufferOverride::processaudio(float const* const* inAudio, float* const* out
}

// write the output samples into the output stream
for (unsigned long ch = 0; ch < numChannels; ch++)
for (size_t ch = 0; ch < numChannels; ch++)
{
outAudio[ch][sampleIndex] = (mAudioOutputValues[ch] * mOutputGain.getValue()) + (inAudio[ch][sampleIndex] * mInputGain.getValue());
}
Expand All @@ -343,7 +346,7 @@ void BufferOverride::processaudio(float const* const* inAudio, float* const* out
auto& midiState = getmidistate();
if (midiState.getBlockEventCount() > 0)
{
for (long eventIndex = 0; eventIndex < midiState.getBlockEventCount(); eventIndex++)
for (size_t eventIndex = 0; eventIndex < midiState.getBlockEventCount(); eventIndex++)
{
if (DfxMidi::isNote(midiState.getBlockEvent(eventIndex).mStatus))
{
Expand Down Expand Up @@ -372,7 +375,7 @@ void BufferOverride::processaudio(float const* const* inAudio, float* const* out
}
}
}
for (long eventIndex = midiState.getBlockEventCount() - 1; eventIndex >= 0; eventIndex--)
for (size_t eventIndex = midiState.getBlockEventCount() - 1; eventIndex >= 0; eventIndex--)
{
if (midiState.getBlockEvent(eventIndex).mStatus == DfxMidi::kStatus_PitchBend)
{
Expand Down
8 changes: 5 additions & 3 deletions dfx-library/dfxmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ static inline double LambertW(double inValue)
//-----------------------------------------------------------------------------
// provides a good enough parameter smoothing update sample interval for frequency-based parameters;
// this is targeting an update granularity of every 4 sample frames at a 44.1 kHz sample rate
static inline unsigned long GetFrequencyBasedSmoothingStride(double inSamplerate)
static inline size_t GetFrequencyBasedSmoothingStride(double inSamplerate)
{
return std::max(static_cast<unsigned long>(inSamplerate) / 11025ul, 1ul);
assert(inSamplerate > 0.);
// TODO C++23: integer literal suffix UZ
return std::max(static_cast<size_t>(inSamplerate) / size_t(11025), size_t(1));
}


Expand Down Expand Up @@ -381,7 +383,7 @@ class RandomEngine
case RandomSeed::Static:
return 1729;
case RandomSeed::Monotonic:
return std::chrono::steady_clock::now().time_since_epoch().count();
return static_cast<EngineType::result_type>(std::chrono::steady_clock::now().time_since_epoch().count());
case RandomSeed::Entropic:
return std::random_device()();
default:
Expand Down
Loading

0 comments on commit 66aaee2

Please sign in to comment.