Effect.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Effect.h - base class for effects
  3. *
  4. * Copyright (c) 2006-2007 Danny McRae <khjklujn/at/users.sourceforge.net>
  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. #ifndef EFFECT_H
  26. #define EFFECT_H
  27. #include "Plugin.h"
  28. #include "Engine.h"
  29. #include "Mixer.h"
  30. #include "AutomatableModel.h"
  31. #include "TempoSyncKnobModel.h"
  32. #include "MemoryManager.h"
  33. class EffectChain;
  34. class EffectControls;
  35. class LMMS_EXPORT Effect : public Plugin
  36. {
  37. MM_OPERATORS
  38. Q_OBJECT
  39. public:
  40. Effect( const Plugin::Descriptor * _desc,
  41. Model * _parent,
  42. const Descriptor::SubPluginFeatures::Key * _key );
  43. virtual ~Effect();
  44. void saveSettings( QDomDocument & _doc, QDomElement & _parent ) override;
  45. void loadSettings( const QDomElement & _this ) override;
  46. inline QString nodeName() const override
  47. {
  48. return "effect";
  49. }
  50. virtual bool processAudioBuffer( sampleFrame * _buf,
  51. const fpp_t _frames ) = 0;
  52. inline ch_cnt_t processorCount() const
  53. {
  54. return m_processors;
  55. }
  56. inline void setProcessorCount( ch_cnt_t _processors )
  57. {
  58. m_processors = _processors;
  59. }
  60. inline bool isOkay() const
  61. {
  62. return m_okay;
  63. }
  64. inline void setOkay( bool _state )
  65. {
  66. m_okay = _state;
  67. }
  68. inline bool isRunning() const
  69. {
  70. return m_running;
  71. }
  72. inline void startRunning()
  73. {
  74. m_bufferCount = 0;
  75. m_running = true;
  76. }
  77. inline void stopRunning()
  78. {
  79. m_running = false;
  80. }
  81. inline bool isEnabled() const
  82. {
  83. return m_enabledModel.value();
  84. }
  85. inline f_cnt_t timeout() const
  86. {
  87. const float samples = Engine::mixer()->processingSampleRate() * m_autoQuitModel.value() / 1000.0f;
  88. return 1 + ( static_cast<int>( samples ) / Engine::mixer()->framesPerPeriod() );
  89. }
  90. inline float wetLevel() const
  91. {
  92. return m_wetDryModel.value();
  93. }
  94. inline float dryLevel() const
  95. {
  96. return 1.0f - m_wetDryModel.value();
  97. }
  98. inline float gate() const
  99. {
  100. const float level = m_gateModel.value();
  101. return level*level * m_processors;
  102. }
  103. inline f_cnt_t bufferCount() const
  104. {
  105. return m_bufferCount;
  106. }
  107. inline void resetBufferCount()
  108. {
  109. m_bufferCount = 0;
  110. }
  111. inline void incrementBufferCount()
  112. {
  113. ++m_bufferCount;
  114. }
  115. inline bool dontRun() const
  116. {
  117. return m_noRun;
  118. }
  119. inline void setDontRun( bool _state )
  120. {
  121. m_noRun = _state;
  122. }
  123. EffectChain * effectChain() const
  124. {
  125. return m_parent;
  126. }
  127. virtual EffectControls * controls() = 0;
  128. static Effect * instantiate( const QString & _plugin_name,
  129. Model * _parent,
  130. Descriptor::SubPluginFeatures::Key * _key );
  131. protected:
  132. /**
  133. Effects should call this at the end of audio processing
  134. If the setting "Keep effects running even without input" is disabled,
  135. after "decay" ms of a signal below "gate", the effect is turned off
  136. and won't be processed again until it receives new audio input
  137. */
  138. void checkGate( double _out_sum );
  139. PluginView * instantiateView( QWidget * ) override;
  140. // some effects might not be capable of higher sample-rates so they can
  141. // sample it down before processing and back after processing
  142. inline void sampleDown( const sampleFrame * _src_buf,
  143. sampleFrame * _dst_buf,
  144. sample_rate_t _dst_sr )
  145. {
  146. resample( 0, _src_buf,
  147. Engine::mixer()->processingSampleRate(),
  148. _dst_buf, _dst_sr,
  149. Engine::mixer()->framesPerPeriod() );
  150. }
  151. inline void sampleBack( const sampleFrame * _src_buf,
  152. sampleFrame * _dst_buf,
  153. sample_rate_t _src_sr )
  154. {
  155. resample( 1, _src_buf, _src_sr, _dst_buf,
  156. Engine::mixer()->processingSampleRate(),
  157. Engine::mixer()->framesPerPeriod() * _src_sr /
  158. Engine::mixer()->processingSampleRate() );
  159. }
  160. void reinitSRC();
  161. private:
  162. EffectChain * m_parent;
  163. void resample( int _i, const sampleFrame * _src_buf,
  164. sample_rate_t _src_sr,
  165. sampleFrame * _dst_buf, sample_rate_t _dst_sr,
  166. const f_cnt_t _frames );
  167. ch_cnt_t m_processors;
  168. bool m_okay;
  169. bool m_noRun;
  170. bool m_running;
  171. f_cnt_t m_bufferCount;
  172. BoolModel m_enabledModel;
  173. FloatModel m_wetDryModel;
  174. FloatModel m_gateModel;
  175. TempoSyncKnobModel m_autoQuitModel;
  176. bool m_autoQuitDisabled;
  177. SRC_DATA m_srcData[2];
  178. SRC_STATE * m_srcState[2];
  179. friend class EffectView;
  180. friend class EffectChain;
  181. } ;
  182. typedef Effect::Descriptor::SubPluginFeatures::Key EffectKey;
  183. typedef Effect::Descriptor::SubPluginFeatures::KeyList EffectKeyList;
  184. #endif