EqFilter.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * eqfilter.cpp - defination of EqFilterclass.
  3. *
  4. * Copyright (c) 2014 David French <dave/dot/french3/at/googlemail/dot/com>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef EQFILTER_H
  25. #define EQFILTER_H
  26. #include "BasicFilters.h"
  27. #include "lmms_math.h"
  28. ///
  29. /// \brief The EqFilter class.
  30. /// A wrapper for the StereoBiQuad class, giving it freq, res, and gain controls.
  31. /// Used on a per channel per frame basis with recalculation of coefficents
  32. /// upon parameter changes. The intention is to use this as a bass class, children override
  33. /// the calcCoefficents() function, providing the coefficents a1, a2, b0, b1, b2.
  34. ///
  35. class EqFilter
  36. {
  37. public:
  38. EqFilter() :
  39. m_sampleRate(0),
  40. m_freq(0),
  41. m_res(0),
  42. m_gain(0),
  43. m_bw(0)
  44. {
  45. }
  46. virtual inline void setSampleRate( int sampleRate )
  47. {
  48. if( sampleRate != m_sampleRate )
  49. {
  50. m_sampleRate = sampleRate;
  51. calcCoefficents();
  52. }
  53. }
  54. virtual inline void setFrequency( float freq ){
  55. if ( freq != m_freq )
  56. {
  57. m_freq = freq;
  58. calcCoefficents();
  59. }
  60. }
  61. virtual inline void setQ( float res )
  62. {
  63. if ( res != m_res )
  64. {
  65. m_res = res;
  66. calcCoefficents();
  67. }
  68. }
  69. virtual inline void setGain( float gain )
  70. {
  71. if ( gain != m_gain )
  72. {
  73. m_gain = gain;
  74. calcCoefficents();
  75. }
  76. }
  77. virtual inline void setParameters( float sampleRate, float freq, float res, float gain )
  78. {
  79. bool hasChanged = ( sampleRate != m_sampleRate ||
  80. freq != m_freq ||
  81. res != m_res ||
  82. gain != m_gain );
  83. if ( hasChanged )
  84. {
  85. m_sampleRate = sampleRate;
  86. m_freq = freq;
  87. m_res = res;
  88. m_gain = gain;
  89. calcCoefficents();
  90. }
  91. }
  92. ///
  93. /// \brief update
  94. /// filters using two BiQuads, then crossfades,
  95. /// depending on on percentage of period processes
  96. /// \param in
  97. /// \param ch
  98. /// \param frameProgress percentage of frame processed
  99. /// \return
  100. ///
  101. inline float update( float in, ch_cnt_t ch, float frameProgress)
  102. {
  103. float initailF = m_biQuadFrameInitial.update( in, ch );
  104. float targetF = m_biQuadFrameTarget.update( in, ch );
  105. if(frameProgress > 0.99999 )
  106. {
  107. m_biQuadFrameInitial= m_biQuadFrameTarget;
  108. }
  109. return (1.0f-frameProgress) * initailF + frameProgress * targetF;
  110. }
  111. protected:
  112. ///
  113. /// \brief calcCoefficents
  114. /// Override this in child classes to provide the coefficents, based on
  115. /// Freq, Res and Gain
  116. virtual void calcCoefficents(){
  117. setCoeffs( 0, 0, 0, 0, 0 );
  118. }
  119. inline void setCoeffs( float a1, float a2, float b0, float b1, float b2 )
  120. {
  121. m_biQuadFrameTarget.setCoeffs( a1, a2, b0, b1, b2 );
  122. }
  123. float m_sampleRate;
  124. float m_freq;
  125. float m_res;
  126. float m_gain;
  127. float m_bw;
  128. StereoBiQuad m_biQuadFrameInitial;
  129. StereoBiQuad m_biQuadFrameTarget;
  130. };
  131. ///
  132. /// \brief The EqHp12Filter class
  133. /// A 2 pole High Pass Filter
  134. /// Coefficent calculations from http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  135. class EqHp12Filter : public EqFilter
  136. {
  137. public :
  138. virtual void calcCoefficents()
  139. {
  140. // calc intermediate
  141. float w0 = F_2PI * m_freq / m_sampleRate;
  142. float c = cosf( w0 );
  143. float s = sinf( w0 );
  144. float alpha = s / ( 2 * m_res );
  145. float a0, a1, a2, b0, b1, b2; // coeffs to calculate
  146. //calc coefficents
  147. b0 = ( 1 + c ) * 0.5;
  148. b1 = ( -( 1 + c ) );
  149. b2 = ( 1 + c ) * 0.5;
  150. a0 = 1 + alpha;
  151. a1 = ( -2 * c );
  152. a2 = 1 - alpha;
  153. //normalise
  154. b0 /= a0;
  155. b1 /= a0;
  156. b2 /= a0;
  157. a1 /= a0;
  158. a2 /= a0;
  159. a0 = 1;
  160. setCoeffs( a1, a2, b0, b1, b2 );
  161. }
  162. };
  163. ///
  164. /// \brief The EqLp12Filter class.
  165. /// A 2 pole low pass filter
  166. /// Coefficent calculations from http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  167. ///
  168. class EqLp12Filter : public EqFilter
  169. {
  170. public :
  171. virtual void calcCoefficents()
  172. {
  173. // calc intermediate
  174. float w0 = F_2PI * m_freq / m_sampleRate;
  175. float c = cosf( w0 );
  176. float s = sinf( w0 );
  177. float alpha = s / ( 2 * m_res );
  178. float a0, a1, a2, b0, b1, b2; // coeffs to calculate
  179. //calc coefficents
  180. b0 = ( 1 - c ) * 0.5;
  181. b1 = 1 - c;
  182. b2 = ( 1 - c ) * 0.5;
  183. a0 = 1 + alpha;
  184. a1 = -2 * c;
  185. a2 = 1 - alpha;
  186. //normalise
  187. b0 /= a0;
  188. b1 /= a0;
  189. b2 /= a0;
  190. a1 /= a0;
  191. a2 /= a0;
  192. a0 = 1;
  193. setCoeffs( a1, a2, b0, b1, b2 );
  194. }
  195. };
  196. ///
  197. /// \brief The EqPeakFilter class
  198. /// A Peak Filter
  199. /// Coefficent calculations from http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  200. ///
  201. class EqPeakFilter : public EqFilter
  202. {
  203. public:
  204. virtual void calcCoefficents()
  205. {
  206. // calc intermediate
  207. float w0 = F_2PI * m_freq / m_sampleRate;
  208. float c = cosf( w0 );
  209. float s = sinf( w0 );
  210. float A = pow( 10, m_gain * 0.025);
  211. float alpha = s * sinh( log( 2 ) / 2 * m_bw * w0 / sinf(w0) );
  212. float a0, a1, a2, b0, b1, b2; // coeffs to calculate
  213. //calc coefficents
  214. b0 = 1 + alpha*A;
  215. b1 = -2*c;
  216. b2 = 1 - alpha*A;
  217. a0 = 1 + alpha/A;
  218. a1 = -2*c;
  219. a2 = 1 - alpha/A;
  220. //normalise
  221. b0 /= a0;
  222. b1 /= a0;
  223. b2 /= a0;
  224. a1 /= a0;
  225. a2 /= a0;
  226. a0 = 1;
  227. setCoeffs( a1, a2, b0, b1, b2 );
  228. }
  229. virtual inline void setParameters( float sampleRate, float freq, float bw, float gain )
  230. {
  231. bool hasChanged = false;
  232. if( sampleRate != m_sampleRate )
  233. {
  234. m_sampleRate = sampleRate;
  235. hasChanged = true;
  236. }
  237. if ( freq != m_freq )
  238. {
  239. m_freq = freq;
  240. hasChanged = true;
  241. }
  242. if ( bw != m_bw )
  243. {
  244. m_bw = bw;
  245. hasChanged = true;
  246. }
  247. if ( gain != m_gain )
  248. {
  249. m_gain = gain;
  250. hasChanged = true;
  251. }
  252. if ( hasChanged ) { calcCoefficents(); }
  253. }
  254. };
  255. class EqLowShelfFilter : public EqFilter
  256. {
  257. public :
  258. virtual void calcCoefficents()
  259. {
  260. // calc intermediate
  261. float w0 = F_2PI * m_freq / m_sampleRate;
  262. float c = cosf( w0 );
  263. float s = sinf( w0 );
  264. float A = pow( 10, m_gain * 0.025);
  265. // float alpha = s / ( 2 * m_res );
  266. float beta = sqrt( A ) / m_res;
  267. float a0, a1, a2, b0, b1, b2; // coeffs to calculate
  268. //calc coefficents
  269. b0 = A * ( ( A+1 ) - ( A-1 ) * c + beta * s );
  270. b1 = 2 * A * ( ( A - 1 ) - ( A + 1 ) * c) ;
  271. b2 = A * ( ( A + 1 ) - ( A - 1 ) * c - beta * s);
  272. a0 = ( A + 1 ) + ( A - 1 ) * c + beta * s;
  273. a1 = -2 * ( ( A - 1 ) + ( A + 1 ) * c );
  274. a2 = ( A + 1 ) + ( A - 1) * c - beta * s;
  275. //normalise
  276. b0 /= a0;
  277. b1 /= a0;
  278. b2 /= a0;
  279. a1 /= a0;
  280. a2 /= a0;
  281. a0 = 1;
  282. setCoeffs( a1, a2, b0, b1, b2 );
  283. }
  284. };
  285. class EqHighShelfFilter : public EqFilter
  286. {
  287. public :
  288. virtual void calcCoefficents()
  289. {
  290. // calc intermediate
  291. float w0 = F_2PI * m_freq / m_sampleRate;
  292. float c = cosf( w0 );
  293. float s = sinf( w0 );
  294. float A = pow( 10, m_gain * 0.025 );
  295. float beta = sqrt( A ) / m_res;
  296. float a0, a1, a2, b0, b1, b2; // coeffs to calculate
  297. //calc coefficents
  298. b0 = A *( ( A +1 ) + ( A - 1 ) * c + beta * s);
  299. b1 = -2 * A * ( ( A - 1 ) + ( A + 1 ) * c );
  300. b2 = A * ( ( A + 1 ) + ( A - 1 ) * c - beta * s);
  301. a0 = ( A + 1 ) - ( A - 1 ) * c + beta * s;
  302. a1 = 2 * ( ( A - 1 ) - ( A + 1 ) * c );
  303. a2 = ( A + 1) - ( A - 1 ) * c - beta * s;
  304. //normalise
  305. b0 /= a0;
  306. b1 /= a0;
  307. b2 /= a0;
  308. a1 /= a0;
  309. a2 /= a0;
  310. a0 = 1;
  311. setCoeffs( a1, a2, b0, b1, b2 );
  312. }
  313. };
  314. class EqLinkwitzRiley : public StereoLinkwitzRiley
  315. {
  316. public:
  317. EqLinkwitzRiley() :
  318. StereoLinkwitzRiley( 44100),
  319. m_freq(0 ),
  320. m_sr( 1 )
  321. {
  322. }
  323. virtual inline void setSR( int sampleRate )
  324. {
  325. if( sampleRate != m_sr )
  326. {
  327. m_sr = sampleRate;
  328. setSampleRate( sampleRate );
  329. setLowpass(m_freq);
  330. }
  331. }
  332. virtual inline void setFrequency( float freq ){
  333. if ( freq != m_freq )
  334. {
  335. m_freq = freq;
  336. setLowpass(m_freq);
  337. }
  338. }
  339. virtual void processBuffer( sampleFrame* buf, const fpp_t frames )
  340. {
  341. for ( fpp_t f = 0 ; f < frames ; ++f)
  342. {
  343. buf[f][0] = update( buf[f][0] , 0);
  344. buf[f][1] = update( buf[f][1] , 1);
  345. }
  346. }
  347. protected:
  348. float m_freq;
  349. int m_sr;
  350. };
  351. #endif // EQFILTER_H