From a802f5d08138556354525d009d85ab482509ff73 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 18 Feb 2013 16:47:15 +0000 Subject: [PATCH] Added method AudioData::Pointer::findMinAndMax --- .../buffers/juce_AudioDataConverters.h | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/modules/juce_audio_basics/buffers/juce_AudioDataConverters.h b/modules/juce_audio_basics/buffers/juce_AudioDataConverters.h index 06c8af08bf..16db9136bc 100644 --- a/modules/juce_audio_basics/buffers/juce_AudioDataConverters.h +++ b/modules/juce_audio_basics/buffers/juce_AudioDataConverters.h @@ -422,8 +422,7 @@ public: { static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead! - Pointer dest (*this); - while (--numSamples >= 0) + for (Pointer dest (*this); --numSamples >= 0;) { dest.data.copyFromSameType (source.data); dest.advance(); @@ -467,6 +466,55 @@ public: dest.clear (dest.data, numSamples); } + /** Scans a block of data, returning the lowest and highest levels as floats */ + void findMinAndMax (size_t numSamples, float& minValue, float& maxValue) const noexcept + { + if (numSamples == 0) + { + minValue = maxValue = 0; + return; + } + + Pointer dest (*this); + + if (isFloatingPoint()) + { + float mn = dest.getAsFloat(); + dest.advance(); + float mx = mn; + + while (--numSamples > 0) + { + const float v = dest.getAsFloat(); + dest.advance(); + + if (mx < v) mx = v; + if (v < mn) mn = v; + } + + minValue = mn; + maxValue = mx; + } + else + { + int32 mn = dest.getAsInt32(); + dest.advance(); + int32 mx = mn; + + while (--numSamples > 0) + { + const int v = dest.getAsInt32(); + dest.advance(); + + if (mx < v) mx = v; + if (v < mn) mn = v; + } + + minValue = mn * (float) (1.0 / (1.0 + Int32::maxValue)); + maxValue = mx * (float) (1.0 / (1.0 + Int32::maxValue)); + } + } + /** Returns true if the pointer is using a floating-point format. */ static bool isFloatingPoint() noexcept { return (bool) SampleFormat::isFloat; }