28 #include "libavutil/mem.h"
29 #include "libavcodec/avfft.h"
36 static const float PI = 3.141592654;
41 T
sqr(T x) {
return x*x; }
48 explicit Impl(
unsigned blocksize=8192)
52 m_dftL((FFTComplex*)av_malloc(sizeof(FFTComplex) *
m_n * 2)),
53 m_dftR((FFTComplex*)av_malloc(sizeof(FFTComplex) *
m_n * 2)),
58 m_src((FFTComplex*)av_malloc(sizeof(FFTComplex) *
m_n * 2))
71 for (
unsigned c=0;c<6;c++) {
78 for (
unsigned k=0;k<
m_n;k++)
121 void decode(
float center_width,
float dimension,
float adaption_rate) {
128 add_output(in_first,in_second,center_width,dimension,adaption_rate,
true);
134 for (
unsigned k=0;k<
m_n;k++) {
145 unsigned int cutoff = (30*
m_n)/srate;
146 for (
unsigned f=0;f<=
m_halfN;f++) {
167 m_a = (v-o)*n;
m_b = (o-u)*n;
m_c = (-o-v)*n;
m_d = (o+u)*n;
168 m_e = (o+v)*n;
m_f = (o+u)*n;
m_g = (o-v)*n;
m_h = (o-u)*n;
173 const std::array<std::array<float,2>,4> modes {{ {0,0}, {0,
PI}, {
PI,0}, {-
PI/2,
PI/2} }};
190 static float amplitude(FFTComplex z) {
return std::hypot(z.re, z.im); }
191 static float phase(FFTComplex z) {
return std::atan2(z.im, z.re); }
200 block_decode(input1,input2,out,center_width,dimension,adaption_rate);
213 for (
unsigned k = 0; k <
m_halfN; k++)
215 m_dftL[k] = (FFTComplex){ .re = input1[0][k] *
m_wnd[k], .im = (FFTSample)0 };
216 m_dftR[k] = (FFTComplex){ .re = input1[1][k] *
m_wnd[k], .im = (FFTSample)0 };
231 for (
unsigned f=0;f<
m_halfN;f++) {
242 float phaseDiff = phaseL - phaseR;
243 if (phaseDiff < -
PI) phaseDiff += 2*
PI;
244 if (phaseDiff >
PI) phaseDiff -= 2*
PI;
245 phaseDiff = abs(phaseDiff);
261 float left = (1-
m_xFs[f])/2;
262 float right = (1+
m_xFs[f])/2;
263 float front = (1+
m_yFs[f])/2;
265 std::array<float, 5> volume
267 front * (left * center_width + std::max(0.0F, -
m_xFs[f]) * (1.0F - center_width) ),
269 front * (right * center_width + std::max(0.0F,
m_xFs[f]) * (1.0F - center_width) ),
275 for (
unsigned c=0;c<5;c++)
276 m_filter[c][f] = (1-adaption_rate)*
m_filter[c][f] + adaption_rate*volume[c];
283 float phaseDiff = phaseL - phaseR;
284 if (phaseDiff < -
PI) phaseDiff += 2*
PI;
285 if (phaseDiff >
PI) phaseDiff -= 2*
PI;
286 phaseDiff = abs(phaseDiff);
292 m_yFs[f] = 1 - (phaseDiff/
PI)*2;
298 m_yFs[f] = (1-frontness) *
m_yFs[f] + frontness * 1;
309 float left = (1-
m_xFs[f])/2;
310 float right = (1+
m_xFs[f])/2;
311 float front = (1+
m_yFs[f])/2;
313 std::array<float, 5> volume
315 front * (left * center_width + std::max(0.0F, -
m_xFs[f]) * (1.0F-center_width)),
317 front * (right * center_width + std::max(0.0F,
m_xFs[f]) * (1.0F-center_width)),
323 for (
unsigned c=0;c<5;c++)
324 m_filter[c][f] = (1-adaption_rate)*
m_filter[c][f] + adaption_rate*volume[c];
347 static double get_yfs(
double ampDiff,
double phaseDiff) {
348 double x = 1-(((1-
sqr(ampDiff))*phaseDiff)/
M_PI*2);
350 double tanX = tan(x);
351 return 0.16468622925824683 + (0.5009268347818189*x) - (0.06462757726992101*x*x)
352 + (0.09170680403453149*x*x*x) + (0.2617754892323973*tanX) - (0.04180413533856156*
sqr(tanX));
354 return 0.16468622925824683 + (0.5009268347818189*x) - (0.06462757726992101*x*x)
355 + (0.09170680403453149*x*x*x) + (0.2617754892323973*tan(x)) - (0.04180413533856156*
sqr(tan(x)));
360 static double get_xfs(
double ampDiff,
double yfs) {
364 double tanX = tan(x);
365 double tanY = tan(y);
366 double asinX = asin(x);
367 double sinX = sin(x);
368 double sinY = sin(y);
372 return (2.464833559224702*x) - (423.52131153259404*x*y) +
373 (67.8557858606918*
x3*y) + (788.2429425544392*x*y2) -
374 (79.97650354902909*
x3*y2) - (513.8966153850349*x*y3) +
375 (35.68117670186306*
x3*y3) + (13867.406173420834*y*asinX) -
376 (2075.8237075786396*y2*asinX) - (908.2722068360281*y3*asinX) -
377 (12934.654772878019*asinX*sinY) - (13216.736529661162*y*tanX) +
378 (1288.6463247741938*y2*tanX) + (1384.372969378453*y3*tanX) +
379 (12699.231471126128*sinY*tanX) + (95.37131275594336*sinX*tanY) -
380 (91.21223198407546*tanX*tanY);
382 return (2.464833559224702*x) - (423.52131153259404*x*y) +
383 (67.8557858606918*x*x*x*y) + (788.2429425544392*x*y*y) -
384 (79.97650354902909*x*x*x*y*y) - (513.8966153850349*x*y*y*y) +
385 (35.68117670186306*x*x*x*y*y*y) + (13867.406173420834*y*asin(x)) -
386 (2075.8237075786396*y*y*asin(x)) - (908.2722068360281*y*y*y*asin(x)) -
387 (12934.654772878019*asin(x)*sin(y)) - (13216.736529661162*y*tan(x)) +
388 (1288.6463247741938*y*y*tan(x)) + (1384.372969378453*y*y*y*tan(x)) +
389 (12699.231471126128*sin(y)*tan(x)) + (95.37131275594336*sin(x)*tan(y)) -
390 (91.21223198407546*tan(x)*tan(y));
405 for (
unsigned f = 0; f <=
m_halfN; f++)
407 m_src[f].re = signal[f].real() * flt[f];
408 m_src[f].im = signal[f].imag() * flt[f];
411 for (
unsigned f = 1; f <
m_halfN; f++)
420 for (
unsigned int k = 0; k <
m_halfN; k++)