LMMS
Loading...
Searching...
No Matches
juce_Panner.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28namespace dsp
29{
30
31//==============================================================================
32template <typename SampleType>
38
39//==============================================================================
40template <typename SampleType>
42{
43 currentRule = newRule;
44 update();
45}
46
47template <typename SampleType>
48void Panner<SampleType>::setPan (SampleType newPan)
49{
50 jassert (newPan >= -1.0 && newPan <= 1.0);
51
52 pan = jlimit (static_cast<SampleType> (-1.0), static_cast<SampleType> (1.0), newPan);
53 update();
54}
55
56//==============================================================================
57template <typename SampleType>
59{
60 jassert (spec.sampleRate > 0);
61 jassert (spec.numChannels > 0);
62
64
65 reset();
66}
67
68template <typename SampleType>
70{
71 leftVolume .reset (sampleRate, 0.05);
72 rightVolume.reset (sampleRate, 0.05);
73}
74
75//==============================================================================
76template <typename SampleType>
78{
79 SampleType leftValue, rightValue, boostValue;
80
81 auto normalisedPan = static_cast<SampleType> (0.5) * (pan + static_cast<SampleType> (1.0));
82
83 switch (currentRule)
84 {
85 case Rule::balanced:
86 leftValue = jmin (static_cast<SampleType> (0.5), static_cast<SampleType> (1.0) - normalisedPan);
87 rightValue = jmin (static_cast<SampleType> (0.5), normalisedPan);
88 boostValue = static_cast<SampleType> (2.0);
89 break;
90
91 case Rule::linear:
92 leftValue = static_cast<SampleType> (1.0) - normalisedPan;
93 rightValue = normalisedPan;
94 boostValue = static_cast<SampleType> (2.0);
95 break;
96
97 case Rule::sin3dB:
98 leftValue = static_cast<SampleType> (std::sin (0.5 * MathConstants<double>::pi * (1.0 - normalisedPan)));
99 rightValue = static_cast<SampleType> (std::sin (0.5 * MathConstants<double>::pi * normalisedPan));
100 boostValue = std::sqrt (static_cast<SampleType> (2.0));
101 break;
102
103 case Rule::sin4p5dB:
104 leftValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * (1.0 - normalisedPan)), 1.5));
105 rightValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * normalisedPan), 1.5));
106 boostValue = static_cast<SampleType> (std::pow (2.0, 3.0 / 4.0));
107 break;
108
109 case Rule::sin6dB:
110 leftValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * (1.0 - normalisedPan)), 2.0));
111 rightValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * normalisedPan), 2.0));
112 boostValue = static_cast<SampleType> (2.0);
113 break;
114
115 case Rule::squareRoot3dB:
116 leftValue = std::sqrt (static_cast<SampleType> (1.0) - normalisedPan);
117 rightValue = std::sqrt (normalisedPan);
118 boostValue = std::sqrt (static_cast<SampleType> (2.0));
119 break;
120
121 case Rule::squareRoot4p5dB:
122 leftValue = static_cast<SampleType> (std::pow (std::sqrt (1.0 - normalisedPan), 1.5));
123 rightValue = static_cast<SampleType> (std::pow (std::sqrt (normalisedPan), 1.5));
124 boostValue = static_cast<SampleType> (std::pow (2.0, 3.0 / 4.0));
125 break;
126
127 default:
128 leftValue = jmin (static_cast<SampleType> (0.5), static_cast<SampleType> (1.0) - normalisedPan);
129 rightValue = jmin (static_cast<SampleType> (0.5), normalisedPan);
130 boostValue = static_cast<SampleType> (2.0);
131 break;
132 }
133
134 leftVolume .setTargetValue (leftValue * boostValue);
135 rightVolume.setTargetValue (rightValue * boostValue);
136}
137
138//==============================================================================
139template class Panner<float>;
140template class Panner<double>;
141
142} // namespace dsp
143} // namespace juce
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
Definition juce_Panner.h:52
Rule currentRule
Definition juce_Panner.h:114
PannerRule Rule
Definition juce_Panner.h:55
SmoothedValue< SampleType > leftVolume
Definition juce_Panner.h:116
void update()
Definition juce_Panner.cpp:77
void prepare(const ProcessSpec &spec)
Definition juce_Panner.cpp:58
void setPan(SampleType newPan)
Definition juce_Panner.cpp:48
void reset()
Definition juce_Panner.cpp:69
void setRule(Rule newRule)
Definition juce_Panner.cpp:41
Panner()
Definition juce_Panner.cpp:33
SmoothedValue< SampleType > rightVolume
Definition juce_Panner.h:116
SampleType pan
Definition juce_Panner.h:115
double sampleRate
Definition juce_Panner.h:117
#define jassert(expression)
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
static constexpr FloatType pi
Definition juce_MathsFunctions.h:382
Definition juce_ProcessContext.h:38
uint32 numChannels
Definition juce_ProcessContext.h:46
double sampleRate
Definition juce_ProcessContext.h:40