From 34692ab826abc8f8faa61bdb2280b742424528f1 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Sat, 7 Dec 2013 15:14:29 -0500 Subject: Remove unnecessary Src/ folders --- Source/Core/AudioCommon/DPL2Decoder.cpp | 391 ++++++++++++++++++++++++++++++++ 1 file changed, 391 insertions(+) create mode 100644 Source/Core/AudioCommon/DPL2Decoder.cpp (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp new file mode 100644 index 0000000000..97bdb5413d --- /dev/null +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -0,0 +1,391 @@ +// Copyright 2013 Dolphin Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +// Dolby Pro Logic 2 decoder from ffdshow-tryout +// * Copyright 2001 Anders Johansson ajh@atri.curtin.edu.au +// * Copyright (c) 2004-2006 Milan Cutka +// * based on mplayer HRTF plugin by ylai + +#include +#include +#include +#include +#include +#include "DPL2Decoder.h" + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif +#ifndef M_SQRT1_2 +#define M_SQRT1_2 0.70710678118654752440 +#endif + +int olddelay = -1; +unsigned int oldfreq = 0; +unsigned int dlbuflen; +int cyc_pos; +float l_fwr, r_fwr, lpr_fwr, lmr_fwr; +std::vector fwrbuf_l, fwrbuf_r; +float adapt_l_gain, adapt_r_gain, adapt_lpr_gain, adapt_lmr_gain; +std::vector lf, rf, lr, rr, cf, cr; +float LFE_buf[256]; +unsigned int lfe_pos; +float *filter_coefs_lfe; +unsigned int len125; + +template static _ftype_t dotproduct(int count,const T *buf,const _ftype_t *coefficients) +{ + float sum0=0,sum1=0,sum2=0,sum3=0; + for (;count>=4;buf+=4,coefficients+=4,count-=4) + { + sum0+=buf[0]*coefficients[0]; + sum1+=buf[1]*coefficients[1]; + sum2+=buf[2]*coefficients[2]; + sum3+=buf[3]*coefficients[3]; + } + while (count--) sum0+= *buf++ * *coefficients++; + return sum0+sum1+sum2+sum3; +} + +template static T firfilter(const T *buf, int pos, int len, int count, const float *coefficients) +{ + int count1, count2; + + if (pos >= count) + { + pos -= count; + count1 = count; count2 = 0; + } + else + { + count2 = pos; + count1 = count - pos; + pos = len - count1; + } + + // high part of window + const T *ptr = &buf[pos]; + + float r1=dotproduct(count1,ptr,coefficients);coefficients+=count1; + float r2=dotproduct(count2,buf,coefficients); + return T(r1+r2); +} + +template inline const T& limit(const T& val, const T& min, const T& max) +{ + if (val < min) { + return min; + } else if (val > max) { + return max; + } else { + return val; + } +} + +/* +// Hamming +// 2*pi*k +// w(k) = 0.54 - 0.46*cos(------), where 0 <= k < N +// N-1 +// +// n window length +// w buffer for the window parameters +*/ +void hamming(int n, float* w) +{ + int i; + float k = float(2*M_PI/((float)(n-1))); // 2*pi/(N-1) + + // Calculate window coefficients + for (i=0; i Fs/2 +flags window and filter type as defined in filter.h +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 +*/ +float* design_fir(unsigned int *n, float* fc, float opt) +{ + unsigned int o = *n & 1; // Indicator for odd filter length + unsigned int end = ((*n + 1) >> 1) - o; // Loop end + unsigned int i; // Loop index + + float k1 = 2 * float(M_PI); // 2*pi*fc1 + float k2 = 0.5f * (float)(1 - o); // Constant used if the filter has even length + float g = 0.0f; // Gain + float t1; // Temporary variables + float fc1; // Cutoff frequencies + + // Sanity check + if(*n==0) return NULL; + fc[0]=limit(fc[0],float(0.001),float(1)); + + float *w=(float*)calloc(sizeof(float),*n); + + // Get window coefficients + hamming(*n,w); + + fc1=*fc; + // Cutoff frequency must be < 0.5 where 0.5 <=> Fs/2 + fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25f; + k1 *= fc1; + + // Low pass filter + + // If the filter length is odd, there is one point which is exactly + // in the middle. The value at this point is 2*fCutoff*sin(x)/x, + // where x is zero. To make sure nothing strange happens, we set this + // value separately. + if (o) + { + w[end] = fc1 * w[end] * 2.0f; + g=w[end]; + } + + // Create filter + for (i=0 ; i M9_03DB * _lpr_fwr ? _lmr_fwr : M9_03DB * _lpr_fwr; + float lpr_gain = (_lpr_fwr + lmr_lim_fwr) / (1 + _lpr_fwr + _lpr_fwr); + float lmr_gain = (_lpr_fwr + lmr_lim_fwr) / (1 + lmr_lim_fwr + lmr_lim_fwr); + float lmr_unlim_gain = (_lpr_fwr + _lmr_fwr) / (1 + _lmr_fwr + _lmr_fwr); + float lpr, lmr; + float l_agc, r_agc, lpr_agc, lmr_agc; + float f, d_gain, c_gain, c_agc_cfk; + + /*** AXIS NO. 1: (Lt, Rt) -> (C, Ls, Rs) ***/ + /* AGC adaption */ + d_gain = (fabs(l_gain - *_adapt_l_gain) + fabs(r_gain - *_adapt_r_gain)) * 0.5f; + f = d_gain * (1.0f / MATAGCTRIG); + f = MATAGCDECAY - MATAGCDECAY / (1 + f * f); + *_adapt_l_gain = (1 - f) * *_adapt_l_gain + f * l_gain; + *_adapt_r_gain = (1 - f) * *_adapt_r_gain + f * r_gain; + /* Matrix */ + l_agc = in[il] * passive_lock(*_adapt_l_gain); + r_agc = in[ir] * passive_lock(*_adapt_r_gain); + _cf[k] = (l_agc + r_agc) * (float)M_SQRT1_2; + if (decode_rear) + { + _lr[kr] = _rr[kr] = (l_agc - r_agc) * (float)M_SQRT1_2; + // Stereo rear channel is steered with the same AGC steering as + // the decoding matrix. Note this requires a fast updating AGC + // at the order of 20 ms (which is the case here). + _lr[kr] *= (_l_fwr + _l_fwr) / (1 + _l_fwr + _r_fwr); + _rr[kr] *= (_r_fwr + _r_fwr) / (1 + _l_fwr + _r_fwr); + } + + /*** AXIS NO. 2: (Lt + Rt, Lt - Rt) -> (L, R) ***/ + lpr = (in[il] + in[ir]) * (float)M_SQRT1_2; + lmr = (in[il] - in[ir]) * (float)M_SQRT1_2; + /* AGC adaption */ + d_gain = fabs(lmr_unlim_gain - *_adapt_lmr_gain); + f = d_gain * (1.0f / MATAGCTRIG); + f = MATAGCDECAY - MATAGCDECAY / (1 + f * f); + *_adapt_lpr_gain = (1 - f) * *_adapt_lpr_gain + f * lpr_gain; + *_adapt_lmr_gain = (1 - f) * *_adapt_lmr_gain + f * lmr_gain; + /* Matrix */ + lpr_agc = lpr * passive_lock(*_adapt_lpr_gain); + lmr_agc = lmr * passive_lock(*_adapt_lmr_gain); + _lf[k] = (lpr_agc + lmr_agc) * (float)M_SQRT1_2; + _rf[k] = (lpr_agc - lmr_agc) * (float)M_SQRT1_2; + + /*** CENTER FRONT CANCELLATION ***/ + // A heuristic approach exploits that Lt + Rt gain contains the + // information about Lt, Rt correlation. This effectively reshapes + // the front and rear "cones" to concentrate Lt + Rt to C and + // introduce Lt - Rt in L, R. + /* 0.67677 is the empirical lower bound for lpr_gain. */ + c_gain = 8 * (*_adapt_lpr_gain - 0.67677f); + c_gain = c_gain > 0 ? c_gain : 0; + // c_gain should not be too high, not even reaching full + // cancellation (~ 0.50 - 0.55 at current AGC implementation), or + // the center will sound too narrow. */ + c_gain = MATCOMPGAIN / (1 + c_gain * c_gain); + c_agc_cfk = c_gain * _cf[k]; + _lf[k] -= c_agc_cfk; + _rf[k] -= c_agc_cfk; + _cf[k] += c_agc_cfk + c_agc_cfk; +} + +void dpl2decode(float *samples, int numsamples, float *out) +{ + static const unsigned int FWRDURATION = 240; // FWR average duration (samples) + static const int cfg_delay = 0; + static const unsigned int fmt_freq = 48000; + static const unsigned int fmt_nchannels = 2; // input channels + + int cur = 0; + + if (olddelay != cfg_delay || oldfreq != fmt_freq) + { + done(); + olddelay = cfg_delay; + oldfreq = fmt_freq; + dlbuflen = std::max(FWRDURATION, (fmt_freq * cfg_delay / 1000)); //+(len7000-1); + cyc_pos = dlbuflen - 1; + fwrbuf_l.resize(dlbuflen); + fwrbuf_r.resize(dlbuflen); + lf.resize(dlbuflen); + rf.resize(dlbuflen); + lr.resize(dlbuflen); + rr.resize(dlbuflen); + cf.resize(dlbuflen); + cr.resize(dlbuflen); + filter_coefs_lfe = calc_coefficients_125Hz_lowpass(fmt_freq); + lfe_pos = 0; + memset(LFE_buf, 0, sizeof(LFE_buf)); + } + + float *in = samples; // Input audio data + float *end = in + numsamples * fmt_nchannels; // Loop end + + while (in < end) + { + const int k = cyc_pos; + + const int fwr_pos = (k + FWRDURATION) % dlbuflen; + /* Update the full wave rectified total amplitude */ + /* Input matrix decoder */ + l_fwr += fabs(in[0]) - fabs(fwrbuf_l[fwr_pos]); + r_fwr += fabs(in[1]) - fabs(fwrbuf_r[fwr_pos]); + lpr_fwr += fabs(in[0] + in[1]) - fabs(fwrbuf_l[fwr_pos] + fwrbuf_r[fwr_pos]); + lmr_fwr += fabs(in[0] - in[1]) - fabs(fwrbuf_l[fwr_pos] - fwrbuf_r[fwr_pos]); + + /* Matrix encoded 2 channel sources */ + fwrbuf_l[k] = in[0]; + fwrbuf_r[k] = in[1]; + matrix_decode(in, k, 0, 1, true, dlbuflen, + l_fwr, r_fwr, + lpr_fwr, lmr_fwr, + &adapt_l_gain, &adapt_r_gain, + &adapt_lpr_gain, &adapt_lmr_gain, + &lf[0], &rf[0], &lr[0], &rr[0], &cf[0]); + + out[cur + 0] = lf[k]; + out[cur + 1] = rf[k]; + out[cur + 2] = cf[k]; + LFE_buf[lfe_pos] = (out[0] + out[1]) / 2; + out[cur + 3] = firfilter(LFE_buf, lfe_pos, len125, len125, filter_coefs_lfe); + lfe_pos++; + if (lfe_pos == len125) + { + lfe_pos = 0; + } + out[cur + 4] = lr[k]; + out[cur + 5] = rr[k]; + // Next sample... + in += 2; + cur += 6; + cyc_pos--; + if (cyc_pos < 0) + { + cyc_pos += dlbuflen; + } + } +} + +void dpl2reset() +{ + olddelay = -1; + oldfreq = 0; + filter_coefs_lfe = NULL; +} -- cgit v1.2.3 From 6b87a0ef202c614e370d74291c1f754492524aaf Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 4 Feb 2014 20:43:07 -0500 Subject: Introduce a generic clamp function to clean up some similarly duplicated code. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 97bdb5413d..0c9fd65400 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -13,6 +13,7 @@ #include #include #include "DPL2Decoder.h" +#include "MathUtil.h" #ifndef M_PI #define M_PI 3.14159265358979323846 @@ -72,17 +73,6 @@ template static T firfilter(const T *buf, int pos, int len, int count, return T(r1+r2); } -template inline const T& limit(const T& val, const T& min, const T& max) -{ - if (val < min) { - return min; - } else if (val > max) { - return max; - } else { - return val; - } -} - /* // Hamming // 2*pi*k @@ -133,7 +123,7 @@ float* design_fir(unsigned int *n, float* fc, float opt) // Sanity check if(*n==0) return NULL; - fc[0]=limit(fc[0],float(0.001),float(1)); + MathUtil::Clamp(fc[0],float(0.001),float(1)); float *w=(float*)calloc(sizeof(float),*n); -- cgit v1.2.3 From 249b00c4691fb4dbd9e1c833ceccc53526babd21 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 5 Feb 2014 04:04:35 -0500 Subject: Change the modified parameter in the Clamp function to be a pointer. Makes it easier to identify the one being modified. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 0c9fd65400..399541c1c9 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -123,7 +123,7 @@ float* design_fir(unsigned int *n, float* fc, float opt) // Sanity check if(*n==0) return NULL; - MathUtil::Clamp(fc[0],float(0.001),float(1)); + MathUtil::Clamp(&fc[0],float(0.001),float(1)); float *w=(float*)calloc(sizeof(float),*n); -- cgit v1.2.3 From 2afe2152712981e21d6bda6f029292ed2b1cf91e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 17 Feb 2014 05:18:15 -0500 Subject: Convert all includes to relative paths. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 399541c1c9..418239fae4 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -7,13 +7,14 @@ // * Copyright (c) 2004-2006 Milan Cutka // * based on mplayer HRTF plugin by ylai +#include +#include #include -#include -#include -#include #include -#include "DPL2Decoder.h" -#include "MathUtil.h" +#include + +#include "AudioCommon/DPL2Decoder.h" +#include "Common/MathUtil.h" #ifndef M_PI #define M_PI 3.14159265358979323846 -- cgit v1.2.3 From d802d392811be44d34ae9cd23f616db93e54c50f Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 9 Mar 2014 21:14:26 +0100 Subject: clang-modernize -use-nullptr and s/\bNULL\b/nullptr/g for *.cpp/h/mm files not compiled on my machine --- Source/Core/AudioCommon/DPL2Decoder.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 418239fae4..29a5f73d14 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -123,7 +123,7 @@ float* design_fir(unsigned int *n, float* fc, float opt) float fc1; // Cutoff frequencies // Sanity check - if(*n==0) return NULL; + if(*n==0) return nullptr; MathUtil::Clamp(&fc[0],float(0.001),float(1)); float *w=(float*)calloc(sizeof(float),*n); @@ -188,7 +188,7 @@ void done(void) { free(filter_coefs_lfe); } - filter_coefs_lfe = NULL; + filter_coefs_lfe = nullptr; } float* calc_coefficients_125Hz_lowpass(int rate) @@ -378,5 +378,5 @@ void dpl2reset() { olddelay = -1; oldfreq = 0; - filter_coefs_lfe = NULL; + filter_coefs_lfe = nullptr; } -- cgit v1.2.3 From 31cfc73a09a8685cbab20502b4bc132e98e2feb5 Mon Sep 17 00:00:00 2001 From: Matthew Parlane Date: Tue, 11 Mar 2014 00:30:55 +1300 Subject: Fixes spacing for "for", "while", "switch" and "if" Also moved && and || to ends of lines instead of start. Fixed misc vertical alignments and some { needed newlining. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 29a5f73d14..1cddf54600 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -123,7 +123,7 @@ float* design_fir(unsigned int *n, float* fc, float opt) float fc1; // Cutoff frequencies // Sanity check - if(*n==0) return nullptr; + if (*n==0) return nullptr; MathUtil::Clamp(&fc[0],float(0.001),float(1)); float *w=(float*)calloc(sizeof(float),*n); -- cgit v1.2.3 From 22e1aa5bb4a159d6d66a321f978917614aa36331 Mon Sep 17 00:00:00 2001 From: degasus Date: Tue, 8 Jul 2014 14:29:26 +0200 Subject: mark all local functions as static --- Source/Core/AudioCommon/DPL2Decoder.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 1cddf54600..883584e650 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -83,7 +83,7 @@ template static T firfilter(const T *buf, int pos, int len, int count, // n window length // w buffer for the window parameters */ -void hamming(int n, float* w) +static void hamming(int n, float* w) { int i; float k = float(2*M_PI/((float)(n-1))); // 2*pi/(N-1) @@ -110,7 +110,7 @@ opt beta constant used only when designing using kaiser windows returns 0 if OK, -1 if fail */ -float* design_fir(unsigned int *n, float* fc, float opt) +static float* design_fir(unsigned int *n, float* fc, float opt) { unsigned int o = *n & 1; // Indicator for odd filter length unsigned int end = ((*n + 1) >> 1) - o; // Loop end @@ -165,7 +165,7 @@ float* design_fir(unsigned int *n, float* fc, float opt) return w; } -void onSeek(void) +static void onSeek(void) { l_fwr = r_fwr = lpr_fwr = lmr_fwr = 0; std::fill(fwrbuf_l.begin(), fwrbuf_l.end(), 0.0f); @@ -181,7 +181,7 @@ void onSeek(void) memset(LFE_buf, 0, sizeof(LFE_buf)); } -void done(void) +static void done(void) { onSeek(); if (filter_coefs_lfe) @@ -191,7 +191,7 @@ void done(void) filter_coefs_lfe = nullptr; } -float* calc_coefficients_125Hz_lowpass(int rate) +static float* calc_coefficients_125Hz_lowpass(int rate) { len125 = 256; float f = 125.0f / (rate / 2); @@ -204,7 +204,7 @@ float* calc_coefficients_125Hz_lowpass(int rate) return coeffs; } -float passive_lock(float x) +static float passive_lock(float x) { static const float MATAGCLOCK = 0.2f; /* AGC range (around 1) where the matrix behaves passively */ const float x1 = x - 1; @@ -212,7 +212,7 @@ float passive_lock(float x) return x1 - x1 / (1 + ax1s * ax1s) + 1; } -void matrix_decode(const float *in, const int k, const int il, +static void matrix_decode(const float *in, const int k, const int il, const int ir, bool decode_rear, const int _dlbuflen, float _l_fwr, float _r_fwr, -- cgit v1.2.3 From 6d3f249dcc746cc7845ef88ddb8ce3bcc9221aca Mon Sep 17 00:00:00 2001 From: degasus Date: Tue, 8 Jul 2014 15:58:25 +0200 Subject: mark all local variables as static --- Source/Core/AudioCommon/DPL2Decoder.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 883584e650..c82a160b29 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -23,18 +23,18 @@ #define M_SQRT1_2 0.70710678118654752440 #endif -int olddelay = -1; -unsigned int oldfreq = 0; -unsigned int dlbuflen; -int cyc_pos; -float l_fwr, r_fwr, lpr_fwr, lmr_fwr; -std::vector fwrbuf_l, fwrbuf_r; -float adapt_l_gain, adapt_r_gain, adapt_lpr_gain, adapt_lmr_gain; -std::vector lf, rf, lr, rr, cf, cr; -float LFE_buf[256]; -unsigned int lfe_pos; -float *filter_coefs_lfe; -unsigned int len125; +static int olddelay = -1; +static unsigned int oldfreq = 0; +static unsigned int dlbuflen; +static int cyc_pos; +static float l_fwr, r_fwr, lpr_fwr, lmr_fwr; +static std::vector fwrbuf_l, fwrbuf_r; +static float adapt_l_gain, adapt_r_gain, adapt_lpr_gain, adapt_lmr_gain; +static std::vector lf, rf, lr, rr, cf, cr; +static float LFE_buf[256]; +static unsigned int lfe_pos; +static float *filter_coefs_lfe; +static unsigned int len125; template static _ftype_t dotproduct(int count,const T *buf,const _ftype_t *coefficients) { -- cgit v1.2.3 From 2c734726711c7dc4ae1e49c57e1a17b4c5bf6376 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 14 Jul 2014 03:03:05 -0400 Subject: Correct the function casing for DPL2Decoder Brings it more in-line with the rest of the codebase. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 57 ++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 25 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index c82a160b29..17b74ea85b 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -36,7 +36,8 @@ static unsigned int lfe_pos; static float *filter_coefs_lfe; static unsigned int len125; -template static _ftype_t dotproduct(int count,const T *buf,const _ftype_t *coefficients) +template +static _ftype_t DotProduct(int count,const T *buf,const _ftype_t *coefficients) { float sum0=0,sum1=0,sum2=0,sum3=0; for (;count>=4;buf+=4,coefficients+=4,count-=4) @@ -46,11 +47,15 @@ template static _ftype_t dotproduct(int count,const T *b sum2+=buf[2]*coefficients[2]; sum3+=buf[3]*coefficients[3]; } - while (count--) sum0+= *buf++ * *coefficients++; + + while (count--) + sum0+= *buf++ * *coefficients++; + return sum0+sum1+sum2+sum3; } -template static T firfilter(const T *buf, int pos, int len, int count, const float *coefficients) +template +static T FIRFilter(const T *buf, int pos, int len, int count, const float *coefficients) { int count1, count2; @@ -69,8 +74,8 @@ template static T firfilter(const T *buf, int pos, int len, int count, // 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,coefficients);coefficients+=count1; + float r2=DotProduct(count2,buf,coefficients); return T(r1+r2); } @@ -83,7 +88,7 @@ template static T firfilter(const T *buf, int pos, int len, int count, // n window length // w buffer for the window parameters */ -static void hamming(int n, float* w) +static void Hamming(int n, float* w) { int i; float k = float(2*M_PI/((float)(n-1))); // 2*pi/(N-1) @@ -110,7 +115,7 @@ opt beta constant used only when designing using kaiser windows returns 0 if OK, -1 if fail */ -static float* design_fir(unsigned int *n, float* fc, float opt) +static float* DesignFIR(unsigned int *n, float* fc, float opt) { unsigned int o = *n & 1; // Indicator for odd filter length unsigned int end = ((*n + 1) >> 1) - o; // Loop end @@ -129,7 +134,7 @@ static float* design_fir(unsigned int *n, float* fc, float opt) float *w=(float*)calloc(sizeof(float),*n); // Get window coefficients - hamming(*n,w); + Hamming(*n,w); fc1=*fc; // Cutoff frequency must be < 0.5 where 0.5 <=> Fs/2 @@ -165,7 +170,7 @@ static float* design_fir(unsigned int *n, float* fc, float opt) return w; } -static void onSeek(void) +static void OnSeek() { l_fwr = r_fwr = lpr_fwr = lmr_fwr = 0; std::fill(fwrbuf_l.begin(), fwrbuf_l.end(), 0.0f); @@ -181,21 +186,23 @@ static void onSeek(void) memset(LFE_buf, 0, sizeof(LFE_buf)); } -static void done(void) +static void Done() { - onSeek(); + OnSeek(); + if (filter_coefs_lfe) { free(filter_coefs_lfe); } + filter_coefs_lfe = nullptr; } -static float* calc_coefficients_125Hz_lowpass(int rate) +static float* CalculateCoefficients125HzLowpass(int rate) { len125 = 256; float f = 125.0f / (rate / 2); - float *coeffs = design_fir(&len125, &f, 0); + float *coeffs = DesignFIR(&len125, &f, 0); static const float M3_01DB = 0.7071067812f; for (unsigned int i = 0; i < len125; i++) { @@ -204,7 +211,7 @@ static float* calc_coefficients_125Hz_lowpass(int rate) return coeffs; } -static float passive_lock(float x) +static float PassiveLock(float x) { static const float MATAGCLOCK = 0.2f; /* AGC range (around 1) where the matrix behaves passively */ const float x1 = x - 1; @@ -212,7 +219,7 @@ static float passive_lock(float x) return x1 - x1 / (1 + ax1s * ax1s) + 1; } -static void matrix_decode(const float *in, const int k, const int il, +static void MatrixDecode(const float *in, const int k, const int il, const int ir, bool decode_rear, const int _dlbuflen, float _l_fwr, float _r_fwr, @@ -251,8 +258,8 @@ static void matrix_decode(const float *in, const int k, const int il, *_adapt_l_gain = (1 - f) * *_adapt_l_gain + f * l_gain; *_adapt_r_gain = (1 - f) * *_adapt_r_gain + f * r_gain; /* Matrix */ - l_agc = in[il] * passive_lock(*_adapt_l_gain); - r_agc = in[ir] * passive_lock(*_adapt_r_gain); + l_agc = in[il] * PassiveLock(*_adapt_l_gain); + r_agc = in[ir] * PassiveLock(*_adapt_r_gain); _cf[k] = (l_agc + r_agc) * (float)M_SQRT1_2; if (decode_rear) { @@ -274,8 +281,8 @@ static void matrix_decode(const float *in, const int k, const int il, *_adapt_lpr_gain = (1 - f) * *_adapt_lpr_gain + f * lpr_gain; *_adapt_lmr_gain = (1 - f) * *_adapt_lmr_gain + f * lmr_gain; /* Matrix */ - lpr_agc = lpr * passive_lock(*_adapt_lpr_gain); - lmr_agc = lmr * passive_lock(*_adapt_lmr_gain); + lpr_agc = lpr * PassiveLock(*_adapt_lpr_gain); + lmr_agc = lmr * PassiveLock(*_adapt_lmr_gain); _lf[k] = (lpr_agc + lmr_agc) * (float)M_SQRT1_2; _rf[k] = (lpr_agc - lmr_agc) * (float)M_SQRT1_2; @@ -297,7 +304,7 @@ static void matrix_decode(const float *in, const int k, const int il, _cf[k] += c_agc_cfk + c_agc_cfk; } -void dpl2decode(float *samples, int numsamples, float *out) +void DPL2Decode(float *samples, int numsamples, float *out) { static const unsigned int FWRDURATION = 240; // FWR average duration (samples) static const int cfg_delay = 0; @@ -308,7 +315,7 @@ void dpl2decode(float *samples, int numsamples, float *out) if (olddelay != cfg_delay || oldfreq != fmt_freq) { - done(); + Done(); olddelay = cfg_delay; oldfreq = fmt_freq; dlbuflen = std::max(FWRDURATION, (fmt_freq * cfg_delay / 1000)); //+(len7000-1); @@ -321,7 +328,7 @@ void dpl2decode(float *samples, int numsamples, float *out) rr.resize(dlbuflen); cf.resize(dlbuflen); cr.resize(dlbuflen); - filter_coefs_lfe = calc_coefficients_125Hz_lowpass(fmt_freq); + filter_coefs_lfe = CalculateCoefficients125HzLowpass(fmt_freq); lfe_pos = 0; memset(LFE_buf, 0, sizeof(LFE_buf)); } @@ -344,7 +351,7 @@ void dpl2decode(float *samples, int numsamples, float *out) /* Matrix encoded 2 channel sources */ fwrbuf_l[k] = in[0]; fwrbuf_r[k] = in[1]; - matrix_decode(in, k, 0, 1, true, dlbuflen, + MatrixDecode(in, k, 0, 1, true, dlbuflen, l_fwr, r_fwr, lpr_fwr, lmr_fwr, &adapt_l_gain, &adapt_r_gain, @@ -355,7 +362,7 @@ void dpl2decode(float *samples, int numsamples, float *out) out[cur + 1] = rf[k]; out[cur + 2] = cf[k]; LFE_buf[lfe_pos] = (out[0] + out[1]) / 2; - out[cur + 3] = firfilter(LFE_buf, lfe_pos, len125, len125, filter_coefs_lfe); + out[cur + 3] = FIRFilter(LFE_buf, lfe_pos, len125, len125, filter_coefs_lfe); lfe_pos++; if (lfe_pos == len125) { @@ -374,7 +381,7 @@ void dpl2decode(float *samples, int numsamples, float *out) } } -void dpl2reset() +void DPL2Reset() { olddelay = -1; oldfreq = 0; -- cgit v1.2.3 From 2f7df4a803ebb451d5f7a1ef765220a189cb7d6b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 30 Aug 2014 21:04:09 -0400 Subject: AudioCommon: Declare iterator variable in loop body in DPL2Decoder --- Source/Core/AudioCommon/DPL2Decoder.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 17b74ea85b..680d193c45 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -7,6 +7,7 @@ // * Copyright (c) 2004-2006 Milan Cutka // * based on mplayer HRTF plugin by ylai +#include #include #include #include @@ -14,6 +15,7 @@ #include #include "AudioCommon/DPL2Decoder.h" +#include "Common/CommonTypes.h" #include "Common/MathUtil.h" #ifndef M_PI @@ -90,11 +92,10 @@ static T FIRFilter(const T *buf, int pos, int len, int count, const float *coeff */ static void Hamming(int n, float* w) { - int i; float k = float(2*M_PI/((float)(n-1))); // 2*pi/(N-1) // Calculate window coefficients - for (i=0; i> 1) - o; // Loop end - unsigned int i; // Loop index float k1 = 2 * float(M_PI); // 2*pi*fc1 float k2 = 0.5f * (float)(1 - o); // Constant used if the filter has even length @@ -154,7 +154,7 @@ static float* DesignFIR(unsigned int *n, float* fc, float opt) } // Create filter - for (i=0 ; i Date: Wed, 7 Jan 2015 10:24:25 +0000 Subject: Audio: Fix subwoofer output in software 5.1 decoding The FIR filter was only using the first sample of each input packet. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 680d193c45..df1bb76fd5 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -361,7 +361,7 @@ void DPL2Decode(float *samples, int numsamples, float *out) out[cur + 0] = lf[k]; out[cur + 1] = rf[k]; out[cur + 2] = cf[k]; - LFE_buf[lfe_pos] = (out[0] + out[1]) / 2; + LFE_buf[lfe_pos] = (out[cur + 0] + out[cur + 1]) / 2; out[cur + 3] = FIRFilter(LFE_buf, lfe_pos, len125, len125, filter_coefs_lfe); lfe_pos++; if (lfe_pos == len125) -- cgit v1.2.3 From 7580069deaf0455b9dc0fe4a576b5ad5cbd8120b Mon Sep 17 00:00:00 2001 From: Adam Moss Date: Wed, 7 Jan 2015 11:58:02 +0000 Subject: Audio: Fuller subwoofer processing for software 5.1 decode Code was only using front-left and front-right to calculate bass, but HRTF code - which this was once based on - uses all five channels and this sounds fuller. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index df1bb76fd5..93a041d136 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -361,7 +361,7 @@ void DPL2Decode(float *samples, int numsamples, float *out) out[cur + 0] = lf[k]; out[cur + 1] = rf[k]; out[cur + 2] = cf[k]; - LFE_buf[lfe_pos] = (out[cur + 0] + out[cur + 1]) / 2; + LFE_buf[lfe_pos] = (lf[k] + rf[k] + 2.0 * cf[k] + lr[k] + rr[k]) / 2.0; out[cur + 3] = FIRFilter(LFE_buf, lfe_pos, len125, len125, filter_coefs_lfe); lfe_pos++; if (lfe_pos == len125) -- cgit v1.2.3 From b1c9f56acd967d4c06bb85c4e85c1706876f6c8d Mon Sep 17 00:00:00 2001 From: Adam Moss Date: Thu, 8 Jan 2015 10:48:30 +0000 Subject: Audio: Fix warning in DPL2 subwoofer change --- Source/Core/AudioCommon/DPL2Decoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 93a041d136..da16ee4af8 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -361,7 +361,7 @@ void DPL2Decode(float *samples, int numsamples, float *out) out[cur + 0] = lf[k]; out[cur + 1] = rf[k]; out[cur + 2] = cf[k]; - LFE_buf[lfe_pos] = (lf[k] + rf[k] + 2.0 * cf[k] + lr[k] + rr[k]) / 2.0; + LFE_buf[lfe_pos] = (lf[k] + rf[k] + 2.0f * cf[k] + lr[k] + rr[k]) / 2.0f; out[cur + 3] = FIRFilter(LFE_buf, lfe_pos, len125, len125, filter_coefs_lfe); lfe_pos++; if (lfe_pos == len125) -- cgit v1.2.3 From 7add7b685f930c2de0e8b08c8b2a30a4b6bd092d Mon Sep 17 00:00:00 2001 From: Scott Mansell Date: Mon, 16 Feb 2015 12:59:45 +1300 Subject: Remove unneeded spaces from code. Line now matches the style of the rest of the function. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index da16ee4af8..ca7afc9afa 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -51,7 +51,7 @@ static _ftype_t DotProduct(int count,const T *buf,const _ftype_t *coefficients) } while (count--) - sum0+= *buf++ * *coefficients++; + sum0+=*buf++**coefficients++; return sum0+sum1+sum2+sum3; } -- cgit v1.2.3 From 43dfec07c92149ba71743c893d231a7efd3c25d2 Mon Sep 17 00:00:00 2001 From: Scott Mansell Date: Mon, 16 Feb 2015 13:38:11 +1300 Subject: Cleanup DotProduct function to make it more readable. --- Source/Core/AudioCommon/DPL2Decoder.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index ca7afc9afa..355d721e15 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -39,21 +39,25 @@ static float *filter_coefs_lfe; static unsigned int len125; template -static _ftype_t DotProduct(int count,const T *buf,const _ftype_t *coefficients) +static _ftype_t DotProduct(int count, const T *buf, const _ftype_t *coefficients) { - float sum0=0,sum1=0,sum2=0,sum3=0; - for (;count>=4;buf+=4,coefficients+=4,count-=4) + int i; + float sum0 = 0.0f, sum1 = 0.0f, sum2 = 0.0f, sum3 = 0.0f; + + // Unrolled loop + for (i = 0; (i + 3) < count; i += 4) { - sum0+=buf[0]*coefficients[0]; - sum1+=buf[1]*coefficients[1]; - sum2+=buf[2]*coefficients[2]; - sum3+=buf[3]*coefficients[3]; + 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]; } - while (count--) - sum0+=*buf++**coefficients++; + // Epilogue of unrolled loop + for (; i < count; i++) + sum0 += buf[i] * coefficients[i]; - return sum0+sum1+sum2+sum3; + return sum0 + sum1 + sum2 + sum3; } template -- cgit v1.2.3 From 93b16a4a2d5f3e6d467d1315c461aff12852b26c Mon Sep 17 00:00:00 2001 From: Stevoisiak Date: Sun, 15 Feb 2015 14:43:31 -0500 Subject: Formatting/Whitespace Cleanup Various fixes to formatting and whitespace --- Source/Core/AudioCommon/DPL2Decoder.cpp | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 355d721e15..4025a073c5 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -80,9 +80,9 @@ 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); - return T(r1+r2); + float r1 = DotProduct(count1, ptr, coefficients); coefficients += count1; + float r2 = DotProduct(count2, buf, coefficients); + return T(r1 + r2); } /* @@ -96,7 +96,7 @@ static T FIRFilter(const T *buf, int pos, int len, int count, const float *coeff */ static void Hamming(int n, float* w) { - float k = float(2*M_PI/((float)(n-1))); // 2*pi/(N-1) + float k = float(2*M_PI/((float)(n - 1))); // 2*pi/(N-1) // Calculate window coefficients for (int i = 0; i < n; i++) @@ -122,27 +122,28 @@ returns 0 if OK, -1 if fail */ static float* DesignFIR(unsigned int *n, float* fc, float opt) { - unsigned int o = *n & 1; // Indicator for odd filter length + unsigned int o = *n & 1; // Indicator for odd filter length unsigned int end = ((*n + 1) >> 1) - o; // Loop end float k1 = 2 * float(M_PI); // 2*pi*fc1 float k2 = 0.5f * (float)(1 - o); // Constant used if the filter has even length - float g = 0.0f; // Gain + float g = 0.0f; // Gain float t1; // Temporary variables float fc1; // Cutoff frequencies // Sanity check - if (*n==0) return nullptr; - MathUtil::Clamp(&fc[0],float(0.001),float(1)); + if (*n == 0) + return nullptr; + MathUtil::Clamp(&fc[0], float(0.001), float(1)); - float *w=(float*)calloc(sizeof(float),*n); + float *w = (float*)calloc(sizeof(float), *n); // Get window coefficients - Hamming(*n,w); + Hamming(*n, w); - fc1=*fc; + fc1 = *fc; // Cutoff frequency must be < 0.5 where 0.5 <=> Fs/2 - fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25f; + fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1 / 2 : 0.25f; k1 *= fc1; // Low pass filter @@ -154,20 +155,20 @@ static float* DesignFIR(unsigned int *n, float* fc, float opt) if (o) { w[end] = fc1 * w[end] * 2.0f; - g=w[end]; + g = w[end]; } // Create filter for (u32 i = 0; i < end; i++) { - t1 = (float)(i+1) - k2; - w[end-i-1] = w[*n-end+i] = float(w[end-i-1] * sin(k1 * t1)/(M_PI * t1)); // Sinc - g += 2*w[end-i-1]; // Total gain in filter + t1 = (float)(i + 1) - k2; + w[end - i - 1] = w[*n - end + i] = float(w[end - i - 1] * sin(k1 * t1)/(M_PI * t1)); // Sinc + g += 2*w[end - i - 1]; // Total gain in filter } // Normalize gain - g=1/g; + g = 1/g; for (u32 i = 0; i < *n; i++) w[i] *= g; -- cgit v1.2.3 From cefcb0ace9d363b3679b4e93bcc9ec05f1e5f4f8 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Mon, 18 May 2015 01:08:10 +0200 Subject: Update license headers to GPLv2+ --- Source/Core/AudioCommon/DPL2Decoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/DPL2Decoder.cpp') diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index 4025a073c5..b1b63250e4 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -1,5 +1,5 @@ // Copyright 2013 Dolphin Emulator Project -// Licensed under GPLv2 +// Licensed under GPLv2+ // Refer to the license.txt file included. // Dolby Pro Logic 2 decoder from ffdshow-tryout -- cgit v1.2.3