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.

lfshelf2.cc 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <math.h>
  22. #include <string.h>
  23. #include "lfshelf2.h"
  24. namespace BLS1 {
  25. LFshelf2::LFshelf2 (void) :
  26. _touch0 (0),
  27. _touch1 (0),
  28. _bypass (true),
  29. _state (BYPASS),
  30. _g0 (1),
  31. _g1 (1),
  32. _f0 (1e2f),
  33. _f1 (1e2f),
  34. _s0 (0),
  35. _s1 (0)
  36. {
  37. setfsamp (0.0f);
  38. }
  39. LFshelf2::~LFshelf2 (void)
  40. {
  41. }
  42. void LFshelf2::setfsamp (float fsamp)
  43. {
  44. _fsamp = fsamp;
  45. reset ();
  46. }
  47. void LFshelf2::reset (void)
  48. {
  49. memset (_z1, 0, sizeof (float) * MAXCH);
  50. memset (_z2, 0, sizeof (float) * MAXCH);
  51. }
  52. void LFshelf2::prepare (int nsamp)
  53. {
  54. bool upd = false;
  55. float g, f, s;
  56. if (_touch1 != _touch0)
  57. {
  58. g = _bypass ? 1 : _g0;
  59. f = _f0;
  60. s = _s0;
  61. if (g != _g1)
  62. {
  63. upd = true;
  64. if (g > 2 * _g1) _g1 *= 2;
  65. else if (_g1 > 2 * g) _g1 /= 2;
  66. else _g1 = g;
  67. }
  68. if (f != _f1)
  69. {
  70. upd = true;
  71. if (f > 2 * _f1) _f1 *= 2;
  72. else if (_f1 > 2 * f) _f1 /= 2;
  73. else _f1 = f;
  74. }
  75. if (s != _s1)
  76. {
  77. upd = true;
  78. if (s > _s1 + 0.3f) _s1 += 0.3f;
  79. else if (s < _s1 - 0.3f) _s1 -= 0.3f;
  80. else _s1 = s;
  81. }
  82. if (upd)
  83. {
  84. if ((_state == BYPASS) && (_g1 == 1))
  85. {
  86. calcpar1 (0, _g1, _f1, _s1);
  87. }
  88. else
  89. {
  90. _state = SMOOTH;
  91. calcpar1 (nsamp, _g1, _f1, _s1);
  92. }
  93. }
  94. else
  95. {
  96. _touch1 = _touch0;
  97. if (fabs (_g1 - 1) < 0.001f)
  98. {
  99. _state = BYPASS;
  100. reset ();
  101. }
  102. else
  103. {
  104. _state = STATIC;
  105. }
  106. }
  107. }
  108. }
  109. float LFshelf2::response (float f)
  110. {
  111. // Compute gain at frequency f from _a0, _a1, _a2, _b1, _b2.
  112. // This is left as an exercise for the reader.
  113. return 0;
  114. }
  115. void LFshelf2::calcpar1 (int nsamp, float g, float f, float s)
  116. {
  117. bool inv;
  118. float a0, a1, a2, b1, b2;
  119. float r, w1, w2, c1, c2, c3, c4, d1, d2;
  120. inv = (g < 1.0f);
  121. if (inv) g = 1.0f / g;
  122. w1 = 2 * M_PI * f / _fsamp;
  123. w2 = w1 * sqrtf (g);
  124. s *= (g - 1) / g;
  125. d1 = 1.8f - 0.55f * s / (1 + 2 * w1);
  126. d2 = 1.8f - 1.50f * s / (1 + 2 * w2);
  127. if (inv)
  128. {
  129. c1 = w1 * w1;
  130. c2 = d1 * w1;
  131. c3 = w2 * w2;
  132. c4 = d2 * w2;
  133. }
  134. else
  135. {
  136. c1 = w2 * w2;
  137. c2 = d2 * w2;
  138. c3 = w1 * w1;
  139. c4 = d1 * w1;
  140. }
  141. r = c3 + 2 * c4 + 4;
  142. b1 = 4 * (c4 - c3) / r;
  143. b2 = 4 * c3 / r;
  144. a0 = (c1 + 2 * c2 + 4) / r - 1;
  145. a1 = 4 * (c2 - c1) / r - b1;
  146. a2 = 4 * c1 / r - b2;
  147. if (nsamp)
  148. {
  149. _da0 = (a0 - _a0) / nsamp + 1e-30f;
  150. _da1 = (a1 - _a1) / nsamp + 1e-30f;
  151. _da2 = (a2 - _a2) / nsamp + 1e-30f;
  152. _db1 = (b1 - _b1) / nsamp + 1e-30f;
  153. _db2 = (b2 - _b2) / nsamp + 1e-30f;
  154. }
  155. else
  156. {
  157. _a0 = a0;
  158. _a1 = a1;
  159. _a2 = a2;
  160. _b1 = b1;
  161. _b2 = b2;
  162. }
  163. }
  164. void LFshelf2::process1 (int nsamp, int nchan, float *data[])
  165. {
  166. int i, j;
  167. float a0, a1, a2, b1, b2;
  168. float x, y, z1, z2;
  169. float *p;
  170. a0 = _a0;
  171. a1 = _a1;
  172. a2 = _a2;
  173. b1 = _b1;
  174. b2 = _b2;
  175. if (_state == SMOOTH)
  176. {
  177. for (i = 0; i < nchan; i++)
  178. {
  179. p = data [i];
  180. z1 = _z1 [i];
  181. z2 = _z2 [i];
  182. a0 = _a0;
  183. a1 = _a1;
  184. a2 = _a2;
  185. b1 = _b1;
  186. b2 = _b2;
  187. for (j = 0; j < nsamp; j++)
  188. {
  189. a0 += _da0;
  190. a1 += _da1;
  191. a2 += _da2;
  192. b1 += _db1;
  193. b2 += _db2;
  194. x = *p;
  195. y = x - b1 * z1 - b2 * z2 + 1e-10f;
  196. *p++ = x + a0 * y + a1 * z1 + a2 * z2;
  197. z2 += z1;
  198. z1 += y;
  199. }
  200. _z1 [i] = z1;
  201. _z2 [i] = z2;
  202. }
  203. _a0 = a0;
  204. _a1 = a1;
  205. _a2 = a2;
  206. _b1 = b1;
  207. _b2 = b2;
  208. }
  209. else
  210. {
  211. for (i = 0; i < nchan; i++)
  212. {
  213. p = data [i];
  214. z1 = _z1 [i];
  215. z2 = _z2 [i];
  216. for (j = 0; j < nsamp; j++)
  217. {
  218. x = *p;
  219. y = x - b1 * z1 - b2 * z2 + 1e-10f;
  220. *p++ = x + a0 * y + a1 * z1 + a2 * z2;
  221. z2 += z1;
  222. z1 += y;
  223. }
  224. _z1 [i] = z1;
  225. _z2 [i] = z2;
  226. }
  227. }
  228. }
  229. }