-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalgorithms.cpp
More file actions
135 lines (119 loc) · 2.68 KB
/
algorithms.cpp
File metadata and controls
135 lines (119 loc) · 2.68 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
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <cstring>
#include "algorithms.h"
inline float float_abs(float val)
{
return val >= 0? val : -val;
}
inline float float_clamp(float x, float min, float max)
{
if (x > max)
{
x = max;
}
if (x < min)
{
x = min;
}
return x;
}
int clip_detect_std_alg(const AudioBuffer& buffer)
{
auto [min_el, max_el] = std::minmax_element(buffer.begin(), buffer.end());
if (*min_el < -1.0f || *max_el > 1.0f)
{
return 1;
}
return 0;
}
int clip_detection_custom_minmax(const AudioBuffer& buffer)
{
float min = std::numeric_limits<float>::max();
float max = std::numeric_limits<float>::min();
for (const auto& sample : buffer)
{
min = sample < min? sample : min;
max = sample > max? sample : max;
}
if (min < -1.0f || max > 1.0f)
{
return 1;
}
return 0;
}
int clip_detect_early_return(const AudioBuffer& buffer)
{
for (auto& sample : buffer)
{
if (sample > 1.0f || sample < -1.0f)
{
return 1;
}
}
return 0;
}
int clip_detect_optimised(const AudioBuffer& buffer)
{
int clipcount = 0;
for (auto& sample : buffer)
{
clipcount += (std::abs(sample) > 1.0f);
// You can also use the custom abs(), they are more or less the same,
// performance wise on all systems I've tried.
//clipcount += (float_abs(sample) > 1.0f);
}
return clipcount > 1;
}
void clamp_alg(AudioBuffer& buffer)
{
for (auto& sample : buffer)
{
sample = std::clamp(sample, -1.0f, 1.0f);
}
}
void clamp_alg_direct_access(AudioBuffer& buffer)
{
for (unsigned int i = 0; i < buffer.size(); ++i)
{
buffer[i] = std::clamp(buffer[i], -1.0f, 1.0f);
}
}
void custom_clamp(AudioBuffer& buffer)
{
for (auto& sample : buffer)
{
sample = float_clamp(sample, -1.0f, 1.0f);
}
}
void alg_fill(AudioBuffer& buffer, float value)
{
std::fill(buffer.begin(), buffer.end(), value);
}
void memset_fill(AudioBuffer& buffer, int value)
{
memset(buffer.data(), value, buffer.size() * sizeof(float));
}
void custom_fill(AudioBuffer& buffer, float value)
{
for (auto& a : buffer)
{
a = value;
}
}
void alg_copy(const AudioBuffer& source, AudioBuffer& dest)
{
std::copy(source.begin(), source.end(), dest.begin());
}
void memcpy_copy(const AudioBuffer& source, AudioBuffer& dest)
{
memcpy(dest.data(), source.data(), source.size() * sizeof(float));
}
void custom_copy(const AudioBuffer& source, AudioBuffer& dest)
{
for (unsigned int i = 0; i < source.size(); ++i)
{
dest[i] = source[i];
}
}