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.h"
  27. #include "BasicFilters.h"
  28. #include "plugin_export.h"
  29. extern "C"
  30. {
  31. Plugin::Descriptor PLUGIN_EXPORT dualfilter_plugin_descriptor =
  32. {
  33. STRINGIFY( PLUGIN_NAME ),
  34. "Dual Filter",
  35. QT_TRANSLATE_NOOP( "PluginBrowser", "A Dual filter plugin" ),
  36. "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
  37. 0x0100,
  38. Plugin::Effect,
  39. new PluginPixmapLoader( "logo" ),
  40. nullptr,
  41. nullptr,
  42. } ;
  43. }
  44. DualFilterEffect::DualFilterEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
  45. Effect( &dualfilter_plugin_descriptor, parent, key ),
  46. m_dfControls( this )
  47. {
  48. m_filter1 = new BasicFilters<2>( Engine::audioEngine()->processingSampleRate() );
  49. m_filter2 = new BasicFilters<2>( Engine::audioEngine()->processingSampleRate() );
  50. // ensure filters get updated
  51. m_filter1changed = true;
  52. m_filter2changed = true;
  53. }
  54. DualFilterEffect::~DualFilterEffect()
  55. {
  56. delete m_filter1;
  57. delete m_filter2;
  58. }
  59. bool DualFilterEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  60. {
  61. if( !isEnabled() || !isRunning () )
  62. {
  63. return( false );
  64. }
  65. double outSum = 0.0;
  66. const float d = dryLevel();
  67. const float w = wetLevel();
  68. if( m_dfControls.m_filter1Model.isValueChanged() || m_filter1changed )
  69. {
  70. m_filter1->setFilterType( m_dfControls.m_filter1Model.value() );
  71. m_filter1changed = true;
  72. }
  73. if( m_dfControls.m_filter2Model.isValueChanged() || m_filter2changed )
  74. {
  75. m_filter2->setFilterType( m_dfControls.m_filter2Model.value() );
  76. m_filter2changed = true;
  77. }
  78. float cut1 = m_dfControls.m_cut1Model.value();
  79. float res1 = m_dfControls.m_res1Model.value();
  80. float gain1 = m_dfControls.m_gain1Model.value();
  81. float cut2 = m_dfControls.m_cut2Model.value();
  82. float res2 = m_dfControls.m_res2Model.value();
  83. float gain2 = m_dfControls.m_gain2Model.value();
  84. float mix = m_dfControls.m_mixModel.value();
  85. ValueBuffer *cut1Buffer = m_dfControls.m_cut1Model.valueBuffer();
  86. ValueBuffer *res1Buffer = m_dfControls.m_res1Model.valueBuffer();
  87. ValueBuffer *gain1Buffer = m_dfControls.m_gain1Model.valueBuffer();
  88. ValueBuffer *cut2Buffer = m_dfControls.m_cut2Model.valueBuffer();
  89. ValueBuffer *res2Buffer = m_dfControls.m_res2Model.valueBuffer();
  90. ValueBuffer *gain2Buffer = m_dfControls.m_gain2Model.valueBuffer();
  91. ValueBuffer *mixBuffer = m_dfControls.m_mixModel.valueBuffer();
  92. int cut1Inc = cut1Buffer ? 1 : 0;
  93. int res1Inc = res1Buffer ? 1 : 0;
  94. int gain1Inc = gain1Buffer ? 1 : 0;
  95. int cut2Inc = cut2Buffer ? 1 : 0;
  96. int res2Inc = res2Buffer ? 1 : 0;
  97. int gain2Inc = gain2Buffer ? 1 : 0;
  98. int mixInc = mixBuffer ? 1 : 0;
  99. float *cut1Ptr = cut1Buffer ? &( cut1Buffer->values()[ 0 ] ) : &cut1;
  100. float *res1Ptr = res1Buffer ? &( res1Buffer->values()[ 0 ] ) : &res1;
  101. float *gain1Ptr = gain1Buffer ? &( gain1Buffer->values()[ 0 ] ) : &gain1;
  102. float *cut2Ptr = cut2Buffer ? &( cut2Buffer->values()[ 0 ] ) : &cut2;
  103. float *res2Ptr = res2Buffer ? &( res2Buffer->values()[ 0 ] ) : &res2;
  104. float *gain2Ptr = gain2Buffer ? &( gain2Buffer->values()[ 0 ] ) : &gain2;
  105. float *mixPtr = mixBuffer ? &( mixBuffer->values()[ 0 ] ) : &mix;
  106. const bool enabled1 = m_dfControls.m_enabled1Model.value();
  107. const bool enabled2 = m_dfControls.m_enabled2Model.value();
  108. // buffer processing loop
  109. for( fpp_t f = 0; f < frames; ++f )
  110. {
  111. // get mix amounts for wet signals of both filters
  112. const float mix2 = ( ( *mixPtr + 1.0f ) * 0.5f );
  113. const float mix1 = 1.0f - mix2;
  114. const float gain1 = *gain1Ptr * 0.01f;
  115. const float gain2 = *gain2Ptr * 0.01f;
  116. sample_t s[2] = { 0.0f, 0.0f }; // mix
  117. sample_t s1[2] = { buf[f][0], buf[f][1] }; // filter 1
  118. sample_t s2[2] = { buf[f][0], buf[f][1] }; // filter 2
  119. // update filter 1
  120. if( enabled1 )
  121. {
  122. //update filter 1 params here
  123. // recalculate only when necessary: either cut/res is changed, or the changed-flag is set (filter type or samplerate changed)
  124. if( ( ( *cut1Ptr != m_currentCut1 ||
  125. *res1Ptr != m_currentRes1 ) ) || m_filter1changed )
  126. {
  127. m_filter1->calcFilterCoeffs( *cut1Ptr, *res1Ptr );
  128. m_filter1changed = false;
  129. m_currentCut1 = *cut1Ptr;
  130. m_currentRes1 = *res1Ptr;
  131. }
  132. s1[0] = m_filter1->update( s1[0], 0 );
  133. s1[1] = m_filter1->update( s1[1], 1 );
  134. // apply gain
  135. s1[0] *= gain1;
  136. s1[1] *= gain1;
  137. // apply mix
  138. s[0] += ( s1[0] * mix1 );
  139. s[1] += ( s1[1] * mix1 );
  140. }
  141. // update filter 2
  142. if( enabled2 )
  143. {
  144. //update filter 2 params here
  145. if( ( ( *cut2Ptr != m_currentCut2 ||
  146. *res2Ptr != m_currentRes2 ) ) || m_filter2changed )
  147. {
  148. m_filter2->calcFilterCoeffs( *cut2Ptr, *res2Ptr );
  149. m_filter2changed = false;
  150. m_currentCut2 = *cut2Ptr;
  151. m_currentRes2 = *res2Ptr;
  152. }
  153. s2[0] = m_filter2->update( s2[0], 0 );
  154. s2[1] = m_filter2->update( s2[1], 1 );
  155. //apply gain
  156. s2[0] *= gain2;
  157. s2[1] *= gain2;
  158. // apply mix
  159. s[0] += ( s2[0] * mix2 );
  160. s[1] += ( s2[1] * mix2 );
  161. }
  162. // do another mix with dry signal
  163. buf[f][0] = d * buf[f][0] + w * s[0];
  164. buf[f][1] = d * buf[f][1] + w * s[1];
  165. outSum += buf[f][0] * buf[f][0] + buf[f][1] * buf[f][1];
  166. //increment pointers
  167. cut1Ptr += cut1Inc;
  168. res1Ptr += res1Inc;
  169. gain1Ptr += gain1Inc;
  170. cut2Ptr += cut2Inc;
  171. res2Ptr += res2Inc;
  172. gain2Ptr += gain2Inc;
  173. mixPtr += mixInc;
  174. }
  175. checkGate( outSum / frames );
  176. return isRunning();
  177. }
  178. extern "C"
  179. {
  180. // necessary for getting instance out of shared lib
  181. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
  182. {
  183. return new DualFilterEffect( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  184. }
  185. }