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.

155 lines
4.3KB

  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. //==============================================================================
  23. template <typename SampleType>
  24. Phaser<SampleType>::Phaser()
  25. {
  26. auto oscFunction = [] (SampleType x) { return std::sin (x); };
  27. osc.initialise (oscFunction);
  28. for (auto n = 0; n < numStages; ++n)
  29. {
  30. filters.add (new FirstOrderTPTFilter<SampleType>());
  31. filters[n]->setType (FirstOrderTPTFilterType::allpass);
  32. }
  33. dryWet.setMixingRule (DryWetMixingRule::linear);
  34. }
  35. template <typename SampleType>
  36. void Phaser<SampleType>::setRate (SampleType newRateHz)
  37. {
  38. jassert (isPositiveAndBelow (newRateHz, static_cast<SampleType> (100.0)));
  39. rate = newRateHz;
  40. update();
  41. }
  42. template <typename SampleType>
  43. void Phaser<SampleType>::setDepth (SampleType newDepth)
  44. {
  45. jassert (isPositiveAndNotGreaterThan (newDepth, static_cast<SampleType> (1.0)));
  46. depth = newDepth;
  47. update();
  48. }
  49. template <typename SampleType>
  50. void Phaser<SampleType>::setCentreFrequency (SampleType newCentreHz)
  51. {
  52. jassert (isPositiveAndBelow (newCentreHz, static_cast<SampleType> (sampleRate * 0.5)));
  53. centreFrequency = newCentreHz;
  54. normCentreFrequency = mapFromLog10 (centreFrequency, static_cast<SampleType> (20.0), static_cast<SampleType> (jmin (20000.0, 0.49 * sampleRate)));
  55. }
  56. template <typename SampleType>
  57. void Phaser<SampleType>::setFeedback (SampleType newFeedback)
  58. {
  59. jassert (newFeedback >= static_cast<SampleType> (-1.0) && newFeedback <= static_cast<SampleType> (1.0));
  60. feedback = newFeedback;
  61. update();
  62. }
  63. template <typename SampleType>
  64. void Phaser<SampleType>::setMix (SampleType newMix)
  65. {
  66. jassert (isPositiveAndNotGreaterThan (newMix, static_cast<SampleType> (1.0)));
  67. mix = newMix;
  68. update();
  69. }
  70. //==============================================================================
  71. template <typename SampleType>
  72. void Phaser<SampleType>::prepare (const ProcessSpec& spec)
  73. {
  74. jassert (spec.sampleRate > 0);
  75. jassert (spec.numChannels > 0);
  76. sampleRate = spec.sampleRate;
  77. for (auto n = 0; n < numStages; ++n)
  78. filters[n]->prepare (spec);
  79. dryWet.prepare (spec);
  80. feedbackVolume.resize (spec.numChannels);
  81. lastOutput.resize (spec.numChannels);
  82. auto specDown = spec;
  83. specDown.sampleRate /= (double) maxUpdateCounter;
  84. specDown.maximumBlockSize = specDown.maximumBlockSize / (uint32) maxUpdateCounter + 1;
  85. osc.prepare (specDown);
  86. bufferFrequency.setSize (1, (int) specDown.maximumBlockSize, false, false, true);
  87. update();
  88. reset();
  89. }
  90. template <typename SampleType>
  91. void Phaser<SampleType>::reset()
  92. {
  93. std::fill (lastOutput.begin(), lastOutput.end(), static_cast<SampleType> (0));
  94. for (auto n = 0; n < numStages; ++n)
  95. filters[n]->reset();
  96. osc.reset();
  97. dryWet.reset();
  98. oscVolume.reset (sampleRate / (double) maxUpdateCounter, 0.05);
  99. for (auto& vol : feedbackVolume)
  100. vol.reset (sampleRate, 0.05);
  101. updateCounter = 0;
  102. }
  103. template <typename SampleType>
  104. void Phaser<SampleType>::update()
  105. {
  106. osc.setFrequency (rate);
  107. oscVolume.setTargetValue (depth * (SampleType) 0.5);
  108. dryWet.setWetMixProportion (mix);
  109. for (auto& vol : feedbackVolume)
  110. vol.setTargetValue (feedback);
  111. }
  112. //==============================================================================
  113. template class Phaser<float>;
  114. template class Phaser<double>;
  115. } // namespace dsp
  116. } // namespace juce