DspEffectLibrary.h 6.5 KB

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