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.

200 lines
6.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. A 6 stage phaser that modulates first order all-pass filters to create sweeping
  19. notches in the magnitude frequency response.
  20. This audio effect can be controlled with standard phaser parameters: the speed
  21. and depth of the LFO controlling the frequency response, a mix control, a
  22. feedback control, and the centre frequency of the modulation.
  23. @tags{DSP}
  24. */
  25. template <typename SampleType>
  26. class Phaser
  27. {
  28. public:
  29. //==============================================================================
  30. /** Constructor. */
  31. Phaser();
  32. //==============================================================================
  33. /** Sets the rate (in Hz) of the LFO modulating the phaser all-pass filters. This
  34. rate must be lower than 100 Hz.
  35. */
  36. void setRate (SampleType newRateHz);
  37. /** Sets the volume (between 0 and 1) of the LFO modulating the phaser all-pass
  38. filters.
  39. */
  40. void setDepth (SampleType newDepth);
  41. /** Sets the centre frequency (in Hz) of the phaser all-pass filters modulation.
  42. */
  43. void setCentreFrequency (SampleType newCentreHz);
  44. /** Sets the feedback volume (between -1 and 1) of the phaser. Negative can be
  45. used to get specific phaser sounds.
  46. */
  47. void setFeedback (SampleType newFeedback);
  48. /** Sets the amount of dry and wet signal in the output of the phaser (between 0
  49. for full dry and 1 for full wet).
  50. */
  51. void setMix (SampleType newMix);
  52. //==============================================================================
  53. /** Initialises the processor. */
  54. void prepare (const ProcessSpec& spec);
  55. /** Resets the internal state variables of the processor. */
  56. void reset();
  57. //==============================================================================
  58. /** Processes the input and output samples supplied in the processing context. */
  59. template <typename ProcessContext>
  60. void process (const ProcessContext& context) noexcept
  61. {
  62. const auto& inputBlock = context.getInputBlock();
  63. auto& outputBlock = context.getOutputBlock();
  64. const auto numChannels = outputBlock.getNumChannels();
  65. const auto numSamples = outputBlock.getNumSamples();
  66. jassert (inputBlock.getNumChannels() == numChannels);
  67. jassert (inputBlock.getNumChannels() == lastOutput.size());
  68. jassert (inputBlock.getNumSamples() == numSamples);
  69. if (context.isBypassed)
  70. {
  71. outputBlock.copyFrom (inputBlock);
  72. return;
  73. }
  74. int numSamplesDown = 0;
  75. auto counter = updateCounter;
  76. for (size_t i = 0; i < numSamples; ++i)
  77. {
  78. if (counter == 0)
  79. numSamplesDown++;
  80. counter++;
  81. if (counter == maxUpdateCounter)
  82. counter = 0;
  83. }
  84. if (numSamplesDown > 0)
  85. {
  86. auto freqBlock = AudioBlock<SampleType>(bufferFrequency).getSubBlock (0, (size_t) numSamplesDown);
  87. auto contextFreq = ProcessContextReplacing<SampleType> (freqBlock);
  88. freqBlock.clear();
  89. osc.process (contextFreq);
  90. freqBlock.multiplyBy (oscVolume);
  91. }
  92. auto* freqSamples = bufferFrequency.getWritePointer (0);
  93. for (int i = 0; i < numSamplesDown; ++i)
  94. {
  95. auto lfo = jlimit (static_cast<SampleType> (0.0),
  96. static_cast<SampleType> (1.0),
  97. freqSamples[i] + normCentreFrequency);
  98. freqSamples[i] = mapToLog10 (lfo, static_cast<SampleType> (20.0),
  99. static_cast<SampleType> (jmin (20000.0, 0.49 * sampleRate)));
  100. }
  101. auto currentFrequency = filters[0]->getCutoffFrequency();
  102. dryWet.pushDrySamples (inputBlock);
  103. for (size_t channel = 0; channel < numChannels; ++channel)
  104. {
  105. counter = updateCounter;
  106. int k = 0;
  107. auto* inputSamples = inputBlock .getChannelPointer (channel);
  108. auto* outputSamples = outputBlock.getChannelPointer (channel);
  109. for (size_t i = 0; i < numSamples; ++i)
  110. {
  111. auto input = inputSamples[i];
  112. auto output = input - lastOutput[channel];
  113. if (i == 0 && counter != 0)
  114. for (int n = 0; n < numStages; ++n)
  115. filters[n]->setCutoffFrequency (currentFrequency);
  116. if (counter == 0)
  117. {
  118. for (int n = 0; n < numStages; ++n)
  119. filters[n]->setCutoffFrequency (freqSamples[k]);
  120. k++;
  121. }
  122. for (int n = 0; n < numStages; ++n)
  123. output = filters[n]->processSample ((int) channel, output);
  124. outputSamples[i] = output;
  125. lastOutput[channel] = output * feedbackVolume[channel].getNextValue();
  126. counter++;
  127. if (counter == maxUpdateCounter)
  128. counter = 0;
  129. }
  130. }
  131. dryWet.mixWetSamples (outputBlock);
  132. updateCounter = (updateCounter + (int) numSamples) % maxUpdateCounter;
  133. }
  134. private:
  135. //==============================================================================
  136. void update();
  137. //==============================================================================
  138. Oscillator<SampleType> osc;
  139. OwnedArray<FirstOrderTPTFilter<SampleType>> filters;
  140. SmoothedValue<SampleType, ValueSmoothingTypes::Linear> oscVolume;
  141. std::vector<SmoothedValue<SampleType, ValueSmoothingTypes::Linear>> feedbackVolume { 2 };
  142. DryWetMixer<SampleType> dryWet;
  143. std::vector<SampleType> lastOutput { 2 };
  144. AudioBuffer<SampleType> bufferFrequency;
  145. SampleType normCentreFrequency = 0.5;
  146. double sampleRate = 44100.0;
  147. int updateCounter = 0;
  148. static constexpr int maxUpdateCounter = 4;
  149. SampleType rate = 1.0, depth = 0.5, feedback = 0.0, mix = 0.5;
  150. SampleType centreFrequency = 1300.0;
  151. static constexpr int numStages = 6;
  152. };
  153. } // namespace dsp
  154. } // namespace juce