-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathGainInterp.cpp
More file actions
154 lines (128 loc) · 5.45 KB
/
GainInterp.cpp
File metadata and controls
154 lines (128 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*############################################################################*/
/*# #*/
/*# Apply a vector of gains to a mono input with interpolation #*/
/*# #*/
/*# Filename: GainInterp.cpp #*/
/*# Version: 0.1 #*/
/*# Date: 30/10/2020 #*/
/*# Author(s): Peter Stitt #*/
/*# Licence: LGPL + proprietary #*/
/*# #*/
/*############################################################################*/
#include "GainInterp.h"
#include <assert.h>
#include <cstddef>
#include <cmath>
namespace spaudio {
template<typename T>
GainInterp<T>::GainInterp(unsigned int nCh) : m_currentGainVec(nCh), m_targetGainVec(nCh), m_targetGainVecTmp(nCh), m_deltaGainVec(nCh)
{
}
template<typename T>
GainInterp<T>::~GainInterp()
{
}
template<typename T>
void GainInterp<T>::SetGainValue(T newGainVal, unsigned int interpTimeInSamples)
{
for (auto& g : m_targetGainVecTmp)
g = newGainVal;
SetGainVector(m_targetGainVecTmp, interpTimeInSamples);
}
template<typename T>
void GainInterp<T>::SetGainVector(const std::vector<T>& newGainVec, unsigned int interpTimeInSamples)
{
assert(newGainVec.size() == m_targetGainVec.size()); //Number of channels must match!
if (m_targetGainVec != newGainVec)
{
if (interpTimeInSamples > 0)
{
m_targetGainVec = newGainVec;
for (size_t i = 0; i < m_targetGainVec.size(); ++i)
m_deltaGainVec[i] = (m_targetGainVec[i] - m_currentGainVec[i]) / static_cast<T>(interpTimeInSamples);
m_interpDurInSamples = interpTimeInSamples;
// Reset the interpolation counter to start interpolation
m_iInterpCount = 0;
}
else
{
// If smoothing time is zero samples then set current and target vectors to the current value and do not interpolate
m_targetGainVec = newGainVec;
m_currentGainVec = newGainVec;
for (auto& g : m_deltaGainVec)
g = static_cast<T>(0.);
m_interpDurInSamples = interpTimeInSamples;
m_iInterpCount = m_interpDurInSamples;
}
}
}
template<typename T>
void GainInterp<T>::Process(const float* pIn, float** ppOut, unsigned int nSamples, unsigned int nOffset)
{
if (m_isFirstCall)
{
Reset();
m_isFirstCall = false;
}
unsigned int nCh = (unsigned int)m_targetGainVec.size();
// The number of samples to interpolate over in this block
unsigned int nInterpSamples = std::min(nSamples, m_interpDurInSamples - m_iInterpCount);
if (m_iInterpCount < m_interpDurInSamples)
{
for (unsigned int iCh = 0; iCh < nCh; ++iCh)
for (unsigned int i = 0; i < nInterpSamples; ++i)
{
ppOut[iCh][i + nOffset] = pIn[i] * static_cast<float>(m_currentGainVec[iCh]);
m_currentGainVec[iCh] += m_deltaGainVec[iCh];
}
m_iInterpCount += nInterpSamples;
}
for (unsigned int iCh = 0; iCh < nCh; ++iCh)
{
float gain = static_cast<float>(m_targetGainVec[iCh]);
if (std::abs(gain) < 1e-5f)
continue;
for (unsigned int i = nInterpSamples; i < nSamples; ++i)
ppOut[iCh][i + nOffset] = pIn[i] * gain;
}
}
template<typename T>
void GainInterp<T>::ProcessAccumul(const float* pIn, float** ppOut, unsigned int nSamples, unsigned int nOffset, T gain)
{
if (m_isFirstCall)
{
Reset();
m_isFirstCall = false;
}
unsigned int nCh = (unsigned int)m_targetGainVec.size();
// The number of samples to interpolate over in this block
unsigned int nInterpSamples = std::min(nSamples, m_interpDurInSamples - m_iInterpCount);
if (m_iInterpCount < m_interpDurInSamples)
{
for (unsigned int iCh = 0; iCh < nCh; ++iCh)
for (unsigned int i = 0; i < nInterpSamples; ++i)
{
ppOut[iCh][i + nOffset] += pIn[i] * static_cast<float>(m_currentGainVec[iCh] * gain);
m_currentGainVec[iCh] += m_deltaGainVec[iCh];
}
m_iInterpCount += nInterpSamples;
}
for (unsigned int iCh = 0; iCh < nCh; ++iCh)
{
float gain = static_cast<float>(m_targetGainVec[iCh]);
if (std::abs(gain) < 1e-5f)
continue;
for (unsigned int i = nInterpSamples; i < nSamples; ++i)
ppOut[iCh][i + nOffset] += pIn[i] * gain;
}
}
template<typename T>
void GainInterp<T>::Reset()
{
m_iInterpCount = m_interpDurInSamples;
m_currentGainVec = m_targetGainVec;
m_isFirstCall = true;
}
template class GainInterp<float>;
template class GainInterp<double>;
} // namespace spaudio