summaryrefslogtreecommitdiff
path: root/Source/Core/AudioCommon/DPL2Decoder.cpp
diff options
context:
space:
mode:
authorMerryMage <MerryMage@users.noreply.github.com>2017-04-22 10:01:26 +0100
committerMerryMage <MerryMage@users.noreply.github.com>2017-04-23 09:21:29 +0100
commit42a1f7939d00e38e8a9c29f593b792d06126f41e (patch)
tree7ae3da16d1cffea39069a216c2604f5c8ba5fbcc /Source/Core/AudioCommon/DPL2Decoder.cpp
parent71dc81041865e9849ac491ab577a01b5bc82f3d4 (diff)
DPL2Decoder: Prefer std::vector to calloc-ed array
Diffstat (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp')
-rw-r--r--Source/Core/AudioCommon/DPL2Decoder.cpp59
1 files changed, 27 insertions, 32 deletions
diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp
index 9ac95a26d1..6ec8d55144 100644
--- a/Source/Core/AudioCommon/DPL2Decoder.cpp
+++ b/Source/Core/AudioCommon/DPL2Decoder.cpp
@@ -10,8 +10,8 @@
#include <algorithm>
#include <cmath>
#include <cstdlib>
+#include <cstring>
#include <functional>
-#include <string.h>
#include <vector>
#include "AudioCommon/DPL2Decoder.h"
@@ -35,11 +35,11 @@ static float adapt_l_gain, adapt_r_gain, adapt_lpr_gain, adapt_lmr_gain;
static std::vector<float> lf, rf, lr, rr, cf, cr;
static float LFE_buf[256];
static unsigned int lfe_pos;
-static float* filter_coefs_lfe;
+static std::vector<float> filter_coefs_lfe;
static unsigned int len125;
-template <class T, class _ftype_t>
-static _ftype_t DotProduct(int count, const T* buf, const _ftype_t* coefficients)
+template <class T>
+static float DotProduct(int count, const T* buf, const std::vector<float>& coeffs, int offset)
{
int i;
float sum0 = 0.0f, sum1 = 0.0f, sum2 = 0.0f, sum3 = 0.0f;
@@ -47,21 +47,21 @@ static _ftype_t DotProduct(int count, const T* buf, const _ftype_t* coefficients
// Unrolled loop
for (i = 0; (i + 3) < count; i += 4)
{
- sum0 += buf[i + 0] * coefficients[i + 0];
- sum1 += buf[i + 1] * coefficients[i + 1];
- sum2 += buf[i + 2] * coefficients[i + 2];
- sum3 += buf[i + 3] * coefficients[i + 3];
+ sum0 += buf[i + 0] * coeffs[offset + i + 0];
+ sum1 += buf[i + 1] * coeffs[offset + i + 1];
+ sum2 += buf[i + 2] * coeffs[offset + i + 2];
+ sum3 += buf[i + 3] * coeffs[offset + i + 3];
}
// Epilogue of unrolled loop
for (; i < count; i++)
- sum0 += buf[i] * coefficients[i];
+ sum0 += buf[i] * coeffs[offset + i];
return sum0 + sum1 + sum2 + sum3;
}
template <class T>
-static T FIRFilter(const T* buf, int pos, int len, int count, const float* coefficients)
+static T FIRFilter(const T* buf, int pos, int len, int count, const std::vector<float>& coeffs)
{
int count1, count2;
@@ -81,9 +81,8 @@ static T FIRFilter(const T* buf, int pos, int len, int count, const float* coeff
// high part of window
const T* ptr = &buf[pos];
- float r1 = DotProduct(count1, ptr, coefficients);
- coefficients += count1;
- float r2 = DotProduct(count2, buf, coefficients);
+ float r1 = DotProduct(count1, ptr, coeffs, 0);
+ float r2 = DotProduct(count2, buf, coeffs, count1);
return T(r1 + r2);
}
@@ -94,15 +93,19 @@ static T FIRFilter(const T* buf, int pos, int len, int count, const float* coeff
// N-1
//
// n window length
-// w buffer for the window parameters
+// returns buffer with the window parameters
*/
-static void Hamming(int n, float* w)
+static std::vector<float> Hamming(int n)
{
+ std::vector<float> w(n);
+
float k = float(2 * M_PI / ((float)(n - 1))); // 2*pi/(N-1)
// Calculate window coefficients
for (int i = 0; i < n; i++)
- *w++ = float(0.54 - 0.46 * cos(k * (float)i));
+ w[i] = float(0.54 - 0.46 * cos(k * (float)i));
+
+ return w;
}
// FIR filter design
@@ -110,7 +113,6 @@ static void Hamming(int n, float* w)
/* Design FIR filter using the Window method
n filter length must be odd for HP and BS filters
-w buffer for the filter taps (must be n long)
fc cutoff frequencies (1 for LP and HP, 2 for BP and BS)
0 < fc < 1 where 1 <=> Fs/2
flags window and filter type as defined in filter.h
@@ -118,9 +120,9 @@ variables are ored together: i.e. LP|HAMMING will give a
low pass filter designed using a hamming window
opt beta constant used only when designing using kaiser windows
-returns 0 if OK, -1 if fail
+returns buffer for the filter taps (will be n long)
*/
-static float* DesignFIR(unsigned int n, float fc, float opt)
+static std::vector<float> DesignFIR(unsigned int n, float fc, float opt)
{
const unsigned int o = n & 1; // Indicator for odd filter length
const unsigned int end = ((n + 1) >> 1) - o; // Loop end
@@ -134,12 +136,10 @@ static float* DesignFIR(unsigned int n, float fc, float opt)
// Sanity check
if (n == 0)
- return nullptr;
-
- float* w = (float*)calloc(sizeof(float), n);
+ return {};
// Get window coefficients
- Hamming(n, w);
+ std::vector<float> w = Hamming(n);
// Low pass filter
@@ -190,19 +190,14 @@ static void Done()
{
OnSeek();
- if (filter_coefs_lfe)
- {
- free(filter_coefs_lfe);
- }
-
- filter_coefs_lfe = nullptr;
+ filter_coefs_lfe.clear();
}
-static float* CalculateCoefficients125HzLowpass(int rate)
+static std::vector<float> CalculateCoefficients125HzLowpass(int rate)
{
len125 = 256;
float f = 125.0f / (rate / 2);
- float* coeffs = DesignFIR(len125, f, 0);
+ std::vector<float> coeffs = DesignFIR(len125, f, 0);
static const float M3_01DB = 0.7071067812f;
for (unsigned int i = 0; i < len125; i++)
{
@@ -380,5 +375,5 @@ void DPL2Reset()
{
olddelay = -1;
oldfreq = 0;
- filter_coefs_lfe = nullptr;
+ filter_coefs_lfe.clear();
}