shaper.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "Shaper.h"
  2. #ifndef M_PI
  3. #define M_PI 3.1415926535897932384626433832795028842
  4. #endif
  5. #define RANDBUFLEN 65536
  6. #define RINT(x) ((x) >= 0 ? ((int)((x) + 0.5)) : ((int)((x) - 0.5)))
  7. const int scoeffreq[] =
  8. {
  9. 0, 48000, 44100, 37800, 32000, 22050, 48000, 44100
  10. };
  11. const int scoeflen[] =
  12. {
  13. 1, 16, 20, 16, 16, 15, 16, 15
  14. };
  15. const int samp[] =
  16. {
  17. 8, 18, 27, 8, 8, 8, 10, 9
  18. };
  19. const double shapercoefs[8][21] =
  20. {
  21. {
  22. -1
  23. }
  24. , /* triangular dither */
  25. { -2.8720729351043701172, 5.0413231849670410156, -6.2442994117736816406, 5.8483986854553222656,
  26. -3.7067542076110839844, 1.0495119094848632812, 1.1830236911773681641, -2.1126792430877685547,
  27. 1.9094531536102294922, -0.99913084506988525391, 0.17090806365013122559, 0.32615602016448974609,
  28. -0.39127644896507263184, 0.26876461505889892578, -0.097676105797290802002, 0.023473845794796943665,
  29. }, /* 48k, N=16, amp=18 */
  30. { -2.6773197650909423828, 4.8308925628662109375, -6.570110321044921875, 7.4572014808654785156,
  31. -6.7263274192810058594, 4.8481650352478027344, -2.0412089824676513672, -0.7006359100341796875,
  32. 2.9537565708160400391, -4.0800385475158691406, 4.1845216751098632812, -3.3311812877655029297,
  33. 2.1179926395416259766, -0.879302978515625, 0.031759146600961685181, 0.42382788658142089844,
  34. -0.47882103919982910156, 0.35490813851356506348, -0.17496839165687561035, 0.060908168554306030273,
  35. }, /* 44.1k, N=20, amp=27 */
  36. { -1.6335992813110351562, 2.2615492343902587891, -2.4077029228210449219, 2.6341717243194580078,
  37. -2.1440362930297851562, 1.8153258562088012695, -1.0816224813461303711, 0.70302653312683105469,
  38. -0.15991993248462677002, -0.041549518704414367676, 0.29416576027870178223, -0.2518316805362701416,
  39. 0.27766478061676025391, -0.15785403549671173096, 0.10165894031524658203, -0.016833892092108726501,
  40. }, /* 37.8k, N=16 */
  41. { -0.82901298999786376953, 0.98922657966613769531, -0.59825712442398071289, 1.0028809309005737305,
  42. -0.59938216209411621094, 0.79502451419830322266, -0.42723315954208374023, 0.54492527246475219727,
  43. -0.30792605876922607422, 0.36871799826622009277, -0.18792048096656799316, 0.2261127084493637085,
  44. -0.10573341697454452515, 0.11435490846633911133, -0.038800679147243499756, 0.040842197835445404053,
  45. }, /* 32k, N=16 */
  46. { -0.065229974687099456787, 0.54981261491775512695, 0.40278548002243041992, 0.31783768534660339355,
  47. 0.28201797604560852051, 0.16985194385051727295, 0.15433363616466522217, 0.12507140636444091797,
  48. 0.08903945237398147583, 0.064410120248794555664, 0.047146003693342208862, 0.032805237919092178345,
  49. 0.028495194390416145325, 0.011695005930960178375, 0.011831838637590408325,
  50. }, /* 22.05k, N=15 */
  51. { -2.3925774097442626953, 3.4350297451019287109, -3.1853709220886230469, 1.8117271661758422852,
  52. 0.20124770700931549072, -1.4759907722473144531, 1.7210904359817504883, -0.97746700048446655273,
  53. 0.13790138065814971924, 0.38185903429985046387, -0.27421241998672485352, -0.066584214568138122559,
  54. 0.35223302245140075684, -0.37672343850135803223, 0.23964276909828186035, -0.068674825131893157959,
  55. }, /* 48k, N=16, amp=10 */
  56. { -2.0833916664123535156, 3.0418450832366943359, -3.2047898769378662109, 2.7571926116943359375,
  57. -1.4978630542755126953, 0.3427594602108001709, 0.71733748912811279297, -1.0737057924270629883,
  58. 1.0225815773010253906, -0.56649994850158691406, 0.20968692004680633545, 0.065378531813621520996,
  59. -0.10322438180446624756, 0.067442022264003753662, 0.00495197344571352005,
  60. }, /* 44.1k, N=15, amp=9 */
  61. #if 0
  62. { -3.0259189605712890625, 6.0268716812133789062, -9.195003509521484375, 11.824929237365722656,
  63. -12.767142295837402344, 11.917946815490722656, -9.1739168167114257812, 5.3712320327758789062,
  64. -1.1393624544143676758, -2.4484779834747314453, 4.9719839096069335938, -6.0392003059387207031,
  65. 5.9359521865844726562, -4.903278350830078125, 3.5527443885803222656, -2.1909697055816650391,
  66. 1.1672389507293701172, -0.4903914332389831543, 0.16519790887832641602, -0.023217858746647834778,
  67. }, /* 44.1k, N=20 */
  68. #endif
  69. };
  70. #define POOLSIZE 97
  71. Shaper::Shaper(int freq, int _nch, int min, int max, int _dtype, int pdf, double noiseamp)
  72. {
  73. int i;
  74. float pool[POOLSIZE] = {0};
  75. nch = _nch;
  76. dtype = _dtype;
  77. for (i = 1;i < 6;i++) if (freq == scoeffreq[i]) break;
  78. /* if ((dtype == 3 || dtype == 4) && i == 6) {
  79. fprintf(stderr,"Warning: ATH based noise shaping for destination frequency %dHz is not available, using triangular dither\n",freq);
  80. }*/
  81. if (dtype == 2 || i == 6) i = 0;
  82. if (dtype == 4 && (i == 1 || i == 2)) i += 5;
  83. shaper_type = i;
  84. shapebuf = (double**)malloc(sizeof(double *) * nch);
  85. shaper_len = scoeflen[shaper_type];
  86. for (i = 0;i < nch;i++)
  87. shapebuf[i] = (double*)calloc(shaper_len, sizeof(double));
  88. shaper_clipmin = min;
  89. shaper_clipmax = max;
  90. randbuf = (REAL*)malloc(sizeof(REAL) * RANDBUFLEN);
  91. for (i = 0;i < POOLSIZE;i++) pool[i] = warandf();
  92. switch (pdf)
  93. {
  94. case DITHER_RECTANGLE: // rectangular
  95. for (i = 0;i < RANDBUFLEN;i++)
  96. {
  97. float r;
  98. int p;
  99. p = warand() % POOLSIZE;
  100. r = pool[p]; pool[p] = warandf();
  101. randbuf[i] = (REAL)(noiseamp * (((double)r) - 0.5));
  102. }
  103. break;
  104. case DITHER_TRIANGLE:
  105. for (i = 0;i < RANDBUFLEN;i++)
  106. {
  107. float r1, r2;
  108. int p;
  109. p = warand() % POOLSIZE;
  110. r1 = pool[p]; pool[p] = warandf();
  111. p = warand() % POOLSIZE;
  112. r2 = pool[p]; pool[p] = warandf();
  113. randbuf[i] = (REAL)(noiseamp * ((((double)r1)) - (((double)r2))));
  114. }
  115. break;
  116. #if 0
  117. case DITHER_GAUSSIAN: // gaussian
  118. for (i = 0;i < RANDBUFLEN;i++)
  119. {
  120. int sw = 0;
  121. double t, u;
  122. double r;
  123. int p;
  124. if (sw == 0)
  125. {
  126. sw = 1;
  127. p = warand() % POOLSIZE;
  128. r = ((double)pool[p]); pool[p] = warandf();
  129. t = sqrt(-2 * log(1 - r));
  130. p = warand() % POOLSIZE;
  131. r = ((double)pool[p]); pool[p] = warandf();
  132. u = 2 * M_PI * r;
  133. randbuf[i] = noiseamp * t * cos(u);
  134. }
  135. else
  136. {
  137. sw = 0;
  138. randbuf[i] = noiseamp * t * sin(u);
  139. }
  140. }
  141. break;
  142. #endif
  143. }
  144. randptr = 0;
  145. // if (dtype == 0 || dtype == 1) return 1;
  146. //return samp[shaper_type];
  147. }
  148. Shaper::~Shaper()
  149. {
  150. int i;
  151. for (i = 0;i < nch;i++) free(shapebuf[i]);
  152. free(shapebuf);
  153. free(randbuf);
  154. }
  155. int Shaper::do_shaping(double s,/*double *peak,*/int ch)
  156. {
  157. double u, h;
  158. int i;
  159. if (dtype == 1)
  160. {
  161. s += randbuf[randptr++ & (RANDBUFLEN-1)];
  162. if (s < shaper_clipmin)
  163. {
  164. //double d = (double)s / shaper_clipmin;
  165. //*peak = *peak < d ? d : *peak;
  166. s = shaper_clipmin;
  167. }
  168. if (s > shaper_clipmax)
  169. {
  170. //double d = (double)s / shaper_clipmax;
  171. //*peak = *peak < d ? d : *peak;
  172. s = shaper_clipmax;
  173. }
  174. return RINT(s);
  175. }
  176. h = 0;
  177. for (i = 0;i < shaper_len;i++)
  178. h += shapercoefs[shaper_type][i] * shapebuf[ch][i];
  179. s += h;
  180. u = s;
  181. s += randbuf[randptr++ & (RANDBUFLEN-1)];
  182. if (s < shaper_clipmin)
  183. {
  184. //double d = (double)s / shaper_clipmin;
  185. //*peak = *peak < d ? d : *peak;
  186. s = shaper_clipmin;
  187. }
  188. if (s > shaper_clipmax)
  189. {
  190. //double d = (double)s / shaper_clipmax;
  191. //*peak = *peak < d ? d : *peak;
  192. s = shaper_clipmax;
  193. }
  194. s = RINT(s);
  195. for (i = shaper_len - 2;i >= 0;i--) shapebuf[ch][i+1] = shapebuf[ch][i];
  196. shapebuf[ch][0] = s - u;
  197. return (int)s;
  198. }