-
Notifications
You must be signed in to change notification settings - Fork 29
/
dither.c
218 lines (171 loc) · 5.15 KB
/
dither.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <assert.h>
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795028842
#endif
#include "prng.h"
#include "shapercoefs.h"
//
#define MAGIC 0x81d80d2b
#define RANDBUFLEN (1 << 16)
typedef struct SSRCDither {
uint32_t magic;
int fs;
int shaperIndex;
int pdfID;
double noisePeak;
int shaperClipMin, shaperClipMax;
double peakBottom, peakTop;
struct SleefRNG *rng;
int randPtr;
double buf[SHAPERLENMAX];
double randBuf[RANDBUFLEN];
} SSRCDither;
//
#define PDF_RECTANGULAR 0
#define PDF_TRIANGULAR 1
#define PDF_GAUSSIAN 2
#define PDF_TWOLEVEL 3
SSRCDither *SSRCDither_init(int32_t fs, int32_t min, int32_t max, int shaperID, int pdfID, double noisePeak, uint64_t seed) {
if (pdfID < 0 || 2 < pdfID) return NULL;
int shaperIndex = -1;
for(int i=0;ditherCoef[i].fs != -1;i++) {
if (fs == ditherCoef[i].fs && shaperID == ditherCoef[i].id) {
shaperIndex = i;
break;
}
}
if (shaperIndex == -1) return NULL;
SSRCDither *thiz = (SSRCDither *)calloc(sizeof(SSRCDither), 1);
thiz->magic = MAGIC;
thiz->fs = fs;
thiz->shaperClipMin = min;
thiz->shaperClipMax = max;
thiz->peakBottom = thiz->peakTop = (min + max)*0.5;
thiz->shaperIndex = shaperIndex;
thiz->pdfID = pdfID;
thiz->noisePeak = noisePeak;
thiz->rng = SleefRNG_init(seed);
switch(thiz->pdfID) {
case PDF_RECTANGULAR:
SleefRNG_fillRectangularDouble(thiz->rng, thiz->randBuf, RANDBUFLEN, -thiz->noisePeak, thiz->noisePeak);
break;
case PDF_TRIANGULAR:
SleefRNG_fillTriangularDouble(thiz->rng, thiz->randBuf, RANDBUFLEN, thiz->noisePeak);
break;
case PDF_TWOLEVEL:
SleefRNG_fillTwoLevelDouble(thiz->rng, thiz->randBuf, RANDBUFLEN, thiz->noisePeak);
break;
}
return thiz;
}
void SSRCDither_getPeaks(SSRCDither *thiz, double *peaks) {
assert(thiz != NULL && thiz->magic == MAGIC);
peaks[0] = thiz->peakTop;
peaks[1] = thiz->peakBottom;
}
double SSRCDither_getMaxNoiseStrength(SSRCDither *thiz) {
assert(thiz != NULL && thiz->magic == MAGIC);
double sum = 0;
for(int j=0;j<ditherCoef[thiz->shaperIndex].len;j++) {
sum += fabs(ditherCoef[thiz->shaperIndex].coefs[j]);
}
return (thiz->noisePeak + 0.5) * sum + thiz->noisePeak;
}
void SSRCDither_dispose(SSRCDither *thiz) {
assert(thiz != NULL && thiz->magic == MAGIC);
SleefRNG_dispose(thiz->rng);
thiz->magic = 0;
free(thiz);
}
const int *SSRCDither_getAllSupportedFS() { return freqs; }
int SSRCDither_getNumAvailableShapers(int32_t fs) {
int cnt = 0;
for(int i=0;ditherCoef[i].fs != -1;i++) {
if (fs == ditherCoef[i].fs) cnt++;
}
return cnt;
}
int SSRCDither_getAvailableShaperIDs(int *ids, int maxnids, int32_t fs) {
int cnt = 0;
for(int i=0;cnt < maxnids && ditherCoef[i].fs != -1;i++) {
if (fs == ditherCoef[i].fs) {
ids[cnt++] = ditherCoef[i].id;
}
}
return cnt;
}
const char *SSRCDither_getNameForShaperID(int32_t fs, int id) {
for(int i=0;ditherCoef[i].fs != -1;i++) {
if (fs == ditherCoef[i].fs && id == ditherCoef[i].id) {
return ditherCoef[i].friendlyName;
}
}
return NULL;
}
double SSRCDither_getStrengthForShaperID(int fs, int id) {
for(int i=0;ditherCoef[i].fs != -1;i++) {
if (fs == ditherCoef[i].fs && id == ditherCoef[i].id) {
double sum = 0;
for(int j=0;j<ditherCoef[i].len;j++) {
sum += fabs(ditherCoef[i].coefs[j]);
}
return sum;
}
}
return -1;
}
#define RINT(x) ((x) >= 0 ? ((int)((x) + 0.5)) : ((int)((x) - 0.5)))
void SSRCDither_quantizeDouble(SSRCDither *thiz, int32_t *out, double *in, int32_t nSamples, double gain) {
assert(thiz != NULL && thiz->magic == MAGIC);
const double *shaperCoefs = ditherCoef[thiz->shaperIndex].coefs;
const int shaperLen = ditherCoef[thiz->shaperIndex].len;
double *buf = thiz->buf, *randBuf = thiz->randBuf;
double min = thiz->shaperClipMin, max = thiz->shaperClipMax;
double peakBottom = thiz->peakBottom, peakTop = thiz->peakTop;
int randPtr = thiz->randPtr;
for(int p=0;p<nSamples;p++) {
double h = shaperCoefs[shaperLen-1]*buf[shaperLen-1];
for(int i=shaperLen-2;i>=0;i--) {
h += shaperCoefs[i]*buf[i];
buf[i+1] = buf[i];
}
double x = gain * in[p] + h;
double y = x + randBuf[randPtr];
if (y < peakBottom) peakBottom = y;
if (y > peakTop) peakTop = y;
double q = RINT(y);
randPtr++;
if (q < min || q > max) {
if (q < min) q = min;
if (q > max) q = max;
buf[0] = q - x;
if (buf[0] < -1) buf[0] = -1;
if (buf[0] > 1) buf[0] = 1;
} else {
buf[0] = q - x;
}
out[p] = (int)q;
//
if (randPtr == RANDBUFLEN) {
switch(thiz->pdfID) {
case PDF_RECTANGULAR:
SleefRNG_fillRectangularDouble(thiz->rng, randBuf, RANDBUFLEN, -thiz->noisePeak, thiz->noisePeak);
break;
case PDF_TRIANGULAR:
SleefRNG_fillTriangularDouble(thiz->rng, randBuf, RANDBUFLEN, thiz->noisePeak);
break;
case PDF_TWOLEVEL:
SleefRNG_fillTwoLevelDouble(thiz->rng, randBuf, RANDBUFLEN, thiz->noisePeak);
break;
}
randPtr = 0;
}
}
thiz->randPtr = randPtr;
thiz->peakTop = peakTop;
thiz->peakBottom = peakBottom;
}