2014-07-29 00:26:26 +02:00
|
|
|
#include "loudness.hpp"
|
|
|
|
|
2017-06-13 18:55:22 +09:00
|
|
|
#include <algorithm>
|
2022-10-08 12:36:45 +02:00
|
|
|
#include <cmath>
|
2022-01-22 22:44:02 +01:00
|
|
|
#include <cstdint>
|
2015-05-23 23:10:53 +02:00
|
|
|
#include <limits>
|
2015-05-12 19:02:56 +02:00
|
|
|
|
2014-07-29 00:26:26 +02:00
|
|
|
namespace MWSound
|
|
|
|
{
|
|
|
|
|
2016-06-27 20:54:42 +02:00
|
|
|
void Sound_Loudness::analyzeLoudness(const std::vector<char>& data)
|
2015-11-23 02:08:27 -08:00
|
|
|
{
|
2016-06-27 20:54:42 +02:00
|
|
|
mQueue.insert(mQueue.end(), data.begin(), data.end());
|
|
|
|
if (!mQueue.size())
|
|
|
|
return;
|
2014-07-29 00:26:26 +02:00
|
|
|
|
2016-06-27 20:54:42 +02:00
|
|
|
int samplesPerSegment = static_cast<int>(mSampleRate / mSamplesPerSec);
|
2024-03-14 23:39:19 +00:00
|
|
|
std::size_t numSamples = bytesToFrames(mQueue.size(), mChannelConfig, mSampleType);
|
|
|
|
std::size_t advance = framesToBytes(1, mChannelConfig, mSampleType);
|
2014-07-29 00:26:26 +02:00
|
|
|
|
2024-03-15 00:47:01 +00:00
|
|
|
std::size_t segment = 0;
|
|
|
|
std::size_t sample = 0;
|
2015-11-23 02:08:27 -08:00
|
|
|
while (segment < numSamples / samplesPerSegment)
|
2014-07-29 00:26:26 +02:00
|
|
|
{
|
2015-11-23 02:08:27 -08:00
|
|
|
float sum = 0;
|
2016-06-27 20:54:42 +02:00
|
|
|
int samplesAdded = 0;
|
|
|
|
while (sample < numSamples && sample < (segment + 1) * samplesPerSegment)
|
2015-11-23 02:08:27 -08:00
|
|
|
{
|
|
|
|
// get sample on a scale from -1 to 1
|
|
|
|
float value = 0;
|
2016-06-27 20:54:42 +02:00
|
|
|
if (mSampleType == SampleType_UInt8)
|
|
|
|
value = ((char)(mQueue[sample * advance] ^ 0x80)) / 128.f;
|
|
|
|
else if (mSampleType == SampleType_Int16)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2016-06-27 20:54:42 +02:00
|
|
|
value = *reinterpret_cast<const int16_t*>(&mQueue[sample * advance]);
|
2015-11-23 02:08:27 -08:00
|
|
|
value /= float(std::numeric_limits<int16_t>::max());
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2016-06-27 20:54:42 +02:00
|
|
|
else if (mSampleType == SampleType_Float32)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2016-06-27 20:54:42 +02:00
|
|
|
value = *reinterpret_cast<const float*>(&mQueue[sample * advance]);
|
2021-11-06 07:30:28 +03:00
|
|
|
value = std::clamp(value, -1.f, 1.f); // Float samples *should* be scaled to [-1,1] already.
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
|
|
|
|
2015-11-23 02:08:27 -08:00
|
|
|
sum += value * value;
|
|
|
|
++samplesAdded;
|
|
|
|
++sample;
|
2014-07-29 00:26:26 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 02:08:27 -08:00
|
|
|
float rms = 0; // root mean square
|
|
|
|
if (samplesAdded > 0)
|
|
|
|
rms = std::sqrt(sum / samplesAdded);
|
|
|
|
mSamples.push_back(rms);
|
|
|
|
++segment;
|
2014-07-29 00:26:26 +02:00
|
|
|
}
|
2015-11-23 02:08:27 -08:00
|
|
|
|
|
|
|
mQueue.erase(mQueue.begin(), mQueue.begin() + sample * advance);
|
2014-07-29 00:26:26 +02:00
|
|
|
}
|
2015-11-27 09:47:14 -08:00
|
|
|
|
2015-11-23 02:08:27 -08:00
|
|
|
float Sound_Loudness::getLoudnessAtTime(float sec) const
|
|
|
|
{
|
|
|
|
if (mSamplesPerSec <= 0.0f || mSamples.empty() || sec < 0.0f)
|
|
|
|
return 0.0f;
|
|
|
|
|
2024-03-15 13:42:28 +00:00
|
|
|
size_t index = std::min(static_cast<size_t>(sec * mSamplesPerSec), mSamples.size() - 1);
|
2015-11-23 02:08:27 -08:00
|
|
|
return mSamples[index];
|
|
|
|
}
|
2014-07-29 00:26:26 +02:00
|
|
|
|
|
|
|
}
|