reVeno/Source/Veno/Audio/VenoBuffer.cpp

85 lines
1.6 KiB
C++
Raw Normal View History

2020-06-13 10:56:20 +02:00
//
// Created by versustune on 12.06.20.
//
#include <cmath>
#include "VenoBuffer.h"
#include "../Utils.h"
2020-06-13 10:56:20 +02:00
VenoBuffer::VenoBuffer ()
{
2020-06-13 10:56:20 +02:00
}
VenoBuffer::~VenoBuffer ()
{
buffer.clear ();
right.clear ();
left.clear ();
2020-06-13 10:56:20 +02:00
}
void VenoBuffer::reset (int size)
{
if (size != buffer.size ())
{
buffer.resize (size);
right.resize (size);
left.resize (size);
2020-06-13 10:56:20 +02:00
}
// reset to 0 dc :D
for (int i = 0; i < size; ++i)
{
2020-06-13 10:56:20 +02:00
buffer[i] = 0;
left[i] = 0;
right[i] = 0;
}
2020-07-09 16:31:33 +02:00
leftPeak = -30;
rightPeak = -30;
monoPeak = -30;
2020-06-13 10:56:20 +02:00
}
void VenoBuffer::addMonoSample (float value, int index)
{
2020-06-13 10:56:20 +02:00
buffer[index] = value;
}
void VenoBuffer::addLeftSample (float value, int index)
{
2020-06-13 10:56:20 +02:00
left[index] = value;
}
void VenoBuffer::addRightSample (float value, int index)
{
2020-06-13 10:56:20 +02:00
right[index] = value;
}
void VenoBuffer::calcPeak ()
{
float leftRMS = 0;
float rightRMS = 0;
auto size = buffer.size();
for (int i = 0; i < size; ++i)
{
leftRMS += left[i] * left[i];
rightRMS += right[i] * right[i];
}
rightPeak = VeNo::Utils::clamp (Decibels::gainToDecibels (std::sqrt (rightRMS / size), -30.0f), -30.0f, 0.0f);
leftPeak = VeNo::Utils::clamp (Decibels::gainToDecibels (std::sqrt (leftRMS / size), -30.0f), -30.0f, 0.0f);
monoPeak = leftPeak;
2020-06-13 10:56:20 +02:00
}
const std::vector<float>& VenoBuffer::getBuffer () const
{
2020-06-13 10:56:20 +02:00
return buffer;
}
const std::vector<float>& VenoBuffer::getRight () const
{
2020-06-13 10:56:20 +02:00
return right;
}
const std::vector<float>& VenoBuffer::getLeft () const
{
2020-06-13 10:56:20 +02:00
return left;
}