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.

201 lines
3.8KB

  1. // ----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2010 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 <math.h>
  22. #include <string.h>
  23. #include "pareq.h"
  24. namespace REV1 {
  25. Pareq::Pareq (void) :
  26. _touch0 (0),
  27. _touch1 (0),
  28. _state (BYPASS),
  29. _g0 (1),
  30. _g1 (1),
  31. _f0 (1e3f),
  32. _f1 (1e3f)
  33. {
  34. setfsamp (0.0f);
  35. }
  36. Pareq::~Pareq (void)
  37. {
  38. }
  39. void Pareq::setfsamp (float fsamp)
  40. {
  41. _fsamp = fsamp;
  42. reset ();
  43. }
  44. void Pareq::reset (void)
  45. {
  46. memset (_z1, 0, sizeof (float) * MAXCH);
  47. memset (_z2, 0, sizeof (float) * MAXCH);
  48. }
  49. void Pareq::prepare (int nsamp)
  50. {
  51. bool upd = false;
  52. float g, f;
  53. if (_touch1 != _touch0)
  54. {
  55. g = _g0;
  56. f = _f0;
  57. if (g != _g1)
  58. {
  59. upd = true;
  60. if (g > 2 * _g1) _g1 *= 2;
  61. else if (_g1 > 2 * g) _g1 /= 2;
  62. else _g1 = g;
  63. }
  64. if (f != _f1)
  65. {
  66. upd = true;
  67. if (f > 2 * _f1) _f1 *= 2;
  68. else if (_f1 > 2 * f) _f1 /= 2;
  69. else _f1 = f;
  70. }
  71. if (upd)
  72. {
  73. if ((_state == BYPASS) && (_g1 == 1))
  74. {
  75. calcpar1 (0, _g1, _f1);
  76. }
  77. else
  78. {
  79. _state = SMOOTH;
  80. calcpar1 (nsamp, _g1, _f1);
  81. }
  82. }
  83. else
  84. {
  85. _touch1 = _touch0;
  86. if (fabs (_g1 - 1) < 0.001f)
  87. {
  88. _state = BYPASS;
  89. reset ();
  90. }
  91. else
  92. {
  93. _state = STATIC;
  94. }
  95. }
  96. }
  97. }
  98. void Pareq::calcpar1 (int nsamp, float g, float f)
  99. {
  100. float b, c1, c2, gg;
  101. f *= float (M_PI) / _fsamp;
  102. b = 2 * f / sqrtf (g);
  103. gg = 0.5f * (g - 1);
  104. c1 = -cosf (2 * f);
  105. c2 = (1 - b) / (1 + b);
  106. if (nsamp)
  107. {
  108. _dc1 = (c1 - _c1) / nsamp + 1e-30f;
  109. _dc2 = (c2 - _c2) / nsamp + 1e-30f;
  110. _dgg = (gg - _gg) / nsamp + 1e-30f;
  111. }
  112. else
  113. {
  114. _c1 = c1;
  115. _c2 = c2;
  116. _gg = gg;
  117. }
  118. }
  119. void Pareq::process1 (int nsamp, int nchan, float *data[])
  120. {
  121. int i, j;
  122. float c1, c2, gg;
  123. float x, y, z1, z2;
  124. float *p;
  125. c1 = _c1;
  126. c2 = _c2;
  127. gg = _gg;
  128. if (_state == SMOOTH)
  129. {
  130. for (i = 0; i < nchan; i++)
  131. {
  132. p = data [i];
  133. z1 = _z1 [i];
  134. z2 = _z2 [i];
  135. c1 = _c1;
  136. c2 = _c2;
  137. gg = _gg;
  138. for (j = 0; j < nsamp; j++)
  139. {
  140. c1 += _dc1;
  141. c2 += _dc2;
  142. gg += _dgg;
  143. x = *p;
  144. y = x - c2 * z2;
  145. *p++ = x - gg * (z2 + c2 * y - x);
  146. y -= c1 * z1;
  147. z2 = z1 + c1 * y;
  148. z1 = y + 1e-20f;
  149. }
  150. _z1 [i] = z1;
  151. _z2 [i] = z2;
  152. }
  153. _c1 = c1;
  154. _c2 = c2;
  155. _gg = gg;
  156. }
  157. else
  158. {
  159. for (i = 0; i < nchan; i++)
  160. {
  161. p = data [i];
  162. z1 = _z1 [i];
  163. z2 = _z2 [i];
  164. for (j = 0; j < nsamp; j++)
  165. {
  166. x = *p;
  167. y = x - c2 * z2;
  168. *p++ = x - gg * (z2 + c2 * y - x);
  169. y -= c1 * z1;
  170. z2 = z1 + c1 * y;
  171. z1 = y + 1e-20f;
  172. }
  173. _z1 [i] = z1;
  174. _z2 [i] = z2;
  175. }
  176. }
  177. }
  178. }