You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BALibrary/src/effects/AudioEffectRmsMeasure.cpp

116 lines
3.4 KiB

/*
* AudioEffectRmsMeasure.cpp
*
* Created on: March 7, 2020
* Author: slascos
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arm_math.h>
#include "BALibrary.h"
#include "AudioEffectRmsMeasure.h"
using namespace BALibrary;
namespace BAEffects {
AudioEffectRmsMeasure::AudioEffectRmsMeasure(unsigned numBlockMeasurements)
: AudioStream(1, m_inputQueueArray),
m_numBlockMeasurements(numBlockMeasurements),
m_accumulatorCount(0),
m_sum(0),
m_rms(0.0f),
m_dbfs(0.0f)
{
}
AudioEffectRmsMeasure::~AudioEffectRmsMeasure()
{
}
void AudioEffectRmsMeasure::update(void)
{
audio_block_t *inputAudioBlock = receiveReadOnly(); // get the next block of input samples
// Check is block is disabled
if (m_enable == false) {
// do not transmit or process any audio, return as quickly as possible.
if (inputAudioBlock) release(inputAudioBlock);
return;
}
// Check is block is bypassed, if so either transmit input directly or create silence
if (m_bypass == true) {
// transmit the input directly
if (!inputAudioBlock) {
// create silence
inputAudioBlock = allocate();
if (!inputAudioBlock) { return; } // failed to allocate
else {
clearAudioBlock(inputAudioBlock);
}
}
transmit(inputAudioBlock, 0);
release(inputAudioBlock);
return;
}
// Calculate the RMS noise over the specified number of audio blocks.
// Once the necessary blocks have been accumulated, calculate and print the
// RMS noise figure.
// RMS noise = sqrt((1/N) * (x1*x1 + x2*x2 + ...) )
// First create the square sum of the input audio block and add it to the accumulator
int64_t dotProduct = 0;
//arm_dot_prod_q15(inputAudioBlock->data, inputAudioBlock->data, AUDIO_BLOCK_SAMPLES, &dotProduct);
for (int i=0; i<AUDIO_BLOCK_SAMPLES; i++) {
dotProduct += ((int64_t)inputAudioBlock->data[i] * (int64_t)inputAudioBlock->data[i]);
if (dotProduct < 0) {
if (Serial) { Serial.println("DOT ERROR!"); Serial.println(inputAudioBlock->data[i], HEX); }
}
}
m_sum += (int64_t)(dotProduct);
m_accumulatorCount++;
// Check if we have enough samples accumulated.
if (m_accumulatorCount == m_numBlockMeasurements) {
// Calculate and print the RMS figure
float div = (float)m_sum / (float)(m_accumulatorCount * AUDIO_BLOCK_SAMPLES);
arm_sqrt_f32(div, &m_rms);
// dbfs = 20*log10(abs(rmsFigure)/32768.0f);
m_dbfs = 20.0f * log10(m_rms/32768.0f);
if (Serial) {
Serial.print("Accumulator: "); Serial.println((int)(m_sum >> 32), HEX);
Serial.print("RAW RMS: "); Serial.println(m_rms);
Serial.print("AudioEffectRmsMeasure: the RMS figure is "); Serial.print(m_dbfs);
Serial.print(" dBFS over "); Serial.print(m_accumulatorCount); Serial.println(" audio blocks");
}
m_sum = 0;
m_accumulatorCount = 0;
}
transmit(inputAudioBlock);
release(inputAudioBlock);
}
}