-
Notifications
You must be signed in to change notification settings - Fork 50
/
choc_SincInterpolator.h
157 lines (131 loc) · 6.34 KB
/
choc_SincInterpolator.h
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
//
// ██████ ██ ██ ██████ ██████
// ██ ██ ██ ██ ██ ██ ** Classy Header-Only Classes **
// ██ ███████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ https://github.com/Tracktion/choc
// ██████ ██ ██ ██████ ██████
//
// CHOC is (C)2022 Tracktion Corporation, and is offered under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef CHOC_SINC_INTERPOLATE_HEADER_INCLUDED
#define CHOC_SINC_INTERPOLATE_HEADER_INCLUDED
#include <cmath>
#include "choc_SampleBuffers.h"
namespace choc::interpolation
{
/**
Resamples the data from a choc::buffer::InterleavedView or choc::buffer::ChannelArrayView
into a destination view, using a sinc interpolation algorithm.
The source and destination buffers are expected to be different sizes, and the data will
be resampled to fit the destination, which may involve up- or down-sampling depending on
the sizes.
The number of zero-crossings determines the quality, and larger numbers will be increasingly
slow to calculate, with diminishing returns. A value of around 50 should be good enough for
any normal purpose.
*/
template <typename DestBufferOrView, typename SourceBufferOrView, uint32_t numZeroCrossings = 50>
void sincInterpolate (DestBufferOrView&& destBuffer, const SourceBufferOrView& sourceBuffer)
{
using Sample = typename std::remove_reference<DestBufferOrView>::type::Sample;
using MonoBufferView = choc::buffer::MonoView<Sample>;
constexpr auto floatZeroCrossings = static_cast<Sample> (numZeroCrossings);
constexpr auto floatPi = static_cast<Sample> (3.141592653589793238);
constexpr auto half = static_cast<Sample> (0.5);
constexpr auto one = static_cast<Sample> (1);
struct InterpolationFunctions
{
static void resampleMono (MonoBufferView destBuffer, MonoBufferView sourceBuffer)
{
auto destSize = destBuffer.getNumFrames();
auto sourceSize = sourceBuffer.getNumFrames();
if (destSize > sourceSize)
{
resampleMono (destBuffer, sourceBuffer, 1.0f);
}
else
{
choc::buffer::MonoBuffer<Sample> bandlimitedIntermediate (1, sourceSize);
resampleMono (bandlimitedIntermediate, sourceBuffer, static_cast<float> (destSize) / static_cast<float> (sourceSize));
resampleMono (destBuffer, bandlimitedIntermediate, 1.0f);
}
}
static void resampleMono (MonoBufferView destBuffer, MonoBufferView sourceBuffer, float ratio) noexcept
{
CHOC_ASSERT (sourceBuffer.data.stride == 1);
auto numFrames = destBuffer.getNumFrames();
double sourcePos = 0;
auto sourceStride = sourceBuffer.getNumFrames() / static_cast<double> (numFrames);
auto dest = destBuffer.data.data;
auto destStride = destBuffer.data.stride;
for (decltype (numFrames) i = 0; i < numFrames; ++i)
{
*dest = calculateSample (sourceBuffer, sourcePos, ratio);
dest += destStride;
sourcePos += sourceStride;
}
}
static Sample sincWindowFunction (Sample position)
{
return std::sin (position)
* (half + half * std::cos (position * (one / floatZeroCrossings)))
/ position;
}
static Sample getSincWindowLevelAt (Sample position)
{
if (position == Sample())
return one;
if (position < -floatZeroCrossings || position > floatZeroCrossings)
return {};
return sincWindowFunction (position * floatPi);
}
static Sample calculateSample (MonoBufferView source, double position, float ratio) noexcept
{
auto sourcePosition = static_cast<int> (position);
auto fractionalOffset = static_cast<float> (position - static_cast<double> (sourcePosition));
if (fractionalOffset > 0)
{
fractionalOffset = one - fractionalOffset;
++sourcePosition;
}
auto data = source.data.data;
auto numSourceFrames = source.getNumFrames();
Sample total = {};
auto numCrossings = static_cast<int> (floatZeroCrossings / ratio);
for (int i = -numCrossings; i <= numCrossings; ++i)
{
auto sourceIndex = static_cast<uint32_t> (sourcePosition + i);
if (sourceIndex < numSourceFrames)
{
auto windowPos = static_cast<Sample> (fractionalOffset + (ratio * static_cast<float> (i)));
total += getSincWindowLevelAt (windowPos) * data[sourceIndex];
}
}
return total * ratio;
}
};
// The source and dest must have the same number of channels
auto numChans = destBuffer.getNumChannels();
CHOC_ASSERT (sourceBuffer.getNumChannels() == numChans);
if (! destBuffer.getSize().isEmpty())
{
if (destBuffer.getNumFrames() != sourceBuffer.getNumFrames())
{
for (decltype (numChans) i = 0; i < numChans; ++i)
InterpolationFunctions::resampleMono (destBuffer.getChannel(i), sourceBuffer.getChannel(i));
}
else
{
copy (destBuffer, sourceBuffer);
}
}
}
} // namespace choc::audio
#endif