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.

173 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace dsp
  16. {
  17. //==============================================================================
  18. template <typename SampleType>
  19. DryWetMixer<SampleType>::DryWetMixer()
  20. : DryWetMixer (0)
  21. {
  22. }
  23. template <typename SampleType>
  24. DryWetMixer<SampleType>::DryWetMixer (int maximumWetLatencyInSamples)
  25. : dryDelayLine (maximumWetLatencyInSamples)
  26. {
  27. dryDelayLine.setDelay (0);
  28. update();
  29. reset();
  30. }
  31. //==============================================================================
  32. template <typename SampleType>
  33. void DryWetMixer<SampleType>::setMixingRule (MixingRule newRule)
  34. {
  35. currentMixingRule = newRule;
  36. update();
  37. }
  38. template <typename SampleType>
  39. void DryWetMixer<SampleType>::setWetMixProportion (SampleType newWetMixProportion)
  40. {
  41. jassert (isPositiveAndNotGreaterThan (newWetMixProportion, 1.0));
  42. mix = jlimit (static_cast<SampleType> (0.0), static_cast<SampleType> (1.0), newWetMixProportion);
  43. update();
  44. }
  45. template <typename SampleType>
  46. void DryWetMixer<SampleType>::setWetLatency (SampleType wetLatencySamples)
  47. {
  48. dryDelayLine.setDelay (wetLatencySamples);
  49. }
  50. //==============================================================================
  51. template <typename SampleType>
  52. void DryWetMixer<SampleType>::prepare (const ProcessSpec& spec)
  53. {
  54. jassert (spec.sampleRate > 0);
  55. jassert (spec.numChannels > 0);
  56. sampleRate = spec.sampleRate;
  57. dryDelayLine.prepare (spec);
  58. bufferDry.setSize ((int) spec.numChannels, (int) spec.maximumBlockSize, false, false, true);
  59. update();
  60. reset();
  61. }
  62. template <typename SampleType>
  63. void DryWetMixer<SampleType>::reset()
  64. {
  65. dryVolume.reset (sampleRate, 0.05);
  66. wetVolume.reset (sampleRate, 0.05);
  67. dryDelayLine.reset();
  68. }
  69. //==============================================================================
  70. template <typename SampleType>
  71. void DryWetMixer<SampleType>::pushDrySamples (const AudioBlock<const SampleType> drySamples)
  72. {
  73. jassert (drySamples.getNumChannels() <= (size_t) bufferDry.getNumChannels());
  74. auto dryBlock = AudioBlock<SampleType> (bufferDry);
  75. dryBlock = dryBlock.getSubsetChannelBlock (0, drySamples.getNumChannels()).getSubBlock (0, drySamples.getNumSamples());
  76. auto context = ProcessContextNonReplacing<SampleType>(drySamples, dryBlock);
  77. dryDelayLine.process (context);
  78. }
  79. template <typename SampleType>
  80. void DryWetMixer<SampleType>::mixWetSamples (AudioBlock<SampleType> inOutBlock)
  81. {
  82. auto dryBlock = AudioBlock<SampleType> (bufferDry);
  83. dryBlock = dryBlock.getSubsetChannelBlock (0, inOutBlock.getNumChannels()).getSubBlock (0, inOutBlock.getNumSamples());
  84. dryBlock.multiplyBy (dryVolume);
  85. inOutBlock.multiplyBy (wetVolume);
  86. inOutBlock.add (dryBlock);
  87. }
  88. //==============================================================================
  89. template <typename SampleType>
  90. void DryWetMixer<SampleType>::update()
  91. {
  92. SampleType dryValue, wetValue;
  93. switch (currentMixingRule)
  94. {
  95. case MixingRule::balanced:
  96. dryValue = static_cast<SampleType> (2.0) * jmin (static_cast<SampleType> (0.5), static_cast<SampleType> (1.0) - mix);
  97. wetValue = static_cast<SampleType> (2.0) * jmin (static_cast<SampleType> (0.5), mix);
  98. break;
  99. case MixingRule::linear:
  100. dryValue = static_cast<SampleType> (1.0) - mix;
  101. wetValue = mix;
  102. break;
  103. case MixingRule::sin3dB:
  104. dryValue = static_cast<SampleType> (std::sin (0.5 * MathConstants<double>::pi * (1.0 - mix)));
  105. wetValue = static_cast<SampleType> (std::sin (0.5 * MathConstants<double>::pi * mix));
  106. break;
  107. case MixingRule::sin4p5dB:
  108. dryValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * (1.0 - mix)), 1.5));
  109. wetValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * mix), 1.5));
  110. break;
  111. case MixingRule::sin6dB:
  112. dryValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * (1.0 - mix)), 2.0));
  113. wetValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * mix), 2.0));
  114. break;
  115. case MixingRule::squareRoot3dB:
  116. dryValue = std::sqrt (static_cast<SampleType> (1.0) - mix);
  117. wetValue = std::sqrt (mix);
  118. break;
  119. case MixingRule::squareRoot4p5dB:
  120. dryValue = static_cast<SampleType> (std::pow (std::sqrt (1.0 - mix), 1.5));
  121. wetValue = static_cast<SampleType> (std::pow (std::sqrt (mix), 1.5));
  122. break;
  123. default:
  124. dryValue = jmin (static_cast<SampleType> (0.5), static_cast<SampleType> (1.0) - mix);
  125. wetValue = jmin (static_cast<SampleType> (0.5), mix);
  126. break;
  127. }
  128. dryVolume.setTargetValue (dryValue);
  129. wetVolume.setTargetValue (wetValue);
  130. }
  131. //==============================================================================
  132. template class DryWetMixer<float>;
  133. template class DryWetMixer<double>;
  134. } // namespace dsp
  135. } // namespace juce