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.

147 lines
4.9KB

  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. */
  30. template <typename FloatType>
  31. class Bias
  32. {
  33. public:
  34. Bias() noexcept {}
  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.setValue(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) const 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 (numChannels == 1)
  89. {
  90. auto* src = inBlock.getChannelPointer (0);
  91. auto* dst = outBlock.getChannelPointer (0);
  92. for (size_t i = 0; i < len; ++i)
  93. dst[i] = src[i] + bias.getNextValue();
  94. }
  95. else
  96. {
  97. auto* biases = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
  98. for (size_t i = 0; i < len; ++i)
  99. biases[i] = bias.getNextValue();
  100. for (size_t chan = 0; chan < numChannels; ++chan)
  101. FloatVectorOperations::add (outBlock.getChannelPointer (chan),
  102. inBlock.getChannelPointer (chan),
  103. biases, static_cast<int> (len));
  104. }
  105. }
  106. private:
  107. //==============================================================================
  108. LinearSmoothedValue<FloatType> bias;
  109. double sampleRate = 0, rampDurationSeconds = 0;
  110. void updateRamp() noexcept
  111. {
  112. if (sampleRate > 0)
  113. bias.reset (sampleRate, rampDurationSeconds);
  114. }
  115. };
  116. } // namespace dsp
  117. } // namespace juce