ReverbSC.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * ReverbSC.cpp - A native reverb based on an algorithm by Sean Costello
  3. *
  4. * This file is part of LMMS - https://lmms.io
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program (see COPYING); if not, write to the
  18. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301 USA.
  20. *
  21. */
  22. #include <math.h>
  23. #include "ReverbSC.h"
  24. #include "embed.h"
  25. #include "plugin_export.h"
  26. #define DB2LIN(X) pow(10, X / 20.0f);
  27. extern "C"
  28. {
  29. Plugin::Descriptor PLUGIN_EXPORT reverbsc_plugin_descriptor =
  30. {
  31. STRINGIFY( PLUGIN_NAME ),
  32. "ReverbSC",
  33. QT_TRANSLATE_NOOP( "pluginBrowser", "Reverb algorithm by Sean Costello" ),
  34. "Paul Batchelor",
  35. 0x0123,
  36. Plugin::Effect,
  37. new PluginPixmapLoader( "logo" ),
  38. NULL,
  39. NULL
  40. } ;
  41. }
  42. ReverbSCEffect::ReverbSCEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
  43. Effect( &reverbsc_plugin_descriptor, parent, key ),
  44. m_reverbSCControls( this )
  45. {
  46. sp_create(&sp);
  47. sp->sr = Engine::mixer()->processingSampleRate();
  48. sp_revsc_create(&revsc);
  49. sp_revsc_init(sp, revsc);
  50. sp_dcblock_create(&dcblk[0]);
  51. sp_dcblock_create(&dcblk[1]);
  52. sp_dcblock_init(sp, dcblk[0], Engine::mixer()->currentQualitySettings().sampleRateMultiplier() );
  53. sp_dcblock_init(sp, dcblk[1], Engine::mixer()->currentQualitySettings().sampleRateMultiplier() );
  54. }
  55. ReverbSCEffect::~ReverbSCEffect()
  56. {
  57. sp_revsc_destroy(&revsc);
  58. sp_dcblock_destroy(&dcblk[0]);
  59. sp_dcblock_destroy(&dcblk[1]);
  60. sp_destroy(&sp);
  61. }
  62. bool ReverbSCEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  63. {
  64. if( !isEnabled() || !isRunning () )
  65. {
  66. return( false );
  67. }
  68. double outSum = 0.0;
  69. const float d = dryLevel();
  70. const float w = wetLevel();
  71. SPFLOAT tmpL, tmpR;
  72. SPFLOAT dcblkL, dcblkR;
  73. ValueBuffer * inGainBuf = m_reverbSCControls.m_inputGainModel.valueBuffer();
  74. ValueBuffer * sizeBuf = m_reverbSCControls.m_sizeModel.valueBuffer();
  75. ValueBuffer * colorBuf = m_reverbSCControls.m_colorModel.valueBuffer();
  76. ValueBuffer * outGainBuf = m_reverbSCControls.m_outputGainModel.valueBuffer();
  77. for( fpp_t f = 0; f < frames; ++f )
  78. {
  79. sample_t s[2] = { buf[f][0], buf[f][1] };
  80. const SPFLOAT inGain = (SPFLOAT)DB2LIN((inGainBuf ?
  81. inGainBuf->values()[f]
  82. : m_reverbSCControls.m_inputGainModel.value()));
  83. const SPFLOAT outGain = (SPFLOAT)DB2LIN((outGainBuf ?
  84. outGainBuf->values()[f]
  85. : m_reverbSCControls.m_outputGainModel.value()));
  86. s[0] *= inGain;
  87. s[1] *= inGain;
  88. revsc->feedback = (SPFLOAT)(sizeBuf ?
  89. sizeBuf->values()[f]
  90. : m_reverbSCControls.m_sizeModel.value());
  91. revsc->lpfreq = (SPFLOAT)(colorBuf ?
  92. colorBuf->values()[f]
  93. : m_reverbSCControls.m_colorModel.value());
  94. sp_revsc_compute(sp, revsc, &s[0], &s[1], &tmpL, &tmpR);
  95. sp_dcblock_compute(sp, dcblk[0], &tmpL, &dcblkL);
  96. sp_dcblock_compute(sp, dcblk[1], &tmpR, &dcblkR);
  97. buf[f][0] = d * buf[f][0] + w * dcblkL * outGain;
  98. buf[f][1] = d * buf[f][1] + w * dcblkR * outGain;
  99. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  100. }
  101. checkGate( outSum / frames );
  102. return isRunning();
  103. }
  104. void ReverbSCEffect::changeSampleRate()
  105. {
  106. // Change sr variable in Soundpipe. does not need to be destroyed
  107. sp->sr = Engine::mixer()->processingSampleRate();
  108. mutex.lock();
  109. sp_revsc_destroy(&revsc);
  110. sp_dcblock_destroy(&dcblk[0]);
  111. sp_dcblock_destroy(&dcblk[1]);
  112. sp_revsc_create(&revsc);
  113. sp_revsc_init(sp, revsc);
  114. sp_dcblock_create(&dcblk[0]);
  115. sp_dcblock_create(&dcblk[1]);
  116. sp_dcblock_init(sp, dcblk[0], Engine::mixer()->currentQualitySettings().sampleRateMultiplier() );
  117. sp_dcblock_init(sp, dcblk[1], Engine::mixer()->currentQualitySettings().sampleRateMultiplier() );
  118. mutex.unlock();
  119. }
  120. extern "C"
  121. {
  122. // necessary for getting instance out of shared lib
  123. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
  124. {
  125. return new ReverbSCEffect(
  126. parent,
  127. static_cast<const Plugin::Descriptor::SubPluginFeatures::Key*>(data)
  128. );
  129. }
  130. }