Audio plugin host https://kx.studio/carla
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.

160 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace dsp
  21. {
  22. /**
  23. Adds a DC offset (voltage bias) to the audio samples.
  24. This is a useful preprocessor for asymmetric waveshaping when a waveshaper is
  25. bookended by a bias on input and a DC-offset removing high pass filter on output.
  26. This is an extremely simple bias implementation that simply adds a value to a signal.
  27. More complicated bias behaviours exist in real circuits - for your homework ;).
  28. @tags{DSP}
  29. */
  30. template <typename FloatType>
  31. class Bias
  32. {
  33. public:
  34. Bias() noexcept = default;
  35. //==============================================================================
  36. /** Sets the DC bias
  37. @param newBias DC offset in range [-1, 1]
  38. */
  39. void setBias (FloatType newBias) noexcept
  40. {
  41. jassert (newBias >= static_cast<FloatType> (-1) && newBias <= static_cast<FloatType> (1));
  42. bias.setTargetValue (newBias);
  43. }
  44. //==============================================================================
  45. /** Returns the DC bias
  46. @return DC bias, which should be in the range [-1, 1]
  47. */
  48. FloatType getBias() const noexcept { return bias.getTargetValue(); }
  49. /** Sets the length of the ramp used for smoothing gain changes. */
  50. void setRampDurationSeconds (double newDurationSeconds) noexcept
  51. {
  52. if (rampDurationSeconds != newDurationSeconds)
  53. {
  54. rampDurationSeconds = newDurationSeconds;
  55. updateRamp();
  56. }
  57. }
  58. double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
  59. //==============================================================================
  60. /** Called before processing starts */
  61. void prepare (const ProcessSpec& spec) noexcept
  62. {
  63. sampleRate = spec.sampleRate;
  64. updateRamp();
  65. }
  66. void reset() noexcept
  67. {
  68. bias.reset (sampleRate, rampDurationSeconds);
  69. }
  70. //==============================================================================
  71. /** Returns the result of processing a single sample. */
  72. template <typename SampleType>
  73. SampleType processSample (SampleType inputSample) noexcept
  74. {
  75. return inputSample + bias.getNextValue();
  76. }
  77. //==============================================================================
  78. /** Processes the input and output buffers supplied in the processing context. */
  79. template <typename ProcessContext>
  80. void process (const ProcessContext& context) noexcept
  81. {
  82. auto&& inBlock = context.getInputBlock();
  83. auto&& outBlock = context.getOutputBlock();
  84. jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
  85. jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
  86. auto len = inBlock.getNumSamples();
  87. auto numChannels = inBlock.getNumChannels();
  88. if (context.isBypassed)
  89. {
  90. bias.skip (static_cast<int> (len));
  91. if (context.usesSeparateInputAndOutputBlocks())
  92. outBlock.copyFrom (inBlock);
  93. return;
  94. }
  95. if (numChannels == 1)
  96. {
  97. auto* src = inBlock.getChannelPointer (0);
  98. auto* dst = outBlock.getChannelPointer (0);
  99. for (size_t i = 0; i < len; ++i)
  100. dst[i] = src[i] + bias.getNextValue();
  101. }
  102. else
  103. {
  104. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6255 6386)
  105. auto* biases = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
  106. for (size_t i = 0; i < len; ++i)
  107. biases[i] = bias.getNextValue();
  108. for (size_t chan = 0; chan < numChannels; ++chan)
  109. FloatVectorOperations::add (outBlock.getChannelPointer (chan),
  110. inBlock.getChannelPointer (chan),
  111. biases, static_cast<int> (len));
  112. JUCE_END_IGNORE_WARNINGS_MSVC
  113. }
  114. }
  115. private:
  116. //==============================================================================
  117. SmoothedValue<FloatType> bias;
  118. double sampleRate = 0, rampDurationSeconds = 0;
  119. void updateRamp() noexcept
  120. {
  121. if (sampleRate > 0)
  122. bias.reset (sampleRate, rampDurationSeconds);
  123. }
  124. };
  125. } // namespace dsp
  126. } // namespace juce