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.

141 lines
5.1KB

  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::dsp
  19. {
  20. //==============================================================================
  21. template <typename SampleType>
  22. Panner<SampleType>::Panner()
  23. {
  24. update();
  25. reset();
  26. }
  27. //==============================================================================
  28. template <typename SampleType>
  29. void Panner<SampleType>::setRule (Rule newRule)
  30. {
  31. currentRule = newRule;
  32. update();
  33. }
  34. template <typename SampleType>
  35. void Panner<SampleType>::setPan (SampleType newPan)
  36. {
  37. jassert (newPan >= -1.0 && newPan <= 1.0);
  38. pan = jlimit (static_cast<SampleType> (-1.0), static_cast<SampleType> (1.0), newPan);
  39. update();
  40. }
  41. //==============================================================================
  42. template <typename SampleType>
  43. void Panner<SampleType>::prepare (const ProcessSpec& spec)
  44. {
  45. jassert (spec.sampleRate > 0);
  46. jassert (spec.numChannels > 0);
  47. sampleRate = spec.sampleRate;
  48. reset();
  49. }
  50. template <typename SampleType>
  51. void Panner<SampleType>::reset()
  52. {
  53. leftVolume .reset (sampleRate, 0.05);
  54. rightVolume.reset (sampleRate, 0.05);
  55. }
  56. //==============================================================================
  57. template <typename SampleType>
  58. void Panner<SampleType>::update()
  59. {
  60. SampleType leftValue, rightValue, boostValue;
  61. auto normalisedPan = static_cast<SampleType> (0.5) * (pan + static_cast<SampleType> (1.0));
  62. switch (currentRule)
  63. {
  64. case Rule::balanced:
  65. leftValue = jmin (static_cast<SampleType> (0.5), static_cast<SampleType> (1.0) - normalisedPan);
  66. rightValue = jmin (static_cast<SampleType> (0.5), normalisedPan);
  67. boostValue = static_cast<SampleType> (2.0);
  68. break;
  69. case Rule::linear:
  70. leftValue = static_cast<SampleType> (1.0) - normalisedPan;
  71. rightValue = normalisedPan;
  72. boostValue = static_cast<SampleType> (2.0);
  73. break;
  74. case Rule::sin3dB:
  75. leftValue = static_cast<SampleType> (std::sin (0.5 * MathConstants<double>::pi * (1.0 - normalisedPan)));
  76. rightValue = static_cast<SampleType> (std::sin (0.5 * MathConstants<double>::pi * normalisedPan));
  77. boostValue = std::sqrt (static_cast<SampleType> (2.0));
  78. break;
  79. case Rule::sin4p5dB:
  80. leftValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * (1.0 - normalisedPan)), 1.5));
  81. rightValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * normalisedPan), 1.5));
  82. boostValue = static_cast<SampleType> (std::pow (2.0, 3.0 / 4.0));
  83. break;
  84. case Rule::sin6dB:
  85. leftValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * (1.0 - normalisedPan)), 2.0));
  86. rightValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * normalisedPan), 2.0));
  87. boostValue = static_cast<SampleType> (2.0);
  88. break;
  89. case Rule::squareRoot3dB:
  90. leftValue = std::sqrt (static_cast<SampleType> (1.0) - normalisedPan);
  91. rightValue = std::sqrt (normalisedPan);
  92. boostValue = std::sqrt (static_cast<SampleType> (2.0));
  93. break;
  94. case Rule::squareRoot4p5dB:
  95. leftValue = static_cast<SampleType> (std::pow (std::sqrt (1.0 - normalisedPan), 1.5));
  96. rightValue = static_cast<SampleType> (std::pow (std::sqrt (normalisedPan), 1.5));
  97. boostValue = static_cast<SampleType> (std::pow (2.0, 3.0 / 4.0));
  98. break;
  99. default:
  100. leftValue = jmin (static_cast<SampleType> (0.5), static_cast<SampleType> (1.0) - normalisedPan);
  101. rightValue = jmin (static_cast<SampleType> (0.5), normalisedPan);
  102. boostValue = static_cast<SampleType> (2.0);
  103. break;
  104. }
  105. leftVolume .setTargetValue (leftValue * boostValue);
  106. rightVolume.setTargetValue (rightValue * boostValue);
  107. }
  108. //==============================================================================
  109. template class Panner<float>;
  110. template class Panner<double>;
  111. } // namespace juce::dsp