33namespace LUFS
44{
55
6- FixedTermLoudnessMeter::FixedTermLoudnessMeter (const std::vector<Channel>& channels , const std::chrono::milliseconds& windowLength) : blockLengthSamples{getBlockLengthSamples (windowLength)}
6+ FixedTermLoudnessMeter::FixedTermLoudnessMeter (const std::vector<Channel>& channelSet , const std::chrono::milliseconds& windowLength) : channels{channelSet}, blockLengthSamples{getBlockLengthSamples (windowLength)}, channelProcessors{ generateChannelProcessors ( )}
77{
8- std::transform (channels.begin (), channels.end (), std::back_insert_iterator (channelProcessors), [this ](const Channel& channel)
9- {
10- return ChannelProcessor (channel.getWeighting (), blockLengthSamples);
11- });
8+
129}
1310
1411FixedTermLoudnessMeter::~FixedTermLoudnessMeter ()
@@ -18,59 +15,66 @@ FixedTermLoudnessMeter::~FixedTermLoudnessMeter()
1815
1916void FixedTermLoudnessMeter::process (std::span<const float * const > audio, int numSamplesPerChannel)
2017{
21- assert (audio.size () == channelProcessors.size ());
18+ std::vector<ChannelProcessor>& processors = channelProcessors.realtimeAquire ();
19+
20+ assert (audio.size () == processors.size ());
2221
2322 for (size_t sampleIndex = 0 ; sampleIndex < numSamplesPerChannel; ++sampleIndex)
2423 {
25- for (int channelIndex = 0 ; channelIndex < channelProcessors .size (); ++channelIndex)
24+ for (int channelIndex = 0 ; channelIndex < processors .size (); ++channelIndex)
2625 {
27- channelProcessors [channelIndex].currentBlockData [blockWritePos] = channelProcessors [channelIndex].filterSample (audio[channelIndex][sampleIndex]);
26+ processors [channelIndex].currentBlockData [blockWritePos] = processors [channelIndex].filterSample (audio[channelIndex][sampleIndex]);
2827 }
2928
3029 if (++blockWritePos >= blockLengthSamples)
3130 {
3231 blockWritePos = 0 ;
3332 }
3433 }
34+
35+ channelProcessors.realtimeRelease ();
3536}
3637
3738void FixedTermLoudnessMeter::process (std::span<const float > audio)
3839{
39- assert (audio.size () % channelProcessors.size () == 0 );
40+ std::vector<ChannelProcessor>& processors = channelProcessors.realtimeAquire ();
41+
42+ assert (audio.size () % processors.size () == 0 );
4043
41- const size_t numSamplesPerChannel = audio.size () / channelProcessors .size ();
44+ const size_t numSamplesPerChannel = audio.size () / processors .size ();
4245
4346 const float * inputData = audio.data ();
4447
4548 for (size_t sampleIndex = 0 ; sampleIndex < numSamplesPerChannel; ++sampleIndex)
4649 {
47- for (int channelIndex = 0 ; channelIndex < channelProcessors .size (); ++channelIndex)
50+ for (int channelIndex = 0 ; channelIndex < processors .size (); ++channelIndex)
4851 {
49- channelProcessors [channelIndex].currentBlockData [blockWritePos] = channelProcessors [channelIndex].filterSample (*inputData++);
52+ processors [channelIndex].currentBlockData [blockWritePos] = processors [channelIndex].filterSample (*inputData++);
5053 }
5154
5255 if (++blockWritePos >= blockLengthSamples)
5356 {
5457 blockWritePos = 0 ;
5558 }
5659 }
60+
61+ channelProcessors.realtimeRelease ();
5762}
5863
5964void FixedTermLoudnessMeter::reset ()
6065{
61- std::for_each (channelProcessors.begin (), channelProcessors.end (), [](ChannelProcessor& processor)
62- {
63- processor.reset ();
64- });
66+ channelProcessors.reset (generateChannelProcessors ());
6567
6668 blockWritePos = 0 ;
6769}
6870
6971float FixedTermLoudnessMeter::getLoudness () const
7072{
73+ const std::vector<ChannelProcessor>& processors = channelProcessors.nonRealtimeRead ();
74+
7175 float accumulatedChannels = 0 .0f ;
7276
73- std::for_each (channelProcessors .begin (), channelProcessors .end (), [&accumulatedChannels](const ChannelProcessor& processor)
77+ std::for_each (processors .begin (), processors .end (), [&accumulatedChannels](const ChannelProcessor& processor)
7478 {
7579 accumulatedChannels += processor.getCurrentBlockMeanSquares () * processor.getWeighting ();
7680 });
@@ -83,4 +87,16 @@ int FixedTermLoudnessMeter::getBlockLengthSamples(const std::chrono::millisecond
8387 return (windowLength.count () / 1000 .0f ) * sampleRate;
8488}
8589
90+ std::vector<ChannelProcessor> FixedTermLoudnessMeter::generateChannelProcessors () const
91+ {
92+ std::vector<ChannelProcessor> tempChannelProcessors;
93+
94+ std::transform (channels.begin (), channels.end (), std::back_insert_iterator (tempChannelProcessors), [this ](const Channel& channel)
95+ {
96+ return ChannelProcessor (channel.getWeighting (), blockLengthSamples);
97+ });
98+
99+ return tempChannelProcessors;
100+ }
101+
86102}
0 commit comments