ReverbSC.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * ReverbSC.cpp - A native reverb based on an algorithm by Sean Costello
  3. *
  4. * This file is part of LMMS - http://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.cpp"
  25. #define DB2LIN(X) pow(10, X / 20.0f);
  26. extern "C"
  27. {
  28. Plugin::Descriptor PLUGIN_EXPORT reverbsc_plugin_descriptor =
  29. {
  30. STRINGIFY( PLUGIN_NAME ),
  31. "ReverbSC",
  32. QT_TRANSLATE_NOOP( "pluginBrowser", "Reverb algorithm by Sean Costello" ),
  33. "Paul Batchelor",
  34. 0x0123,
  35. Plugin::Effect,
  36. new PluginPixmapLoader( "logo" ),
  37. NULL,
  38. NULL
  39. } ;
  40. }
  41. ReverbSCEffect::ReverbSCEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
  42. Effect( &reverbsc_plugin_descriptor, parent, key ),
  43. m_reverbSCControls( this )
  44. {
  45. sp_create(&sp);
  46. sp->sr = Engine::mixer()->processingSampleRate();
  47. sp_revsc_create(&revsc);
  48. sp_revsc_init(sp, revsc);
  49. sp_dcblock_create(&dcblk[0]);
  50. sp_dcblock_create(&dcblk[1]);
  51. sp_dcblock_init(sp, dcblk[0], Engine::mixer()->currentQualitySettings().sampleRateMultiplier() );
  52. sp_dcblock_init(sp, dcblk[1], Engine::mixer()->currentQualitySettings().sampleRateMultiplier() );
  53. }
  54. ReverbSCEffect::~ReverbSCEffect()
  55. {
  56. sp_revsc_destroy(&revsc);
  57. sp_dcblock_destroy(&dcblk[0]);
  58. sp_dcblock_destroy(&dcblk[1]);
  59. sp_destroy(&sp);
  60. }
  61. bool ReverbSCEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  62. {
  63. if( !isEnabled() || !isRunning () )
  64. {
  65. return( false );
  66. }
  67. double outSum = 0.0;
  68. const float d = dryLevel();
  69. const float w = wetLevel();
  70. SPFLOAT tmpL, tmpR;
  71. SPFLOAT dcblkL, dcblkR;
  72. ValueBuffer * inGainBuf = m_reverbSCControls.m_inputGainModel.valueBuffer();
  73. ValueBuffer * sizeBuf = m_reverbSCControls.m_sizeModel.valueBuffer();
  74. ValueBuffer * colorBuf = m_reverbSCControls.m_colorModel.valueBuffer();
  75. ValueBuffer * outGainBuf = m_reverbSCControls.m_outputGainModel.valueBuffer();
  76. for( fpp_t f = 0; f < frames; ++f )
  77. {
  78. sample_t s[2] = { buf[f][0], buf[f][1] };
  79. const SPFLOAT inGain = (SPFLOAT)DB2LIN((inGainBuf ?
  80. inGainBuf->values()[f]
  81. : m_reverbSCControls.m_inputGainModel.value()));
  82. const SPFLOAT outGain = (SPFLOAT)DB2LIN((outGainBuf ?
  83. outGainBuf->values()[f]
  84. : m_reverbSCControls.m_outputGainModel.value()));
  85. s[0] *= inGain;
  86. s[1] *= inGain;
  87. revsc->feedback = (SPFLOAT)(sizeBuf ?
  88. sizeBuf->values()[f]
  89. : m_reverbSCControls.m_sizeModel.value());
  90. revsc->lpfreq = (SPFLOAT)(colorBuf ?
  91. colorBuf->values()[f]
  92. : m_reverbSCControls.m_colorModel.value());
  93. sp_revsc_compute(sp, revsc, &s[0], &s[1], &tmpL, &tmpR);
  94. sp_dcblock_compute(sp, dcblk[0], &tmpL, &dcblkL);
  95. sp_dcblock_compute(sp, dcblk[1], &tmpR, &dcblkR);
  96. buf[f][0] = d * buf[f][0] + w * dcblkL * outGain;
  97. buf[f][1] = d * buf[f][1] + w * dcblkR * outGain;
  98. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  99. }
  100. checkGate( outSum / frames );
  101. return isRunning();
  102. }
  103. void ReverbSCEffect::changeSampleRate()
  104. {
  105. // Change sr variable in Soundpipe. does not need to be destroyed
  106. sp->sr = Engine::mixer()->processingSampleRate();
  107. mutex.lock();
  108. sp_revsc_destroy(&revsc);
  109. sp_dcblock_destroy(&dcblk[0]);
  110. sp_dcblock_destroy(&dcblk[1]);
  111. sp_revsc_create(&revsc);
  112. sp_revsc_init(sp, revsc);
  113. sp_dcblock_create(&dcblk[0]);
  114. sp_dcblock_create(&dcblk[1]);
  115. sp_dcblock_init(sp, dcblk[0], Engine::mixer()->currentQualitySettings().sampleRateMultiplier() );
  116. sp_dcblock_init(sp, dcblk[1], Engine::mixer()->currentQualitySettings().sampleRateMultiplier() );
  117. mutex.unlock();
  118. }
  119. extern "C"
  120. {
  121. // necessary for getting instance out of shared lib
  122. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model* parent, void* data )
  123. {
  124. return new ReverbSCEffect(
  125. parent,
  126. static_cast<const Plugin::Descriptor::SubPluginFeatures::Key*>(data)
  127. );
  128. }
  129. }