pa_dither.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * $Id: pa_dither.c 1418 2009-10-12 21:00:53Z philburk $
  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. * 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.
  26. */
  27. /*
  28. * The text above constitutes the entire PortAudio license; however,
  29. * the PortAudio community also makes the following non-binding requests:
  30. *
  31. * Any person wishing to distribute modifications to the Software is
  32. * requested to send the modifications to the original developer so that
  33. * they can be incorporated into the canonical version. It is also
  34. * requested that these non-binding requests be included along with the
  35. * license above.
  36. */
  37. /** @file
  38. @ingroup common_src
  39. @brief Functions for generating dither noise
  40. */
  41. #include "pa_types.h"
  42. #include "pa_dither.h"
  43. /* Note that the linear congruential algorithm requires 32 bit integers
  44. * because it uses arithmetic overflow. So use PaUint32 instead of
  45. * unsigned long so it will work on 64 bit systems.
  46. */
  47. #define PA_DITHER_BITS_ (15)
  48. void PaUtil_InitializeTriangularDitherState( PaUtilTriangularDitherGenerator *state )
  49. {
  50. state->previous = 0;
  51. state->randSeed1 = 22222;
  52. state->randSeed2 = 5555555;
  53. }
  54. PaInt32 PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *state )
  55. {
  56. PaInt32 current, highPass;
  57. /* Generate two random numbers. */
  58. state->randSeed1 = (state->randSeed1 * 196314165) + 907633515;
  59. state->randSeed2 = (state->randSeed2 * 196314165) + 907633515;
  60. /* Generate triangular distribution about 0.
  61. * Shift before adding to prevent overflow which would skew the distribution.
  62. * Also shift an extra bit for the high pass filter.
  63. */
  64. #define DITHER_SHIFT_ ((sizeof(PaInt32)*8 - PA_DITHER_BITS_) + 1)
  65. current = (((PaInt32)state->randSeed1)>>DITHER_SHIFT_) +
  66. (((PaInt32)state->randSeed2)>>DITHER_SHIFT_);
  67. /* High pass filter to reduce audibility. */
  68. highPass = current - state->previous;
  69. state->previous = current;
  70. return highPass;
  71. }
  72. /* Multiply by PA_FLOAT_DITHER_SCALE_ to get a float between -2.0 and +1.99999 */
  73. #define PA_FLOAT_DITHER_SCALE_ (1.0f / ((1<<PA_DITHER_BITS_)-1))
  74. static const float const_float_dither_scale_ = PA_FLOAT_DITHER_SCALE_;
  75. float PaUtil_GenerateFloatTriangularDither( PaUtilTriangularDitherGenerator *state )
  76. {
  77. PaInt32 current, highPass;
  78. /* Generate two random numbers. */
  79. state->randSeed1 = (state->randSeed1 * 196314165) + 907633515;
  80. state->randSeed2 = (state->randSeed2 * 196314165) + 907633515;
  81. /* Generate triangular distribution about 0.
  82. * Shift before adding to prevent overflow which would skew the distribution.
  83. * Also shift an extra bit for the high pass filter.
  84. */
  85. current = (((PaInt32)state->randSeed1)>>DITHER_SHIFT_) +
  86. (((PaInt32)state->randSeed2)>>DITHER_SHIFT_);
  87. /* High pass filter to reduce audibility. */
  88. highPass = current - state->previous;
  89. state->previous = current;
  90. return ((float)highPass) * const_float_dither_scale_;
  91. }
  92. /*
  93. The following alternate dither algorithms (from musicdsp.org) could be
  94. considered
  95. */
  96. /*Noise shaped dither (March 2000)
  97. -------------------
  98. This is a simple implementation of highpass triangular-PDF dither with
  99. 2nd-order noise shaping, for use when truncating floating point audio
  100. data to fixed point.
  101. The noise shaping lowers the noise floor by 11dB below 5kHz (@ 44100Hz
  102. sample rate) compared to triangular-PDF dither. The code below assumes
  103. input data is in the range +1 to -1 and doesn't check for overloads!
  104. To save time when generating dither for multiple channels you can do
  105. things like this: r3=(r1 & 0x7F)<<8; instead of calling rand() again.
  106. int r1, r2; //rectangular-PDF random numbers
  107. float s1, s2; //error feedback buffers
  108. float s = 0.5f; //set to 0.0f for no noise shaping
  109. float w = pow(2.0,bits-1); //word length (usually bits=16)
  110. float wi= 1.0f/w;
  111. float d = wi / RAND_MAX; //dither amplitude (2 lsb)
  112. float o = wi * 0.5f; //remove dc offset
  113. float in, tmp;
  114. int out;
  115. //for each sample...
  116. r2=r1; //can make HP-TRI dither by
  117. r1=rand(); //subtracting previous rand()
  118. in += s * (s1 + s1 - s2); //error feedback
  119. tmp = in + o + d * (float)(r1 - r2); //dc offset and dither
  120. out = (int)(w * tmp); //truncate downwards
  121. if(tmp<0.0f) out--; //this is faster than floor()
  122. s2 = s1;
  123. s1 = in - wi * (float)out; //error
  124. --
  125. paul.kellett@maxim.abel.co.uk
  126. http://www.maxim.abel.co.uk
  127. */
  128. /*
  129. 16-to-8-bit first-order dither
  130. Type : First order error feedforward dithering code
  131. References : Posted by Jon Watte
  132. Notes :
  133. This is about as simple a dithering algorithm as you can implement, but it's
  134. likely to sound better than just truncating to N bits.
  135. Note that you might not want to carry forward the full difference for infinity.
  136. It's probably likely that the worst performance hit comes from the saturation
  137. conditionals, which can be avoided with appropriate instructions on many DSPs
  138. and integer SIMD type instructions, or CMOV.
  139. Last, if sound quality is paramount (such as when going from > 16 bits to 16
  140. bits) you probably want to use a higher-order dither function found elsewhere
  141. on this site.
  142. Code :
  143. // This code will down-convert and dither a 16-bit signed short
  144. // mono signal into an 8-bit unsigned char signal, using a first
  145. // order forward-feeding error term dither.
  146. #define uchar unsigned char
  147. void dither_one_channel_16_to_8( short * input, uchar * output, int count, int * memory )
  148. {
  149. int m = *memory;
  150. while( count-- > 0 ) {
  151. int i = *input++;
  152. i += m;
  153. int j = i + 32768 - 128;
  154. uchar o;
  155. if( j < 0 ) {
  156. o = 0;
  157. }
  158. else if( j > 65535 ) {
  159. o = 255;
  160. }
  161. else {
  162. o = (uchar)((j>>8)&0xff);
  163. }
  164. m = ((j-32768+128)-i);
  165. *output++ = o;
  166. }
  167. *memory = m;
  168. }
  169. */