Files
lmms/include/Delay.h
Levin Oehlmann f742710758 Macro cleanup (#6095)
Summary:

* `NULL` -> `nullptr`
* `gui` -> Function `getGUI()`
* `pluginFactory` -> Function `getPluginFactory()`
* `assert` (redefinition) -> using `NDEBUG` instead, which standard `assert` respects.
* `powf` (C stdlib symbol clash) -> removed and all expansions replaced with calls to `std::pow`.
* `exp10` (nonstandard function symbol clash) -> removed and all expansions replaced with calls to `std::pow`.
* `PATH_DEV_DSP` -> File-scope QString of identical name and value.
* `VST_SNC_SHM_KEY_FILE` -> constexpr char* with identical name and value.
* `MM_ALLOC` and `MM_FREE` -> Functions with identical name and implementation.
* `INVAL`, `OUTVAL`, etc. for automation nodes -> Functions with identical names and implementations.
* BandLimitedWave.h: All integer constant macros replaced with constexpr ints of same name and value.
* `FAST_RAND_MAX` -> constexpr int of same name and value.
* `QSTR_TO_STDSTR` -> Function with identical name and equivalent implementation.
* `CCONST` -> constexpr function template with identical name and implementation.
* `F_OPEN_UTF8` -> Function with identical name and equivalent implementation.
* `LADSPA_PATH_SEPARATOR` -> constexpr char with identical name and value.
* `UI_CTRL_KEY` -> constexpr char* with identical name and value.
* `ALIGN_SIZE` -> Renamed to `LMMS_ALIGN_SIZE` and converted from a macro to a constexpr size_t.
* `JACK_MIDI_BUFFER_MAX` -> constexpr size_t with identical name and value.
* versioninfo.h: `PLATFORM`, `MACHINE` and `COMPILER_VERSION` -> prefixed with `LMMS_BUILDCONF_` and converted from macros to constexpr char* literals.
* Header guard _OSCILLOSCOPE -> renamed to OSCILLOSCOPE_H
* Header guard _TIME_DISPLAY_WIDGET -> renamed to TIME_DISPLAY_WIDGET_H
* C-style typecasts in DrumSynth.cpp have been replaced with `static_cast`.
* constexpr numerical constants are initialized with assignment notation instead of curly brace intializers.
* In portsmf, `Alg_seq::operator[]` will throw an exception instead of returning null if the operator index is out of range.

Additionally, in many places, global constants that were declared as `const T foo = bar;` were changed from const to constexpr, leaving them const and making them potentially evaluable at compile time.

Some macros that only appeared in single source files and were unused in those files have been removed entirely.
2021-09-30 18:01:27 +02:00

364 lines
8.4 KiB
C++

/*
* Delay.h - Delay effect objects to use as building blocks in DSP
*
* Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
* Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of LMMS - https://lmms.io
*
* This program 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.
*
* This program 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 this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#ifndef DELAY_H
#define DELAY_H
#include "lmms_basics.h"
#include "lmms_math.h"
#include "interpolation.h"
#include "MemoryManager.h"
// brief usage
// Classes:
// CombFeedback: a feedback comb filter - basically a simple delay line, makes a comb shape in the freq response
// CombFeedfwd: a feed-forward comb filter - an "inverted" comb filter, can be combined with CombFeedback to create a net allpass if negative gain is used
// CombFeedbackDualtap: same as CombFeedback but takes two delay values
// AllpassDelay: an allpass delay - combines feedback and feed-forward - has flat frequency response
// all classes are templated with channel count, any arbitrary channel count can be used for each fx
// Methods (for all classes):
// setDelay sets delay amount in frames. It's up to you to make this samplerate-agnostic.
// Fractions are allowed - linear interpolation is used to deal with them
// CombFeedbackDualTap is a special case: it requires 2 delay times
// setMaxDelay (re)sets the maximum allowed delay, in frames
// NOTE: for performance reasons, there's no bounds checking at setDelay, so make sure you set maxDelay >= delay!
// clearHistory clears the delay buffer
// setGain sets the feedback/feed-forward gain, in linear amplitude, negative values are allowed
// 1.0 is full feedback/feed-forward, -1.0 is full negative feedback/feed-forward
// update runs the fx for one frame - takes as arguments input and number of channel to run, returns output
template<ch_cnt_t CHANNELS>
class CombFeedback
{
public:
typedef double frame[CHANNELS];
CombFeedback( int maxDelay ) :
m_size( maxDelay ),
m_position( 0 ),
m_feedBack( 0.0 ),
m_delay( 0 ),
m_fraction( 0.0 )
{
m_buffer = MM_ALLOC<frame>(maxDelay );
memset( m_buffer, 0, sizeof( frame ) * maxDelay );
}
virtual ~CombFeedback()
{
MM_FREE( m_buffer );
}
inline void setMaxDelay( int maxDelay )
{
if( maxDelay > m_size )
{
MM_FREE( m_buffer );
m_buffer = MM_ALLOC<frame>( maxDelay );
memset( m_buffer, 0, sizeof( frame ) * maxDelay );
}
m_size = maxDelay;
m_position %= m_size;
}
inline void clearHistory()
{
memset( m_buffer, 0, sizeof( frame ) * m_size );
}
inline void setDelay( double delay )
{
m_delay = static_cast<int>( ceil( delay ) );
m_fraction = 1.0 - ( delay - floor( delay ) );
}
inline void setGain( double gain )
{
m_gain = gain;
}
inline double update( double in, ch_cnt_t ch )
{
int readPos = m_position - m_delay;
if( readPos < 0 ) { readPos += m_size; }
const double y = linearInterpolate( m_buffer[readPos][ch], m_buffer[( readPos + 1 ) % m_size][ch], m_fraction );
++m_position %= m_size;
m_buffer[m_position][ch] = in + m_gain * y;
return y;
}
private:
frame * m_buffer;
int m_size;
int m_position;
double m_gain;
int m_delay;
double m_fraction;
};
template<ch_cnt_t CHANNELS>
class CombFeedfwd
{
typedef double frame[CHANNELS];
CombFeedfwd( int maxDelay ) :
m_size( maxDelay ),
m_position( 0 ),
m_feedBack( 0.0 ),
m_delay( 0 ),
m_fraction( 0.0 )
{
m_buffer = MM_ALLOC<frame>( maxDelay );
memset( m_buffer, 0, sizeof( frame ) * maxDelay );
}
virtual ~CombFeedfwd()
{
MM_FREE( m_buffer );
}
inline void setMaxDelay( int maxDelay )
{
if( maxDelay > m_size )
{
MM_FREE( m_buffer );
m_buffer = MM_ALLOC<frame>( maxDelay );
memset( m_buffer, 0, sizeof( frame ) * maxDelay );
}
m_size = maxDelay;
m_position %= m_size;
}
inline void clearHistory()
{
memset( m_buffer, 0, sizeof( frame ) * m_size );
}
inline void setDelay( double delay )
{
m_delay = static_cast<int>( ceil( delay ) );
m_fraction = 1.0 - ( delay - floor( delay ) );
}
inline void setGain( double gain )
{
m_gain = gain;
}
inline double update( double in, ch_cnt_t ch )
{
int readPos = m_position - m_delay;
if( readPos < 0 ) { readPos += m_size; }
const double y = linearInterpolate( m_buffer[readPos][ch], m_buffer[( readPos + 1 ) % m_size][ch], m_fraction ) + in * m_gain;
++m_position %= m_size;
m_buffer[m_position][ch] = in;
return y;
}
private:
frame * m_buffer;
int m_size;
int m_position;
double m_gain;
int m_delay;
double m_fraction;
};
template<ch_cnt_t CHANNELS>
class CombFeedbackDualtap
{
typedef double frame[CHANNELS];
CombFeedbackDualtap( int maxDelay ) :
m_size( maxDelay ),
m_position( 0 ),
m_feedBack( 0.0 ),
m_delay( 0 ),
m_fraction( 0.0 )
{
m_buffer = MM_ALLOC<frame>( maxDelay );
memset( m_buffer, 0, sizeof( frame ) * maxDelay );
}
virtual ~CombFeedbackDualtap()
{
MM_FREE( m_buffer );
}
inline void setMaxDelay( int maxDelay )
{
if( maxDelay > m_size )
{
MM_FREE( m_buffer );
m_buffer = MM_ALLOC<frame>( maxDelay );
memset( m_buffer, 0, sizeof( frame ) * maxDelay );
}
m_size = maxDelay;
m_position %= m_size;
}
inline void clearHistory()
{
memset( m_buffer, 0, sizeof( frame ) * m_size );
}
inline void setDelays( double delay1, double delay2 )
{
m_delay1 = static_cast<int>( ceil( delay1 ) );
m_fraction1 = 1.0 - ( delay1 - floor( delay1 ) );
m_delay2 = static_cast<int>( ceil( delay2 ) );
m_fraction2 = 1.0 - ( delay2 - floor( delay2 ) );
}
inline void setGain( double gain )
{
m_gain = gain;
}
inline double update( double in, ch_cnt_t ch )
{
int readPos1 = m_position - m_delay1;
if( readPos1 < 0 ) { readPos1 += m_size; }
int readPos2 = m_position - m_delay2;
if( readPos2 < 0 ) { readPos2 += m_size; }
const double y = linearInterpolate( m_buffer[readPos1][ch], m_buffer[( readPos1 + 1 ) % m_size][ch], m_fraction1 ) +
linearInterpolate( m_buffer[readPos2][ch], m_buffer[( readPos2 + 1 ) % m_size][ch], m_fraction2 );
++m_position %= m_size;
m_buffer[m_position][ch] = in + m_gain * y;
return y;
}
private:
frame * m_buffer;
int m_size;
int m_position;
double m_gain;
int m_delay1;
int m_delay2;
double m_fraction1;
double m_fraction2;
};
template<ch_cnt_t CHANNELS>
class AllpassDelay
{
public:
typedef double frame[CHANNELS];
AllpassDelay( int maxDelay ) :
m_size( maxDelay ),
m_position( 0 ),
m_feedBack( 0.0 ),
m_delay( 0 ),
m_fraction( 0.0 )
{
m_buffer = MM_ALLOC<frame>( maxDelay );
memset( m_buffer, 0, sizeof( frame ) * maxDelay );
}
virtual ~AllpassDelay()
{
MM_FREE( m_buffer );
}
inline void setMaxDelay( int maxDelay )
{
if( maxDelay > m_size )
{
MM_FREE( m_buffer );
m_buffer = MM_ALLOC<frame>( maxDelay );
memset( m_buffer, 0, sizeof( frame ) * maxDelay );
}
m_size = maxDelay;
m_position %= m_size;
}
inline void clearHistory()
{
memset( m_buffer, 0, sizeof( frame ) * m_size );
}
inline void setDelay( double delay )
{
m_delay = static_cast<int>( ceil( delay ) );
m_fraction = 1.0 - ( delay - floor( delay ) );
}
inline void setGain( double gain )
{
m_gain = gain;
}
inline double update( double in, ch_cnt_t ch )
{
int readPos = m_position - m_delay;
if( readPos < 0 ) { readPos += m_size; }
const double y = linearInterpolate( m_buffer[readPos][ch], m_buffer[( readPos + 1 ) % m_size][ch], m_fraction ) + in * -m_gain;
const double x = in + m_gain * y;
++m_position %= m_size;
m_buffer[m_position][ch] = x;
return y;
}
private:
frame * m_buffer;
int m_size;
int m_position;
double m_gain;
int m_delay;
double m_fraction;
};
// convenience typedefs for stereo effects
typedef CombFeedback<2> StereoCombFeedback;
typedef CombFeedfwd<2> StereoCombFeedfwd;
typedef CombFeedbackDualtap<2> StereoCombFeedbackDualtap;
typedef AllpassDelay<2> StereoAllpassDelay;
#endif