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.

159 lines
5.1KB

  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. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. /**
  24. Adds a DC offset (voltage bias) to the audio samples.
  25. This is a useful preprocessor for asymmetric waveshaping when a waveshaper is
  26. bookended by a bias on input and a DC-offset removing high pass filter on output.
  27. This is an extremely simple bias implementation that simply adds a value to a signal.
  28. More complicated bias behaviours exist in real circuits - for your homework ;).
  29. @tags{DSP}
  30. */
  31. template <typename FloatType>
  32. class Bias
  33. {
  34. public:
  35. Bias() noexcept {}
  36. //==============================================================================
  37. /** Sets the DC bias
  38. @param newBias DC offset in range [-1, 1]
  39. */
  40. void setBias (FloatType newBias) noexcept
  41. {
  42. jassert (newBias >= static_cast<FloatType> (-1) && newBias <= static_cast<FloatType> (1));
  43. bias.setValue(newBias);
  44. }
  45. //==============================================================================
  46. /** Returns the DC bias
  47. @return DC bias, which should be in the range [-1, 1]
  48. */
  49. FloatType getBias() const noexcept { return bias.getTargetValue(); }
  50. /** Sets the length of the ramp used for smoothing gain changes. */
  51. void setRampDurationSeconds (double newDurationSeconds) noexcept
  52. {
  53. if (rampDurationSeconds != newDurationSeconds)
  54. {
  55. rampDurationSeconds = newDurationSeconds;
  56. updateRamp();
  57. }
  58. }
  59. double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
  60. //==============================================================================
  61. /** Called before processing starts */
  62. void prepare (const ProcessSpec& spec) noexcept
  63. {
  64. sampleRate = spec.sampleRate;
  65. updateRamp();
  66. }
  67. void reset() noexcept
  68. {
  69. bias.reset (sampleRate, rampDurationSeconds);
  70. }
  71. //==============================================================================
  72. /** Returns the result of processing a single sample. */
  73. template <typename SampleType>
  74. SampleType processSample (SampleType inputSample) const noexcept
  75. {
  76. return inputSample + bias.getNextValue();
  77. }
  78. //==============================================================================
  79. /** Processes the input and output buffers supplied in the processing context. */
  80. template<typename ProcessContext>
  81. void process (const ProcessContext& context) noexcept
  82. {
  83. auto&& inBlock = context.getInputBlock();
  84. auto&& outBlock = context.getOutputBlock();
  85. jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
  86. jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
  87. auto len = inBlock.getNumSamples();
  88. auto numChannels = inBlock.getNumChannels();
  89. if (context.isBypassed)
  90. {
  91. bias.skip (static_cast<int> (len));
  92. if (context.usesSeparateInputAndOutputBlocks())
  93. outBlock.copy (inBlock);
  94. return;
  95. }
  96. if (numChannels == 1)
  97. {
  98. auto* src = inBlock.getChannelPointer (0);
  99. auto* dst = outBlock.getChannelPointer (0);
  100. for (size_t i = 0; i < len; ++i)
  101. dst[i] = src[i] + bias.getNextValue();
  102. }
  103. else
  104. {
  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. }
  113. }
  114. private:
  115. //==============================================================================
  116. LinearSmoothedValue<FloatType> bias;
  117. double sampleRate = 0, rampDurationSeconds = 0;
  118. void updateRamp() noexcept
  119. {
  120. if (sampleRate > 0)
  121. bias.reset (sampleRate, rampDurationSeconds);
  122. }
  123. };
  124. } // namespace dsp
  125. } // namespace juce