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.

hp3filt.cc 3.4KB

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