Check for the upsampler to determine if HRTF uses HOA
[openal-soft.git] / Alc / uhjfilter.c
blob8e2febae8d5028e5515081b72c28cc6cff01c42c
2 #include "config.h"
4 #include "alu.h"
5 #include "uhjfilter.h"
7 /* This is the maximum number of samples processed for each inner loop
8 * iteration. */
9 #define MAX_UPDATE_SAMPLES 128
12 static const ALfloat Filter1Coeff[4] = {
13 0.6923878f, 0.9360654322959f, 0.9882295226860f, 0.9987488452737f
15 static const ALfloat Filter2Coeff[4] = {
16 0.4021921162426f, 0.8561710882420f, 0.9722909545651f, 0.9952884791278f
19 static void allpass_process(AllPassState *state, ALfloat *restrict dst, const ALfloat *restrict src, const ALfloat aa, ALsizei todo)
21 ALsizei i;
23 if(todo > 1)
25 dst[0] = aa*(src[0] + state->y[1]) - state->x[1];
26 dst[1] = aa*(src[1] + state->y[0]) - state->x[0];
27 for(i = 2;i < todo;i++)
28 dst[i] = aa*(src[i] + dst[i-2]) - src[i-2];
29 state->x[1] = src[i-2];
30 state->x[0] = src[i-1];
31 state->y[1] = dst[i-2];
32 state->y[0] = dst[i-1];
34 else if(todo == 1)
36 dst[0] = aa*(src[0] + state->y[1]) - state->x[1];
37 state->x[1] = state->x[0];
38 state->x[0] = src[0];
39 state->y[1] = state->y[0];
40 state->y[0] = dst[0];
45 /* NOTE: There seems to be a bit of an inconsistency in how this encoding is
46 * supposed to work. Some references, such as
48 * http://members.tripod.com/martin_leese/Ambisonic/UHJ_file_format.html
50 * specify a pre-scaling of sqrt(2) on the W channel input, while other
51 * references, such as
53 * https://en.wikipedia.org/wiki/Ambisonic_UHJ_format#Encoding.5B1.5D
54 * and
55 * https://wiki.xiph.org/Ambisonics#UHJ_format
57 * do not. The sqrt(2) scaling is in line with B-Format decoder coefficients
58 * which include such a scaling for the W channel input, however the original
59 * source for this equation is a 1985 paper by Michael Gerzon, which does not
60 * apparently include the scaling. Applying the extra scaling creates a louder
61 * result with a narrower stereo image compared to not scaling, and I don't
62 * know which is the intended result.
65 void EncodeUhj2(Uhj2Encoder *enc, ALfloat *restrict LeftOut, ALfloat *restrict RightOut, ALfloat (*restrict InSamples)[BUFFERSIZE], ALsizei SamplesToDo)
67 ALfloat D[MAX_UPDATE_SAMPLES], S[MAX_UPDATE_SAMPLES];
68 ALfloat temp[2][MAX_UPDATE_SAMPLES];
69 ALsizei base, i;
71 for(base = 0;base < SamplesToDo;)
73 ALsizei todo = mini(SamplesToDo - base, MAX_UPDATE_SAMPLES);
75 /* D = 0.6554516*Y */
76 for(i = 0;i < todo;i++)
77 temp[0][i] = 0.6554516f*InSamples[2][base+i];
78 allpass_process(&enc->Filter1_Y[0], temp[1], temp[0],
79 Filter1Coeff[0]*Filter1Coeff[0], todo);
80 allpass_process(&enc->Filter1_Y[1], temp[0], temp[1],
81 Filter1Coeff[1]*Filter1Coeff[1], todo);
82 allpass_process(&enc->Filter1_Y[2], temp[1], temp[0],
83 Filter1Coeff[2]*Filter1Coeff[2], todo);
84 /* NOTE: Filter1 requires a 1 sample delay for the final output, so
85 * take the last processed sample from the previous run as the first
86 * output sample.
88 D[0] = enc->Filter1_Y[3].y[0];
89 allpass_process(&enc->Filter1_Y[3], temp[0], temp[1],
90 Filter1Coeff[3]*Filter1Coeff[3], todo);
91 for(i = 1;i < todo;i++)
92 D[i] = temp[0][i-1];
94 /* D += j(-0.3420201*W + 0.5098604*X) */
95 for(i = 0;i < todo;i++)
96 temp[0][i] = -0.3420201f*InSamples[0][base+i] +
97 0.5098604f*InSamples[1][base+i];
98 allpass_process(&enc->Filter2_WX[0], temp[1], temp[0],
99 Filter2Coeff[0]*Filter2Coeff[0], todo);
100 allpass_process(&enc->Filter2_WX[1], temp[0], temp[1],
101 Filter2Coeff[1]*Filter2Coeff[1], todo);
102 allpass_process(&enc->Filter2_WX[2], temp[1], temp[0],
103 Filter2Coeff[2]*Filter2Coeff[2], todo);
104 allpass_process(&enc->Filter2_WX[3], temp[0], temp[1],
105 Filter2Coeff[3]*Filter2Coeff[3], todo);
106 for(i = 0;i < todo;i++)
107 D[i] += temp[0][i];
109 /* S = 0.9396926*W + 0.1855740*X */
110 for(i = 0;i < todo;i++)
111 temp[0][i] = 0.9396926f*InSamples[0][base+i] +
112 0.1855740f*InSamples[1][base+i];
113 allpass_process(&enc->Filter1_WX[0], temp[1], temp[0],
114 Filter1Coeff[0]*Filter1Coeff[0], todo);
115 allpass_process(&enc->Filter1_WX[1], temp[0], temp[1],
116 Filter1Coeff[1]*Filter1Coeff[1], todo);
117 allpass_process(&enc->Filter1_WX[2], temp[1], temp[0],
118 Filter1Coeff[2]*Filter1Coeff[2], todo);
119 S[0] = enc->Filter1_WX[3].y[0];
120 allpass_process(&enc->Filter1_WX[3], temp[0], temp[1],
121 Filter1Coeff[3]*Filter1Coeff[3], todo);
122 for(i = 1;i < todo;i++)
123 S[i] = temp[0][i-1];
125 /* Left = (S + D)/2.0 */
126 for(i = 0;i < todo;i++)
127 *(LeftOut++) += (S[i] + D[i]) * 0.5f;
128 /* Right = (S - D)/2.0 */
129 for(i = 0;i < todo;i++)
130 *(RightOut++) += (S[i] - D[i]) * 0.5f;
132 base += todo;