Free Electron
uhjfilter.h
1 #ifndef UHJFILTER_H
2 #define UHJFILTER_H
3 
4 #include "AL/al.h"
5 
6 #include "alMain.h"
7 
8 typedef struct AllPassState {
9  ALfloat z[2];
10 } AllPassState;
11 
12 /* Encoding 2-channel UHJ from B-Format is done as:
13  *
14  * S = 0.9396926*W + 0.1855740*X
15  * D = j(-0.3420201*W + 0.5098604*X) + 0.6554516*Y
16  *
17  * Left = (S + D)/2.0
18  * Right = (S - D)/2.0
19  *
20  * where j is a wide-band +90 degree phase shift.
21  *
22  * The phase shift is done using a Hilbert transform, described here:
23  * https://web.archive.org/web/20060708031958/http://www.biochem.oulu.fi/~oniemita/dsp/hilbert/
24  * It works using 2 sets of 4 chained filters. The first filter chain produces
25  * a phase shift of varying magnitude over a wide range of frequencies, while
26  * the second filter chain produces a phase shift 90 degrees ahead of the
27  * first over the same range.
28  *
29  * Combining these two stages requires the use of three filter chains. S-
30  * channel output uses a Filter1 chain on the W and X channel mix, while the D-
31  * channel output uses a Filter1 chain on the Y channel plus a Filter2 chain on
32  * the W and X channel mix. This results in the W and X input mix on the D-
33  * channel output having the required +90 degree phase shift relative to the
34  * other inputs.
35  */
36 
37 typedef struct Uhj2Encoder {
38  AllPassState Filter1_Y[4];
39  AllPassState Filter2_WX[4];
40  AllPassState Filter1_WX[4];
41  ALfloat LastY, LastWX;
42 } Uhj2Encoder;
43 
44 /* Encodes a 2-channel UHJ (stereo-compatible) signal from a B-Format input
45  * signal. The input must use FuMa channel ordering and scaling.
46  */
47 void EncodeUhj2(Uhj2Encoder *enc, ALfloat *restrict LeftOut, ALfloat *restrict RightOut, ALfloat (*restrict InSamples)[BUFFERSIZE], ALsizei SamplesToDo);
48 
49 #endif /* UHJFILTER_H */