DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.8KB

  1. /*
  2. * Simple Gain audio effect based on DISTRHO Plugin Framework (DPF)
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Copyright (C) 2021 Jean Pierre Cimalando <jp-dev@inbox.ru>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. */
  26. #include "PluginSimpleGain.hpp"
  27. START_NAMESPACE_DISTRHO
  28. // -----------------------------------------------------------------------
  29. PluginSimpleGain::PluginSimpleGain()
  30. : Plugin(paramCount, 0, 0), // parameters, programs, states
  31. fSampleRate(getSampleRate()),
  32. fGainDB(0.0f),
  33. fGainLinear(1.0f)
  34. {
  35. fSmoothGain = new CParamSmooth(20.0f, fSampleRate);
  36. }
  37. // -----------------------------------------------------------------------
  38. // Init
  39. void PluginSimpleGain::initParameter(uint32_t index, Parameter& parameter)
  40. {
  41. DISTRHO_SAFE_ASSERT_RETURN(index == 0,);
  42. parameter.ranges.min = -90.0f;
  43. parameter.ranges.max = 30.0f;
  44. parameter.ranges.def = -0.0f;
  45. parameter.hints = kParameterIsAutomatable;
  46. parameter.name = "Gain";
  47. parameter.shortName = "Gain";
  48. parameter.symbol = "gain";
  49. parameter.unit = "dB";
  50. }
  51. // -----------------------------------------------------------------------
  52. // Internal data
  53. /**
  54. Get the current value of a parameter.
  55. */
  56. float PluginSimpleGain::getParameterValue(uint32_t index) const
  57. {
  58. DISTRHO_SAFE_ASSERT_RETURN(index == 0, 0.0f);
  59. return fGainDB;
  60. }
  61. /**
  62. Change a parameter value.
  63. */
  64. void PluginSimpleGain::setParameterValue(uint32_t index, float value)
  65. {
  66. DISTRHO_SAFE_ASSERT_RETURN(index == 0,);
  67. fGainDB = value;
  68. fGainLinear = DB_CO(CLAMP(value, -90.0, 30.0));
  69. }
  70. // -----------------------------------------------------------------------
  71. // Process
  72. void PluginSimpleGain::activate()
  73. {
  74. fSmoothGain->flush();
  75. }
  76. void PluginSimpleGain::run(const float** inputs, float** outputs, uint32_t frames)
  77. {
  78. // get the left and right audio inputs
  79. const float* const inpL = inputs[0];
  80. const float* const inpR = inputs[1];
  81. // get the left and right audio outputs
  82. float* const outL = outputs[0];
  83. float* const outR = outputs[1];
  84. // apply gain against all samples
  85. for (uint32_t i=0; i < frames; ++i)
  86. {
  87. const float gain = fSmoothGain->process(fGainLinear);
  88. outL[i] = inpL[i] * gain;
  89. outR[i] = inpR[i] * gain;
  90. }
  91. }
  92. // -----------------------------------------------------------------------
  93. /**
  94. Optional callback to inform the plugin about a sample rate change.
  95. */
  96. void PluginSimpleGain::sampleRateChanged(double newSampleRate)
  97. {
  98. fSampleRate = newSampleRate;
  99. fSmoothGain->setSampleRate(newSampleRate);
  100. }
  101. // -----------------------------------------------------------------------
  102. Plugin* createPlugin()
  103. {
  104. return new PluginSimpleGain();
  105. }
  106. // -----------------------------------------------------------------------
  107. END_NAMESPACE_DISTRHO