resampler-table.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // ----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2006-2012 Fons Adriaensen <fons@linuxaudio.org>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. // ----------------------------------------------------------------------------
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <zita-resampler/resampler-table.h>
  24. int zita_resampler_major_version (void)
  25. {
  26. return ZITA_RESAMPLER_MAJOR_VERSION;
  27. }
  28. int zita_resampler_minor_version (void)
  29. {
  30. return ZITA_RESAMPLER_MINOR_VERSION;
  31. }
  32. static double sinc (double x)
  33. {
  34. x = fabs (x);
  35. if (x < 1e-6) return 1.0;
  36. x *= M_PI;
  37. return sin (x) / x;
  38. }
  39. static double wind (double x)
  40. {
  41. x = fabs (x);
  42. if (x >= 1.0) return 0.0f;
  43. x *= M_PI;
  44. return 0.384 + 0.500 * cos (x) + 0.116 * cos (2 * x);
  45. }
  46. Resampler_table *Resampler_table::_list = 0;
  47. Resampler_mutex Resampler_table::_mutex;
  48. Resampler_table::Resampler_table (double fr, unsigned int hl, unsigned int np) :
  49. _next (0),
  50. _refc (0),
  51. _fr (fr),
  52. _hl (hl),
  53. _np (np)
  54. {
  55. unsigned int i, j;
  56. double t;
  57. float *p;
  58. _ctab = new float [hl * (np + 1)];
  59. p = _ctab;
  60. for (j = 0; j <= np; j++)
  61. {
  62. t = (double) j / (double) np;
  63. for (i = 0; i < hl; i++)
  64. {
  65. p [hl - i - 1] = (float)(fr * sinc (t * fr) * wind (t / hl));
  66. t += 1;
  67. }
  68. p += hl;
  69. }
  70. }
  71. Resampler_table::~Resampler_table (void)
  72. {
  73. delete[] _ctab;
  74. }
  75. Resampler_table *Resampler_table::create (double fr, unsigned int hl, unsigned int np)
  76. {
  77. Resampler_table *P;
  78. _mutex.lock ();
  79. P = _list;
  80. while (P)
  81. {
  82. if ((fr >= P->_fr * 0.999) && (fr <= P->_fr * 1.001) && (hl == P->_hl) && (np == P->_np))
  83. {
  84. P->_refc++;
  85. _mutex.unlock ();
  86. return P;
  87. }
  88. P = P->_next;
  89. }
  90. P = new Resampler_table (fr, hl, np);
  91. P->_refc = 1;
  92. P->_next = _list;
  93. _list = P;
  94. _mutex.unlock ();
  95. return P;
  96. }
  97. void Resampler_table::destroy (Resampler_table *T)
  98. {
  99. Resampler_table *P, *Q;
  100. _mutex.lock ();
  101. if (T)
  102. {
  103. T->_refc--;
  104. if (T->_refc == 0)
  105. {
  106. P = _list;
  107. Q = 0;
  108. while (P)
  109. {
  110. if (P == T)
  111. {
  112. if (Q) Q->_next = T->_next;
  113. else _list = T->_next;
  114. break;
  115. }
  116. Q = P;
  117. P = P->_next;
  118. }
  119. delete T;
  120. }
  121. }
  122. _mutex.unlock ();
  123. }
  124. void Resampler_table::print_list (void)
  125. {
  126. Resampler_table *P;
  127. printf ("Resampler table\n----\n");
  128. for (P = _list; P; P = P->_next)
  129. {
  130. printf ("refc = %3d fr = %10.6lf hl = %4d np = %4d\n", P->_refc, P->_fr, P->_hl, P->_np);
  131. }
  132. printf ("----\n\n");
  133. }