tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_AudioFadeCurve.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
15{
17 struct Linear
18 {
19 inline static double preadjust (double alpha) noexcept { return alpha; }
20 inline static float get (float alpha) noexcept { return alpha; }
21 };
22
24 struct Convex
25 {
26 inline static double preadjust (double alpha) noexcept { return alpha * (0.5 * juce::MathConstants<double>::pi); }
27 inline static float get (float alpha) noexcept { return std::sin (alpha); }
28 };
29
31 struct Concave
32 {
33 inline static double preadjust (double alpha) noexcept { return alpha * (0.5 * juce::MathConstants<double>::pi); }
34 inline static float get (float alpha) noexcept { return 1.0f - std::cos (alpha); }
35 };
36
38 struct SCurve
39 {
40 inline static double preadjust (double alpha) noexcept { return alpha; }
41 inline static float get (float alpha) noexcept { return (1.0f - alpha) * (1.0f - std::cos (alpha * (0.5f * juce::MathConstants<float>::pi)))
42 + alpha * std::sin (alpha * (0.5f * juce::MathConstants<float>::pi)); }
43 };
44
46 enum Type
47 {
48 linear = 1,
49 convex = 2,
50 concave = 3,
51 sCurve = 4
52 };
53
55 template <typename CurveClass>
56 static float alphaToGain (float alpha) noexcept
57 {
58 jassert (alpha >= 0 && alpha <= 1.0f);
59 return CurveClass::get ((float) CurveClass::preadjust (alpha));
60 }
61
63 static float alphaToGainForType (Type type, float alpha) noexcept
64 {
65 if (type == convex) return alphaToGain<Convex> (alpha);
66 if (type == concave) return alphaToGain<Concave> (alpha);
67 if (type == sCurve) return alphaToGain<SCurve> (alpha);
68
69 jassert (type == linear);
70 return alpha;
71 }
72
77 template <typename CurveClass, typename DestSamplePointer>
78 static void renderBlock (DestSamplePointer& dest, int numSamples,
79 float startAlpha, float endAlpha) noexcept
80 {
81 jassert (numSamples > 0); // can only be used on non-empty blocks!
82
83 const double a1 = CurveClass::preadjust (startAlpha);
84 const double a2 = CurveClass::preadjust (endAlpha);
85 const double delta = (a2 - a1) / numSamples;
86 double alpha = a1;
87
88 while (--numSamples >= 0)
89 {
90 dest.apply (CurveClass::get ((float) alpha));
91 alpha += delta;
92 }
93 }
94
99 template <typename DestSamplePointer>
100 static void renderBlockForType (DestSamplePointer& dest, int numSamples,
101 float startAlpha, float endAlpha, Type type) noexcept
102 {
103 jassert (numSamples > 0);
104
105 switch (type)
106 {
107 case linear: renderBlock<Linear> (dest, numSamples, startAlpha, endAlpha); break;
108 case convex: renderBlock<Convex> (dest, numSamples, startAlpha, endAlpha); break;
109 case concave: renderBlock<Concave> (dest, numSamples, startAlpha, endAlpha); break;
110 case sCurve: renderBlock<SCurve> (dest, numSamples, startAlpha, endAlpha); break;
111 default: jassertfalse; break;
112 }
113 }
114
122 {
127 CrossfadeLevels (float alpha) noexcept
128 : gain1 (alphaToGain<AudioFadeCurve::Convex> (alpha)),
129 gain2 (alphaToGain<AudioFadeCurve::Convex> (1.0f - alpha))
130 {
131 }
132
133 float gain1, gain2;
134 };
135
136 static void applyCrossfadeSection (juce::AudioBuffer<float>&, int channel,
137 int startSample, int numSamples,
138 Type type,
139 float startAlpha,
140 float endAlpha);
141
142 static void applyCrossfadeSection (juce::AudioBuffer<float>&,
143 int startSample, int numSamples,
144 Type type,
145 float startAlpha,
146 float endAlpha);
147
148 static void addWithCrossfade (juce::AudioBuffer<float>& dest,
149 const juce::AudioBuffer<float>& src,
150 int destChannel, int destStartIndex,
151 int sourceChannel, int sourceStartIndex,
152 int numSamples,
153 Type type,
154 float startAlpha,
155 float endAlpha);
156
157 static void drawFadeCurve (juce::Graphics&, const AudioFadeCurve::Type,
158 float x1, float x2, float top, float bottom, juce::Rectangle<int> clip);
159};
160
161}} // namespace tracktion { inline namespace engine
162
163
164namespace juce
165{
166 template <>
167 struct VariantConverter<tracktion::engine::AudioFadeCurve::Type>
168 {
169 static tracktion::engine::AudioFadeCurve::Type fromVar (const var& v) { return (tracktion::engine::AudioFadeCurve::Type) static_cast<int> (v); }
170 static var toVar (tracktion::engine::AudioFadeCurve::Type v) { return static_cast<int> (v); }
171 };
172}
T cos(T... args)
#define jassert(expression)
#define jassertfalse
T sin(T... args)
Calculates the two gain multipliers to use for mixing between two sources, given a position alpha fro...
CrossfadeLevels(float alpha) noexcept
Creates the object and calculates the two gain levels.
static float alphaToGain(float alpha) noexcept
Converts an alpha position along the curve (0 to 1.0) into the gain at that point.
Type
A enumeration of the curve classes available.
static void renderBlockForType(DestSamplePointer &dest, int numSamples, float startAlpha, float endAlpha, Type type) noexcept
Multiplies a block of samples by the curve shape between two alpha-positions.
static float alphaToGainForType(Type type, float alpha) noexcept
Converts an alpha position along the curve (0 to 1.0) into the gain at that point.
static void renderBlock(DestSamplePointer &dest, int numSamples, float startAlpha, float endAlpha) noexcept
Multiplies a block of samples by the curve shape between two alpha-positions.