From 4107bc02b98d9e21ad9036acb221339782ab5eb6 Mon Sep 17 00:00:00 2001 From: Thomas Mueller <64359888+thmueller64@users.noreply.github.com> Date: Sat, 2 Jan 2021 23:08:05 +0100 Subject: [PATCH] Double check for correct normalization --- plugins/bit_invader/bit_invader.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/plugins/bit_invader/bit_invader.cpp b/plugins/bit_invader/bit_invader.cpp index 3b59fce5edc..2b3a2c3c6ba 100644 --- a/plugins/bit_invader/bit_invader.cpp +++ b/plugins/bit_invader/bit_invader.cpp @@ -45,6 +45,7 @@ #include "plugin_export.h" static const int wavetableSize = 200; +static const float defaultNormalizationFactor = 1.0f; extern "C" { @@ -77,7 +78,17 @@ bSynth::bSynth( float * _shape, NotePlayHandle * _nph, bool _interpolation, sample_shape = new float[wavetableSize]; for (int i=0; i < wavetableSize; ++i) { - sample_shape[i] = _shape[i] * _factor; + float buf = _shape[i] * _factor; + + /* Double check that normalization has been performed correctly, + i.e., the absolute value of all samples is <= 1.0 if _factor + is different to the default normalization factor. If there is + a value > 1.0, clip the sample to 1.0 to limit the range. */ + if ((_factor != defaultNormalizationFactor) && (fabsf(buf) > 1.0f)) + { + buf = (buf < 0) ? -1.0f : 1.0f; + } + sample_shape[i] = buf; } } @@ -273,7 +284,7 @@ void bitInvader::playNote( NotePlayHandle * _n, float factor; if( !m_normalize.value() ) { - factor = 1.0f; + factor = defaultNormalizationFactor; } else {