Skip to content

Commit

Permalink
Remove typeInfo struct from lmms_basics.h (#7380)
Browse files Browse the repository at this point in the history
* remove typeInfo struct from lmms_basics.h

* Code review

Co-authored-by: saker <sakertooth@gmail.com>

* converted epsilon to a constant

* renamed to approximatelyEqual and moved to top

---------

Co-authored-by: saker <sakertooth@gmail.com>
  • Loading branch information
Rossmaxx and sakertooth committed Aug 7, 2024
1 parent c16616c commit 828cefb
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 61 deletions.
2 changes: 2 additions & 0 deletions include/InstrumentTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#ifndef LMMS_INSTRUMENT_TRACK_H
#define LMMS_INSTRUMENT_TRACK_H

#include <limits>

#include "AudioPort.h"
#include "InstrumentFunctions.h"
#include "InstrumentSoundShaping.h"
Expand Down
52 changes: 0 additions & 52 deletions include/lmms_basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#define LMMS_TYPES_H

#include <cstddef>
#include <limits>

#include "lmmsconfig.h"

Expand Down Expand Up @@ -55,57 +54,6 @@ using mix_ch_t = uint16_t; // Mixer-channel (0 to MAX_CHANNEL)

using jo_id_t = uint32_t; // (unique) ID of a journalling object

// windows headers define "min" and "max" macros, breaking the methods bwloe
#undef min
#undef max

template<typename T>
struct typeInfo
{
static inline T min()
{
return std::numeric_limits<T>::min();
}

static inline T max()
{
return std::numeric_limits<T>::max();
}

static inline T minEps()
{
return 1;
}

static inline bool isEqual( T x, T y )
{
return x == y;
}

static inline T absVal( T t )
{
return t >= 0 ? t : -t;
}
} ;


template<>
inline float typeInfo<float>::minEps()
{
return 1.0e-10f;
}

template<>
inline bool typeInfo<float>::isEqual( float x, float y )
{
if( x == y )
{
return true;
}
return absVal( x - y ) < minEps();
}



constexpr ch_cnt_t DEFAULT_CHANNELS = 2;

Expand Down
2 changes: 2 additions & 0 deletions include/lmms_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ constexpr float F_E = (float) LD_E;
constexpr float F_E_R = (float) LD_E_R;
constexpr float F_SQRT_2 = (float) LD_SQRT_2;

constexpr float F_EPSILON = 1.0e-10f; // 10^-10

// Microtuner
constexpr unsigned int MaxScaleCount = 10; //!< number of scales per project
constexpr unsigned int MaxKeymapCount = 10; //!< number of keyboard mappings per project
Expand Down
5 changes: 5 additions & 0 deletions include/lmms_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
namespace lmms
{

static inline bool approximatelyEqual(float x, float y)
{
return x == y ? true : std::abs(x - y) < F_EPSILON;
}

#ifdef __INTEL_COMPILER

static inline float absFraction( const float _x )
Expand Down
3 changes: 2 additions & 1 deletion plugins/Eq/EqCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "embed.h"
#include "Engine.h"
#include "lmms_constants.h"
#include "lmms_math.h"


namespace lmms::gui
Expand Down Expand Up @@ -65,7 +66,7 @@ QRectF EqHandle::boundingRect() const

float EqHandle::freqToXPixel( float freq , int w )
{
if (typeInfo<float>::isEqual(freq, 0.0f)) { return 0.0f; }
if (approximatelyEqual(freq, 0.0f)) { return 0.0f; }
float min = log10f( 20 );
float max = log10f( 20000 );
float range = max - min;
Expand Down
5 changes: 2 additions & 3 deletions src/core/AutomatableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ float AutomatableModel::inverseScaledValue( float value ) const
template<class T>
void roundAt( T& value, const T& where, const T& step_size )
{
if (std::abs(value - where)
< typeInfo<float>::minEps() * std::abs(step_size))
if (std::abs(value - where) < F_EPSILON * std::abs(step_size))
{
value = where;
}
Expand Down Expand Up @@ -583,7 +582,7 @@ float AutomatableModel::controllerValue( int frameOffset ) const
"lacks implementation for a scale type");
break;
}
if( typeInfo<float>::isEqual( m_step, 1 ) && m_hasStrictStepSize )
if (approximatelyEqual(m_step, 1) && m_hasStrictStepSize)
{
return std::round(v);
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "ConfigManager.h"
#include "SampleFrame.h"
#include "lmms_constants.h"

namespace lmms
{
Expand Down Expand Up @@ -154,7 +155,7 @@ void Effect::checkGate( double _out_sum )

// Check whether we need to continue processing input. Restart the
// counter if the threshold has been exceeded.
if( _out_sum - gate() <= typeInfo<float>::minEps() )
if (_out_sum - gate() <= F_EPSILON)
{
incrementBufferCount();
if( bufferCount() > timeout() )
Expand Down
2 changes: 1 addition & 1 deletion src/core/NotePlayHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ void NotePlayHandle::processTimePos(const TimePos& time, float pitchValue, bool
else
{
const float v = detuning()->automationClip()->valueAt(time - songGlobalParentOffset() - pos());
if (!typeInfo<float>::isEqual(v, m_baseDetuning->value()))
if (!approximatelyEqual(v, m_baseDetuning->value()))
{
m_baseDetuning->setValue(v);
updateFrequency();
Expand Down
2 changes: 1 addition & 1 deletion src/core/Oscillator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ void Oscillator::updateFM( SampleFrame* _ab, const fpp_t _frames,
// should be called every time phase-offset is changed...
inline void Oscillator::recalcPhase()
{
if( !typeInfo<float>::isEqual( m_phaseOffset, m_ext_phaseOffset ) )
if (!approximatelyEqual(m_phaseOffset, m_ext_phaseOffset))
{
m_phase -= m_phaseOffset;
m_phaseOffset = m_ext_phaseOffset;
Expand Down
2 changes: 1 addition & 1 deletion src/core/PresetPreviewPlayHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ PresetPreviewPlayHandle::PresetPreviewPlayHandle( const QString & _preset_file,
// create note-play-handle for it
m_previewNote = NotePlayHandleManager::acquire(
s_previewTC->previewInstrumentTrack(), 0,
typeInfo<f_cnt_t>::max() / 2,
std::numeric_limits<f_cnt_t>::max() / 2,
Note( 0, 0, DefaultKey, 100 ) );

setAudioPort( s_previewTC->previewInstrumentTrack()->audioPort() );
Expand Down
2 changes: 1 addition & 1 deletion src/tracks/InstrumentTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ void InstrumentTrack::processInEvent( const MidiEvent& event, const TimePos& tim
NotePlayHandle* nph =
NotePlayHandleManager::acquire(
this, offset,
typeInfo<f_cnt_t>::max() / 2,
std::numeric_limits<f_cnt_t>::max() / 2,
Note(TimePos(), Engine::getSong()->getPlayPos(Engine::getSong()->playMode()),
event.key(), event.volume(midiPort()->baseVelocity())),
nullptr, event.channel(),
Expand Down

0 comments on commit 828cefb

Please sign in to comment.