External plugins for 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.

430 lines
12KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. AnalogFilter.cpp - Several analog filters (lowpass, highpass...)
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Copyright (C) 2010-2010 Mark McCurry
  6. Author: Nasca Octavian Paul
  7. Mark McCurry
  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. */
  13. #include <cstring> //memcpy
  14. #include <cmath>
  15. #include <cassert>
  16. #include "../Misc/Util.h"
  17. #include "AnalogFilter.h"
  18. namespace zyncarla {
  19. AnalogFilter::AnalogFilter(unsigned char Ftype,
  20. float Ffreq,
  21. float Fq,
  22. unsigned char Fstages,
  23. unsigned int srate, int bufsize)
  24. :Filter(srate, bufsize),
  25. type(Ftype),
  26. stages(Fstages),
  27. freq(Ffreq),
  28. q(Fq),
  29. gain(1.0),
  30. abovenq(false),
  31. oldabovenq(false)
  32. {
  33. for(int i = 0; i < 3; ++i)
  34. coeff.c[i] = coeff.d[i] = oldCoeff.c[i] = oldCoeff.d[i] = 0.0f;
  35. if(stages >= MAX_FILTER_STAGES)
  36. stages = MAX_FILTER_STAGES;
  37. cleanup();
  38. firsttime = false;
  39. setfreq_and_q(Ffreq, Fq);
  40. firsttime = true;
  41. coeff.d[0] = 0; //this is not used
  42. outgain = 1.0f;
  43. }
  44. AnalogFilter::~AnalogFilter()
  45. {}
  46. void AnalogFilter::cleanup()
  47. {
  48. for(int i = 0; i < MAX_FILTER_STAGES + 1; ++i) {
  49. history[i].x1 = 0.0f;
  50. history[i].x2 = 0.0f;
  51. history[i].y1 = 0.0f;
  52. history[i].y2 = 0.0f;
  53. oldHistory[i] = history[i];
  54. }
  55. needsinterpolation = false;
  56. }
  57. AnalogFilter::Coeff AnalogFilter::computeCoeff(int type, float cutoff, float q,
  58. int stages, float gain, float fs, int &order)
  59. {
  60. AnalogFilter::Coeff coeff;
  61. bool zerocoefs = false; //this is used if the freq is too high
  62. const float samplerate_f = fs;
  63. const float halfsamplerate_f = fs/2;
  64. //do not allow frequencies bigger than samplerate/2
  65. float freq = cutoff;
  66. if(freq > (halfsamplerate_f - 500.0f)) {
  67. freq = halfsamplerate_f - 500.0f;
  68. zerocoefs = true;
  69. }
  70. if(freq < 0.1f)
  71. freq = 0.1f;
  72. //do not allow bogus Q
  73. if(q < 0.0f)
  74. q = 0.0f;
  75. float tmpq, tmpgain;
  76. if(stages == 0) {
  77. tmpq = q;
  78. tmpgain = gain;
  79. } else {
  80. tmpq = (q > 1.0f) ? powf(q, 1.0f / (stages + 1)) : q;
  81. tmpgain = powf(gain, 1.0f / (stages + 1));
  82. }
  83. //Alias Terms
  84. float *c = coeff.c;
  85. float *d = coeff.d;
  86. //General Constants
  87. const float omega = 2 * PI * freq / samplerate_f;
  88. const float sn = sinf(omega), cs = cosf(omega);
  89. float alpha, beta;
  90. //most of theese are implementations of
  91. //the "Cookbook formulae for audio EQ" by Robert Bristow-Johnson
  92. //The original location of the Cookbook is:
  93. //http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt
  94. float tmp;
  95. float tgp1;
  96. float tgm1;
  97. switch(type) {
  98. case 0: //LPF 1 pole
  99. if(!zerocoefs)
  100. tmp = expf(-2.0f * PI * freq / samplerate_f);
  101. else
  102. tmp = 0.0f;
  103. c[0] = 1.0f - tmp;
  104. c[1] = 0.0f;
  105. c[2] = 0.0f;
  106. d[1] = tmp;
  107. d[2] = 0.0f;
  108. order = 1;
  109. break;
  110. case 1: //HPF 1 pole
  111. if(!zerocoefs)
  112. tmp = expf(-2.0f * PI * freq / samplerate_f);
  113. else
  114. tmp = 0.0f;
  115. c[0] = (1.0f + tmp) / 2.0f;
  116. c[1] = -(1.0f + tmp) / 2.0f;
  117. c[2] = 0.0f;
  118. d[1] = tmp;
  119. d[2] = 0.0f;
  120. order = 1;
  121. break;
  122. case 2: //LPF 2 poles
  123. if(!zerocoefs) {
  124. alpha = sn / (2.0f * tmpq);
  125. tmp = 1 + alpha;
  126. c[1] = (1.0f - cs) / tmp;
  127. c[0] = c[2] = c[1] / 2.0f;
  128. d[1] = -2.0f * cs / tmp * -1.0f;
  129. d[2] = (1.0f - alpha) / tmp * -1.0f;
  130. }
  131. else {
  132. c[0] = 1.0f;
  133. c[1] = c[2] = d[1] = d[2] = 0.0f;
  134. }
  135. order = 2;
  136. break;
  137. case 3: //HPF 2 poles
  138. if(!zerocoefs) {
  139. alpha = sn / (2.0f * tmpq);
  140. tmp = 1 + alpha;
  141. c[0] = (1.0f + cs) / 2.0f / tmp;
  142. c[1] = -(1.0f + cs) / tmp;
  143. c[2] = (1.0f + cs) / 2.0f / tmp;
  144. d[1] = -2.0f * cs / tmp * -1.0f;
  145. d[2] = (1.0f - alpha) / tmp * -1.0f;
  146. }
  147. else
  148. c[0] = c[1] = c[2] = d[1] = d[2] = 0.0f;
  149. order = 2;
  150. break;
  151. case 4: //BPF 2 poles
  152. if(!zerocoefs) {
  153. alpha = sn / (2.0f * tmpq);
  154. tmp = 1.0f + alpha;
  155. c[0] = alpha / tmp *sqrtf(tmpq + 1.0f);
  156. c[1] = 0.0f;
  157. c[2] = -alpha / tmp *sqrtf(tmpq + 1.0f);
  158. d[1] = -2.0f * cs / tmp * -1.0f;
  159. d[2] = (1.0f - alpha) / tmp * -1.0f;
  160. }
  161. else
  162. c[0] = c[1] = c[2] = d[1] = d[2] = 0.0f;
  163. order = 2;
  164. break;
  165. case 5: //NOTCH 2 poles
  166. if(!zerocoefs) {
  167. alpha = sn / (2.0f * sqrtf(tmpq));
  168. tmp = 1.0f + alpha;
  169. c[0] = 1.0f / tmp;
  170. c[1] = -2.0f * cs / tmp;
  171. c[2] = 1.0f / tmp;
  172. d[1] = -2.0f * cs / tmp * -1.0f;
  173. d[2] = (1.0f - alpha) / tmp * -1.0f;
  174. }
  175. else {
  176. c[0] = 1.0f;
  177. c[1] = c[2] = d[1] = d[2] = 0.0f;
  178. }
  179. order = 2;
  180. break;
  181. case 6: //PEAK (2 poles)
  182. if(!zerocoefs) {
  183. tmpq *= 3.0f;
  184. alpha = sn / (2.0f * tmpq);
  185. tmp = 1.0f + alpha / tmpgain;
  186. c[0] = (1.0f + alpha * tmpgain) / tmp;
  187. c[1] = (-2.0f * cs) / tmp;
  188. c[2] = (1.0f - alpha * tmpgain) / tmp;
  189. d[1] = -2.0f * cs / tmp * -1.0f;
  190. d[2] = (1.0f - alpha / tmpgain) / tmp * -1.0f;
  191. }
  192. else {
  193. c[0] = 1.0f;
  194. c[1] = c[2] = d[1] = d[2] = 0.0f;
  195. }
  196. order = 2;
  197. break;
  198. case 7: //Low Shelf - 2 poles
  199. if(!zerocoefs) {
  200. tmpq = sqrtf(tmpq);
  201. beta = sqrtf(tmpgain) / tmpq;
  202. tgp1 = tmpgain + 1.0f;
  203. tgm1 = tmpgain - 1.0f;
  204. tmp = tgp1 + tgm1 * cs + beta * sn;
  205. c[0] = tmpgain * (tgp1 - tgm1 * cs + beta * sn) / tmp;
  206. c[1] = 2.0f * tmpgain * (tgm1 - tgp1 * cs) / tmp;
  207. c[2] = tmpgain * (tgp1 - tgm1 * cs - beta * sn) / tmp;
  208. d[1] = -2.0f * (tgm1 + tgp1 * cs) / tmp * -1.0f;
  209. d[2] = (tgp1 + tgm1 * cs - beta * sn) / tmp * -1.0f;
  210. }
  211. else {
  212. c[0] = tmpgain;
  213. c[1] = c[2] = d[1] = d[2] = 0.0f;
  214. }
  215. order = 2;
  216. break;
  217. case 8: //High Shelf - 2 poles
  218. if(!zerocoefs) {
  219. tmpq = sqrtf(tmpq);
  220. beta = sqrtf(tmpgain) / tmpq;
  221. tgp1 = tmpgain + 1.0f;
  222. tgm1 = tmpgain - 1.0f;
  223. tmp = tgp1 - tgm1 * cs + beta * sn;
  224. c[0] = tmpgain * (tgp1 + tgm1 * cs + beta * sn) / tmp;
  225. c[1] = -2.0f * tmpgain * (tgm1 + tgp1 * cs) / tmp;
  226. c[2] = tmpgain * (tgp1 + tgm1 * cs - beta * sn) / tmp;
  227. d[1] = 2.0f * (tgm1 - tgp1 * cs) / tmp * -1.0f;
  228. d[2] = (tgp1 - tgm1 * cs - beta * sn) / tmp * -1.0f;
  229. }
  230. else {
  231. c[0] = 1.0f;
  232. c[1] = c[2] = d[1] = d[2] = 0.0f;
  233. }
  234. order = 2;
  235. break;
  236. default: //wrong type
  237. assert(false && "wrong type for a filter");
  238. break;
  239. }
  240. return coeff;
  241. }
  242. void AnalogFilter::computefiltercoefs(void)
  243. {
  244. coeff = AnalogFilter::computeCoeff(type, freq, q, stages, gain,
  245. samplerate_f, order);
  246. }
  247. void AnalogFilter::setfreq(float frequency)
  248. {
  249. if(frequency < 0.1f)
  250. frequency = 0.1f;
  251. float rap = freq / frequency;
  252. if(rap < 1.0f)
  253. rap = 1.0f / rap;
  254. oldabovenq = abovenq;
  255. abovenq = frequency > (halfsamplerate_f - 500.0f);
  256. bool nyquistthresh = (abovenq ^ oldabovenq);
  257. //if the frequency is changed fast, it needs interpolation
  258. if((rap > 3.0f) || nyquistthresh) { //(now, filter and coeficients backup)
  259. oldCoeff = coeff;
  260. for(int i = 0; i < MAX_FILTER_STAGES + 1; ++i)
  261. oldHistory[i] = history[i];
  262. if(!firsttime)
  263. needsinterpolation = true;
  264. }
  265. freq = frequency;
  266. computefiltercoefs();
  267. firsttime = false;
  268. }
  269. void AnalogFilter::setfreq_and_q(float frequency, float q_)
  270. {
  271. q = q_;
  272. setfreq(frequency);
  273. }
  274. void AnalogFilter::setq(float q_)
  275. {
  276. q = q_;
  277. computefiltercoefs();
  278. }
  279. void AnalogFilter::settype(int type_)
  280. {
  281. type = type_;
  282. computefiltercoefs();
  283. }
  284. void AnalogFilter::setgain(float dBgain)
  285. {
  286. gain = dB2rap(dBgain);
  287. computefiltercoefs();
  288. }
  289. void AnalogFilter::setstages(int stages_)
  290. {
  291. if(stages_ >= MAX_FILTER_STAGES)
  292. stages_ = MAX_FILTER_STAGES - 1;
  293. if(stages_ != stages) {
  294. stages = stages_;
  295. cleanup();
  296. computefiltercoefs();
  297. }
  298. }
  299. inline void AnalogBiquadFilterA(const float coeff[5], float &src, float work[4])
  300. {
  301. work[3] = src*coeff[0]
  302. + work[0]*coeff[1]
  303. + work[1]*coeff[2]
  304. + work[2]*coeff[3]
  305. + work[3]*coeff[4];
  306. work[1] = src;
  307. src = work[3];
  308. }
  309. inline void AnalogBiquadFilterB(const float coeff[5], float &src, float work[4])
  310. {
  311. work[2] = src*coeff[0]
  312. + work[1]*coeff[1]
  313. + work[0]*coeff[2]
  314. + work[3]*coeff[3]
  315. + work[2]*coeff[4];
  316. work[0] = src;
  317. src = work[2];
  318. }
  319. void AnalogFilter::singlefilterout(float *smp, fstage &hist,
  320. const Coeff &coeff)
  321. {
  322. assert((buffersize % 8) == 0);
  323. if(order == 1) { //First order filter
  324. for(int i = 0; i < buffersize; ++i) {
  325. float y0 = smp[i] * coeff.c[0] + hist.x1 * coeff.c[1]
  326. + hist.y1 * coeff.d[1];
  327. hist.y1 = y0;
  328. hist.x1 = smp[i];
  329. smp[i] = y0;
  330. }
  331. } else if(order == 2) {//Second order filter
  332. const float coeff_[5] = {coeff.c[0], coeff.c[1], coeff.c[2], coeff.d[1], coeff.d[2]};
  333. float work[4] = {hist.x1, hist.x2, hist.y1, hist.y2};
  334. for(int i = 0; i < buffersize; i+=8) {
  335. AnalogBiquadFilterA(coeff_, smp[i + 0], work);
  336. AnalogBiquadFilterB(coeff_, smp[i + 1], work);
  337. AnalogBiquadFilterA(coeff_, smp[i + 2], work);
  338. AnalogBiquadFilterB(coeff_, smp[i + 3], work);
  339. AnalogBiquadFilterA(coeff_, smp[i + 4], work);
  340. AnalogBiquadFilterB(coeff_, smp[i + 5], work);
  341. AnalogBiquadFilterA(coeff_, smp[i + 6], work);
  342. AnalogBiquadFilterB(coeff_, smp[i + 7], work);
  343. }
  344. hist.x1 = work[0];
  345. hist.x2 = work[1];
  346. hist.y1 = work[2];
  347. hist.y2 = work[3];
  348. }
  349. }
  350. void AnalogFilter::filterout(float *smp)
  351. {
  352. for(int i = 0; i < stages + 1; ++i)
  353. singlefilterout(smp, history[i], coeff);
  354. if(needsinterpolation) {
  355. //Merge Filter at old coeff with new coeff
  356. float ismp[buffersize];
  357. memcpy(ismp, smp, bufferbytes);
  358. for(int i = 0; i < stages + 1; ++i)
  359. singlefilterout(ismp, oldHistory[i], oldCoeff);
  360. for(int i = 0; i < buffersize; ++i) {
  361. float x = (float)i / buffersize_f;
  362. smp[i] = ismp[i] * (1.0f - x) + smp[i] * x;
  363. }
  364. needsinterpolation = false;
  365. }
  366. for(int i = 0; i < buffersize; ++i)
  367. smp[i] *= outgain;
  368. }
  369. float AnalogFilter::H(float freq)
  370. {
  371. float fr = freq / samplerate_f * PI * 2.0f;
  372. float x = coeff.c[0], y = 0.0f;
  373. for(int n = 1; n < 3; ++n) {
  374. x += cosf(n * fr) * coeff.c[n];
  375. y -= sinf(n * fr) * coeff.c[n];
  376. }
  377. float h = x * x + y * y;
  378. x = 1.0f;
  379. y = 0.0f;
  380. for(int n = 1; n < 3; ++n) {
  381. x -= cosf(n * fr) * coeff.d[n];
  382. y += sinf(n * fr) * coeff.d[n];
  383. }
  384. h = h / (x * x + y * y);
  385. return powf(h, (stages + 1.0f) / 2.0f);
  386. }
  387. }