uhjfilter.h 1.7 KB

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