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.

juce_Panner.cpp 5.1KB

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