Compressor.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Compressor.h
  3. *
  4. * Copyright (c) 2020 Lost Robot <r94231@gmail.com>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef COMPRESSOR_H
  25. #define COMPRESSOR_H
  26. #include "CompressorControls.h"
  27. #include "Effect.h"
  28. #include "ValueBuffer.h"
  29. #include "RmsHelper.h"
  30. constexpr float COMP_LOG = -2.2;
  31. class CompressorEffect : public Effect
  32. {
  33. Q_OBJECT
  34. public:
  35. CompressorEffect(Model* parent, const Descriptor::SubPluginFeatures::Key* key);
  36. ~CompressorEffect() override;
  37. bool processAudioBuffer(sampleFrame* buf, const fpp_t frames) override;
  38. EffectControls* controls() override
  39. {
  40. return &m_compressorControls;
  41. }
  42. private slots:
  43. void calcAutoMakeup();
  44. void calcAttack();
  45. void calcRelease();
  46. void calcAutoAttack();
  47. void calcAutoRelease();
  48. void calcHold();
  49. void calcOutGain();
  50. void calcRatio();
  51. void calcRange();
  52. void resizeRMS();
  53. void calcLookaheadLength();
  54. void calcThreshold();
  55. void calcKnee();
  56. void calcInGain();
  57. void calcTiltCoeffs();
  58. void calcMix();
  59. void changeSampleRate();
  60. void redrawKnee();
  61. private:
  62. CompressorControls m_compressorControls;
  63. float msToCoeff(float ms);
  64. inline void calcTiltFilter(sample_t inputSample, sample_t &outputSample, int filtNum);
  65. inline int realmod(int k, int n);
  66. inline float realfmod(float k, float n);
  67. enum StereoLinkModes { Unlinked, Maximum, Average, Minimum, Blend };
  68. std::vector<float> m_preLookaheadBuf[2];
  69. int m_preLookaheadBufLoc[2] = {0};
  70. std::vector<float> m_lookaheadBuf[2];
  71. int m_lookaheadBufLoc[2] = {0};
  72. std::vector<float> m_inputBuf[2];
  73. int m_inputBufLoc = 0;
  74. float m_attCoeff;
  75. float m_relCoeff;
  76. float m_autoAttVal;
  77. float m_autoRelVal;
  78. int m_holdLength = 0;
  79. int m_holdTimer[2] = {0, 0};
  80. int m_lookaheadLength;
  81. int m_lookaheadDelayLength;
  82. int m_preLookaheadLength;
  83. float m_thresholdAmpVal;
  84. float m_autoMakeupVal;
  85. float m_outGainVal;
  86. float m_inGainVal;
  87. float m_rangeVal;
  88. float m_tiltVal;
  89. float m_mixVal;
  90. float m_coeffPrecalc;
  91. sampleFrame m_maxLookaheadVal;
  92. int m_maxLookaheadTimer[2] = {1, 1};
  93. float m_rmsTimeConst;
  94. float m_rmsVal[2] = {0, 0};
  95. float m_crestPeakVal[2] = {0, 0};
  96. float m_crestRmsVal[2] = {0, 0};
  97. float m_crestFactorVal[2] = {0, 0};
  98. float m_crestTimeConst;
  99. float m_tiltOut[2] = {0};
  100. bool m_cleanedBuffers = false;
  101. float m_sampleRate;
  102. float m_lgain;
  103. float m_hgain;
  104. float m_a0;
  105. float m_b1;
  106. float m_prevOut[2] = {0};
  107. float m_yL[2];
  108. float m_gainResult[2];
  109. float m_displayPeak[2];
  110. float m_displayGain[2];
  111. float m_kneeVal;
  112. float m_thresholdVal;
  113. float m_ratioVal;
  114. bool m_redrawKnee = true;
  115. bool m_redrawThreshold = true;
  116. friend class CompressorControls;
  117. friend class CompressorControlDialog;
  118. } ;
  119. #endif