1#include "envelope.h"
2
3#include <base/dbg.h>
4
5#include <algorithm>
6#include <chrono>
7#include <limits>
8
9using namespace std::chrono_literals;
10
11CEnvelope::CEnvelopePointAccess::CEnvelopePointAccess(std::vector<CEnvPoint_runtime> *pvPoints)
12{
13 m_pvPoints = pvPoints;
14}
15
16int CEnvelope::CEnvelopePointAccess::NumPoints() const
17{
18 return m_pvPoints->size();
19}
20
21const CEnvPoint *CEnvelope::CEnvelopePointAccess::GetPoint(int Index) const
22{
23 if(Index < 0 || (size_t)Index >= m_pvPoints->size())
24 return nullptr;
25 return &m_pvPoints->at(n: Index);
26}
27
28const CEnvPointBezier *CEnvelope::CEnvelopePointAccess::GetBezier(int Index) const
29{
30 if(Index < 0 || (size_t)Index >= m_pvPoints->size())
31 return nullptr;
32 return &m_pvPoints->at(n: Index).m_Bezier;
33}
34
35CEnvelope::CEnvelope(EType Type) :
36 m_Type(Type), m_PointsAccess(&m_vPoints) {}
37
38CEnvelope::CEnvelope(int NumChannels) :
39 m_PointsAccess(&m_vPoints)
40{
41 switch(NumChannels)
42 {
43 case 1:
44 m_Type = EType::SOUND;
45 break;
46 case 3:
47 m_Type = EType::POSITION;
48 break;
49 case 4:
50 m_Type = EType::COLOR;
51 break;
52 default:
53 dbg_assert_failed("invalid number of channels for envelope");
54 }
55}
56
57void CEnvelope::Resort()
58{
59 std::sort(first: m_vPoints.begin(), last: m_vPoints.end());
60}
61
62std::pair<float, float> CEnvelope::GetValueRange(int ChannelMask)
63{
64 if(m_vPoints.empty() || (ChannelMask & ((1 << GetChannels()) - 1)) == 0)
65 {
66 return {0.0f, 1.0f};
67 }
68 float Top = -std::numeric_limits<float>::infinity();
69 float Bottom = std::numeric_limits<float>::infinity();
70 for(size_t PointIndex = 0; PointIndex < m_vPoints.size(); ++PointIndex)
71 {
72 const auto &Point = m_vPoints[PointIndex];
73 for(int c = 0; c < GetChannels(); c++)
74 {
75 if(ChannelMask & (1 << c))
76 {
77 {
78 // value handle
79 const float v = fx2f(v: Point.m_aValues[c]);
80 Top = std::max(a: Top, b: v);
81 Bottom = std::min(a: Bottom, b: v);
82 }
83
84 if(PointIndex < m_vPoints.size() - 1 && Point.m_Curvetype == CURVETYPE_BEZIER)
85 {
86 // out-tangent handle
87 const float v = fx2f(v: Point.m_aValues[c] + Point.m_Bezier.m_aOutTangentDeltaY[c]);
88 Top = std::max(a: Top, b: v);
89 Bottom = std::min(a: Bottom, b: v);
90 }
91
92 if(PointIndex > 0 && m_vPoints[PointIndex - 1].m_Curvetype == CURVETYPE_BEZIER)
93 {
94 // in-tangent handle
95 const float v = fx2f(v: Point.m_aValues[c] + Point.m_Bezier.m_aInTangentDeltaY[c]);
96 Top = std::max(a: Top, b: v);
97 Bottom = std::min(a: Bottom, b: v);
98 }
99 }
100 }
101 }
102 return {Bottom, Top};
103}
104
105void CEnvelope::Eval(float Time, ColorRGBA &Result, size_t Channels) const
106{
107 Channels = std::min(l: {Channels, (size_t)GetChannels(), (size_t)CEnvPoint::MAX_CHANNELS});
108 CRenderMap::RenderEvalEnvelope(pPoints: &m_PointsAccess, TimeNanos: std::chrono::nanoseconds((int64_t)((double)Time * (double)std::chrono::nanoseconds(1s).count())), Result, Channels);
109}
110
111void CEnvelope::AddPoint(CFixedTime Time, std::array<int, CEnvPoint::MAX_CHANNELS> aValues)
112{
113 CEnvPoint_runtime Point;
114 Point.m_Time = Time;
115 Point.m_Curvetype = CURVETYPE_LINEAR;
116 std::copy_n(first: aValues.begin(), n: std::size(Point.m_aValues), result: Point.m_aValues);
117 std::fill(first: std::begin(arr&: Point.m_Bezier.m_aInTangentDeltaX), last: std::end(arr&: Point.m_Bezier.m_aInTangentDeltaX), value: CFixedTime(0));
118 std::fill(first: std::begin(arr&: Point.m_Bezier.m_aInTangentDeltaY), last: std::end(arr&: Point.m_Bezier.m_aInTangentDeltaY), value: 0);
119 std::fill(first: std::begin(arr&: Point.m_Bezier.m_aOutTangentDeltaX), last: std::end(arr&: Point.m_Bezier.m_aOutTangentDeltaX), value: CFixedTime(0));
120 std::fill(first: std::begin(arr&: Point.m_Bezier.m_aOutTangentDeltaY), last: std::end(arr&: Point.m_Bezier.m_aOutTangentDeltaY), value: 0);
121 m_vPoints.emplace_back(args&: Point);
122 Resort();
123}
124
125float CEnvelope::EndTime() const
126{
127 if(m_vPoints.empty())
128 return 0.0f;
129 return m_vPoints.back().m_Time.AsSeconds();
130}
131
132int CEnvelope::FindPointIndex(CFixedTime Time) const
133{
134 return m_PointsAccess.FindPointIndex(Time);
135}
136
137int CEnvelope::GetChannels() const
138{
139 switch(m_Type)
140 {
141 case EType::POSITION:
142 return 3;
143 case EType::COLOR:
144 return 4;
145 case EType::SOUND:
146 return 1;
147 default:
148 dbg_assert_failed("unknown envelope type");
149 }
150}
151