Skip to content

Commit

Permalink
eliminate static function local initialization from realtime contexts…
Browse files Browse the repository at this point in the history
… as it is guaranteed to be thread-safe serialized since C++11, which typically means an compiler-inserted implicit lock
  • Loading branch information
sophiapoirier committed Jul 24, 2023
1 parent 6468a2c commit 78ea9c0
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 54 deletions.
40 changes: 27 additions & 13 deletions brokenfft/brokenfft.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
/* Broken FFT 2.0: Featuring the Super Destroy FX Windowing System! */
/*---------------------------------------------------------------
Destroy FX Library is a collection of foundation code
for creating audio processing plug-ins.
Copyright (C) 2001-2023 Sophia Poirier
This file is part of the Destroy FX Library (version 1.0).
Destroy FX Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Destroy FX Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Destroy FX Library. If not, see <http:https://www.gnu.org/licenses/>.
To contact the author, use the contact form at http:https://destroyfx.org
Broken FFT 2.0: Featuring the Super Destroy FX Windowing System!
---------------------------------------------------------------*/

#include "brokenfft.hpp"
#include "fourier.h"

#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <math.h>

#define MKBUFSIZE(c) (buffersizes[(int)((c)*(BUFFERSIZESSIZE-1))])

const int PLUGIN::buffersizes[BUFFERSIZESSIZE] = {
2, 4, 8, 16, 32, 64, 128, 256, 512,
1024, 2048, 4096, 8192, 16384, 32768,
};
#define MKBUFSIZE(c) (buffersizes[(int)((c)*(buffersizes.size()-1))])

