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.

162 lines
4.6KB

  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, presetCount, 0) // paramCount param(s), presetCount program(s), 0 states
  31. {
  32. smooth_gain = new CParamSmooth(20.0f, getSampleRate());
  33. for (unsigned p = 0; p < paramCount; ++p) {
  34. Parameter param;
  35. initParameter(p, param);
  36. setParameterValue(p, param.ranges.def);
  37. }
  38. }
  39. PluginSimpleGain::~PluginSimpleGain() {
  40. delete smooth_gain;
  41. }
  42. // -----------------------------------------------------------------------
  43. // Init
  44. void PluginSimpleGain::initParameter(uint32_t index, Parameter& parameter) {
  45. if (index >= paramCount)
  46. return;
  47. parameter.ranges.min = -90.0f;
  48. parameter.ranges.max = 30.0f;
  49. parameter.ranges.def = -0.0f;
  50. parameter.unit = "db";
  51. parameter.hints = kParameterIsAutomable;
  52. switch (index) {
  53. case paramGain:
  54. parameter.name = "Gain (dB)";
  55. parameter.shortName = "Gain";
  56. parameter.symbol = "gain";
  57. break;
  58. }
  59. }
  60. /**
  61. Set the name of the program @a index.
  62. This function will be called once, shortly after the plugin is created.
  63. */
  64. void PluginSimpleGain::initProgramName(uint32_t index, String& programName) {
  65. if (index < presetCount) {
  66. programName = factoryPresets[index].name;
  67. }
  68. }
  69. // -----------------------------------------------------------------------
  70. // Internal data
  71. /**
  72. Optional callback to inform the plugin about a sample rate change.
  73. */
  74. void PluginSimpleGain::sampleRateChanged(double newSampleRate) {
  75. fSampleRate = newSampleRate;
  76. smooth_gain->setSampleRate(newSampleRate);
  77. }
  78. /**
  79. Get the current value of a parameter.
  80. */
  81. float PluginSimpleGain::getParameterValue(uint32_t index) const {
  82. return fParams[index];
  83. }
  84. /**
  85. Change a parameter value.
  86. */
  87. void PluginSimpleGain::setParameterValue(uint32_t index, float value) {
  88. fParams[index] = value;
  89. switch (index) {
  90. case paramGain:
  91. gain = DB_CO(CLAMP(fParams[paramGain], -90.0, 30.0));
  92. break;
  93. }
  94. }
  95. /**
  96. Load a program.
  97. The host may call this function from any context,
  98. including realtime processing.
  99. */
  100. void PluginSimpleGain::loadProgram(uint32_t index) {
  101. if (index < presetCount) {
  102. for (int i=0; i < paramCount; i++) {
  103. setParameterValue(i, factoryPresets[index].params[i]);
  104. }
  105. }
  106. }
  107. // -----------------------------------------------------------------------
  108. // Process
  109. void PluginSimpleGain::activate() {
  110. // plugin is activated
  111. }
  112. void PluginSimpleGain::run(const float** inputs, float** outputs,
  113. uint32_t frames) {
  114. // get the left and right audio inputs
  115. const float* const inpL = inputs[0];
  116. const float* const inpR = inputs[1];
  117. // get the left and right audio outputs
  118. float* const outL = outputs[0];
  119. float* const outR = outputs[1];
  120. // apply gain against all samples
  121. for (uint32_t i=0; i < frames; ++i) {
  122. float gainval = smooth_gain->process(gain);
  123. outL[i] = inpL[i] * gainval;
  124. outR[i] = inpR[i] * gainval;
  125. }
  126. }
  127. // -----------------------------------------------------------------------
  128. Plugin* createPlugin() {
  129. return new PluginSimpleGain();
  130. }
  131. // -----------------------------------------------------------------------
  132. END_NAMESPACE_DISTRHO