🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.7 KiB

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