2
* $Id: pa_dither.c,v 1.1.2.2 2006/04/08 16:12:25 richardash1981 Exp $
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
* Any person wishing to distribute modifications to the Software is
20
* requested to send the modifications to the original developer so that
21
* they can be incorporated into the canonical version.
23
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
27
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
28
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
@brief Functions for generating dither noise
37
#include "pa_dither.h"
40
#define PA_DITHER_BITS_ (15)
43
void PaUtil_InitializeTriangularDitherState( PaUtilTriangularDitherGenerator *state )
46
state->randSeed1 = 22222;
47
state->randSeed2 = 5555555;
51
signed long PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *state )
53
signed long current, highPass;
55
/* Generate two random numbers. */
56
state->randSeed1 = (state->randSeed1 * 196314165) + 907633515;
57
state->randSeed2 = (state->randSeed2 * 196314165) + 907633515;
59
/* Generate triangular distribution about 0.
60
* Shift before adding to prevent overflow which would skew the distribution.
61
* Also shift an extra bit for the high pass filter.
63
#define DITHER_SHIFT_ ((SIZEOF_LONG*8 - PA_DITHER_BITS_) + 1)
64
current = (((signed long)state->randSeed1)>>DITHER_SHIFT_) +
65
(((signed long)state->randSeed2)>>DITHER_SHIFT_);
67
/* High pass filter to reduce audibility. */
68
highPass = current - state->previous;
69
state->previous = current;
74
/* Multiply by PA_FLOAT_DITHER_SCALE_ to get a float between -2.0 and +1.99999 */
75
#define PA_FLOAT_DITHER_SCALE_ (1.0f / ((1<<PA_DITHER_BITS_)-1))
76
static const float const_float_dither_scale_ = PA_FLOAT_DITHER_SCALE_;
78
float PaUtil_GenerateFloatTriangularDither( PaUtilTriangularDitherGenerator *state )
80
signed long current, highPass;
82
/* Generate two random numbers. */
83
state->randSeed1 = (state->randSeed1 * 196314165) + 907633515;
84
state->randSeed2 = (state->randSeed2 * 196314165) + 907633515;
86
/* Generate triangular distribution about 0.
87
* Shift before adding to prevent overflow which would skew the distribution.
88
* Also shift an extra bit for the high pass filter.
90
#define DITHER_SHIFT_ ((SIZEOF_LONG*8 - PA_DITHER_BITS_) + 1)
91
current = (((signed long)state->randSeed1)>>DITHER_SHIFT_) +
92
(((signed long)state->randSeed2)>>DITHER_SHIFT_);
94
/* High pass filter to reduce audibility. */
95
highPass = current - state->previous;
96
state->previous = current;
97
return ((float)highPass) * const_float_dither_scale_;
102
The following alternate dither algorithms (from musicdsp.org) could be
106
/*Noise shaped dither (March 2000)
109
This is a simple implementation of highpass triangular-PDF dither with
110
2nd-order noise shaping, for use when truncating floating point audio
113
The noise shaping lowers the noise floor by 11dB below 5kHz (@ 44100Hz
114
sample rate) compared to triangular-PDF dither. The code below assumes
115
input data is in the range +1 to -1 and doesn't check for overloads!
117
To save time when generating dither for multiple channels you can do
118
things like this: r3=(r1 & 0x7F)<<8; instead of calling rand() again.
122
int r1, r2; //rectangular-PDF random numbers
123
float s1, s2; //error feedback buffers
124
float s = 0.5f; //set to 0.0f for no noise shaping
125
float w = pow(2.0,bits-1); //word length (usually bits=16)
127
float d = wi / RAND_MAX; //dither amplitude (2 lsb)
128
float o = wi * 0.5f; //remove dc offset
135
r2=r1; //can make HP-TRI dither by
136
r1=rand(); //subtracting previous rand()
138
in += s * (s1 + s1 - s2); //error feedback
139
tmp = in + o + d * (float)(r1 - r2); //dc offset and dither
141
out = (int)(w * tmp); //truncate downwards
142
if(tmp<0.0f) out--; //this is faster than floor()
145
s1 = in - wi * (float)out; //error
149
paul.kellett@maxim.abel.co.uk
150
http://www.maxim.abel.co.uk
155
16-to-8-bit first-order dither
157
Type : First order error feedforward dithering code
158
References : Posted by Jon Watte
161
This is about as simple a dithering algorithm as you can implement, but it's
162
likely to sound better than just truncating to N bits.
164
Note that you might not want to carry forward the full difference for infinity.
165
It's probably likely that the worst performance hit comes from the saturation
166
conditionals, which can be avoided with appropriate instructions on many DSPs
167
and integer SIMD type instructions, or CMOV.
169
Last, if sound quality is paramount (such as when going from > 16 bits to 16
170
bits) you probably want to use a higher-order dither function found elsewhere
175
// This code will down-convert and dither a 16-bit signed short
176
// mono signal into an 8-bit unsigned char signal, using a first
177
// order forward-feeding error term dither.
179
#define uchar unsigned char
181
void dither_one_channel_16_to_8( short * input, uchar * output, int count, int * memory )
184
while( count-- > 0 ) {
187
int j = i + 32768 - 128;
192
else if( j > 65535 ) {
196
o = (uchar)((j>>8)&0xff);
198
m = ((j-32768+128)-i);