OpenShot Audio Library | OpenShotAudio 0.4.0

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