-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResample.h
More file actions
229 lines (191 loc) · 6.08 KB
/
Resample.h
File metadata and controls
229 lines (191 loc) · 6.08 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2020-2024 Parabola Research Limited
// SPDX-License-Identifier: MPL-2.0
#pragma once
#include "Assert.h"
#include "Modes.h"
#include "bungee/Bungee.h"
#include <Eigen/Dense>
#include <type_traits>
namespace Bungee::Resample {
typedef Eigen::Ref<Eigen::ArrayXXf> Ref;
typedef Eigen::Block<Ref, 1, Eigen::Dynamic, false> Row;
struct FixedToVariable
{
static inline float applyGain(float coefficient, float)
{
return coefficient;
}
template <bool first>
static inline void tap(float fixed, float &__restrict variable, float coefficient)
{
if constexpr (first)
variable = fixed * coefficient;
else
variable += fixed * coefficient;
}
};
struct VariableToFixed
{
static inline float applyGain(float coefficient, float gain)
{
return coefficient * gain;
}
template <bool>
static inline void tap(float &__restrict fixed, float variable, float coefficient)
{
fixed += variable * coefficient;
}
};
struct None
{
template <class Mode>
static inline void step(float, Ref, Row, float)
{
}
};
struct Nearest
{
template <class Mode>
static inline void step(float offset, Ref fixed, Row variable, float gain)
{
int x = int(offset + 0.5f);
for (int c = 0; c < fixed.cols(); ++c)
Mode::template tap<true>(fixed(x, c), variable(0, c), Mode::applyGain(1.f, gain));
}
};
struct Bilinear
{
template <class Mode>
static inline void step(float offset, Ref fixed, Row variable, float gain)
{
int x = int(offset);
float k = offset - x;
for (int c = 0; c < fixed.cols(); ++c)
{
Mode::template tap<true>(fixed(x + 1, c), variable(0, c), Mode::applyGain(k, gain));
Mode::template tap<false>(fixed(x, c), variable(0, c), Mode::applyGain(1.f - k, gain));
}
}
};
struct Padded
{
static constexpr auto align = std::max<int>(EIGEN_DEFAULT_ALIGN_BYTES / sizeof(float), 1);
static constexpr auto padding = (6 + align - 1) / align * align;
Eigen::ArrayXXf array;
int frameCount{};
bool allZeros{true};
Padded(int maxFrameCount, int channelCount) :
array(padding + maxFrameCount + padding, channelCount)
{
}
inline Eigen::Ref<Eigen::ArrayXXf> ref(int pad = 0)
{
return array.middleRows(padding - pad, array.rows() - 2 * (padding - pad));
}
inline Eigen::Ref<Eigen::ArrayXXf> unpadded()
{
return array.middleRows(padding, array.rows() - 2 * padding);
}
};
template <class Mode, class Interpolation, bool ratioChange>
inline void resampleInner(int variableFrameCount, Padded &fixedBuffer, float &fixedBufferOffset, Ref variableBuffer, float ratioBegin, float ratioEnd)
{
const float ratioGradient = (ratioEnd - ratioBegin) / variableFrameCount;
BUNGEE_ASSERT1(ratioChange || ratioGradient == 0.f);
if constexpr (std::is_same_v<Mode, VariableToFixed>)
fixedBuffer.array.setZero();
const auto offset = Padded::padding + fixedBufferOffset;
float ratio = ratioBegin;
for (int i = 0; i < variableFrameCount; ++i)
if constexpr (ratioChange)
{
const float x = offset + i * (ratioBegin + ratio) * .5f;
ratio = ratioBegin + ratioGradient * i;
Interpolation::template step<Mode>(x, fixedBuffer.array, variableBuffer.row(i), ratio);
}
else
{
const float x = offset + i * ratio;
Interpolation::template step<Mode>(x, fixedBuffer.array, variableBuffer.row(i), ratio);
}
fixedBufferOffset += variableFrameCount * (ratioBegin + ratio) * .5f;
fixedBufferOffset -= fixedBuffer.frameCount;
}
template <class Mode, class Interpolation>
inline int resample(Padded &fixedBuffer, float &fixedBufferOffset, Ref variableBuffer, float ratioBegin, float ratioEnd, bool alignEnd)
{
int variableFrameCount = (int)std::round(2 * (fixedBuffer.frameCount + ratioEnd - fixedBufferOffset) / (ratioBegin + ratioEnd) - 1);
const bool truncate = variableFrameCount > variableBuffer.rows();
if (truncate)
{
BUNGEE_ASSERT1(!"Resample::resample: variableBuffer is too short");
variableFrameCount = (int)variableBuffer.rows();
}
if (alignEnd)
{
const float dividend = 2 * (fixedBuffer.frameCount - fixedBufferOffset) - (variableFrameCount + 1) * ratioBegin;
const float divisor = variableFrameCount - 1;
ratioEnd = dividend / divisor;
}
if (ratioBegin != ratioEnd)
resampleInner<Mode, Interpolation, true>(variableFrameCount, fixedBuffer, fixedBufferOffset, variableBuffer, ratioBegin, ratioEnd);
else
resampleInner<Mode, Interpolation, false>(variableFrameCount, fixedBuffer, fixedBufferOffset, variableBuffer, ratioBegin, ratioEnd);
if (!(truncate || std::abs(fixedBufferOffset) < (alignEnd ? 1e-2f : (ratioBegin + ratioEnd) * 0.3f)))
{
BUNGEE_ASSERT1(!"resample landed badly");
fixedBufferOffset = 0.f;
}
return variableFrameCount;
}
typedef decltype(&resample<FixedToVariable, Nearest>) Function;
struct Operation
{
Function function;
float ratio;
};
struct Operations
{
Operation input, output;
double setup(const SampleRates &sampleRates, double pitch, ResampleMode::Enum resampleMode = {})
{
const double resampleRatio = pitch * sampleRates.input / sampleRates.output;
input.ratio = 1.f / resampleRatio;
output.ratio = resampleRatio;
if constexpr (true)
{
input.function = &resample<VariableToFixed, Bilinear>;
output.function = &resample<FixedToVariable, Bilinear>;
}
else
{
input.function = &resample<VariableToFixed, Nearest>;
output.function = &resample<FixedToVariable, Nearest>;
}
if (resampleMode == ResampleMode::forceOut)
input.function = nullptr;
else if (resampleMode == ResampleMode::forceIn)
output.function = nullptr;
else if (resampleRatio == 1.)
input.function = output.function = nullptr;
else if (resampleMode == ResampleMode::autoIn)
output.function = nullptr;
else if (resampleMode == ResampleMode::autoOut)
input.function = nullptr;
else if (resampleMode == ResampleMode::autoInOut && resampleRatio > 1.)
output.function = nullptr;
else if (resampleMode == ResampleMode::autoInOut && resampleRatio < 1.)
input.function = nullptr;
else
{
BUNGEE_ASSERT1(false);
input.function = nullptr;
}
if (!input.function)
input.ratio = 1.f;
if (!output.function)
output.ratio = 1.f;
return ((double)sampleRates.input / sampleRates.output) / output.ratio;
}
};
} // namespace Bungee::Resample