DualFilter.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * DualFilter.cpp - A native dual filter effect plugin with two parallel filters
  3. *
  4. * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
  5. * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include "DualFilter.h"
  26. #include "embed.cpp"
  27. #include "BasicFilters.h"
  28. extern "C"
  29. {
  30. Plugin::Descriptor PLUGIN_EXPORT dualfilter_plugin_descriptor =
  31. {
  32. STRINGIFY( PLUGIN_NAME ),
  33. "Dual Filter",
  34. QT_TRANSLATE_NOOP( "pluginBrowser", "A Dual filter plugin" ),
  35. "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
  36. 0x0100,
  37. Plugin::Effect,
  38. new PluginPixmapLoader( "logo" ),
  39. NULL,
  40. NULL
  41. } ;
  42. }
  43. DualFilterEffect::DualFilterEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
  44. Effect( &dualfilter_plugin_descriptor, parent, key ),
  45. m_dfControls( this )
  46. {
  47. m_filter1 = new BasicFilters<2>( Engine::mixer()->processingSampleRate() );
  48. m_filter2 = new BasicFilters<2>( Engine::mixer()->processingSampleRate() );
  49. // ensure filters get updated
  50. m_filter1changed = true;
  51. m_filter2changed = true;
  52. }
  53. DualFilterEffect::~DualFilterEffect()
  54. {
  55. delete m_filter1;
  56. delete m_filter2;
  57. }
  58. bool DualFilterEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  59. {
  60. if( !isEnabled() || !isRunning () )
  61. {
  62. return( false );
  63. }
  64. double outSum = 0.0;
  65. const float d = dryLevel();
  66. const float w = wetLevel();
  67. if( m_dfControls.m_filter1Model.isValueChanged() || m_filter1changed )
  68. {
  69. m_filter1->setFilterType( m_dfControls.m_filter1Model.value() );
  70. m_filter1changed = true;
  71. }
  72. if( m_dfControls.m_filter2Model.isValueChanged() || m_filter2changed )
  73. {
  74. m_filter2->setFilterType( m_dfControls.m_filter2Model.value() );
  75. m_filter2changed = true;
  76. }
  77. float cut1 = m_dfControls.m_cut1Model.value();
  78. float res1 = m_dfControls.m_res1Model.value();
  79. float gain1 = m_dfControls.m_gain1Model.value();
  80. float cut2 = m_dfControls.m_cut2Model.value();
  81. float res2 = m_dfControls.m_res2Model.value();
  82. float gain2 = m_dfControls.m_gain2Model.value();
  83. float mix = m_dfControls.m_mixModel.value();
  84. ValueBuffer *cut1Buffer = m_dfControls.m_cut1Model.valueBuffer();
  85. ValueBuffer *res1Buffer = m_dfControls.m_res1Model.valueBuffer();
  86. ValueBuffer *gain1Buffer = m_dfControls.m_gain1Model.valueBuffer();
  87. ValueBuffer *cut2Buffer = m_dfControls.m_cut2Model.valueBuffer();
  88. ValueBuffer *res2Buffer = m_dfControls.m_res2Model.valueBuffer();
  89. ValueBuffer *gain2Buffer = m_dfControls.m_gain2Model.valueBuffer();
  90. ValueBuffer *mixBuffer = m_dfControls.m_mixModel.valueBuffer();
  91. int cut1Inc = cut1Buffer ? 1 : 0;
  92. int res1Inc = res1Buffer ? 1 : 0;
  93. int gain1Inc = gain1Buffer ? 1 : 0;
  94. int cut2Inc = cut2Buffer ? 1 : 0;
  95. int res2Inc = res2Buffer ? 1 : 0;
  96. int gain2Inc = gain2Buffer ? 1 : 0;
  97. int mixInc = mixBuffer ? 1 : 0;
  98. float *cut1Ptr = cut1Buffer ? &( cut1Buffer->values()[ 0 ] ) : &cut1;
  99. float *res1Ptr = res1Buffer ? &( res1Buffer->values()[ 0 ] ) : &res1;
  100. float *gain1Ptr = gain1Buffer ? &( gain1Buffer->values()[ 0 ] ) : &gain1;
  101. float *cut2Ptr = cut2Buffer ? &( cut2Buffer->values()[ 0 ] ) : &cut2;
  102. float *res2Ptr = res2Buffer ? &( res2Buffer->values()[ 0 ] ) : &res2;
  103. float *gain2Ptr = gain2Buffer ? &( gain2Buffer->values()[ 0 ] ) : &gain2;
  104. float *mixPtr = mixBuffer ? &( mixBuffer->values()[ 0 ] ) : &mix;
  105. const bool enabled1 = m_dfControls.m_enabled1Model.value();
  106. const bool enabled2 = m_dfControls.m_enabled2Model.value();
  107. // buffer processing loop
  108. for( fpp_t f = 0; f < frames; ++f )
  109. {
  110. // get mix amounts for wet signals of both filters
  111. const float mix2 = ( ( *mixPtr + 1.0f ) * 0.5f );
  112. const float mix1 = 1.0f - mix2;
  113. const float gain1 = *gain1Ptr * 0.01f;
  114. const float gain2 = *gain2Ptr * 0.01f;
  115. sample_t s[2] = { 0.0f, 0.0f }; // mix
  116. sample_t s1[2] = { buf[f][0], buf[f][1] }; // filter 1
  117. sample_t s2[2] = { buf[f][0], buf[f][1] }; // filter 2
  118. // update filter 1
  119. if( enabled1 )
  120. {
  121. //update filter 1 params here
  122. // recalculate only when necessary: either cut/res is changed, or the changed-flag is set (filter type or samplerate changed)
  123. if( ( ( *cut1Ptr != m_currentCut1 ||
  124. *res1Ptr != m_currentRes1 ) ) || m_filter1changed )
  125. {
  126. m_filter1->calcFilterCoeffs( *cut1Ptr, *res1Ptr );
  127. m_filter1changed = false;
  128. m_currentCut1 = *cut1Ptr;
  129. m_currentRes1 = *res1Ptr;
  130. }
  131. s1[0] = m_filter1->update( s1[0], 0 );
  132. s1[1] = m_filter1->update( s1[1], 1 );
  133. // apply gain
  134. s1[0] *= gain1;
  135. s1[1] *= gain1;
  136. // apply mix
  137. s[0] += ( s1[0] * mix1 );
  138. s[1] += ( s1[1] * mix1 );
  139. }
  140. // update filter 2
  141. if( enabled2 )
  142. {
  143. //update filter 2 params here
  144. if( ( ( *cut2Ptr != m_currentCut2 ||
  145. *res2Ptr != m_currentRes2 ) ) || m_filter2changed )
  146. {
  147. m_filter2->calcFilterCoeffs( *cut2Ptr, *res2Ptr );
  148. m_filter2changed = false;
  149. m_currentCut2 = *cut2Ptr;
  150. m_currentRes2 = *res2Ptr;
  151. }
  152. s2[0] = m_filter2->update( s2[0], 0 );
  153. s2[1] = m_filter2->update( s2[1], 1 );
  154. //apply gain
  155. s2[0] *= gain2;
  156. s2[1] *= gain2;
  157. // apply mix
  158. s[0] += ( s2[0] * mix2 );
  159. s[1] += ( s2[1] * mix2 );
  160. }
  161. // do another mix with dry signal
  162. buf[f][0] = d * buf[f][0] + w * s[0];
  163. buf[f][1] = d * buf[f][1] + w * s[1];
  164. outSum += buf[f][0] * buf[f][0] + buf[f][1] * buf[f][1];
  165. //increment pointers
  166. cut1Ptr += cut1Inc;
  167. res1Ptr += res1Inc;
  168. gain1Ptr += gain1Inc;
  169. cut2Ptr += cut2Inc;
  170. res2Ptr += res2Inc;
  171. gain2Ptr += gain2Inc;
  172. mixPtr += mixInc;
  173. }
  174. checkGate( outSum / frames );
  175. return isRunning();
  176. }
  177. extern "C"
  178. {
  179. // necessary for getting instance out of shared lib
  180. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model* parent, void* data )
  181. {
  182. return new DualFilterEffect( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  183. }
  184. }