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.

122 lines
4.4KB

  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. enum class PannerRule
  23. {
  24. linear, // regular 6 dB or linear panning rule, allows the panned sound to be
  25. // perceived as having a constant level when summed to mono
  26. balanced, // both left and right are 1 when pan value is 0, with left decreasing
  27. // to 0 above this value and right decreasing to 0 below it
  28. sin3dB, // alternate version of the regular 3 dB panning rule with a sine curve
  29. sin4p5dB, // alternate version of the regular 4.5 dB panning rule with a sine curve
  30. sin6dB, // alternate version of the regular 6 dB panning rule with a sine curve
  31. squareRoot3dB, // regular 3 dB or constant power panning rule, allows the panned sound
  32. // to be perceived as having a constant level regardless of the pan position
  33. squareRoot4p5dB // regular 4.5 dB panning rule, a compromise option between 3 dB and 6 dB panning rules
  34. };
  35. /**
  36. A processor to perform panning operations on stereo buffers.
  37. @tags{DSP}
  38. */
  39. template <typename SampleType>
  40. class Panner
  41. {
  42. public:
  43. //==============================================================================
  44. using Rule = PannerRule;
  45. //==============================================================================
  46. /** Constructor. */
  47. Panner();
  48. //==============================================================================
  49. /** Sets the panning rule. */
  50. void setRule (Rule newRule);
  51. /** Sets the current panning value, between -1 (full left) and 1 (full right). */
  52. void setPan (SampleType newPan);
  53. //==============================================================================
  54. /** Initialises the processor. */
  55. void prepare (const ProcessSpec& spec);
  56. /** Resets the internal state variables of the processor. */
  57. void reset();
  58. //==============================================================================
  59. /** Processes the input and output samples supplied in the processing context. */
  60. template <typename ProcessContext>
  61. void process (const ProcessContext& context) noexcept
  62. {
  63. const auto& inputBlock = context.getInputBlock();
  64. auto& outputBlock = context.getOutputBlock();
  65. const auto numInputChannels = inputBlock.getNumChannels();
  66. const auto numOutputChannels = outputBlock.getNumChannels();
  67. const auto numSamples = outputBlock.getNumSamples();
  68. jassertquiet (inputBlock.getNumSamples() == numSamples);
  69. if (numOutputChannels != 2 || numInputChannels == 0 || numInputChannels > 2)
  70. return;
  71. if (numInputChannels == 2)
  72. {
  73. outputBlock.copyFrom (inputBlock);
  74. }
  75. else
  76. {
  77. outputBlock.getSingleChannelBlock (0).copyFrom (inputBlock);
  78. outputBlock.getSingleChannelBlock (1).copyFrom (inputBlock);
  79. }
  80. if (context.isBypassed)
  81. return;
  82. outputBlock.getSingleChannelBlock (0).multiplyBy (leftVolume);
  83. outputBlock.getSingleChannelBlock (1).multiplyBy (rightVolume);
  84. }
  85. private:
  86. //==============================================================================
  87. void update();
  88. //==============================================================================
  89. Rule currentRule = Rule::balanced;
  90. SampleType pan = 0.0;
  91. SmoothedValue<SampleType> leftVolume, rightVolume;
  92. double sampleRate = 44100.0;
  93. };
  94. } // namespace dsp
  95. } // namespace juce