-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsoftclip.go
More file actions
224 lines (204 loc) · 3.91 KB
/
softclip.go
File metadata and controls
224 lines (204 loc) · 3.91 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
package gopus
// opusPCMSoftClip applies the libopus soft clipping algorithm in-place.
// It expects interleaved samples in the range of roughly [-1, 1].
// This mirrors opus_pcm_soft_clip_impl() in libopus for float builds.
func opusPCMSoftClip(x []float32, n, channels int, declipMem []float32) {
if channels < 1 || n < 1 || len(x) == 0 || len(declipMem) < channels {
return
}
total := n * channels
if total > len(x) {
total = len(x)
}
// Clamp to [-2, 2] while detecting the common no-op case. When all samples
// are already within [-1, 1] and no declip curve carries over from the
// previous frame, the libopus soft-clip algorithm leaves the buffer
// unchanged and clears the memory to zero.
allWithinNeg1Pos1 := true
for i := 0; i < total; i++ {
v := x[i]
if v > 2 {
x[i] = 2
allWithinNeg1Pos1 = false
} else if v < -2 {
x[i] = -2
allWithinNeg1Pos1 = false
} else if v > 1 || v < -1 {
allWithinNeg1Pos1 = false
}
}
if allWithinNeg1Pos1 {
for c := 0; c < channels; c++ {
if declipMem[c] != 0 {
goto applySoftClip
}
}
return
}
applySoftClip:
for c := 0; c < channels; c++ {
a := declipMem[c]
// Continue applying the non-linearity from the previous frame.
for i := 0; i < n; i++ {
idx := i*channels + c
if idx >= len(x) {
break
}
v := x[idx]
if v*a >= 0 {
break
}
x[idx] = v + a*v*v
}
curr := 0
if c >= len(x) {
declipMem[c] = a
continue
}
x0 := x[c]
for {
var i int
if allWithinNeg1Pos1 {
i = n
} else {
for i = curr; i < n; i++ {
idx := i*channels + c
if idx >= len(x) {
i = n
break
}
v := x[idx]
if v > 1 || v < -1 {
break
}
}
}
if i == n {
a = 0
break
}
peakPos := i
start := i
end := i
idx := i*channels + c
if idx >= len(x) {
a = 0
break
}
vref := x[idx]
maxval := float32Abs(vref)
for start > 0 {
idxPrev := (start-1)*channels + c
if idxPrev >= len(x) {
break
}
if vref*x[idxPrev] < 0 {
break
}
start--
}
for end < n {
idxEnd := end*channels + c
if idxEnd >= len(x) {
break
}
if vref*x[idxEnd] < 0 {
break
}
val := float32Abs(x[idxEnd])
if val > maxval {
maxval = val
peakPos = end
}
end++
}
special := start == 0 && vref*x[c] >= 0
if maxval > 0 {
a = (maxval - 1) / (maxval * maxval)
a += a * 2.4e-7
if vref > 0 {
a = -a
}
} else {
a = 0
}
for i = start; i < end; i++ {
idx2 := i*channels + c
if idx2 >= len(x) {
break
}
v := x[idx2]
x[idx2] = v + a*v*v
}
if special && peakPos >= 2 {
offset := x0 - x[c]
delta := offset / float32(peakPos)
for i = curr; i < peakPos; i++ {
offset -= delta
idx2 := i*channels + c
if idx2 >= len(x) {
break
}
v := x[idx2] + offset
if v > 1 {
v = 1
} else if v < -1 {
v = -1
}
x[idx2] = v
}
}
curr = end
if curr == n {
break
}
}
declipMem[c] = a
}
}
func softClipAndFloat32ToInt16(dst []int16, src []float32, n, channels int, declipMem []float32) {
if channels < 1 || n < 1 || len(src) == 0 || len(dst) == 0 {
return
}
total := n * channels
if total > len(src) {
total = len(src)
}
if total > len(dst) {
total = len(dst)
}
if total <= 0 {
return
}
if len(declipMem) >= channels {
for c := 0; c < channels; c++ {
if declipMem[c] != 0 {
goto fallback
}
}
if convertFloat32ToInt16Unit(dst, src, total) {
return
}
_ = src[total-1]
_ = dst[total-1]
for i := 0; i < total; i++ {
v := src[i]
if v > 1 || v < -1 {
goto fallback
}
dst[i] = float32ToInt16(v)
}
return
}
fallback:
opusPCMSoftClip(src[:total], n, channels, declipMem)
for i := 0; i < total; i++ {
dst[i] = float32ToInt16(src[i])
}
}
func float32Abs(v float32) float32 {
if v < 0 {
return -v
}
return v
}