Audio plugin host https://kx.studio/carla
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.

179 lines
3.3KB

  1. // ----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2011 Fons Adriaensen <fons@linuxaudio.org>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. //
  19. // ----------------------------------------------------------------------
  20. #include <string.h>
  21. #include <math.h>
  22. #include "hp3filt.h"
  23. HP3filt::HP3filt (void) :
  24. _touch0 (0),
  25. _touch1 (0),
  26. _state (BYPASS),
  27. _f0 (0),
  28. _f1 (0),
  29. _c1 (0),
  30. _c2 (0),
  31. _c3 (0),
  32. _g (0),
  33. _d (0)
  34. {
  35. setfsamp (0.0f);
  36. }
  37. HP3filt::~HP3filt (void)
  38. {
  39. }
  40. void HP3filt::setfsamp (float fsamp)
  41. {
  42. _fsamp = fsamp;
  43. reset ();
  44. }
  45. void HP3filt::reset (void)
  46. {
  47. memset (_z1, 0, sizeof (float) * MAXCH);
  48. memset (_z2, 0, sizeof (float) * MAXCH);
  49. memset (_z3, 0, sizeof (float) * MAXCH);
  50. }
  51. void HP3filt::prepare (int nsamp)
  52. {
  53. float f;
  54. f = _f0;
  55. if (_touch1 != _touch0)
  56. {
  57. if (_f1 == f)
  58. {
  59. _touch1 = _touch0;
  60. if (_state == FADING)
  61. {
  62. _state = (_d > 0) ? STATIC : BYPASS;
  63. }
  64. }
  65. else if (_f1 == 0)
  66. {
  67. _f1 = f;
  68. _a = 0.0f;
  69. _d = 1.0f / nsamp;
  70. calcpar1 (0, _f1);
  71. reset ();
  72. _state = FADING;
  73. }
  74. else if (f == 0)
  75. {
  76. _f1 = f;
  77. _a = 1.0f;
  78. _d = -1.0f / nsamp;
  79. _state = FADING;
  80. }
  81. else
  82. {
  83. if (f > 1.25f * _f1) _f1 *= 1.25f;
  84. if (f < 0.80f * _f1) _f1 *= 0.80f;
  85. else _f1 = f;
  86. calcpar1 (0, _f1);
  87. }
  88. }
  89. }
  90. float HP3filt::response (float f)
  91. {
  92. // Compute gain at frequency f from _c1 _c2, _c3, _g.
  93. // This is left as an exercise for the reader.
  94. return 0;
  95. }
  96. void HP3filt::calcpar1 (int nsamp, float f)
  97. {
  98. float a, b, t;
  99. _g = 1;
  100. a = (float)(M_PI) * f / _fsamp;
  101. b = a * a;
  102. t = 1 + a + b;
  103. _g /= t;
  104. _c1 = 2 * a + 4 * b;
  105. _c2 = 4 * b / _c1;
  106. _c1 /= t;
  107. t = 1 + a;
  108. _g /= t;
  109. _c3 = 2 * a / t;
  110. }
  111. void HP3filt::process1 (int nsamp, int nchan, float *data[])
  112. {
  113. int i, j;
  114. float a, d, x, y, z1, z2, z3;
  115. float *p;
  116. a = _a;
  117. d = _d;
  118. for (i = 0; i < nchan; i++)
  119. {
  120. p = data [i];
  121. z1 = _z1 [i];
  122. z2 = _z2 [i];
  123. z3 = _z3 [i];
  124. if (_state == FADING)
  125. {
  126. a = _a;
  127. for (j = 0; j < nsamp; j++)
  128. {
  129. x = *p;
  130. y = x - z1 - z2 + 1e-20f;
  131. z2 += _c2 * z1;
  132. z1 += _c1 * y;
  133. y -= z3 - 1e-20f;
  134. z3 += _c3 * y;
  135. a += d;
  136. *p++ = a * (_g * y) + (1 - a) * x;
  137. }
  138. }
  139. else
  140. {
  141. for (j = 0; j < nsamp; j++)
  142. {
  143. x = *p;
  144. y = x - z1 - z2 + 1e-20f;
  145. z2 += _c2 * z1;
  146. z1 += _c1 * y;
  147. y -= z3 - 1e-20f;
  148. z3 += _c3 * y;
  149. *p++ = _g * y;
  150. }
  151. }
  152. _z1 [i] = z1;
  153. _z2 [i] = z2;
  154. _z3 [i] = z3;
  155. }
  156. if (_state == FADING) _a = a;
  157. }