-
Notifications
You must be signed in to change notification settings - Fork 16
/
Dither.cs
342 lines (308 loc) · 10.7 KB
/
Dither.cs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) 2006, 2007 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
public enum DitherType
{
NONE = 0,
TRIANGULAR = 1,
SHAPED = 2,
SBM = 3
}
public class Dither
{
// F-weighted
// These from "Psychoacoustically Optimal Noise Shaping," by Robert A. Wannamaker
// (From AES Journal Volume 40, No. 7/8, 1992 July/August)
// !!!NOTE: This dither FIR is only good for 44.1kHz sampling rate!!!
private static double[] _Wannamaker = {
2.412f,
-3.370f,
3.937f,
-4.174f,
3.353f,
-2.205f,
1.281f,
-0.569f,
0.0847f };
// These from the Sony Super Bit Mapping (SBM)
private static double[] _SBM = {
1.47933,
-1.59032,
1.64436,
-1.36613,
9.26704e-1,
-5.57931e-1,
2.67859e-1,
-1.06726e-1,
2.85161e-2,
1.23066e-3,
-6.16555e-3,
3.06700e-3};
private const double _scale8 = 128f;
private const double _scale16 = 32768f;
private const double _scale24 = 8388608f;
private const double _scale32 = 2147483648f;
private DitherType _type = DitherType.NONE;
private uint _sampleRate;
private ushort _bitsPerSample;
private double[] _filter;
private double _minv;
private double _maxv;
private double _peak;
// Error history array for noise shaping
private double[] _EH;
private int _HistPos;
// Cached random-number generator
private Random _random;
// Previously tried to cache a block of random numbers. But fail:
// with any reasonably small cache the LF behavior is audible pumping
// at (samplerate/cachesize). So instead just use the proper rand gen
// even though it's a bit slower.
/*
private const int _cachelen = 4096;
private static double[] _randArr;
private static double[] _tpdfArr;
private static int _randPos;
private static int _tpdfPos;
*/
private bool _clipping = false;
public Dither(DitherType type, uint sampleRate, ushort bitsPerSample)
{
_type = type;
_sampleRate = sampleRate;
_bitsPerSample = bitsPerSample;
_minv = MinValue(_bitsPerSample);
_maxv = MaxValue(_bitsPerSample);
_filter = FilterArray(type);
_EH = new double[2 * _filter.Length];
_HistPos = _filter.Length - 1;
// Tweak the random number source
_random = new Random();
}
static private double[] FilterArray(DitherType type)
{
switch (type)
{
case DitherType.SHAPED:
return _Wannamaker;
case DitherType.SBM:
return _SBM;
case DitherType.NONE:
case DitherType.TRIANGULAR:
default:
return new double[0];
}
}
private static double MaxValue(ushort bitsPerSample)
{
switch (bitsPerSample)
{
case 8:
return _scale8; // -1;
case 16:
return _scale16; // -1;
case 24:
return _scale24; // -1;
case 32:
return _scale32; // -1;
default:
return Math.Pow(2, bitsPerSample - 1) - 1;
}
}
private static double MinValue(ushort bitsPerSample)
{
switch (bitsPerSample)
{
case 8:
return -_scale8;
case 16:
return -_scale16;
case 24:
return -_scale24;
case 32:
return -_scale32;
default:
return -(Math.Pow(2, bitsPerSample - 1));
}
}
// Random value from -1 to +1 with rectangular PDF
private double NextRandom()
{
return (_random.NextDouble() - 0.5);
/*
lock (_random)
{
if (_randArr == null)
{
_randArr = new double[_cachelen];
for (int j = 0; j < _cachelen; j++)
{
_randArr[j] = (_random.NextDouble() - 0.5) * 2;
}
_randPos = 0;
}
_randPos++;
if (_randPos >= _cachelen)
{
_randPos = 0;
}
return _randArr[_randPos];
}
*/
}
// Random value from -1 to +1 with triangular PDF
private double NextRandom2()
{
return (_random.NextDouble() + _random.NextDouble() - 1);
/*
lock (_random)
{
if (_tpdfArr == null)
{
_tpdfArr = new double[_cachelen];
for (int j = 0; j < _cachelen; j++)
{
_tpdfArr[j] = (_random.NextDouble() + _random.NextDouble() - 1) * 2;
}
_tpdfPos = 0;
}
_tpdfPos++;
if (_tpdfPos >= _cachelen)
{
_tpdfPos = 0;
}
return _tpdfArr[_tpdfPos];
}
*/
}
public bool clipping
{
get { return _clipping; }
set { _clipping = value; }
}
public double dbfsPeak
{
get
{
// _peak is from 0 to 1 (unless we clipped, in which case it might be more than 1)
return MathUtil.dB(_peak);
}
}
public double processDouble(double samp)
{
int quantized = process(samp);
return (double)quantized / _maxv;
}
/// <summary>
/// Process a ISample (in-place; the original object is returned, with modified data)
/// </summary>
/// <param name="samp"></param>
public ISample process(ISample samp)
{
for (int c = 0; c < samp.NumChannels; c++)
{
samp[c] = processDouble(samp[c]);
}
return samp;
}
// Dither double-precision float to arbitrary bits-per-sample (up to 32)
public int process(double samp)
{
// peak is absolute, normal range should be 0 thru 1
if (double.IsNaN(samp) || double.IsInfinity(samp))
{
samp = 0;
}
_peak = Math.Max(_peak, Math.Abs(samp));
int output = 0;
double noise;
double error;
try
{
// Scale the output first,
// - we're still working with floats
// - max scale is dependent on the target bit-depth (eg. 32768 for 16-bit)
// - min scale is +/- 1, any fractional component will be rounded
samp *= _maxv;
switch (_type)
{
case DitherType.NONE:
output = (int)Math.Round(samp); //, MidpointRounding.AwayFromZero);
break;
case DitherType.TRIANGULAR:
// Triangular dither
// Make +/- 1LSB white randomness
noise = NextRandom2();
// Add this noise
samp += noise;
// Round the sample
output = (int)Math.Round(samp); //, MidpointRounding.AwayFromZero);
break;
case DitherType.SHAPED:
case DitherType.SBM:
// Make +/- 1LSB white randomness
noise = NextRandom();
// Add this noise and subtract the previous noise (filtered)
samp += noise;
for (int x = 0; x < _filter.Length; x++)
{
samp -= _filter[x] * _EH[_HistPos + x];
}
// Round the sample
output = (int)Math.Round(samp); //, MidpointRounding.AwayFromZero);
break;
}
if (_filter.Length > 0)
{
// Find the error of this output sample (before we clip...)
error = (double)output - samp;
if (_HistPos < 1) _HistPos += _filter.Length;
_HistPos--;
// Update buffer (both copies)
_EH[_HistPos + _filter.Length] = _EH[_HistPos] = error;
}
}
catch (OverflowException)
{
if (samp > (_maxv-1))
output = (int)(_maxv-1);
else
output = (int)_minv;
}
if (output < _minv)
{
if (!_clipping)
{
_clipping = true;
Trace.WriteLine("CLIPPING-");
}
output = (int)_minv;
}
else if (output > (_maxv-1))
{
if (!clipping)
{
_clipping = true;
Trace.WriteLine("CLIPPING+");
}
output = (int)(_maxv-1);
}
return output;
}
public int process(float samp)
{
return process((double)samp);
}
public void reset()
{
for (int x = 0; x < _filter.Length; x++)
{
_EH[x] = 0.0;
}
_HistPos = _filter.Length - 1;
}
}
}