The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

139 lines
4.8KB

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