Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygendither.c
Go to the documentation of this file.
00001 /* 00002 dither: Generate shaped noise for dithering 00003 00004 copyright 2009 by the mpg123 project - free software under the terms of the LGPL 2.1 00005 see COPYING and AUTHORS files in distribution or http://mpg123.org 00006 initially written by Taihei Monma 00007 */ 00008 00009 #include "config.h" 00010 #include "compat.h" 00011 #include "dither.h" 00012 00013 const uint32_t init_seed = 2463534242UL; 00014 00015 #define LAP 100 00016 00017 /* 00018 xorshift random number generator, with output scaling to [-0.5, 0.5] 00019 This is the white noise... 00020 See http://www.jstatsoft.org/v08/i14/paper on XOR shift random number generators. 00021 */ 00022 static float rand_xorshift32(uint32_t *seed) 00023 { 00024 union 00025 { 00026 uint32_t i; 00027 float f; 00028 } fi; 00029 00030 fi.i = *seed; 00031 fi.i ^= (fi.i<<13); 00032 fi.i ^= (fi.i>>17); 00033 fi.i ^= (fi.i<<5); 00034 *seed = fi.i; 00035 00036 /* scale the number to [-0.5, 0.5] */ 00037 #ifdef IEEE_FLOAT 00038 fi.i = (fi.i>>9)|0x3f800000; 00039 fi.f -= 1.5f; 00040 #else 00041 fi.f = (double)fi.i / 4294967295.0; 00042 fi.f -= 0.5f; 00043 #endif 00044 return fi.f; 00045 } 00046 00047 static void white_noise(float *table, size_t count) 00048 { 00049 size_t i; 00050 uint32_t seed = init_seed; 00051 00052 for(i=0; i<count; ++i) 00053 table[i] = rand_xorshift32(&seed); 00054 } 00055 00056 static void tpdf_noise(float *table, size_t count) 00057 { 00058 size_t i; 00059 uint32_t seed = init_seed; 00060 00061 for(i=0; i<count; ++i) 00062 table[i] = rand_xorshift32(&seed) + rand_xorshift32(&seed); 00063 } 00064 00065 static void highpass_tpdf_noise(float *table, size_t count) 00066 { 00067 size_t i; 00068 uint32_t seed = init_seed; 00069 /* Ensure some minimum lap for keeping the high-pass filter circular. */ 00070 size_t lap = count > 2*LAP ? LAP : count/2; 00071 00072 float input_noise; 00073 float xv[9], yv[9]; 00074 00075 for(i=0;i<9;i++) 00076 { 00077 xv[i] = yv[i] = 0.0f; 00078 } 00079 00080 for(i=0;i<count+lap;i++) 00081 { 00082 if(i==count) seed=init_seed; 00083 00084 /* generate and add 2 random numbers, to make a TPDF noise distribution */ 00085 input_noise = rand_xorshift32(&seed) + rand_xorshift32(&seed); 00086 00087 /* apply 8th order Chebyshev high-pass IIR filter */ 00088 /* Coefficients are from http://www-users.cs.york.ac.uk/~fisher/mkfilter/trad.html 00089 Given parameters are: Chebyshev, Highpass, ripple=-1, order=8, samplerate=44100, corner1=19000 */ 00090 xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; 00091 xv[8] = input_noise / 1.382814179e+07; 00092 yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; 00093 yv[8] = (xv[0] + xv[8]) - 8 * (xv[1] + xv[7]) + 28 * (xv[2] + xv[6]) 00094 - 56 * (xv[3] + xv[5]) + 70 * xv[4] 00095 + ( -0.6706204984 * yv[0]) + ( -5.3720827038 * yv[1]) 00096 + (-19.0865382480 * yv[2]) + (-39.2831607860 * yv[3]) 00097 + (-51.2308985070 * yv[4]) + (-43.3590135780 * yv[5]) 00098 + (-23.2632305320 * yv[6]) + ( -7.2370122050 * yv[7]); 00099 if(i>=lap) table[i-lap] = yv[8] * 3.0f; 00100 } 00101 } 00102 00103 void mpg123_noise(float* table, size_t count, enum mpg123_noise_type noisetype) 00104 { 00105 switch(noisetype) 00106 { 00107 case mpg123_white_noise: white_noise(table, count); break; 00108 case mpg123_tpdf_noise: tpdf_noise(table, count); break; 00109 case mpg123_highpass_tpdf_noise: 00110 highpass_tpdf_noise(table, count); 00111 break; 00112 } 00113 } 00114 00115 /* Generate white noise and shape it with a high pass filter. */ 00116 void dither_table_init(float *dithertable) 00117 { 00118 highpass_tpdf_noise(dithertable, DITHERSIZE); 00119 } Generated on Sun May 27 2012 04:34:09 for ReactOS by
1.7.6.1
|