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.

137 lines
4.9KB

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