HRTF.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. HRTF.cc
  3. Copyright 2002-7 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. high-order IIR filtering modeled after HRTF impulse responses
  6. */
  7. /*
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. 02111-1307, USA or point your web browser to http://www.gnu.org.
  20. */
  21. #include "basics.h"
  22. #include "HRTF.h"
  23. #include "Descriptor.h"
  24. #include "elev0.h"
  25. /* //////////////////////////////////////////////////////////////////////// */
  26. void
  27. HRTF::init()
  28. {
  29. h = 0;
  30. }
  31. void
  32. HRTF::set_pan (int p)
  33. {
  34. n = 31;
  35. pan = p;
  36. if (p >= 0)
  37. {
  38. left.a = elev0[p].left.a;
  39. left.b = elev0[p].left.b;
  40. right.a = elev0[p].right.a;
  41. right.b = elev0[p].right.b;
  42. }
  43. else
  44. {
  45. p = -p;
  46. left.a = elev0[p].right.a;
  47. left.b = elev0[p].right.b;
  48. right.a = elev0[p].left.a;
  49. right.b = elev0[p].left.b;
  50. }
  51. memset (left.y, 0, sizeof (left.y));
  52. memset (right.y, 0, sizeof (right.y));
  53. }
  54. template <sample_func_t F>
  55. void
  56. HRTF::one_cycle (int frames)
  57. {
  58. sample_t * s = ports[0];
  59. int p = (int) getport(1);
  60. if (p != pan) set_pan (p);
  61. sample_t * dl = ports[2];
  62. sample_t * dr = ports[3];
  63. double l, r;
  64. for (int i = 0; i < frames; ++i)
  65. {
  66. x[h] = l = r = s[i] + normal;
  67. l *= left.a[0];
  68. r *= right.a[0];
  69. for (int j = 1, z = h - 1; j < n; --z, ++j)
  70. {
  71. z &= 31;
  72. l += left.a[j] * x[z];
  73. l += left.b[j] * left.y[z];
  74. r += right.a[j] * x[z];
  75. r += right.b[j] * right.y[z];
  76. }
  77. left.y[h] = l;
  78. right.y[h] = r;
  79. h = (h + 1) & 31;
  80. F (dl, i, l, adding_gain);
  81. F (dr, i, r, adding_gain);
  82. }
  83. }
  84. /* //////////////////////////////////////////////////////////////////////// */
  85. PortInfo
  86. HRTF::port_info [] =
  87. {
  88. {
  89. "in",
  90. INPUT | AUDIO,
  91. {BOUNDED, -1, 1}
  92. }, {
  93. "pan",
  94. INPUT | CONTROL,
  95. {BOUNDED | INTEGER | DEFAULT_0, -36, 36}
  96. }, {
  97. "out:l",
  98. OUTPUT | AUDIO,
  99. {0}
  100. }, {
  101. "out:r",
  102. OUTPUT | AUDIO,
  103. {0}
  104. }
  105. };
  106. template <> void
  107. Descriptor<HRTF>::setup()
  108. {
  109. UniqueID = 1787;
  110. Label = "HRTF";
  111. Properties = HARD_RT;
  112. Name = CAPS "HRTF - Head-related transfer function at elevation 0";
  113. Maker = "Tim Goetze <tim@quitte.de>";
  114. Copyright = "GPL, 2004-7";
  115. /* fill port info and vtable */
  116. autogen();
  117. }