1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

AudioFormatReader: Update searchForLevel to work for more than two channels

This commit is contained in:
reuk 2024-11-25 17:06:56 +00:00
parent 3186522b0b
commit 89a2407deb
No known key found for this signature in database

View file

@ -299,11 +299,12 @@ int64 AudioFormatReader::searchForLevel (int64 startSample,
return -1; return -1;
const int bufferSize = 4096; const int bufferSize = 4096;
HeapBlock<int> tempSpace (bufferSize * 2 + 64); const size_t channels = numChannels;
HeapBlock<int> tempSpace (bufferSize * channels + 64);
std::vector<int*> channelPointers (channels);
int* tempBuffer[3] = { tempSpace.get(), for (auto [index, ptr] : enumerate (channelPointers, size_t{}))
tempSpace.get() + bufferSize, ptr = tempSpace + (bufferSize * index);
nullptr };
int consecutive = 0; int consecutive = 0;
int64 firstMatchPos = -1; int64 firstMatchPos = -1;
@ -326,7 +327,7 @@ int64 AudioFormatReader::searchForLevel (int64 startSample,
if (bufferStart >= lengthInSamples) if (bufferStart >= lengthInSamples)
break; break;
read (tempBuffer, 2, bufferStart, numThisTime, false); read (channelPointers.data(), (int) channels, bufferStart, numThisTime, false);
auto num = numThisTime; auto num = numThisTime;
while (--num >= 0) while (--num >= 0)
@ -334,43 +335,25 @@ int64 AudioFormatReader::searchForLevel (int64 startSample,
if (numSamplesToSearch < 0) if (numSamplesToSearch < 0)
--startSample; --startSample;
bool matches = false;
auto index = (int) (startSample - bufferStart); auto index = (int) (startSample - bufferStart);
const auto matches = std::invoke ([&]
{
if (usesFloatingPointData) if (usesFloatingPointData)
{ {
const float sample1 = std::abs (((float*) tempBuffer[0]) [index]); return std::any_of (channelPointers.begin(), channelPointers.end(), [&] (const auto& ptr)
{
const float sample = std::abs (((float*) ptr) [index]);
return magnitudeRangeMinimum <= sample && sample <= magnitudeRangeMaximum;
});
}
if (sample1 >= magnitudeRangeMinimum return std::any_of (channelPointers.begin(), channelPointers.end(), [&] (const auto& ptr)
&& sample1 <= magnitudeRangeMaximum)
{ {
matches = true; const int sample = std::abs (ptr[index]);
} return intMagnitudeRangeMinimum <= sample && sample <= intMagnitudeRangeMaximum;
else if (numChannels > 1) });
{ });
const float sample2 = std::abs (((float*) tempBuffer[1]) [index]);
matches = (sample2 >= magnitudeRangeMinimum
&& sample2 <= magnitudeRangeMaximum);
}
}
else
{
const int sample1 = std::abs (tempBuffer[0] [index]);
if (sample1 >= intMagnitudeRangeMinimum
&& sample1 <= intMagnitudeRangeMaximum)
{
matches = true;
}
else if (numChannels > 1)
{
const int sample2 = std::abs (tempBuffer[1][index]);
matches = (sample2 >= intMagnitudeRangeMinimum
&& sample2 <= intMagnitudeRangeMaximum);
}
}
if (matches) if (matches)
{ {