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