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.

121 lines
4.5KB

  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. enum class DryWetMixingRule
  23. {
  24. linear, // dry volume is equal to 1 - wet volume
  25. balanced, // both dry and wet are 1 when mix is 0.5, with dry decreasing to 0
  26. // above this value and wet decreasing to 0 below it
  27. sin3dB, // alternate dry/wet mixing rule using the 3 dB sine panning rule
  28. sin4p5dB, // alternate dry/wet mixing rule using the 4.5 dB sine panning rule
  29. sin6dB, // alternate dry/wet mixing rule using the 6 dB sine panning rule
  30. squareRoot3dB, // alternate dry/wet mixing rule using the regular 3 dB panning rule
  31. squareRoot4p5dB // alternate dry/wet mixing rule using the regular 4.5 dB panning rule
  32. };
  33. /**
  34. A processor to handle dry/wet mixing of two audio signals, where the wet signal
  35. may have additional latency.
  36. Once a DryWetMixer object is configured, push the dry samples using pushDrySamples
  37. and mix into the fully wet samples using mixWetSamples.
  38. @tags{DSP}
  39. */
  40. template <typename SampleType>
  41. class DryWetMixer
  42. {
  43. public:
  44. //==============================================================================
  45. using MixingRule = DryWetMixingRule;
  46. //==============================================================================
  47. /** Default constructor. */
  48. DryWetMixer();
  49. /** Constructor. */
  50. explicit DryWetMixer (int maximumWetLatencyInSamples);
  51. //==============================================================================
  52. /** Sets the mix rule. */
  53. void setMixingRule (MixingRule newRule);
  54. /** Sets the current dry/wet mix proportion, with 0.0 being full dry and 1.0
  55. being fully wet.
  56. */
  57. void setWetMixProportion (SampleType newWetMixProportion);
  58. /** Sets the relative latency of the wet signal path compared to the dry signal
  59. path, and thus the amount of latency compensation that will be added to the
  60. dry samples in this processor.
  61. */
  62. void setWetLatency (SampleType wetLatencyInSamples);
  63. //==============================================================================
  64. /** Initialises the processor. */
  65. void prepare (const ProcessSpec& spec);
  66. /** Resets the internal state variables of the processor. */
  67. void reset();
  68. //==============================================================================
  69. /** Copies the dry path samples into an internal delay line. */
  70. void pushDrySamples (const AudioBlock<const SampleType> drySamples);
  71. /** Mixes the supplied wet samples with the latency-compensated dry samples from
  72. pushDrySamples.
  73. @param wetSamples Input: The AudioBlock references fully wet samples.
  74. Output: The AudioBlock references the wet samples mixed
  75. with the latency compensated dry samples.
  76. @see pushDrySamples
  77. */
  78. void mixWetSamples (AudioBlock<SampleType> wetSamples);
  79. private:
  80. //==============================================================================
  81. void update();
  82. //==============================================================================
  83. SmoothedValue<SampleType, ValueSmoothingTypes::Linear> dryVolume, wetVolume;
  84. DelayLine<SampleType, DelayLineInterpolationTypes::Thiran> dryDelayLine;
  85. AudioBuffer<SampleType> bufferDry;
  86. SingleThreadedAbstractFifo fifo;
  87. SampleType mix = 1.0;
  88. MixingRule currentMixingRule = MixingRule::linear;
  89. double sampleRate = 44100.0;
  90. int maximumWetLatencyInSamples = 0;
  91. };
  92. } // namespace dsp
  93. } // namespace juce