EqEffect.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * eqeffect.cpp - defination of EqEffect class.
  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. #include "EqEffect.h"
  25. #include "Engine.h"
  26. #include "EqFader.h"
  27. #include "interpolation.h"
  28. #include "lmms_math.h"
  29. #include "embed.h"
  30. #include "plugin_export.h"
  31. extern "C"
  32. {
  33. Plugin::Descriptor PLUGIN_EXPORT eq_plugin_descriptor =
  34. {
  35. STRINGIFY( PLUGIN_NAME ),
  36. "Equalizer",
  37. QT_TRANSLATE_NOOP( "PluginBrowser", "A native eq plugin" ),
  38. "Dave French <contact/dot/dave/dot/french3/at/googlemail/dot/com>",
  39. 0x0100,
  40. Plugin::Effect,
  41. new PluginPixmapLoader("logo"),
  42. NULL,
  43. NULL
  44. } ;
  45. }
  46. EqEffect::EqEffect( Model *parent, const Plugin::Descriptor::SubPluginFeatures::Key *key) :
  47. Effect( &eq_plugin_descriptor, parent, key ),
  48. m_eqControls( this ),
  49. m_inGain( 1.0 ),
  50. m_outGain( 1.0 )
  51. {
  52. }
  53. EqEffect::~EqEffect()
  54. {
  55. }
  56. bool EqEffect::processAudioBuffer( sampleFrame *buf, const fpp_t frames )
  57. {
  58. const int sampleRate = Engine::mixer()->processingSampleRate();
  59. //wet/dry controls
  60. const float dry = dryLevel();
  61. const float wet = wetLevel();
  62. sample_t dryS[2];
  63. // setup sample exact controls
  64. float hpRes = m_eqControls.m_hpResModel.value();
  65. float lowShelfRes = m_eqControls.m_lowShelfResModel.value();
  66. float para1Bw = m_eqControls.m_para1BwModel.value();
  67. float para2Bw = m_eqControls.m_para2BwModel.value();
  68. float para3Bw = m_eqControls.m_para3BwModel.value();
  69. float para4Bw = m_eqControls.m_para4BwModel.value();
  70. float highShelfRes = m_eqControls.m_highShelfResModel.value();
  71. float lpRes = m_eqControls.m_lpResModel.value();
  72. float hpFreq = m_eqControls.m_hpFeqModel.value();
  73. float lowShelfFreq = m_eqControls.m_lowShelfFreqModel.value();
  74. float para1Freq = m_eqControls.m_para1FreqModel.value();
  75. float para2Freq = m_eqControls.m_para2FreqModel.value();
  76. float para3Freq = m_eqControls.m_para3FreqModel.value();
  77. float para4Freq = m_eqControls.m_para4FreqModel.value();
  78. float highShelfFreq = m_eqControls.m_highShelfFreqModel.value();
  79. float lpFreq = m_eqControls.m_lpFreqModel.value();
  80. bool hpActive = m_eqControls.m_hpActiveModel.value();
  81. bool hp24Active = m_eqControls.m_hp24Model.value();
  82. bool hp48Active = m_eqControls.m_hp48Model.value();
  83. bool lowShelfActive = m_eqControls.m_lowShelfActiveModel.value();
  84. bool para1Active = m_eqControls.m_para1ActiveModel.value();
  85. bool para2Active = m_eqControls.m_para2ActiveModel.value();
  86. bool para3Active = m_eqControls.m_para3ActiveModel.value();
  87. bool para4Active = m_eqControls.m_para4ActiveModel.value();
  88. bool highShelfActive = m_eqControls.m_highShelfActiveModel.value();
  89. bool lpActive = m_eqControls.m_lpActiveModel.value();
  90. bool lp24Active = m_eqControls.m_lp24Model.value();
  91. bool lp48Active = m_eqControls.m_lp48Model.value();
  92. float lowShelfGain = m_eqControls.m_lowShelfGainModel.value();
  93. float para1Gain = m_eqControls.m_para1GainModel.value();
  94. float para2Gain = m_eqControls.m_para2GainModel.value();
  95. float para3Gain = m_eqControls.m_para3GainModel.value();
  96. float para4Gain = m_eqControls.m_para4GainModel.value();
  97. float highShelfGain = m_eqControls.m_highShelfGainModel.value();
  98. //set all filter parameters once per frame, EqFilter handles
  99. //smooth xfading, reducing pops clicks and dc bias offsets
  100. m_hp12.setParameters( sampleRate, hpFreq, hpRes, 1 );
  101. m_hp24.setParameters( sampleRate, hpFreq, hpRes, 1 );
  102. m_hp480.setParameters( sampleRate, hpFreq, hpRes, 1 );
  103. m_hp481.setParameters( sampleRate, hpFreq, hpRes, 1 );
  104. m_lowShelf.setParameters( sampleRate, lowShelfFreq, lowShelfRes, lowShelfGain );
  105. m_para1.setParameters( sampleRate, para1Freq, para1Bw, para1Gain );
  106. m_para2.setParameters( sampleRate, para2Freq, para2Bw, para2Gain );
  107. m_para3.setParameters( sampleRate, para3Freq, para3Bw, para3Gain );
  108. m_para4.setParameters( sampleRate, para4Freq, para4Bw, para4Gain );
  109. m_highShelf.setParameters( sampleRate, highShelfFreq, highShelfRes, highShelfGain );
  110. m_lp12.setParameters( sampleRate, lpFreq, lpRes, 1 );
  111. m_lp24.setParameters( sampleRate, lpFreq, lpRes, 1 );
  112. m_lp480.setParameters( sampleRate, lpFreq, lpRes, 1 );
  113. m_lp481.setParameters( sampleRate, lpFreq, lpRes, 1 );
  114. if( !isEnabled() || !isRunning () )
  115. {
  116. return( false );
  117. }
  118. if( m_eqControls.m_outGainModel.isValueChanged() )
  119. {
  120. m_outGain = dbfsToAmp(m_eqControls.m_outGainModel.value());
  121. }
  122. if( m_eqControls.m_inGainModel.isValueChanged() )
  123. {
  124. m_inGain = dbfsToAmp(m_eqControls.m_inGainModel.value());
  125. }
  126. m_eqControls.m_inProgress = true;
  127. double outSum = 0.0;
  128. for( fpp_t f = 0; f < frames; ++f )
  129. {
  130. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  131. }
  132. const float outGain = m_outGain;
  133. sampleFrame m_inPeak = { 0, 0 };
  134. if(m_eqControls.m_analyseInModel.value( true ) && outSum > 0 && m_eqControls.isViewVisible() )
  135. {
  136. m_eqControls.m_inFftBands.analyze( buf, frames );
  137. }
  138. else
  139. {
  140. m_eqControls.m_inFftBands.clear();
  141. }
  142. gain( buf, frames, m_inGain, &m_inPeak );
  143. m_eqControls.m_inPeakL = m_eqControls.m_inPeakL < m_inPeak[0] ? m_inPeak[0] : m_eqControls.m_inPeakL;
  144. m_eqControls.m_inPeakR = m_eqControls.m_inPeakR < m_inPeak[1] ? m_inPeak[1] : m_eqControls.m_inPeakR;
  145. float periodProgress = 0.0f; // percentage of period processed
  146. for( fpp_t f = 0; f < frames; ++f)
  147. {
  148. periodProgress = (float)f / (float)(frames-1);
  149. //wet dry buffer
  150. dryS[0] = buf[f][0];
  151. dryS[1] = buf[f][1];
  152. if( hpActive )
  153. {
  154. buf[f][0] = m_hp12.update( buf[f][0], 0, periodProgress );
  155. buf[f][1] = m_hp12.update( buf[f][1], 1, periodProgress );
  156. if( hp24Active || hp48Active )
  157. {
  158. buf[f][0] = m_hp24.update( buf[f][0], 0, periodProgress );
  159. buf[f][1] = m_hp24.update( buf[f][1], 1, periodProgress );
  160. }
  161. if( hp48Active )
  162. {
  163. buf[f][0] = m_hp480.update( buf[f][0], 0, periodProgress );
  164. buf[f][1] = m_hp480.update( buf[f][1], 1, periodProgress );
  165. buf[f][0] = m_hp481.update( buf[f][0], 0, periodProgress );
  166. buf[f][1] = m_hp481.update( buf[f][1], 1, periodProgress );
  167. }
  168. }
  169. if( lowShelfActive )
  170. {
  171. buf[f][0] = m_lowShelf.update( buf[f][0], 0, periodProgress );
  172. buf[f][1] = m_lowShelf.update( buf[f][1], 1, periodProgress );
  173. }
  174. if( para1Active )
  175. {
  176. buf[f][0] = m_para1.update( buf[f][0], 0, periodProgress );
  177. buf[f][1] = m_para1.update( buf[f][1], 1, periodProgress );
  178. }
  179. if( para2Active )
  180. {
  181. buf[f][0] = m_para2.update( buf[f][0], 0, periodProgress );
  182. buf[f][1] = m_para2.update( buf[f][1], 1, periodProgress );
  183. }
  184. if( para3Active )
  185. {
  186. buf[f][0] = m_para3.update( buf[f][0], 0, periodProgress );
  187. buf[f][1] = m_para3.update( buf[f][1], 1, periodProgress );
  188. }
  189. if( para4Active )
  190. {
  191. buf[f][0] = m_para4.update( buf[f][0], 0, periodProgress );
  192. buf[f][1] = m_para4.update( buf[f][1], 1, periodProgress );
  193. }
  194. if( highShelfActive )
  195. {
  196. buf[f][0] = m_highShelf.update( buf[f][0], 0, periodProgress );
  197. buf[f][1] = m_highShelf.update( buf[f][1], 1, periodProgress );
  198. }
  199. if( lpActive ){
  200. buf[f][0] = m_lp12.update( buf[f][0], 0, periodProgress );
  201. buf[f][1] = m_lp12.update( buf[f][1], 1, periodProgress );
  202. if( lp24Active || lp48Active )
  203. {
  204. buf[f][0] = m_lp24.update( buf[f][0], 0, periodProgress );
  205. buf[f][1] = m_lp24.update( buf[f][1], 1, periodProgress );
  206. }
  207. if( lp48Active )
  208. {
  209. buf[f][0] = m_lp480.update( buf[f][0], 0, periodProgress );
  210. buf[f][1] = m_lp480.update( buf[f][1], 1, periodProgress );
  211. buf[f][0] = m_lp481.update( buf[f][0], 0, periodProgress );
  212. buf[f][1] = m_lp481.update( buf[f][1], 1, periodProgress );
  213. }
  214. }
  215. //apply wet / dry levels
  216. buf[f][1] = ( dry * dryS[1] ) + ( wet * buf[f][1] );
  217. buf[f][0] = ( dry * dryS[0] ) + ( wet * buf[f][0] );
  218. }
  219. sampleFrame outPeak = { 0, 0 };
  220. gain( buf, frames, outGain, &outPeak );
  221. m_eqControls.m_outPeakL = m_eqControls.m_outPeakL < outPeak[0] ? outPeak[0] : m_eqControls.m_outPeakL;
  222. m_eqControls.m_outPeakR = m_eqControls.m_outPeakR < outPeak[1] ? outPeak[1] : m_eqControls.m_outPeakR;
  223. checkGate( outSum / frames );
  224. if(m_eqControls.m_analyseOutModel.value( true ) && outSum > 0 && m_eqControls.isViewVisible() )
  225. {
  226. m_eqControls.m_outFftBands.analyze( buf, frames );
  227. setBandPeaks( &m_eqControls.m_outFftBands , ( int )( sampleRate ) );
  228. }
  229. else
  230. {
  231. m_eqControls.m_outFftBands.clear();
  232. }
  233. m_eqControls.m_inProgress = false;
  234. return isRunning();
  235. }
  236. float EqEffect::peakBand( float minF, float maxF, EqAnalyser *fft, int sr )
  237. {
  238. float peak = -60;
  239. float *b = fft->m_bands;
  240. float h = 0;
  241. for( int x = 0; x < MAX_BANDS; x++, b++ )
  242. {
  243. if( bandToFreq( x ,sr) >= minF && bandToFreq( x,sr ) <= maxF )
  244. {
  245. h = 20 * ( log10( *b / fft->getEnergy() ) );
  246. peak = h > peak ? h : peak;
  247. }
  248. }
  249. return ( peak + 60 ) / 100;
  250. }
  251. void EqEffect::setBandPeaks( EqAnalyser *fft, int samplerate )
  252. {
  253. m_eqControls.m_lowShelfPeakR = m_eqControls.m_lowShelfPeakL =
  254. peakBand( m_eqControls.m_lowShelfFreqModel.value()
  255. * ( 1 - m_eqControls.m_lowShelfResModel.value() * 0.5 ),
  256. m_eqControls.m_lowShelfFreqModel.value(),
  257. fft , samplerate );
  258. m_eqControls.m_para1PeakL = m_eqControls.m_para1PeakR =
  259. peakBand( m_eqControls.m_para1FreqModel.value()
  260. * ( 1 - m_eqControls.m_para1BwModel.value() * 0.5 ),
  261. m_eqControls.m_para1FreqModel.value()
  262. * ( 1 + m_eqControls.m_para1BwModel.value() * 0.5 ),
  263. fft , samplerate );
  264. m_eqControls.m_para2PeakL = m_eqControls.m_para2PeakR =
  265. peakBand( m_eqControls.m_para2FreqModel.value()
  266. * ( 1 - m_eqControls.m_para2BwModel.value() * 0.5 ),
  267. m_eqControls.m_para2FreqModel.value()
  268. * ( 1 + m_eqControls.m_para2BwModel.value() * 0.5 ),
  269. fft , samplerate );
  270. m_eqControls.m_para3PeakL = m_eqControls.m_para3PeakR =
  271. peakBand( m_eqControls.m_para3FreqModel.value()
  272. * ( 1 - m_eqControls.m_para3BwModel.value() * 0.5 ),
  273. m_eqControls.m_para3FreqModel.value()
  274. * ( 1 + m_eqControls.m_para3BwModel.value() * 0.5 ),
  275. fft , samplerate );
  276. m_eqControls.m_para4PeakL = m_eqControls.m_para4PeakR =
  277. peakBand( m_eqControls.m_para4FreqModel.value()
  278. * ( 1 - m_eqControls.m_para4BwModel.value() * 0.5 ),
  279. m_eqControls.m_para4FreqModel.value()
  280. * ( 1 + m_eqControls.m_para4BwModel.value() * 0.5 ),
  281. fft , samplerate );
  282. m_eqControls.m_highShelfPeakL = m_eqControls.m_highShelfPeakR =
  283. peakBand( m_eqControls.m_highShelfFreqModel.value(),
  284. m_eqControls.m_highShelfFreqModel.value()
  285. * ( 1 + m_eqControls.m_highShelfResModel.value() * 0.5 ),
  286. fft, samplerate );
  287. }
  288. extern "C"
  289. {
  290. //needed for getting plugin out of shared lib
  291. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
  292. {
  293. return new EqEffect( parent , static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  294. }
  295. }