diff options
author | Uwe Klotz <uwe_klotz@web.de> | 2014-11-12 14:08:00 +0100 |
---|---|---|
committer | Uwe Klotz <uwe_klotz@web.de> | 2014-11-15 19:42:51 +0100 |
commit | 6d5c64a511da6141fbda8d3e4a04b5ee0d593309 (patch) | |
tree | 4f3dc40b78e0647c91c9e72fd439b82079f676ff | |
parent | 0f598f779920436279458632be2955bdc1241ba6 (diff) |
New types/constants/functions for sample and gain values
-rw-r--r-- | src/engine/enginebuffer.cpp | 8 | ||||
-rw-r--r-- | src/engine/enginevinylsoundemu.cpp | 8 | ||||
-rw-r--r-- | src/sampleutil.cpp | 99 | ||||
-rw-r--r-- | src/sampleutil.h | 48 | ||||
-rw-r--r-- | src/test/sampleutiltest.cpp | 4 | ||||
-rw-r--r-- | src/util/types.h | 51 | ||||
-rw-r--r-- | src/vinylcontrol/vinylcontrolxwax.cpp | 12 |
7 files changed, 135 insertions, 95 deletions
diff --git a/src/engine/enginebuffer.cpp b/src/engine/enginebuffer.cpp index 987bf6b906..cbbf938b18 100644 --- a/src/engine/enginebuffer.cpp +++ b/src/engine/enginebuffer.cpp @@ -109,12 +109,12 @@ EngineBuffer::EngineBuffer(QString group, ConfigObject<ConfigValue>* _config, m_iCrossFadeSamples(0), m_iLastBufferSize(0) { - // Generate dither values. When engine samples used to be within [SHRT_MIN, - // SHRT_MAX] dithering values were in the range [-0.5, 0.5]. Now that we - // normalize engine samples to the range [-1.0, 1.0] we divide by SHRT_MAX + // Generate dither values. When engine samples used to be within [SAMPLE_MIN, + // SAMPLE_MAX] dithering values were in the range [-0.5, 0.5]. Now that we + // normalize engine samples to the range [-1.0, 1.0] we divide by SAMPLE_MAX // to preserve the previous behavior. for (unsigned int i = 0; i < MAX_BUFFER_LEN; ++i) { - m_pDitherBuffer[i] = (static_cast<CSAMPLE>(rand() % RAND_MAX) / RAND_MAX - 0.5) / SHRT_MAX; + m_pDitherBuffer[i] = (static_cast<CSAMPLE>(rand() % RAND_MAX) / RAND_MAX - 0.5) / SAMPLE_MAX; } // zero out crossfade buffer diff --git a/src/engine/enginevinylsoundemu.cpp b/src/engine/enginevinylsoundemu.cpp index bb5715a59a..b9f0e260ac 100644 --- a/src/engine/enginevinylsoundemu.cpp +++ b/src/engine/enginevinylsoundemu.cpp @@ -32,12 +32,12 @@ EngineVinylSoundEmu::EngineVinylSoundEmu(QString group) m_dOldSpeed(0.0), m_iNoisePos(0) { Q_UNUSED(group); - // Generate dither values. When engine samples used to be within [SHRT_MIN, - // SHRT_MAX] dithering values were in the range [-0.5, 0.5]. Now that we - // normalize engine samples to the range [-1.0, 1.0] we divide by SHRT_MAX + // Generate dither values. When engine samples used to be within [SAMPLE_MIN, + // SAMPLE_MAX] dithering values were in the range [-0.5, 0.5]. Now that we + // normalize engine samples to the range [-1.0, 1.0] we divide by SAMPLE_MAX // to preserve the previous behavior. for (int i = 0; i < NOISE_BUFFER_SIZE; ++i) { - m_fNoise[i] = (static_cast<CSAMPLE>(rand() % RAND_MAX) / RAND_MAX - 0.5) / SHRT_MAX; + m_fNoise[i] = (static_cast<CSAMPLE>(rand() % RAND_MAX) / RAND_MAX - 0.5) / SAMPLE_MAX; } SampleUtil::applyGain(m_crossfadeBuffer, 0, MAX_BUFFER_LEN); } diff --git a/src/sampleutil.cpp b/src/sampleutil.cpp index 41aa840c34..a323c8d063 100644 --- a/src/sampleutil.cpp +++ b/src/sampleutil.cpp @@ -4,9 +4,8 @@ #include "sampleutil.h" #include "util/math.h" -#include <QtDebug> - #include <functional> + #ifdef __WINDOWS__ #include <QtGlobal> typedef qint64 int64_t; @@ -71,11 +70,11 @@ void SampleUtil::copyNarrowMultiToStereo(CSAMPLE* pDest, const CSAMPLE* pSrc, } // static -void SampleUtil::applyGain(CSAMPLE* pBuffer, CSAMPLE gain, +void SampleUtil::applyGain(CSAMPLE* pBuffer, CSAMPLE_GAIN gain, unsigned int iNumSamples) { - if (gain == CSAMPLE_PEAK) + if (gain == CSAMPLE_GAIN_ONE) return; - if (gain == CSAMPLE_ZERO) { + if (gain == CSAMPLE_GAIN_ZERO) { clear(pBuffer, iNumSamples); return; } @@ -85,19 +84,19 @@ void SampleUtil::applyGain(CSAMPLE* pBuffer, CSAMPLE gain, } // static -void SampleUtil::applyRampingGain(CSAMPLE* pBuffer, CSAMPLE old_gain, - CSAMPLE new_gain, unsigned int iNumSamples) { - if (old_gain == CSAMPLE_PEAK && new_gain == CSAMPLE_PEAK) { +void SampleUtil::applyRampingGain(CSAMPLE* pBuffer, CSAMPLE_GAIN old_gain, + CSAMPLE_GAIN new_gain, unsigned int iNumSamples) { + if (old_gain == CSAMPLE_GAIN_ONE && new_gain == CSAMPLE_GAIN_ONE) { return; } - if (old_gain == CSAMPLE_ZERO && new_gain == CSAMPLE_ZERO) { + if (old_gain == CSAMPLE_GAIN_ZERO && new_gain == CSAMPLE_GAIN_ZERO) { clear(pBuffer, iNumSamples); return; } - const CSAMPLE gain_delta = (CSAMPLE_PEAK + CSAMPLE_PEAK) * (new_gain - old_gain) - / iNumSamples; - CSAMPLE gain = old_gain; + const CSAMPLE_GAIN gain_delta = (new_gain - old_gain) + / CSAMPLE_GAIN(iNumSamples / 2); + CSAMPLE_GAIN gain = old_gain; for (unsigned int i = 0; i < iNumSamples; i += 2, gain += gain_delta) { pBuffer[i] *= gain; pBuffer[i + 1] *= gain; @@ -107,7 +106,7 @@ void SampleUtil::applyRampingGain(CSAMPLE* pBuffer, CSAMPLE old_gain, // static void SampleUtil::applyAlternatingGain(CSAMPLE* pBuffer, CSAMPLE gain1, CSAMPLE gain2, unsigned int iNumSamples) { - // This handles gain1 == CSAMPLE_PEAK && gain2 == CSAMPLE_PEAK as well. + // This handles gain1 == CSAMPLE_GAIN_ONE && gain2 == CSAMPLE_GAIN_ONE as well. if (gain1 == gain2) { return applyGain(pBuffer, gain1, iNumSamples); } @@ -119,9 +118,9 @@ void SampleUtil::applyAlternatingGain(CSAMPLE* pBuffer, CSAMPLE gain1, } // static -void SampleUtil::addWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, CSAMPLE gain, - unsigned int iNumSamples) { - if (gain == CSAMPLE_ZERO) { +void SampleUtil::addWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, + CSAMPLE_GAIN gain, unsigned int iNumSamples) { + if (gain == CSAMPLE_GAIN_ZERO) { return; } @@ -131,14 +130,15 @@ void SampleUtil::addWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, CSAMPLE gain, } void SampleUtil::addWithRampingGain(CSAMPLE* pDest, const CSAMPLE* pSrc, - CSAMPLE old_gain, CSAMPLE new_gain, unsigned int iNumSamples) { - if (old_gain == CSAMPLE_ZERO && new_gain == CSAMPLE_ZERO) { + CSAMPLE_GAIN old_gain, CSAMPLE_GAIN new_gain, + unsigned int iNumSamples) { + if (old_gain == CSAMPLE_GAIN_ZERO && new_gain == CSAMPLE_GAIN_ZERO) { return; } - const CSAMPLE gain_delta = (CSAMPLE_PEAK + CSAMPLE_PEAK) * (new_gain - old_gain) - / iNumSamples; - CSAMPLE gain = old_gain; + const CSAMPLE_GAIN gain_delta = (new_gain - old_gain) + / CSAMPLE_GAIN(iNumSamples / 2); + CSAMPLE_GAIN gain = old_gain; for (unsigned int i = 0; i < iNumSamples; i += 2, gain += gain_delta) { pDest[i] += pSrc[i] * gain; pDest[i + 1] += pSrc[i + 1] * gain; @@ -147,10 +147,11 @@ void SampleUtil::addWithRampingGain(CSAMPLE* pDest, const CSAMPLE* pSrc, // static void SampleUtil::add2WithGain(CSAMPLE* pDest, const CSAMPLE* pSrc1, - CSAMPLE gain1, const CSAMPLE* pSrc2, CSAMPLE gain2, unsigned int iNumSamples) { - if (gain1 == CSAMPLE_ZERO) { + CSAMPLE_GAIN gain1, const CSAMPLE* pSrc2, CSAMPLE_GAIN gain2, + unsigned int iNumSamples) { + if (gain1 == CSAMPLE_GAIN_ZERO) { return addWithGain(pDest, pSrc2, gain2, iNumSamples); - } else if (gain2 == CSAMPLE_ZERO) { + } else if (gain2 == CSAMPLE_GAIN_ZERO) { return addWithGain(pDest, pSrc1, gain1, iNumSamples); } @@ -161,13 +162,13 @@ void SampleUtil::add2WithGain(CSAMPLE* pDest, const CSAMPLE* pSrc1, // static void SampleUtil::add3WithGain(CSAMPLE* pDest, const CSAMPLE* pSrc1, - CSAMPLE gain1, const CSAMPLE* pSrc2, CSAMPLE gain2, - const CSAMPLE* pSrc3, CSAMPLE gain3, unsigned int iNumSamples) { - if (gain1 == CSAMPLE_ZERO) { + CSAMPLE_GAIN gain1, const CSAMPLE* pSrc2, CSAMPLE_GAIN gain2, + const CSAMPLE* pSrc3, CSAMPLE_GAIN gain3, unsigned int iNumSamples) { + if (gain1 == CSAMPLE_GAIN_ZERO) { return add2WithGain(pDest, pSrc2, gain2, pSrc3, gain3, iNumSamples); - } else if (gain2 == CSAMPLE_ZERO) { + } else if (gain2 == CSAMPLE_GAIN_ZERO) { return add2WithGain(pDest, pSrc1, gain1, pSrc3, gain3, iNumSamples); - } else if (gain3 == CSAMPLE_ZERO) { + } else if (gain3 == CSAMPLE_GAIN_ZERO) { return add2WithGain(pDest, pSrc1, gain1, pSrc2, gain2, iNumSamples); } @@ -177,16 +178,16 @@ void SampleUtil::add3WithGain(CSAMPLE* pDest, const CSAMPLE* pSrc1, } // static -void SampleUtil::copyWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, CSAMPLE gain, - unsigned int iNumSamples) { +void SampleUtil::copyWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, + CSAMPLE_GAIN gain, unsigned int iNumSamples) { if (pDest == pSrc) { return applyGain(pDest, gain, iNumSamples); } - if (gain == CSAMPLE_PEAK) { + if (gain == CSAMPLE_GAIN_ONE) { copy(pDest, pSrc, iNumSamples); return; } - if (gain == CSAMPLE_ZERO) { + if (gain == CSAMPLE_GAIN_ZERO) { clear(pDest, iNumSamples); return; } @@ -202,23 +203,24 @@ void SampleUtil::copyWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, CSAMPLE gain, // static void SampleUtil::copyWithRampingGain(CSAMPLE* pDest, const CSAMPLE* pSrc, - CSAMPLE old_gain, CSAMPLE new_gain, unsigned int iNumSamples) { + CSAMPLE_GAIN old_gain, CSAMPLE_GAIN new_gain, + unsigned int iNumSamples) { if (pDest == pSrc) { return applyRampingGain(pDest, old_gain, new_gain, iNumSamples); } - if (old_gain == CSAMPLE_PEAK && new_gain == CSAMPLE_PEAK) { + if (old_gain == CSAMPLE_GAIN_ONE && new_gain == CSAMPLE_GAIN_ONE) { copy(pDest, pSrc, iNumSamples); return; } - if (old_gain == CSAMPLE_ZERO && new_gain == CSAMPLE_ZERO) { + if (old_gain == CSAMPLE_GAIN_ZERO && new_gain == CSAMPLE_GAIN_ZERO) { clear(pDest, iNumSamples); return; } - const CSAMPLE delta = (CSAMPLE_PEAK + CSAMPLE_PEAK) * (new_gain - old_gain) - / iNumSamples; - CSAMPLE gain = old_gain; - for (unsigned int i = 0; i < iNumSamples; i += 2, gain += delta) { + const CSAMPLE_GAIN gain_delta = (new_gain - old_gain) + / CSAMPLE_GAIN(iNumSamples / 2); + CSAMPLE_GAIN gain = old_gain; + for (unsigned int i = 0; i < iNumSamples; i += 2, gain += gain_delta) { pDest[i] = pSrc[i] * gain; pDest[i + 1] = pSrc[i + 1] * gain; } @@ -232,7 +234,7 @@ void SampleUtil::copyWithRampingGain(CSAMPLE* pDest, const CSAMPLE* pSrc, void SampleUtil::convertS16ToFloat32(CSAMPLE* pDest, const SAMPLE* pSrc, unsigned int iNumSamples) { for (unsigned int i = 0; i < iNumSamples; ++i) { - pDest[i] = CSAMPLE(pSrc[i]) / CSAMPLE(SHRT_MAX); + pDest[i] = CSAMPLE(pSrc[i]) / CSAMPLE(SAMPLE_MAX); } } @@ -240,7 +242,7 @@ void SampleUtil::convertS16ToFloat32(CSAMPLE* pDest, const SAMPLE* pSrc, void SampleUtil::convertFloat32ToS16(SAMPLE* pDest, const CSAMPLE* pSrc, unsigned int iNumSamples) { for (unsigned int i = 0; i < iNumSamples; ++i) { - pDest[i] = SAMPLE(pSrc[i] * CSAMPLE(SHRT_MAX)); + pDest[i] = SAMPLE(pSrc[i] * CSAMPLE(SAMPLE_MAX)); } } @@ -314,14 +316,14 @@ void SampleUtil::deinterleaveBuffer(CSAMPLE* pDest1, CSAMPLE* pDest2, void SampleUtil::linearCrossfadeBuffers(CSAMPLE* pDest, const CSAMPLE* pSrcFadeOut, const CSAMPLE* pSrcFadeIn, unsigned int iNumSamples) { - CSAMPLE cross_mix = CSAMPLE_ZERO; - CSAMPLE cross_inc = (CSAMPLE_PEAK + CSAMPLE_PEAK) - / static_cast<double>(iNumSamples); + const CSAMPLE_GAIN cross_inc = CSAMPLE_GAIN_ONE + / CSAMPLE_GAIN(iNumSamples / 2); + CSAMPLE_GAIN cross_mix = CSAMPLE_GAIN_ZERO; for (unsigned int i = 0; i + 1 < iNumSamples; i += 2) { pDest[i] = pSrcFadeIn[i] * cross_mix - + pSrcFadeOut[i] * (CSAMPLE_PEAK - cross_mix); + + pSrcFadeOut[i] * (CSAMPLE_GAIN_ONE - cross_mix); pDest[i + 1] = pSrcFadeIn[i + 1] * cross_mix - + pSrcFadeOut[i + 1] * (CSAMPLE_PEAK - cross_mix); + + pSrcFadeOut[i + 1] * (CSAMPLE_GAIN_ONE - cross_mix); cross_mix += cross_inc; } } @@ -329,7 +331,8 @@ void SampleUtil::linearCrossfadeBuffers(CSAMPLE* pDest, // static void SampleUtil::mixStereoToMono(CSAMPLE* pDest, const CSAMPLE* pSrc, unsigned int iNumSamples) { - const CSAMPLE mixScale = CSAMPLE_PEAK / (CSAMPLE_PEAK + CSAMPLE_PEAK); + const CSAMPLE_GAIN mixScale = CSAMPLE_GAIN_ONE + / (CSAMPLE_GAIN_ONE + CSAMPLE_GAIN_ONE); for (unsigned int i = 0; i + 1 < iNumSamples; i += 2) { pDest[i] = (pSrc[i] + pSrc[i + 1]) * mixScale; pDest[i + 1] = pDest[i]; diff --git a/src/sampleutil.h b/src/sampleutil.h index 9f8fd60424..92ebb0a92a 100644 --- a/src/sampleutil.h +++ b/src/sampleutil.h @@ -5,7 +5,6 @@ #define SAMPLEUTIL_H #include "util/types.h" -#include "util/math.h" // NOTE(uklotzde): We assume that the STL algorithms are thoroughly optimized! #include <algorithm> @@ -61,7 +60,8 @@ public: // Sets every sample in pBuffer to value inline - static void fill(CSAMPLE* pBuffer, CSAMPLE value, unsigned int iNumSamples) { + static void fill(CSAMPLE* pBuffer, CSAMPLE value, + unsigned int iNumSamples) { std::fill(pBuffer, pBuffer + iNumSamples, value); } @@ -108,50 +108,52 @@ public: unsigned int numFrames, unsigned int numChannels); // Apply a different gain to every other sample. - static void applyAlternatingGain(CSAMPLE* pBuffer, CSAMPLE gain1, - CSAMPLE gain2, unsigned int iNumSamples); + static void applyAlternatingGain(CSAMPLE* pBuffer, CSAMPLE_GAIN gain1, + CSAMPLE_GAIN gain2, unsigned int iNumSamples); // Multiply every sample in pBuffer ramping from gain1 to gain2. // We use ramping as often as possible to prevent soundwave discontinuities // which can cause audible clicks and pops. - static void applyRampingGain(CSAMPLE* pBuffer, CSAMPLE old_gain, - CSAMPLE new_gain, unsigned int iNumSamples); + static void applyRampingGain(CSAMPLE* pBuffer, CSAMPLE_GAIN old_gain, + CSAMPLE_GAIN new_gain, unsigned int iNumSamples); // Add each sample of pSrc, multiplied by the gain, to pDest - static void addWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, CSAMPLE gain, - unsigned int iNumSamples); + static void addWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, + CSAMPLE_GAIN gain, unsigned int iNumSamples); // Add each sample of pSrc, multiplied by the gain, to pDest static void addWithRampingGain(CSAMPLE* pDest, const CSAMPLE* pSrc, - CSAMPLE old_gain, CSAMPLE new_gain, unsigned int iNumSamples); + CSAMPLE_GAIN old_gain, CSAMPLE_GAIN new_gain, + unsigned int iNumSamples); // Add to each sample of pDest, pSrc1 multiplied by gain1 plus pSrc2 // multiplied by gain2 static void add2WithGain(CSAMPLE* pDest, const CSAMPLE* pSrc1, - CSAMPLE gain1, const CSAMPLE* pSrc2, CSAMPLE gain2, + CSAMPLE_GAIN gain1, const CSAMPLE* pSrc2, CSAMPLE_GAIN gain2, unsigned int iNumSamples); // Add to each sample of pDest, pSrc1 multiplied by gain1 plus pSrc2 // multiplied by gain2 plus pSrc3 multiplied by gain3 static void add3WithGain(CSAMPLE* pDest, const CSAMPLE* pSrc1, - CSAMPLE gain1, const CSAMPLE* pSrc2, CSAMPLE gain2, - const CSAMPLE* pSrc3, CSAMPLE gain3, unsigned int iNumSamples); + CSAMPLE_GAIN gain1, const CSAMPLE* pSrc2, CSAMPLE_GAIN gain2, + const CSAMPLE* pSrc3, CSAMPLE_GAIN gain3, unsigned int iNumSamples); // Copy pSrc to pDest and multiply each sample by a factor of gain. - static void copyWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, CSAMPLE gain, - unsigned int iNumSamples); + static void copyWithGain(CSAMPLE* pDest, const CSAMPLE* pSrc, + CSAMPLE_GAIN gain, unsigned int iNumSamples); // Copy pSrc to pDest and ramp gain static void copyWithRampingGain(CSAMPLE* pDest, const CSAMPLE* pSrc, - CSAMPLE old_gain, CSAMPLE new_gain, unsigned int iNumSamples); + CSAMPLE_GAIN old_gain, CSAMPLE_GAIN new_gain, + unsigned int iNumSamples); - // Convert and normalize a buffer of SAMPLEs in the range [-SHRT_MAX, SHRT_MAX] + // Convert and normalize a buffer of SAMPLEs in the range [-SAMPLE_MAX, SAMPLE_MAX] // to a buffer of CSAMPLEs in the range [-1.0, 1.0]. static void convertS16ToFloat32(CSAMPLE* pDest, const SAMPLE* pSrc, unsigned int iNumSamples); // Convert and normalize a buffer of CSAMPLEs in the range [CSAMPLE_MIN, CSAMPLE_MAX] - // to a buffer of SAMPLEs in the range [-SHRT_MAX, SHRT_MAX]. + // to a buffer of SAMPLEs in the range [-SAMPLE_MAX, SAMPLE_MAX]. static void convertFloat32ToS16(SAMPLE* pDest, const CSAMPLE* pSrc, unsigned int iNumSamples); @@ -167,16 +169,16 @@ public: static bool isOutsideRange(CSAMPLE fMax, CSAMPLE fMin, const CSAMPLE* pBuffer, unsigned int iNumSamples); - // Copied every sample in pSrc to pDest, limiting the values in pDest to the - // range [fMin, fMax]. If pDest and pSrc are aliases, will not copy -- will - // only clamp. Returns true if any samples in pSrc were outside the range - // [fMin, fMax]. + // Copies every sample in pSrc to pDest, limiting the values in pDest + // to the valid range of CSAMPLE. If pDest and pSrc are aliases, will + // not copy will only clamp. Returns true if any samples in pSrc were + // outside the valid range of CSAMPLE. static void copyClampBuffer(CSAMPLE* pDest, const CSAMPLE* pSrc, unsigned int iNumSamples); - // returns a SAMPLE that is between CSAMPLE_MIN and CSAMPLE_MAX + // Limits a CSAMPLE value to the valid range [-CSAMPLE_PEAK, CSAMPLE_PEAK] inline static CSAMPLE clampSample(CSAMPLE in) { - return math_clamp(in, CSAMPLE_MIN, CSAMPLE_MAX); + return CSAMPLE_clamp(in); } // Interleave the samples in pSrc1 and pSrc2 into pDest. iNumSamples must be diff --git a/src/test/sampleutiltest.cpp b/src/test/sampleutiltest.cpp index 5addde2a72..c0b9b944a7 100644 --- a/src/test/sampleutiltest.cpp +++ b/src/test/sampleutiltest.cpp @@ -322,7 +322,7 @@ TEST_F(SampleUtilTest, convertS16ToFloat32) { SAMPLE* s16 = new SAMPLE[size]; FillBuffer(buffer, 1.0f, size); for (int j = 0; j < size; ++j) { - s16[j] = SHRT_MAX; + s16[j] = SAMPLE_MAX; } SampleUtil::convertS16ToFloat32(buffer, s16, size); for (int j = 0; j < size; ++j) { @@ -338,7 +338,7 @@ TEST_F(SampleUtilTest, convertS16ToFloat32) { } FillBuffer(buffer, -1.0f, size); for (int j = 0; j < size; ++j) { - s16[j] = -SHRT_MAX; + s16[j] = -SAMPLE_MAX; } SampleUtil::convertS16ToFloat32(buffer, s16, size); for (int j = 0; j < size; ++j) { diff --git a/src/util/types.h b/src/util/types.h index e3056887dd..f0f3c3d992 100644 --- a/src/util/types.h +++ b/src/util/types.h @@ -1,24 +1,59 @@ #ifndef TYPES_H #define TYPES_H -#include <climits> +#include "util/math.h" +#include <climits> -// 16-bit integer sample data +// 16-bit integer sample data within the asymmetric +// range [SHRT_MIN, SHRT_MAX]. typedef short int SAMPLE; const SAMPLE SAMPLE_ZERO = 0; -// asymmetric range const SAMPLE SAMPLE_MIN = SHRT_MIN; const SAMPLE SAMPLE_MAX = SHRT_MAX; +// Limits the range of a SAMPLE value to [SAMPLE_MIN, SAMPLE_MAX]. +inline +SAMPLE SAMPLE_clamp(SAMPLE in) { + return math_clamp(in, SAMPLE_MIN, SAMPLE_MAX); +} + +// Limits the range of a SAMPLE value to [-SAMPLE_MAX, SAMPLE_MAX]. +inline +SAMPLE SAMPLE_clampSymmetric(SAMPLE in) { + Q_ASSERT(SAMPLE_MIN <= SAMPLE_MAX); // just to be sure + return math_clamp(in, static_cast<SAMPLE>(-SAMPLE_MAX), SAMPLE_MAX); +} -// 32-bit floating-point sample data +// 32-bit single precision floating-point sample data +// normalized within the range [-1.0, 1.0] with a peak +// amplitude of 1.0. No min/max constants here to +// emphasize the symmetric value range of CSAMPLE +// data! typedef float CSAMPLE; const CSAMPLE CSAMPLE_ZERO = 0.0f; -const CSAMPLE CSAMPLE_PEAK = 1.0f; -// symmetric range -const CSAMPLE CSAMPLE_MAX = CSAMPLE_PEAK; -const CSAMPLE CSAMPLE_MIN = -CSAMPLE_PEAK; +const CSAMPLE CSAMPLE_ONE = 1.0f; +const CSAMPLE CSAMPLE_PEAK = CSAMPLE_ONE; + +// Limits the range of a CSAMPLE value to [-CSAMPLE_PEAK, CSAMPLE_PEAK]. +inline +CSAMPLE CSAMPLE_clamp(CSAMPLE in) { + return math_clamp(in, -CSAMPLE_PEAK, CSAMPLE_PEAK); +} + +// Gain values for weighted calculations of CSAMPLE +// data in the range [0.0, 1.0]. Same data type as +// CSAMPLE to avoid type conversions in calculations. +typedef CSAMPLE CSAMPLE_GAIN; +const float CSAMPLE_GAIN_ZERO = CSAMPLE_ZERO; +const float CSAMPLE_GAIN_ONE = CSAMPLE_ONE; +const float CSAMPLE_GAIN_MIN = CSAMPLE_GAIN_ZERO; +const float CSAMPLE_GAIN_MAX = CSAMPLE_GAIN_ONE; +// Limits the range of a CSAMPLE_GAIN value to [CSAMPLE_GAIN_MIN, CSAMPLE_GAIN_MAX]. +inline +CSAMPLE_GAIN CSAMPLE_GAIN_clamp(CSAMPLE_GAIN in) { + return math_clamp(in, CSAMPLE_GAIN_MIN, CSAMPLE_GAIN_MAX); +} #endif /* TYPES_H */ diff --git a/src/vinylcontrol/vinylcontrolxwax.cpp b/src/vinylcontrol/vinylcontrolxwax.cpp index 0e12c22247..8741714f9b 100644 --- a/src/vinylcontrol/vinylcontrolxwax.cpp +++ b/src/vinylcontrol/vinylcontrolxwax.cpp @@ -40,7 +40,7 @@ ********************/ // Sample threshold below which we consider there to be no signal. -const double kMinSignal = 75.0 / SHRT_MAX; +const double kMinSignal = 75.0 / SAMPLE_MAX; bool VinylControlXwax::s_bLUTInitialized = false; QMutex VinylControlXwax::s_xwaxLUTMutex; @@ -217,12 +217,12 @@ void VinylControlXwax::analyzeSamples(CSAMPLE* pSamples, size_t nFrames) { // Convert CSAMPLE samples to shorts, preventing overflow. for (int i = 0; i < static_cast<int>(samplesSize); ++i) { - CSAMPLE sample = pSamples[i] * gain * SHRT_MAX; + CSAMPLE sample = pSamples[i] * gain * SAMPLE_MAX; - if (sample > SHRT_MAX) { - m_pWorkBuffer[i] = SHRT_MAX; - } else if (sample < SHRT_MIN) { - m_pWorkBuffer[i] = SHRT_MIN; + if (sample > SAMPLE_MAX) { + m_pWorkBuffer[i] = SAMPLE_MAX; + } else if (sample < SAMPLE_MIN) { + m_pWorkBuffer[i] = SAMPLE_MIN; } else { m_pWorkBuffer[i] = static_cast<short>(sample); } |