2
* $Id: pa_dither.c 1418 2009-10-12 21:00:53Z philburk $
3
* Portable Audio I/O Library triangular dither generator
5
* Based on the Open Source API proposed by Ross Bencina
6
* Copyright (c) 1999-2002 Phil Burk, Ross Bencina
8
* Permission is hereby granted, free of charge, to any person obtaining
9
* a copy of this software and associated documentation files
10
* (the "Software"), to deal in the Software without restriction,
11
* including without limitation the rights to use, copy, modify, merge,
12
* publish, distribute, sublicense, and/or sell copies of the Software,
13
* and to permit persons to whom the Software is furnished to do so,
14
* subject to the following conditions:
16
* The above copyright notice and this permission notice shall be
17
* included in all copies or substantial portions of the Software.
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
23
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
* The text above constitutes the entire PortAudio license; however,
30
* the PortAudio community also makes the following non-binding requests:
32
* Any person wishing to distribute modifications to the Software is
33
* requested to send the modifications to the original developer so that
34
* they can be incorporated into the canonical version. It is also
35
* requested that these non-binding requests be included along with the
42
@brief Functions for generating dither noise
46
#include "pa_dither.h"
49
/* Note that the linear congruential algorithm requires 32 bit integers
50
* because it uses arithmetic overflow. So use PaUint32 instead of
51
* unsigned long so it will work on 64 bit systems.
54
#define PA_DITHER_BITS_ (15)
57
void PaUtil_InitializeTriangularDitherState( PaUtilTriangularDitherGenerator *state )
60
state->randSeed1 = 22222;
61
state->randSeed2 = 5555555;
65
PaInt32 PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *state )
67
PaInt32 current, highPass;
69
/* Generate two random numbers. */
70
state->randSeed1 = (state->randSeed1 * 196314165) + 907633515;
71
state->randSeed2 = (state->randSeed2 * 196314165) + 907633515;
73
/* Generate triangular distribution about 0.
74
* Shift before adding to prevent overflow which would skew the distribution.
75
* Also shift an extra bit for the high pass filter.
77
#define DITHER_SHIFT_ ((sizeof(PaInt32)*8 - PA_DITHER_BITS_) + 1)
79
current = (((PaInt32)state->randSeed1)>>DITHER_SHIFT_) +
80
(((PaInt32)state->randSeed2)>>DITHER_SHIFT_);
82
/* High pass filter to reduce audibility. */
83
highPass = current - state->previous;
84
state->previous = current;
89
/* Multiply by PA_FLOAT_DITHER_SCALE_ to get a float between -2.0 and +1.99999 */
90
#define PA_FLOAT_DITHER_SCALE_ (1.0f / ((1<<PA_DITHER_BITS_)-1))
91
static const float const_float_dither_scale_ = PA_FLOAT_DITHER_SCALE_;
93
float PaUtil_GenerateFloatTriangularDither( PaUtilTriangularDitherGenerator *state )
95
PaInt32 current, highPass;
97
/* Generate two random numbers. */
98
state->randSeed1 = (state->randSeed1 * 196314165) + 907633515;
99
state->randSeed2 = (state->randSeed2 * 196314165) + 907633515;
101
/* Generate triangular distribution about 0.
102
* Shift before adding to prevent overflow which would skew the distribution.
103
* Also shift an extra bit for the high pass filter.
105
current = (((PaInt32)state->randSeed1)>>DITHER_SHIFT_) +
106
(((PaInt32)state->randSeed2)>>DITHER_SHIFT_);
108
/* High pass filter to reduce audibility. */
109
highPass = current - state->previous;
110
state->previous = current;
111
return ((float)highPass) * const_float_dither_scale_;
116
The following alternate dither algorithms (from musicdsp.org) could be
120
/*Noise shaped dither (March 2000)
123
This is a simple implementation of highpass triangular-PDF dither with
124
2nd-order noise shaping, for use when truncating floating point audio
127
The noise shaping lowers the noise floor by 11dB below 5kHz (@ 44100Hz
128
sample rate) compared to triangular-PDF dither. The code below assumes
129
input data is in the range +1 to -1 and doesn't check for overloads!
131
To save time when generating dither for multiple channels you can do
132
things like this: r3=(r1 & 0x7F)<<8; instead of calling rand() again.
136
int r1, r2; //rectangular-PDF random numbers
137
float s1, s2; //error feedback buffers
138
float s = 0.5f; //set to 0.0f for no noise shaping
139
float w = pow(2.0,bits-1); //word length (usually bits=16)
141
float d = wi / RAND_MAX; //dither amplitude (2 lsb)
142
float o = wi * 0.5f; //remove dc offset
149
r2=r1; //can make HP-TRI dither by
150
r1=rand(); //subtracting previous rand()
152
in += s * (s1 + s1 - s2); //error feedback
153
tmp = in + o + d * (float)(r1 - r2); //dc offset and dither
155
out = (int)(w * tmp); //truncate downwards
156
if(tmp<0.0f) out--; //this is faster than floor()
159
s1 = in - wi * (float)out; //error
164
paul.kellett@maxim.abel.co.uk
165
http://www.maxim.abel.co.uk
170
16-to-8-bit first-order dither
172
Type : First order error feedforward dithering code
173
References : Posted by Jon Watte
176
This is about as simple a dithering algorithm as you can implement, but it's
177
likely to sound better than just truncating to N bits.
179
Note that you might not want to carry forward the full difference for infinity.
180
It's probably likely that the worst performance hit comes from the saturation
181
conditionals, which can be avoided with appropriate instructions on many DSPs
182
and integer SIMD type instructions, or CMOV.
184
Last, if sound quality is paramount (such as when going from > 16 bits to 16
185
bits) you probably want to use a higher-order dither function found elsewhere
190
// This code will down-convert and dither a 16-bit signed short
191
// mono signal into an 8-bit unsigned char signal, using a first
192
// order forward-feeding error term dither.
194
#define uchar unsigned char
196
void dither_one_channel_16_to_8( short * input, uchar * output, int count, int * memory )
199
while( count-- > 0 ) {
202
int j = i + 32768 - 128;
207
else if( j > 65535 ) {
211
o = (uchar)((j>>8)&0xff);
213
m = ((j-32768+128)-i);