| 1 | /* |
| 2 | * $Id: pa_dither.c,v 1.1 2006/06/10 21:30:55 dmazzoni Exp $ |
| 3 | * Portable Audio I/O Library triangular dither generator |
| 4 | * |
| 5 | * Based on the Open Source API proposed by Ross Bencina |
| 6 | * Copyright (c) 1999-2002 Phil Burk, Ross Bencina |
| 7 | * |
| 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: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be |
| 17 | * included in all copies or substantial portions of the Software. |
| 18 | * |
| 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. |
| 22 | * |
| 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. |
| 30 | */ |
| 31 | |
| 32 | /** @file |
| 33 | @brief Functions for generating dither noise |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | #include "pa_dither.h" |
| 38 | #include "pa_types.h" |
| 39 | |
| 40 | #define PA_DITHER_BITS_ (15) |
| 41 | |
| 42 | |
| 43 | void PaUtil_InitializeTriangularDitherState( PaUtilTriangularDitherGenerator *state ) |
| 44 | { |
| 45 | state->previous = 0; |
| 46 | state->randSeed1 = 22222; |
| 47 | state->randSeed2 = 5555555; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | signed long PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *state ) |
| 52 | { |
| 53 | signed long current, highPass; |
| 54 | |
| 55 | /* Generate two random numbers. */ |
| 56 | state->randSeed1 = (state->randSeed1 * 196314165) + 907633515; |
| 57 | state->randSeed2 = (state->randSeed2 * 196314165) + 907633515; |
| 58 | |
| 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. |
| 62 | */ |
| 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_); |
| 66 | |
| 67 | /* High pass filter to reduce audibility. */ |
| 68 | highPass = current - state->previous; |
| 69 | state->previous = current; |
| 70 | return highPass; |
| 71 | } |
| 72 | |
| 73 | |
| 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_; |
| 77 | |
| 78 | float PaUtil_GenerateFloatTriangularDither( PaUtilTriangularDitherGenerator *state ) |
| 79 | { |
| 80 | signed long current, highPass; |
| 81 | |
| 82 | /* Generate two random numbers. */ |
| 83 | state->randSeed1 = (state->randSeed1 * 196314165) + 907633515; |
| 84 | state->randSeed2 = (state->randSeed2 * 196314165) + 907633515; |
| 85 | |
| 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. |
| 89 | */ |
| 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_); |
| 93 | |
| 94 | /* High pass filter to reduce audibility. */ |
| 95 | highPass = current - state->previous; |
| 96 | state->previous = current; |
| 97 | return ((float)highPass) * const_float_dither_scale_; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /* |
| 102 | The following alternate dither algorithms (from musicdsp.org) could be |
| 103 | considered |
| 104 | */ |
| 105 | |
| 106 | /*Noise shaped dither (March 2000) |
| 107 | ------------------- |
| 108 | |
| 109 | This is a simple implementation of highpass triangular-PDF dither with |
| 110 | 2nd-order noise shaping, for use when truncating floating point audio |
| 111 | data to fixed point. |
| 112 | |
| 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! |
| 116 | |
| 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. |
| 119 | |
| 120 | |
| 121 | |
| 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) |
| 126 | float wi= 1.0f/w; |
| 127 | float d = wi / RAND_MAX; //dither amplitude (2 lsb) |
| 128 | float o = wi * 0.5f; //remove dc offset |
| 129 | float in, tmp; |
| 130 | int out; |
| 131 | |
| 132 | |
| 133 | //for each sample... |
| 134 | |
| 135 | r2=r1; //can make HP-TRI dither by |
| 136 | r1=rand(); //subtracting previous rand() |
| 137 | |
| 138 | in += s * (s1 + s1 - s2); //error feedback |
| 139 | tmp = in + o + d * (float)(r1 - r2); //dc offset and dither |
| 140 | |
| 141 | out = (int)(w * tmp); //truncate downwards |
| 142 | if(tmp<0.0f) out--; //this is faster than floor() |
| 143 | |
| 144 | s2 = s1; |
| 145 | s1 = in - wi * (float)out; //error |
| 146 | |
| 147 | |
| 148 | |
| 149 | -- |
| 150 | [email protected] |
| 151 | http://www.maxim.abel.co.uk |
| 152 | */ |
| 153 | |
| 154 | |
| 155 | /* |
| 156 | 16-to-8-bit first-order dither |
| 157 | |
| 158 | Type : First order error feedforward dithering code |
| 159 | References : Posted by Jon Watte |
| 160 | |
| 161 | Notes : |
| 162 | This is about as simple a dithering algorithm as you can implement, but it's |
| 163 | likely to sound better than just truncating to N bits. |
| 164 | |
| 165 | Note that you might not want to carry forward the full difference for infinity. |
| 166 | It's probably likely that the worst performance hit comes from the saturation |
| 167 | conditionals, which can be avoided with appropriate instructions on many DSPs |
| 168 | and integer SIMD type instructions, or CMOV. |
| 169 | |
| 170 | Last, if sound quality is paramount (such as when going from > 16 bits to 16 |
| 171 | bits) you probably want to use a higher-order dither function found elsewhere |
| 172 | on this site. |
| 173 | |
| 174 | |
| 175 | Code : |
| 176 | // This code will down-convert and dither a 16-bit signed short |
| 177 | // mono signal into an 8-bit unsigned char signal, using a first |
| 178 | // order forward-feeding error term dither. |
| 179 | |
| 180 | #define uchar unsigned char |
| 181 | |
| 182 | void dither_one_channel_16_to_8( short * input, uchar * output, int count, int * memory ) |
| 183 | { |
| 184 | int m = *memory; |
| 185 | while( count-- > 0 ) { |
| 186 | int i = *input++; |
| 187 | i += m; |
| 188 | int j = i + 32768 - 128; |
| 189 | uchar o; |
| 190 | if( j < 0 ) { |
| 191 | o = 0; |
| 192 | } |
| 193 | else if( j > 65535 ) { |
| 194 | o = 255; |
| 195 | } |
| 196 | else { |
| 197 | o = (uchar)((j>>8)&0xff); |
| 198 | } |
| 199 | m = ((j-32768+128)-i); |
| 200 | *output++ = o; |
| 201 | } |
| 202 | *memory = m; |
| 203 | } |
| 204 | */ |