PLUGIN::PLUGIN(audioMasterCallback audioMaster)
: AudioEffectX(audioMaster, NUM_PROGRAMS, NUM_PARAMS) {
Expand Down Expand Up @@ -50,9 +69,7 @@ PLUGIN::PLUGIN(audioMasterCallback audioMaster)
FPARAM(norm, P_NORM, "anorm", 0.0f, "?");


long maxframe = 0;
for (long i=0; i<BUFFERSIZESSIZE; i++)
maxframe = ( buffersizes[i] > maxframe ? buffersizes[i] : maxframe );
constexpr auto maxframe = *std::ranges::max_element(buffersizes);

setup();

Expand Down Expand Up @@ -551,9 +568,6 @@ void PLUGIN::processw(float * in, float * out) {

int samples = framesize;

static int ss;
ss = !ss;

int i = 0;

for (int c = 0; c < samples; c++) tmp[c] = in[c];
Expand Down
31 changes: 28 additions & 3 deletions brokenfft/brokenfft.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
/*---------------------------------------------------------------
Destroy FX Library is a collection of foundation code
for creating audio processing plug-ins.
Copyright (C) 2001-2023 Sophia Poirier
/* BrokenFFT 2, featuring the Super Destroy FX Windowing System! */
This file is part of the Destroy FX Library (version 1.0).
Destroy FX Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Destroy FX Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Destroy FX Library. If not, see <http:https://www.gnu.org/licenses/>.
To contact the author, use the contact form at http:https://destroyfx.org
BrokenFFT 2, featuring the Super Destroy FX Windowing System!
---------------------------------------------------------------*/

#ifndef _DFX_BROKENFFT_H
#define _DFX_BROKENFFT_H

#include <array>
#include <audioeffectx.h>
#include "rfftw.h"

Expand Down Expand Up @@ -174,8 +197,10 @@ class PLUGIN : public AudioEffectX {
int outsize;
int outstart;

#define BUFFERSIZESSIZE 15
static const int buffersizes[BUFFERSIZESSIZE];
static constexpr std::array buffersizes {
2, 4, 8, 16, 32, 64, 128, 256, 512,
1024, 2048, 4096, 8192, 16384, 32768
};

static void tqsort(amplentry * low, int n, int stop);

Expand Down
16 changes: 10 additions & 6 deletions dfx-library/dfxparameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This is our class for doing all kinds of fancy plugin parameter stuff.
#include <cmath>
#include <concepts>
#include <limits>
#include <numbers>
#include <unordered_set>

#include "dfxmath.h"
Expand All @@ -40,41 +41,44 @@ This is our class for doing all kinds of fancy plugin parameter stuff.

//-----------------------------------------------------------------------------
// interpret fractional numbers as integral
static int64_t Float2Int(double const& inValue)
constexpr int64_t Float2Int(double const& inValue)
{
constexpr auto padding = DfxParam::kIntegerPadding;
return static_cast<int64_t>(inValue + ((inValue < 0.) ? -padding : padding));
}

//-----------------------------------------------------------------------------
// interpret fractional numbers as booleans
static bool Float2Boolean(double const& inValue)
constexpr bool Float2Boolean(double const& inValue)
{
return !dfx::math::IsZero(inValue);
}

//-----------------------------------------------------------------------------
// interpret integral numbers as booleans
static bool Int2Boolean(int64_t const& inValue)
constexpr bool Int2Boolean(int64_t const& inValue)
{
return (inValue != 0);
}

//-----------------------------------------------------------------------------
// TODO C++26: constexpr
template <std::floating_point T>
auto sqrt_safe(T inValue)
{
return std::sqrt(std::max(inValue, T(0)));
}

//-----------------------------------------------------------------------------
// TODO C++26: constexpr
template <std::floating_point T>
auto pow_safe(T inBase, T inExponent)
{
return std::pow(std::max(inBase, T(0)), inExponent);
}

//-----------------------------------------------------------------------------
// TODO C++26: constexpr
template <std::floating_point T>
auto log_safe(T inValue)
{
Expand Down Expand Up @@ -392,8 +396,8 @@ double DfxParam::contract(double inLiteralValue) const
double DfxParam::contract(double inLiteralValue, double inMinValue, double inMaxValue, Curve inCurveType, double inCurveSpec)
{
auto const valueRange = inMaxValue - inMinValue;
static constexpr double oneDivThree = 1.0 / 3.0;
static double const logTwo = std::log(2.0);
constexpr double oneDivThree = 1. / 3.;
constexpr auto logTwo = std::numbers::ln2_v<double>;

switch (inCurveType)
{
Expand Down Expand Up @@ -521,7 +525,7 @@ double DfxParam::expand(double inGenValue) const
double DfxParam::expand(double inGenValue, double inMinValue, double inMaxValue, Curve inCurveType, double inCurveSpec)
{
auto const valueRange = inMaxValue - inMinValue;
static double const logTwoInv = 1.0 / std::log(2.0);
constexpr auto logTwoInv = 1. / std::numbers::ln2_v<double>;

switch (inCurveType)
{
Expand Down
41 changes: 29 additions & 12 deletions firefly/firefly.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
/*---------------------------------------------------------------
Destroy FX Library is a collection of foundation code
for creating audio processing plug-ins.
Copyright (C) 2009-2023 Sophia Poirier
/* DFX Firefly : Universal FIR filter; EXTREME style. */
This file is part of the Destroy FX Library (version 1.0).
Destroy FX Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Destroy FX Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Destroy FX Library. If not, see <http:https://www.gnu.org/licenses/>.
To contact the author, use the contact form at http:https://destroyfx.org
DFX Firefly : Universal FIR filter; EXTREME style.
---------------------------------------------------------------*/

#include "firefly.hpp"

#include "fireflyeditor.hpp"

#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <math.h>

#define MKBUFSIZE(c) (buffersizes[(int)((c)*(BUFFERSIZESSIZE-1))])

const int PLUGIN::buffersizes[BUFFERSIZESSIZE] = {
2, 4, 8, 16, 32, 64, 128, 256, 512,
1024, 2048, 4096, 8192, 16384, 32768,
};
#define MKBUFSIZE(c) (buffersizes[(int)((c)*(buffersizes.size()-1))])


PLUGIN::PLUGIN(audioMasterCallback audioMaster)
Expand All @@ -25,9 +43,7 @@ PLUGIN::PLUGIN(audioMasterCallback audioMaster)
FPARAM(bufsizep, P_BUFSIZE, "wsize", 0.5, "samples");
FPARAM(shape, P_SHAPE, "shape", 0.0, "");

long maxframe = 0;
for (long i=0; i<BUFFERSIZESSIZE; i++)
maxframe = ( buffersizes[i] > maxframe ? buffersizes[i] : maxframe );
constexpr auto maxframe = *std::ranges::max_element(buffersizes);

setup();

Expand Down Expand Up @@ -85,6 +101,9 @@ void PLUGIN::resume() {

changed = 0;

ss = false;
t = 0;

}

void PLUGIN::suspend () {
Expand Down Expand Up @@ -187,8 +206,6 @@ void PLUGIN::getParameterLabel(long index, char *label) {
*/
void PLUGIN::processw(float * in, float * out, long samples) {

static int ss;
static int t;
ss = !ss;

#if 1
Expand Down
34 changes: 31 additions & 3 deletions firefly/firefly.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
/*---------------------------------------------------------------
Destroy FX Library is a collection of foundation code
for creating audio processing plug-ins.
Copyright (C) 2009-2023 Sophia Poirier
/* DFX Firefly : Universal FIR filter; EXTREME style. */
This file is part of the Destroy FX Library (version 1.0).
Destroy FX Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Destroy FX Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Destroy FX Library. If not, see <http:https://www.gnu.org/licenses/>.
To contact the author, use the contact form at http:https://destroyfx.org
DFX Firefly : Universal FIR filter; EXTREME style.
---------------------------------------------------------------*/

#ifndef _DFX_FIREFLY_H
#define _DFX_FIREFLY_H

#include <array>
#include <audioeffectx.h>

#ifdef WIN32
Expand Down Expand Up @@ -156,8 +179,10 @@ class PLUGIN : public AudioEffectX {
int outsize;
int outstart;

#define BUFFERSIZESSIZE 15
static const int buffersizes[BUFFERSIZESSIZE];
static constexpr std::array buffersizes {
2, 4, 8, 16, 32, 64, 128, 256, 512,
1024, 2048, 4096, 8192, 16384, 32768
};

/* buffersize is 3 * third, framesize is 2 * third
buffersize is used for outbuf.
Expand All @@ -167,6 +192,9 @@ class PLUGIN : public AudioEffectX {
/* 1 if need to do ioChanged since buffer settings are different now */
int changed;

bool ss = false;
unsigned int t = 0;

};

#define FPARAM(pname, idx, nm, init, un) do { paramptrs[idx].ptr = &pname; paramptrs[idx].name = (nm); paramptrs[idx].units = (un); paramptrs[idx].def = (init); pname = paramptrs[idx].def; } while (0)
Expand Down
41 changes: 27 additions & 14 deletions vardelay/vardelay.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
/* Super Destroy FX : Vardelay! */
/*---------------------------------------------------------------
Destroy FX Library is a collection of foundation code
for creating audio processing plug-ins.
Copyright (C) 2002-2023 Sophia Poirier
This file is part of the Destroy FX Library (version 1.0).
Destroy FX Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Destroy FX Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Destroy FX Library. If not, see <http:https://www.gnu.org/licenses/>.
To contact the author, use the contact form at http:https://destroyfx.org
Super Destroy FX : Vardelay!
---------------------------------------------------------------*/

#include "vardelay.hpp"

#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <math.h>

#define MKBUFSIZE(c) (buffersizes[(int)((c)*(BUFFERSIZESSIZE-1))])

const int PLUGIN::buffersizes[BUFFERSIZESSIZE] = {
2, 4, 8, 16, 32, 64, 128, 256, 512,
1024, 2048, 4096, 8192, 16384, 32768,
};
#define MKBUFSIZE(c) (buffersizes[(int)((c)*(buffersizes.size()-1))])


PLUGIN::PLUGIN(audioMasterCallback audioMaster)
Expand All @@ -29,9 +48,7 @@ PLUGIN::PLUGIN(audioMasterCallback audioMaster)
FPARAM(band[zz], P_DELAYS + zz, "band", 0.0f, "dist");
}

long maxframe = 0;
for (long i=0; i<BUFFERSIZESSIZE; i++)
maxframe = ( buffersizes[i] > maxframe ? buffersizes[i] : maxframe );
constexpr auto maxframe = *std::ranges::max_element(buffersizes);

setup();

Expand Down Expand Up @@ -189,10 +206,6 @@ void PLUGIN::getParameterLabel(long index, char *label) {
*/
void PLUGIN::processw(float * in, float * out, long samples) {

static int ss;
static int t;
ss = !ss;

/* XXX memset */
for(int u=0; u < samples; u ++) out[u] = 0.0f;

Expand Down
Loading

0 comments on commit 78ea9c0

Please sign in to comment.