BasicFilters.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * BasicFilters.h - simple but powerful filter-class with most used filters
  3. *
  4. * original file by ???
  5. * modified and enhanced by Tobias Doerffel
  6. *
  7. * Lowpass_SV code originally from Nekobee, Copyright (C) 2004 Sean Bolton and others
  8. * adapted & modified for use in LMMS
  9. *
  10. * Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  11. *
  12. * This file is part of LMMS - https://lmms.io
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2 of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public
  25. * License along with this program (see COPYING); if not, write to the
  26. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  27. * Boston, MA 02110-1301 USA.
  28. *
  29. */
  30. #ifndef BASIC_FILTERS_H
  31. #define BASIC_FILTERS_H
  32. #ifndef __USE_XOPEN
  33. #define __USE_XOPEN
  34. #endif
  35. #include <math.h>
  36. #include "lmms_basics.h"
  37. #include "templates.h"
  38. #include "lmms_constants.h"
  39. #include "interpolation.h"
  40. #include "MemoryManager.h"
  41. template<ch_cnt_t CHANNELS> class BasicFilters;
  42. template<ch_cnt_t CHANNELS>
  43. class LinkwitzRiley
  44. {
  45. MM_OPERATORS
  46. public:
  47. LinkwitzRiley( float sampleRate )
  48. {
  49. m_sampleRate = sampleRate;
  50. clearHistory();
  51. }
  52. virtual ~LinkwitzRiley() {}
  53. inline void clearHistory()
  54. {
  55. for( int i = 0; i < CHANNELS; ++i )
  56. {
  57. m_z1[i] = m_z2[i] = m_z3[i] = m_z4[i] = 0.0f;
  58. }
  59. }
  60. inline void setSampleRate( float sampleRate )
  61. {
  62. m_sampleRate = sampleRate;
  63. }
  64. inline void setCoeffs( float freq )
  65. {
  66. // wc
  67. const double wc = D_2PI * freq;
  68. const double wc2 = wc * wc;
  69. const double wc3 = wc2 * wc;
  70. m_wc4 = wc2 * wc2;
  71. // k
  72. const double k = wc / tan( D_PI * freq / m_sampleRate );
  73. const double k2 = k * k;
  74. const double k3 = k2 * k;
  75. m_k4 = k2 * k2;
  76. // a
  77. static const double sqrt2 = sqrt( 2.0 );
  78. const double sq_tmp1 = sqrt2 * wc3 * k;
  79. const double sq_tmp2 = sqrt2 * wc * k3;
  80. m_a = 1.0 / ( 4.0 * wc2 * k2 + 2.0 * sq_tmp1 + m_k4 + 2.0 * sq_tmp2 + m_wc4 );
  81. // b
  82. m_b1 = ( 4.0 * ( m_wc4 + sq_tmp1 - m_k4 - sq_tmp2 ) ) * m_a;
  83. m_b2 = ( 6.0 * m_wc4 - 8.0 * wc2 * k2 + 6.0 * m_k4 ) * m_a;
  84. m_b3 = ( 4.0 * ( m_wc4 - sq_tmp1 + sq_tmp2 - m_k4 ) ) * m_a;
  85. m_b4 = ( m_k4 - 2.0 * sq_tmp1 + m_wc4 - 2.0 * sq_tmp2 + 4.0 * wc2 * k2 ) * m_a;
  86. }
  87. inline void setLowpass( float freq )
  88. {
  89. setCoeffs( freq );
  90. m_a0 = m_wc4 * m_a;
  91. m_a1 = 4.0 * m_a0;
  92. m_a2 = 6.0 * m_a0;
  93. }
  94. inline void setHighpass( float freq )
  95. {
  96. setCoeffs( freq );
  97. m_a0 = m_k4 * m_a;
  98. m_a1 = -4.0 * m_a0;
  99. m_a2 = 6.0 * m_a0;
  100. }
  101. inline float update( float in, ch_cnt_t ch )
  102. {
  103. const double x = in - ( m_z1[ch] * m_b1 ) - ( m_z2[ch] * m_b2 ) -
  104. ( m_z3[ch] * m_b3 ) - ( m_z4[ch] * m_b4 );
  105. const double y = ( m_a0 * x ) + ( m_z1[ch] * m_a1 ) + ( m_z2[ch] * m_a2 ) +
  106. ( m_z3[ch] * m_a1 ) + ( m_z4[ch] * m_a0 );
  107. m_z4[ch] = m_z3[ch];
  108. m_z3[ch] = m_z2[ch];
  109. m_z2[ch] = m_z1[ch];
  110. m_z1[ch] = x;
  111. return y;
  112. }
  113. private:
  114. float m_sampleRate;
  115. double m_wc4;
  116. double m_k4;
  117. double m_a, m_a0, m_a1, m_a2;
  118. double m_b1, m_b2, m_b3, m_b4;
  119. typedef double frame[CHANNELS];
  120. frame m_z1, m_z2, m_z3, m_z4;
  121. };
  122. typedef LinkwitzRiley<2> StereoLinkwitzRiley;
  123. template<ch_cnt_t CHANNELS>
  124. class BiQuad
  125. {
  126. MM_OPERATORS
  127. public:
  128. BiQuad()
  129. {
  130. clearHistory();
  131. }
  132. virtual ~BiQuad() {}
  133. inline void setCoeffs( float a1, float a2, float b0, float b1, float b2 )
  134. {
  135. m_a1 = a1;
  136. m_a2 = a2;
  137. m_b0 = b0;
  138. m_b1 = b1;
  139. m_b2 = b2;
  140. }
  141. inline void clearHistory()
  142. {
  143. for( int i = 0; i < CHANNELS; ++i )
  144. {
  145. m_z1[i] = 0.0f;
  146. m_z2[i] = 0.0f;
  147. }
  148. }
  149. inline float update( float in, ch_cnt_t ch )
  150. {
  151. // biquad filter in transposed form
  152. const float out = m_z1[ch] + m_b0 * in;
  153. m_z1[ch] = m_b1 * in + m_z2[ch] - m_a1 * out;
  154. m_z2[ch] = m_b2 * in - m_a2 * out;
  155. return out;
  156. }
  157. private:
  158. float m_a1, m_a2, m_b0, m_b1, m_b2;
  159. float m_z1 [CHANNELS], m_z2 [CHANNELS];
  160. friend class BasicFilters<CHANNELS>; // needed for subfilter stuff in BasicFilters
  161. };
  162. typedef BiQuad<2> StereoBiQuad;
  163. template<ch_cnt_t CHANNELS>
  164. class OnePole
  165. {
  166. MM_OPERATORS
  167. public:
  168. OnePole()
  169. {
  170. m_a0 = 1.0;
  171. m_b1 = 0.0;
  172. for( int i = 0; i < CHANNELS; ++i )
  173. {
  174. m_z1[i] = 0.0;
  175. }
  176. }
  177. virtual ~OnePole() {}
  178. inline void setCoeffs( float a0, float b1 )
  179. {
  180. m_a0 = a0;
  181. m_b1 = b1;
  182. }
  183. inline float update( float s, ch_cnt_t ch )
  184. {
  185. if( qAbs( s ) < 1.0e-10f && qAbs( m_z1[ch] ) < 1.0e-10f ) return 0.0f;
  186. return m_z1[ch] = s * m_a0 + m_z1[ch] * m_b1;
  187. }
  188. private:
  189. float m_a0, m_b1;
  190. float m_z1 [CHANNELS];
  191. };
  192. typedef OnePole<2> StereoOnePole;
  193. template<ch_cnt_t CHANNELS>
  194. class BasicFilters
  195. {
  196. MM_OPERATORS
  197. public:
  198. enum FilterTypes
  199. {
  200. LowPass,
  201. HiPass,
  202. BandPass_CSG,
  203. BandPass_CZPG,
  204. Notch,
  205. AllPass,
  206. Moog,
  207. DoubleLowPass,
  208. Lowpass_RC12,
  209. Bandpass_RC12,
  210. Highpass_RC12,
  211. Lowpass_RC24,
  212. Bandpass_RC24,
  213. Highpass_RC24,
  214. Formantfilter,
  215. DoubleMoog,
  216. Lowpass_SV,
  217. Bandpass_SV,
  218. Highpass_SV,
  219. Notch_SV,
  220. FastFormant,
  221. Tripole,
  222. NumFilters
  223. };
  224. static inline float minFreq()
  225. {
  226. return( 5.0f );
  227. }
  228. static inline float minQ()
  229. {
  230. return( 0.01f );
  231. }
  232. inline void setFilterType( const int _idx )
  233. {
  234. m_doubleFilter = _idx == DoubleLowPass || _idx == DoubleMoog;
  235. if( !m_doubleFilter )
  236. {
  237. m_type = static_cast<FilterTypes>( _idx );
  238. return;
  239. }
  240. // Double lowpass mode, backwards-compat for the goofy
  241. // Add-NumFilters to signify doubleFilter stuff
  242. m_type = _idx == DoubleLowPass
  243. ? LowPass
  244. : Moog;
  245. if( m_subFilter == NULL )
  246. {
  247. m_subFilter = new BasicFilters<CHANNELS>(
  248. static_cast<sample_rate_t>(
  249. m_sampleRate ) );
  250. }
  251. m_subFilter->m_type = m_type;
  252. }
  253. inline BasicFilters( const sample_rate_t _sample_rate ) :
  254. m_doubleFilter( false ),
  255. m_sampleRate( (float) _sample_rate ),
  256. m_sampleRatio( 1.0f / m_sampleRate ),
  257. m_subFilter( NULL )
  258. {
  259. clearHistory();
  260. }
  261. inline ~BasicFilters()
  262. {
  263. delete m_subFilter;
  264. }
  265. inline void clearHistory()
  266. {
  267. // reset in/out history for biquads
  268. m_biQuad.clearHistory();
  269. // reset in/out history
  270. for( ch_cnt_t _chnl = 0; _chnl < CHANNELS; ++_chnl )
  271. {
  272. // reset in/out history for moog-filter
  273. m_y1[_chnl] = m_y2[_chnl] = m_y3[_chnl] = m_y4[_chnl] =
  274. m_oldx[_chnl] = m_oldy1[_chnl] =
  275. m_oldy2[_chnl] = m_oldy3[_chnl] = 0.0f;
  276. // tripole
  277. m_last[_chnl] = 0.0f;
  278. // reset in/out history for RC-filters
  279. m_rclp0[_chnl] = m_rcbp0[_chnl] = m_rchp0[_chnl] = m_rclast0[_chnl] = 0.0f;
  280. m_rclp1[_chnl] = m_rcbp1[_chnl] = m_rchp1[_chnl] = m_rclast1[_chnl] = 0.0f;
  281. for(int i=0; i<6; i++)
  282. m_vfbp[i][_chnl] = m_vfhp[i][_chnl] = m_vflast[i][_chnl] = 0.0f;
  283. // reset in/out history for SV-filters
  284. m_delay1[_chnl] = 0.0f;
  285. m_delay2[_chnl] = 0.0f;
  286. m_delay3[_chnl] = 0.0f;
  287. m_delay4[_chnl] = 0.0f;
  288. }
  289. }
  290. inline sample_t update( sample_t _in0, ch_cnt_t _chnl )
  291. {
  292. sample_t out;
  293. switch( m_type )
  294. {
  295. case Moog:
  296. {
  297. sample_t x = _in0 - m_r*m_y4[_chnl];
  298. // four cascaded onepole filters
  299. // (bilinear transform)
  300. m_y1[_chnl] = qBound( -10.0f,
  301. ( x + m_oldx[_chnl] ) * m_p
  302. - m_k * m_y1[_chnl],
  303. 10.0f );
  304. m_y2[_chnl] = qBound( -10.0f,
  305. ( m_y1[_chnl] + m_oldy1[_chnl] ) * m_p
  306. - m_k * m_y2[_chnl],
  307. 10.0f );
  308. m_y3[_chnl] = qBound( -10.0f,
  309. ( m_y2[_chnl] + m_oldy2[_chnl] ) * m_p
  310. - m_k * m_y3[_chnl],
  311. 10.0f );
  312. m_y4[_chnl] = qBound( -10.0f,
  313. ( m_y3[_chnl] + m_oldy3[_chnl] ) * m_p
  314. - m_k * m_y4[_chnl],
  315. 10.0f );
  316. m_oldx[_chnl] = x;
  317. m_oldy1[_chnl] = m_y1[_chnl];
  318. m_oldy2[_chnl] = m_y2[_chnl];
  319. m_oldy3[_chnl] = m_y3[_chnl];
  320. out = m_y4[_chnl] - m_y4[_chnl] * m_y4[_chnl] *
  321. m_y4[_chnl] * ( 1.0f / 6.0f );
  322. break;
  323. }
  324. // 3x onepole filters with 4x oversampling and interpolation of oversampled signal:
  325. // input signal is linear-interpolated after oversampling, output signal is averaged from oversampled outputs
  326. case Tripole:
  327. {
  328. out = 0.0f;
  329. float ip = 0.0f;
  330. for( int i = 0; i < 4; ++i )
  331. {
  332. ip += 0.25f;
  333. sample_t x = linearInterpolate( m_last[_chnl], _in0, ip ) - m_r * m_y3[_chnl];
  334. m_y1[_chnl] = qBound( -10.0f,
  335. ( x + m_oldx[_chnl] ) * m_p
  336. - m_k * m_y1[_chnl],
  337. 10.0f );
  338. m_y2[_chnl] = qBound( -10.0f,
  339. ( m_y1[_chnl] + m_oldy1[_chnl] ) * m_p
  340. - m_k * m_y2[_chnl],
  341. 10.0f );
  342. m_y3[_chnl] = qBound( -10.0f,
  343. ( m_y2[_chnl] + m_oldy2[_chnl] ) * m_p
  344. - m_k * m_y3[_chnl],
  345. 10.0f );
  346. m_oldx[_chnl] = x;
  347. m_oldy1[_chnl] = m_y1[_chnl];
  348. m_oldy2[_chnl] = m_y2[_chnl];
  349. out += ( m_y3[_chnl] - m_y3[_chnl] * m_y3[_chnl] * m_y3[_chnl] * ( 1.0f / 6.0f ) );
  350. }
  351. out *= 0.25f;
  352. m_last[_chnl] = _in0;
  353. return out;
  354. }
  355. // 4-pole state-variant lowpass filter, adapted from Nekobee source code
  356. // and extended to other SV filter types
  357. // /* Hal Chamberlin's state variable filter */
  358. case Lowpass_SV:
  359. case Bandpass_SV:
  360. {
  361. float highpass;
  362. for( int i = 0; i < 2; ++i ) // 2x oversample
  363. {
  364. m_delay2[_chnl] = m_delay2[_chnl] + m_svf1 * m_delay1[_chnl]; /* delay2/4 = lowpass output */
  365. highpass = _in0 - m_delay2[_chnl] - m_svq * m_delay1[_chnl];
  366. m_delay1[_chnl] = m_svf1 * highpass + m_delay1[_chnl]; /* delay1/3 = bandpass output */
  367. m_delay4[_chnl] = m_delay4[_chnl] + m_svf2 * m_delay3[_chnl];
  368. highpass = m_delay2[_chnl] - m_delay4[_chnl] - m_svq * m_delay3[_chnl];
  369. m_delay3[_chnl] = m_svf2 * highpass + m_delay3[_chnl];
  370. }
  371. /* mix filter output into output buffer */
  372. return m_type == Lowpass_SV
  373. ? m_delay4[_chnl]
  374. : m_delay3[_chnl];
  375. }
  376. case Highpass_SV:
  377. {
  378. float hp;
  379. for( int i = 0; i < 2; ++i ) // 2x oversample
  380. {
  381. m_delay2[_chnl] = m_delay2[_chnl] + m_svf1 * m_delay1[_chnl];
  382. hp = _in0 - m_delay2[_chnl] - m_svq * m_delay1[_chnl];
  383. m_delay1[_chnl] = m_svf1 * hp + m_delay1[_chnl];
  384. }
  385. return hp;
  386. }
  387. case Notch_SV:
  388. {
  389. float hp1, hp2;
  390. for( int i = 0; i < 2; ++i ) // 2x oversample
  391. {
  392. m_delay2[_chnl] = m_delay2[_chnl] + m_svf1 * m_delay1[_chnl]; /* delay2/4 = lowpass output */
  393. hp1 = _in0 - m_delay2[_chnl] - m_svq * m_delay1[_chnl];
  394. m_delay1[_chnl] = m_svf1 * hp1 + m_delay1[_chnl]; /* delay1/3 = bandpass output */
  395. m_delay4[_chnl] = m_delay4[_chnl] + m_svf2 * m_delay3[_chnl];
  396. hp2 = m_delay2[_chnl] - m_delay4[_chnl] - m_svq * m_delay3[_chnl];
  397. m_delay3[_chnl] = m_svf2 * hp2 + m_delay3[_chnl];
  398. }
  399. /* mix filter output into output buffer */
  400. return m_delay4[_chnl] + hp1;
  401. }
  402. // 4-times oversampled simulation of an active RC-Bandpass,-Lowpass,-Highpass-
  403. // Filter-Network as it was used in nearly all modern analog synthesizers. This
  404. // can be driven up to self-oscillation (BTW: do not remove the limits!!!).
  405. // (C) 1998 ... 2009 S.Fendt. Released under the GPL v2.0 or any later version.
  406. case Lowpass_RC12:
  407. {
  408. sample_t lp, bp, hp, in;
  409. for( int n = 4; n != 0; --n )
  410. {
  411. in = _in0 + m_rcbp0[_chnl] * m_rcq;
  412. in = qBound( -1.0f, in, 1.0f );
  413. lp = in * m_rcb + m_rclp0[_chnl] * m_rca;
  414. lp = qBound( -1.0f, lp, 1.0f );
  415. hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
  416. hp = qBound( -1.0f, hp, 1.0f );
  417. bp = hp * m_rcb + m_rcbp0[_chnl] * m_rca;
  418. bp = qBound( -1.0f, bp, 1.0f );
  419. m_rclast0[_chnl] = in;
  420. m_rclp0[_chnl] = lp;
  421. m_rchp0[_chnl] = hp;
  422. m_rcbp0[_chnl] = bp;
  423. }
  424. return lp;
  425. }
  426. case Highpass_RC12:
  427. case Bandpass_RC12:
  428. {
  429. sample_t hp, bp, in;
  430. for( int n = 4; n != 0; --n )
  431. {
  432. in = _in0 + m_rcbp0[_chnl] * m_rcq;
  433. in = qBound( -1.0f, in, 1.0f );
  434. hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
  435. hp = qBound( -1.0f, hp, 1.0f );
  436. bp = hp * m_rcb + m_rcbp0[_chnl] * m_rca;
  437. bp = qBound( -1.0f, bp, 1.0f );
  438. m_rclast0[_chnl] = in;
  439. m_rchp0[_chnl] = hp;
  440. m_rcbp0[_chnl] = bp;
  441. }
  442. return m_type == Highpass_RC12 ? hp : bp;
  443. }
  444. case Lowpass_RC24:
  445. {
  446. sample_t lp, bp, hp, in;
  447. for( int n = 4; n != 0; --n )
  448. {
  449. // first stage is as for the 12dB case...
  450. in = _in0 + m_rcbp0[_chnl] * m_rcq;
  451. in = qBound( -1.0f, in, 1.0f );
  452. lp = in * m_rcb + m_rclp0[_chnl] * m_rca;
  453. lp = qBound( -1.0f, lp, 1.0f );
  454. hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
  455. hp = qBound( -1.0f, hp, 1.0f );
  456. bp = hp * m_rcb + m_rcbp0[_chnl] * m_rca;
  457. bp = qBound( -1.0f, bp, 1.0f );
  458. m_rclast0[_chnl] = in;
  459. m_rclp0[_chnl] = lp;
  460. m_rcbp0[_chnl] = bp;
  461. m_rchp0[_chnl] = hp;
  462. // second stage gets the output of the first stage as input...
  463. in = lp + m_rcbp1[_chnl] * m_rcq;
  464. in = qBound( -1.0f, in, 1.0f );
  465. lp = in * m_rcb + m_rclp1[_chnl] * m_rca;
  466. lp = qBound( -1.0f, lp, 1.0f );
  467. hp = m_rcc * ( m_rchp1[_chnl] + in - m_rclast1[_chnl] );
  468. hp = qBound( -1.0f, hp, 1.0f );
  469. bp = hp * m_rcb + m_rcbp1[_chnl] * m_rca;
  470. bp = qBound( -1.0f, bp, 1.0f );
  471. m_rclast1[_chnl] = in;
  472. m_rclp1[_chnl] = lp;
  473. m_rcbp1[_chnl] = bp;
  474. m_rchp1[_chnl] = hp;
  475. }
  476. return lp;
  477. }
  478. case Highpass_RC24:
  479. case Bandpass_RC24:
  480. {
  481. sample_t hp, bp, in;
  482. for( int n = 4; n != 0; --n )
  483. {
  484. // first stage is as for the 12dB case...
  485. in = _in0 + m_rcbp0[_chnl] * m_rcq;
  486. in = qBound( -1.0f, in, 1.0f );
  487. hp = m_rcc * ( m_rchp0[_chnl] + in - m_rclast0[_chnl] );
  488. hp = qBound( -1.0f, hp, 1.0f );
  489. bp = hp * m_rcb + m_rcbp0[_chnl] * m_rca;
  490. bp = qBound( -1.0f, bp, 1.0f );
  491. m_rclast0[_chnl] = in;
  492. m_rchp0[_chnl] = hp;
  493. m_rcbp0[_chnl] = bp;
  494. // second stage gets the output of the first stage as input...
  495. in = m_type == Highpass_RC24
  496. ? hp + m_rcbp1[_chnl] * m_rcq
  497. : bp + m_rcbp1[_chnl] * m_rcq;
  498. in = qBound( -1.0f, in, 1.0f );
  499. hp = m_rcc * ( m_rchp1[_chnl] + in - m_rclast1[_chnl] );
  500. hp = qBound( -1.0f, hp, 1.0f );
  501. bp = hp * m_rcb + m_rcbp1[_chnl] * m_rca;
  502. bp = qBound( -1.0f, bp, 1.0f );
  503. m_rclast1[_chnl] = in;
  504. m_rchp1[_chnl] = hp;
  505. m_rcbp1[_chnl] = bp;
  506. }
  507. return m_type == Highpass_RC24 ? hp : bp;
  508. }
  509. case Formantfilter:
  510. case FastFormant:
  511. {
  512. if( qAbs( _in0 ) < 1.0e-10f && qAbs( m_vflast[0][_chnl] ) < 1.0e-10f ) { return 0.0f; } // performance hack - skip processing when the numbers get too small
  513. sample_t hp, bp, in;
  514. out = 0;
  515. const int os = m_type == FastFormant ? 1 : 4; // no oversampling for fast formant
  516. for( int o = 0; o < os; ++o )
  517. {
  518. // first formant
  519. in = _in0 + m_vfbp[0][_chnl] * m_vfq;
  520. in = qBound( -1.0f, in, 1.0f );
  521. hp = m_vfc[0] * ( m_vfhp[0][_chnl] + in - m_vflast[0][_chnl] );
  522. hp = qBound( -1.0f, hp, 1.0f );
  523. bp = hp * m_vfb[0] + m_vfbp[0][_chnl] * m_vfa[0];
  524. bp = qBound( -1.0f, bp, 1.0f );
  525. m_vflast[0][_chnl] = in;
  526. m_vfhp[0][_chnl] = hp;
  527. m_vfbp[0][_chnl] = bp;
  528. in = bp + m_vfbp[2][_chnl] * m_vfq;
  529. in = qBound( -1.0f, in, 1.0f );
  530. hp = m_vfc[0] * ( m_vfhp[2][_chnl] + in - m_vflast[2][_chnl] );
  531. hp = qBound( -1.0f, hp, 1.0f );
  532. bp = hp * m_vfb[0] + m_vfbp[2][_chnl] * m_vfa[0];
  533. bp = qBound( -1.0f, bp, 1.0f );
  534. m_vflast[2][_chnl] = in;
  535. m_vfhp[2][_chnl] = hp;
  536. m_vfbp[2][_chnl] = bp;
  537. in = bp + m_vfbp[4][_chnl] * m_vfq;
  538. in = qBound( -1.0f, in, 1.0f );
  539. hp = m_vfc[0] * ( m_vfhp[4][_chnl] + in - m_vflast[4][_chnl] );
  540. hp = qBound( -1.0f, hp, 1.0f );
  541. bp = hp * m_vfb[0] + m_vfbp[4][_chnl] * m_vfa[0];
  542. bp = qBound( -1.0f, bp, 1.0f );
  543. m_vflast[4][_chnl] = in;
  544. m_vfhp[4][_chnl] = hp;
  545. m_vfbp[4][_chnl] = bp;
  546. out += bp;
  547. // second formant
  548. in = _in0 + m_vfbp[0][_chnl] * m_vfq;
  549. in = qBound( -1.0f, in, 1.0f );
  550. hp = m_vfc[1] * ( m_vfhp[1][_chnl] + in - m_vflast[1][_chnl] );
  551. hp = qBound( -1.0f, hp, 1.0f );
  552. bp = hp * m_vfb[1] + m_vfbp[1][_chnl] * m_vfa[1];
  553. bp = qBound( -1.0f, bp, 1.0f );
  554. m_vflast[1][_chnl] = in;
  555. m_vfhp[1][_chnl] = hp;
  556. m_vfbp[1][_chnl] = bp;
  557. in = bp + m_vfbp[3][_chnl] * m_vfq;
  558. in = qBound( -1.0f, in, 1.0f );
  559. hp = m_vfc[1] * ( m_vfhp[3][_chnl] + in - m_vflast[3][_chnl] );
  560. hp = qBound( -1.0f, hp, 1.0f );
  561. bp = hp * m_vfb[1] + m_vfbp[3][_chnl] * m_vfa[1];
  562. bp = qBound( -1.0f, bp, 1.0f );
  563. m_vflast[3][_chnl] = in;
  564. m_vfhp[3][_chnl] = hp;
  565. m_vfbp[3][_chnl] = bp;
  566. in = bp + m_vfbp[5][_chnl] * m_vfq;
  567. in = qBound( -1.0f, in, 1.0f );
  568. hp = m_vfc[1] * ( m_vfhp[5][_chnl] + in - m_vflast[5][_chnl] );
  569. hp = qBound( -1.0f, hp, 1.0f );
  570. bp = hp * m_vfb[1] + m_vfbp[5][_chnl] * m_vfa[1];
  571. bp = qBound( -1.0f, bp, 1.0f );
  572. m_vflast[5][_chnl] = in;
  573. m_vfhp[5][_chnl] = hp;
  574. m_vfbp[5][_chnl] = bp;
  575. out += bp;
  576. }
  577. return m_type == FastFormant ? out * 2.0f : out * 0.5f;
  578. }
  579. default:
  580. out = m_biQuad.update( _in0, _chnl );
  581. break;
  582. }
  583. if( m_doubleFilter )
  584. {
  585. return m_subFilter->update( out, _chnl );
  586. }
  587. // Clipper band limited sigmoid
  588. return out;
  589. }
  590. inline void calcFilterCoeffs( float _freq, float _q )
  591. {
  592. // temp coef vars
  593. _q = qMax( _q, minQ() );
  594. if( m_type == Lowpass_RC12 ||
  595. m_type == Bandpass_RC12 ||
  596. m_type == Highpass_RC12 ||
  597. m_type == Lowpass_RC24 ||
  598. m_type == Bandpass_RC24 ||
  599. m_type == Highpass_RC24 )
  600. {
  601. _freq = qBound( 50.0f, _freq, 20000.0f );
  602. const float sr = m_sampleRatio * 0.25f;
  603. const float f = 1.0f / ( _freq * F_2PI );
  604. m_rca = 1.0f - sr / ( f + sr );
  605. m_rcb = 1.0f - m_rca;
  606. m_rcc = f / ( f + sr );
  607. // Stretch Q/resonance, as self-oscillation reliably starts at a q of ~2.5 - ~2.6
  608. m_rcq = _q * 0.25f;
  609. return;
  610. }
  611. if( m_type == Formantfilter ||
  612. m_type == FastFormant )
  613. {
  614. _freq = qBound( minFreq(), _freq, 20000.0f ); // limit freq and q for not getting bad noise out of the filter...
  615. // formats for a, e, i, o, u, a
  616. static const float _f[6][2] = { { 1000, 1400 }, { 500, 2300 },
  617. { 320, 3200 },
  618. { 500, 1000 },
  619. { 320, 800 },
  620. { 1000, 1400 } };
  621. static const float freqRatio = 4.0f / 14000.0f;
  622. // Stretch Q/resonance
  623. m_vfq = _q * 0.25f;
  624. // frequency in lmms ranges from 1Hz to 14000Hz
  625. const float vowelf = _freq * freqRatio;
  626. const int vowel = static_cast<int>( vowelf );
  627. const float fract = vowelf - vowel;
  628. // interpolate between formant frequencies
  629. const float f0 = 1.0f / ( linearInterpolate( _f[vowel+0][0], _f[vowel+1][0], fract ) * F_2PI );
  630. const float f1 = 1.0f / ( linearInterpolate( _f[vowel+0][1], _f[vowel+1][1], fract ) * F_2PI );
  631. // samplerate coeff: depends on oversampling
  632. const float sr = m_type == FastFormant ? m_sampleRatio : m_sampleRatio * 0.25f;
  633. m_vfa[0] = 1.0f - sr / ( f0 + sr );
  634. m_vfb[0] = 1.0f - m_vfa[0];
  635. m_vfc[0] = f0 / ( f0 + sr );
  636. m_vfa[1] = 1.0f - sr / ( f1 + sr );
  637. m_vfb[1] = 1.0f - m_vfa[1];
  638. m_vfc[1] = f1 / ( f1 + sr );
  639. return;
  640. }
  641. if( m_type == Moog ||
  642. m_type == DoubleMoog )
  643. {
  644. // [ 0 - 0.5 ]
  645. const float f = qBound( minFreq(), _freq, 20000.0f ) * m_sampleRatio;
  646. // (Empirical tunning)
  647. m_p = ( 3.6f - 3.2f * f ) * f;
  648. m_k = 2.0f * m_p - 1;
  649. m_r = _q * powf( F_E, ( 1 - m_p ) * 1.386249f );
  650. if( m_doubleFilter )
  651. {
  652. m_subFilter->m_r = m_r;
  653. m_subFilter->m_p = m_p;
  654. m_subFilter->m_k = m_k;
  655. }
  656. return;
  657. }
  658. if( m_type == Tripole )
  659. {
  660. const float f = qBound( 20.0f, _freq, 20000.0f ) * m_sampleRatio * 0.25f;
  661. m_p = ( 3.6f - 3.2f * f ) * f;
  662. m_k = 2.0f * m_p - 1.0f;
  663. m_r = _q * 0.1f * powf( F_E, ( 1 - m_p ) * 1.386249f );
  664. return;
  665. }
  666. if( m_type == Lowpass_SV ||
  667. m_type == Bandpass_SV ||
  668. m_type == Highpass_SV ||
  669. m_type == Notch_SV )
  670. {
  671. const float f = sinf( qMax( minFreq(), _freq ) * m_sampleRatio * F_PI );
  672. m_svf1 = qMin( f, 0.825f );
  673. m_svf2 = qMin( f * 2.0f, 0.825f );
  674. m_svq = qMax( 0.0001f, 2.0f - ( _q * 0.1995f ) );
  675. return;
  676. }
  677. // other filters
  678. _freq = qBound( minFreq(), _freq, 20000.0f );
  679. const float omega = F_2PI * _freq * m_sampleRatio;
  680. const float tsin = sinf( omega ) * 0.5f;
  681. const float tcos = cosf( omega );
  682. const float alpha = tsin / _q;
  683. const float a0 = 1.0f / ( 1.0f + alpha );
  684. const float a1 = -2.0f * tcos * a0;
  685. const float a2 = ( 1.0f - alpha ) * a0;
  686. switch( m_type )
  687. {
  688. case LowPass:
  689. {
  690. const float b1 = ( 1.0f - tcos ) * a0;
  691. const float b0 = b1 * 0.5f;
  692. m_biQuad.setCoeffs( a1, a2, b0, b1, b0 );
  693. break;
  694. }
  695. case HiPass:
  696. {
  697. const float b1 = ( -1.0f - tcos ) * a0;
  698. const float b0 = b1 * -0.5f;
  699. m_biQuad.setCoeffs( a1, a2, b0, b1, b0 );
  700. break;
  701. }
  702. case BandPass_CSG:
  703. {
  704. const float b0 = tsin * a0;
  705. m_biQuad.setCoeffs( a1, a2, b0, 0.0f, -b0 );
  706. break;
  707. }
  708. case BandPass_CZPG:
  709. {
  710. const float b0 = alpha * a0;
  711. m_biQuad.setCoeffs( a1, a2, b0, 0.0f, -b0 );
  712. break;
  713. }
  714. case Notch:
  715. {
  716. m_biQuad.setCoeffs( a1, a2, a0, a1, a0 );
  717. break;
  718. }
  719. case AllPass:
  720. {
  721. m_biQuad.setCoeffs( a1, a2, a2, a1, 1.0f );
  722. break;
  723. }
  724. default:
  725. break;
  726. }
  727. if( m_doubleFilter )
  728. {
  729. m_subFilter->m_biQuad.setCoeffs( m_biQuad.m_a1, m_biQuad.m_a2, m_biQuad.m_b0, m_biQuad.m_b1, m_biQuad.m_b2 );
  730. }
  731. }
  732. private:
  733. // biquad filter
  734. BiQuad<CHANNELS> m_biQuad;
  735. // coeffs for moog-filter
  736. float m_r, m_p, m_k;
  737. // coeffs for RC-type-filters
  738. float m_rca, m_rcb, m_rcc, m_rcq;
  739. // coeffs for formant-filters
  740. float m_vfa[4], m_vfb[4], m_vfc[4], m_vfq;
  741. // coeffs for Lowpass_SV (state-variant lowpass)
  742. float m_svf1, m_svf2, m_svq;
  743. typedef sample_t frame[CHANNELS];
  744. // in/out history for moog-filter
  745. frame m_y1, m_y2, m_y3, m_y4, m_oldx, m_oldy1, m_oldy2, m_oldy3;
  746. // additional one for Tripole filter
  747. frame m_last;
  748. // in/out history for RC-type-filters
  749. frame m_rcbp0, m_rclp0, m_rchp0, m_rclast0;
  750. frame m_rcbp1, m_rclp1, m_rchp1, m_rclast1;
  751. // in/out history for Formant-filters
  752. frame m_vfbp[6], m_vfhp[6], m_vflast[6];
  753. // in/out history for Lowpass_SV (state-variant lowpass)
  754. frame m_delay1, m_delay2, m_delay3, m_delay4;
  755. FilterTypes m_type;
  756. bool m_doubleFilter;
  757. float m_sampleRate;
  758. float m_sampleRatio;
  759. BasicFilters<CHANNELS> * m_subFilter;
  760. } ;
  761. #endif