DspEffectLibrary.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * DspEffectLibrary.h - library with template-based inline-effects
  3. *
  4. * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  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 DSP_EFFECT_LIBRARY_H
  25. #define DSP_EFFECT_LIBRARY_H
  26. #include "lmms_math.h"
  27. #include "templates.h"
  28. #include "lmms_constants.h"
  29. #include "lmms_basics.h"
  30. namespace DspEffectLibrary
  31. {
  32. template<typename T>
  33. class MonoBase
  34. {
  35. public:
  36. typedef class MonoBypass bypassType;
  37. static void process( sample_t * * _buf, const f_cnt_t _frames )
  38. {
  39. for( f_cnt_t f = 0; f < _frames; ++f )
  40. {
  41. _buf[f][0] = T::nextSample( _buf[f][0] );
  42. }
  43. }
  44. } ;
  45. template<typename T>
  46. class StereoBase
  47. {
  48. public:
  49. typedef class StereoBypass bypassType;
  50. static void process( sample_t * * _buf, const f_cnt_t _frames )
  51. {
  52. for( f_cnt_t f = 0; f < _frames; ++f )
  53. {
  54. T::nextSample( _buf[f][0], _buf[f][1] );
  55. }
  56. }
  57. } ;
  58. template<class FXL, class FXR = FXL>
  59. class MonoToStereoAdaptor : public StereoBase<MonoToStereoAdaptor<FXL, FXR> >
  60. {
  61. public:
  62. MonoToStereoAdaptor( const FXL& monoFX ) :
  63. m_leftFX( monoFX ),
  64. m_rightFX( monoFX )
  65. {
  66. }
  67. MonoToStereoAdaptor( const FXL& leftFX, const FXR& rightFX ) :
  68. m_leftFX( leftFX ),
  69. m_rightFX( rightFX )
  70. {
  71. }
  72. void nextSample( sample_t& inLeft, sample_t& inRight )
  73. {
  74. inLeft = m_leftFX.nextSample( inLeft );
  75. inRight = m_rightFX.nextSample( inRight );
  76. }
  77. FXL& leftFX()
  78. {
  79. return( m_leftFX );
  80. }
  81. FXR& rightFX()
  82. {
  83. return( m_rightFX );
  84. }
  85. private:
  86. FXL m_leftFX;
  87. FXR m_rightFX;
  88. } ;
  89. template<class FX>
  90. class StereoToMonoAdaptor : public MonoBase<StereoToMonoAdaptor<FX> >
  91. {
  92. public:
  93. StereoToMonoAdaptor( const FX& fx ) :
  94. m_FX( fx )
  95. {
  96. }
  97. sample_t nextSample( sample_t in )
  98. {
  99. sample_t s[2] = { in, in };
  100. m_FX.nextSample( s[0], s[1] );
  101. return ( s[0] + s[1] ) / 2.0f;
  102. }
  103. private:
  104. FX m_FX;
  105. } ;
  106. class MonoBypass : public MonoBase<MonoBypass>
  107. {
  108. public:
  109. sample_t nextSample( sample_t in )
  110. {
  111. return in;
  112. }
  113. } ;
  114. class StereoBypass : public StereoBase<StereoBypass>
  115. {
  116. public:
  117. void nextSample( sample_t&, sample_t& )
  118. {
  119. }
  120. } ;
  121. /* convenient class to build up static FX chains, for example
  122. using namespace DspEffectLib;
  123. chain<MonoToStereoAdaptor<bassBoost<> >,
  124. chain<StereoEnhancer<>,
  125. MonoToStereoAdaptor<FoldbackDistortion<> > > >
  126. fxchain( bassBoost<>( 60.0, 1.0, 4.0f ),
  127. chain<StereoEnhancer<>,
  128. MonoToStereoAdaptor<FoldbackDistortion<> > >(
  129. StereoEnhancer<>( 1.0 ),
  130. FoldbackDistortion<>( 1.0f, 1.0f ) ) );
  131. // now you can do simple calls such as which will process a bass-boost-,
  132. // stereo enhancer- and foldback distortion effect on your buffer
  133. fx_chain.process( (sample_t * *) buf, frames );
  134. */
  135. template<class FX0, class FX1 = typename FX0::bypassType>
  136. class Chain : public FX0::bypassType
  137. {
  138. public:
  139. typedef typename FX0::sample_t sample_t;
  140. Chain( const FX0& fx0, const FX1& fx1 = FX1() ) :
  141. m_FX0( fx0 ),
  142. m_FX1( fx1 )
  143. {
  144. }
  145. void process( sample_t** buf, const f_cnt_t frames )
  146. {
  147. m_FX0.process( buf, frames );
  148. m_FX1.process( buf, frames );
  149. }
  150. private:
  151. FX0 m_FX0;
  152. FX1 m_FX1;
  153. } ;
  154. template<typename sample_t>
  155. inline sample_t saturate( sample_t x )
  156. {
  157. return qMin<sample_t>( qMax<sample_t>( -1.0f, x ), 1.0f );
  158. }
  159. class FastBassBoost : public MonoBase<FastBassBoost>
  160. {
  161. public:
  162. FastBassBoost( const sample_t _frequency,
  163. const sample_t _gain,
  164. const sample_t _ratio,
  165. const FastBassBoost & _orig = FastBassBoost() ) :
  166. m_frequency( qMax<sample_t>( _frequency, 10.0 ) ),
  167. m_gain1( 1.0 / ( m_frequency + 1.0 ) ),
  168. m_gain2( _gain ),
  169. m_ratio( _ratio ),
  170. m_cap( _orig.m_cap )
  171. {
  172. }
  173. inline sample_t nextSample( sample_t _in )
  174. {
  175. // TODO: somehow remove these horrible aliases...
  176. m_cap = ( _in + m_cap*m_frequency ) * m_gain1;
  177. return( ( _in + m_cap*m_ratio ) * m_gain2 );
  178. }
  179. void setFrequency( const sample_t _frequency )
  180. {
  181. m_frequency = _frequency;
  182. m_gain1 = 1.0 / ( m_frequency + 1.0 );
  183. }
  184. void setGain( const sample_t _gain )
  185. {
  186. m_gain2 = _gain;
  187. }
  188. void setRatio( const sample_t _ratio )
  189. {
  190. m_ratio = _ratio;
  191. }
  192. private:
  193. FastBassBoost() :
  194. m_cap( 0.0 )
  195. {
  196. }
  197. sample_t m_frequency;
  198. sample_t m_gain1;
  199. sample_t m_gain2;
  200. sample_t m_ratio;
  201. sample_t m_cap;
  202. } ;
  203. template<class T>
  204. class DistortionBase : public MonoBase<T>
  205. {
  206. public:
  207. DistortionBase( float threshold, float gain ) :
  208. m_threshold( threshold ),
  209. m_gain( gain )
  210. {
  211. }
  212. void setThreshold( float threshold )
  213. {
  214. m_threshold = threshold;
  215. }
  216. void setGain( float gain )
  217. {
  218. m_gain = gain;
  219. }
  220. protected:
  221. float m_threshold;
  222. float m_gain;
  223. };
  224. class FoldbackDistortion : public DistortionBase<FoldbackDistortion>
  225. {
  226. public:
  227. using DistortionBase<FoldbackDistortion>::DistortionBase;
  228. sample_t nextSample( sample_t in )
  229. {
  230. if( in >= m_threshold || in < -m_threshold )
  231. {
  232. return ( fabsf( fabsf( fmodf( in - m_threshold, m_threshold*4 ) ) - m_threshold*2 ) - m_threshold ) * m_gain;
  233. }
  234. return in * m_gain;
  235. }
  236. } ;
  237. class Distortion : public DistortionBase<Distortion>
  238. {
  239. public:
  240. using DistortionBase<Distortion>::DistortionBase;
  241. sample_t nextSample( sample_t in )
  242. {
  243. return m_gain * ( in * ( fabsf( in )+m_threshold ) / ( in*in +( m_threshold-1 )* fabsf( in ) + 1 ) );
  244. }
  245. } ;
  246. class StereoEnhancer : public StereoBase<StereoEnhancer>
  247. {
  248. public:
  249. StereoEnhancer( float wideCoeff ) :
  250. m_wideCoeff( wideCoeff )
  251. {
  252. }
  253. void setWideCoeff( float wideCoeff )
  254. {
  255. m_wideCoeff = wideCoeff;
  256. }
  257. float wideCoeff()
  258. {
  259. return m_wideCoeff;
  260. }
  261. void nextSample( sample_t& inLeft, sample_t& inRight )
  262. {
  263. const float toRad = F_PI / 180;
  264. const sample_t tmp = inLeft;
  265. inLeft += inRight * sinf( m_wideCoeff * ( .5 * toRad ) );
  266. inRight -= tmp * sinf( m_wideCoeff * ( .5 * toRad ) );
  267. }
  268. private:
  269. float m_wideCoeff;
  270. } ;
  271. } ;
  272. #endif