-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinearSmoother.cpp
153 lines (138 loc) · 5.53 KB
/
LinearSmoother.cpp
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
// SPDX-License-Identifier: BSD-2-Clause
// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#include "LinearSmoother.h"
#include <simde/simde-features.h>
#if SIMDE_NATURAL_VECTOR_SIZE_GE(128)
#include <simde/x86/sse.h>
#endif
#include <algorithm>
void LinearSmoother::setSampleRate(float sampleRate)
{
sampleRate_ = sampleRate;
updateParameters();
}
void LinearSmoother::setSmoothTime(float smoothTime)
{
smoothTime_ = smoothTime;
updateParameters();
}
void LinearSmoother::clear(float value)
{
current_ = value;
target_ = value;
step_ = 0.0;
}
void LinearSmoother::clearToTarget()
{
clear(target_);
}
void LinearSmoother::process(const float *input, float *output, unsigned count, bool canShortcut)
{
if (count == 0)
return;
unsigned i = 0;
float current = current_;
float target = target_;
const int smoothFrames = smoothFrames_;
if (smoothFrames < 2 || (canShortcut && current == target && current == input[0])) {
if (input != output)
std::copy_n(input, count, output);
clear(input[count - 1]);
return;
}
float step = step_;
#if SIMDE_NATURAL_VECTOR_SIZE_GE(128)
for (; i + 15 < count; i += 16) {
const float nextTarget = input[i + 15];
if (target != nextTarget) {
target = nextTarget;
step = (target - current) / std::max(16, smoothFrames);
}
const simde__m128 targetX4 = simde_mm_set1_ps(target);
if (target > current) {
simde__m128 stepX4 = simde_mm_set1_ps(step);
simde__m128 tmp1X4 = simde_mm_mul_ps(stepX4, simde_mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f));
simde__m128 tmp2X4 = simde_mm_shuffle_ps(tmp1X4, tmp1X4, SIMDE_MM_SHUFFLE(3, 3, 3, 3));
simde__m128 current1X4 = simde_mm_add_ps(simde_mm_set1_ps(current), tmp1X4);
simde_mm_storeu_ps(&output[i], simde_mm_min_ps(current1X4, targetX4));
simde__m128 current2X4 = simde_mm_add_ps(current1X4, tmp2X4);
simde_mm_storeu_ps(&output[i + 4], simde_mm_min_ps(current2X4, targetX4));
simde__m128 current3X4 = simde_mm_add_ps(current2X4, tmp2X4);
simde_mm_storeu_ps(&output[i + 8], simde_mm_min_ps(current3X4, targetX4));
simde__m128 current4X4 = simde_mm_add_ps(current3X4, tmp2X4);
simde__m128 limited4X4 = simde_mm_min_ps(current4X4, targetX4);
simde_mm_storeu_ps(&output[i + 12], limited4X4);
current = simde_mm_cvtss_f32(simde_mm_shuffle_ps(limited4X4, limited4X4, SIMDE_MM_SHUFFLE(3, 3, 3, 3)));
}
else if (target < current) {
simde__m128 stepX4 = simde_mm_set1_ps(step);
simde__m128 tmp1X4 = simde_mm_mul_ps(stepX4, simde_mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f));
simde__m128 tmp2X4 = simde_mm_shuffle_ps(tmp1X4, tmp1X4, SIMDE_MM_SHUFFLE(3, 3, 3, 3));
simde__m128 current1X4 = simde_mm_add_ps(simde_mm_set1_ps(current), tmp1X4);
simde_mm_storeu_ps(&output[i], simde_mm_max_ps(current1X4, targetX4));
simde__m128 current2X4 = simde_mm_add_ps(current1X4, tmp2X4);
simde_mm_storeu_ps(&output[i + 4], simde_mm_max_ps(current2X4, targetX4));
simde__m128 current3X4 = simde_mm_add_ps(current2X4, tmp2X4);
simde_mm_storeu_ps(&output[i + 8], simde_mm_max_ps(current3X4, targetX4));
simde__m128 current4X4 = simde_mm_add_ps(current3X4, tmp2X4);
simde__m128 limited4X4 = simde_mm_max_ps(current4X4, targetX4);
simde_mm_storeu_ps(&output[i + 12], limited4X4);
current = simde_mm_cvtss_f32(simde_mm_shuffle_ps(limited4X4, limited4X4, SIMDE_MM_SHUFFLE(3, 3, 3, 3)));
}
else {
simde_mm_storeu_ps(&output[i], targetX4);
simde_mm_storeu_ps(&output[i + 4], targetX4);
simde_mm_storeu_ps(&output[i + 8], targetX4);
simde_mm_storeu_ps(&output[i + 12], targetX4);
}
}
#else
for (; i + 15 < count; i += 16) {
const float nextTarget = input[i + 15];
if (target != nextTarget) {
target = nextTarget;
step = (target - current) / std::max(16, smoothFrames);
}
if (target > current) {
for (size_t j = 0; j < 16; ++j)
output[i + j] = current = std::min(target, current + step);
}
else if (target < current) {
for (size_t j = 0; j < 16; ++j)
output[i + j] = current = std::max(target, current + step);
}
else {
for (size_t j = 0; j < 16; ++j)
output[i + j] = target;
}
}
#endif
if (i < count) {
const float nextTarget = input[count - 1];
if (target != nextTarget) {
target = nextTarget;
step = (target - current) / std::max((int)(count - i), smoothFrames);
}
if (target > current) {
for (; i < count; ++i)
output[i] = current = std::min(target, current + step);
}
else if (target < current) {
for (; i < count; ++i)
output[i] = current = std::max(target, current + step);
}
else {
for (; i < count; ++i)
output[i] = target;
}
}
current_ = current;
target_ = target;
step_ = step;
}
void LinearSmoother::updateParameters()
{
smoothFrames_ = (int)(smoothTime_ * sampleRate_);
}