Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resampler-table.cc 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "resampler-table.h"
  24. static double sinc (double x)
  25. {
  26. x = fabs (x);
  27. if (x < 1e-6) return 1.0;
  28. x *= M_PI;
  29. return sin (x) / x;
  30. }
  31. static double wind (double x)
  32. {
  33. x = fabs (x);
  34. if (x >= 1.0) return 0.0f;
  35. x *= M_PI;
  36. return 0.384 + 0.500 * cos (x) + 0.116 * cos (2 * x);
  37. }
  38. Resampler_table *Resampler_table::_list = 0;
  39. Resampler_mutex Resampler_table::_mutex;
  40. Resampler_table::Resampler_table (double fr, unsigned int hl, unsigned int np) :
  41. _next (0),
  42. _refc (0),
  43. _fr (fr),
  44. _hl (hl),
  45. _np (np)
  46. {
  47. unsigned int i, j;
  48. double t;
  49. float *p;
  50. _ctab = new float [hl * (np + 1)];
  51. p = _ctab;
  52. for (j = 0; j <= np; j++)
  53. {
  54. t = (double) j / (double) np;
  55. for (i = 0; i < hl; i++)
  56. {
  57. p [hl - i - 1] = (float)(fr * sinc (t * fr) * wind (t / hl));
  58. t += 1;
  59. }
  60. p += hl;
  61. }
  62. }
  63. Resampler_table::~Resampler_table (void)
  64. {
  65. delete[] _ctab;
  66. }
  67. Resampler_table *Resampler_table::create (double fr, unsigned int hl, unsigned int np)
  68. {
  69. Resampler_table *P;
  70. _mutex.lock ();
  71. P = _list;
  72. while (P)
  73. {
  74. if ((fr >= P->_fr * 0.999) && (fr <= P->_fr * 1.001) && (hl == P->_hl) && (np == P->_np))
  75. {
  76. P->_refc++;
  77. _mutex.unlock ();
  78. return P;
  79. }
  80. P = P->_next;
  81. }
  82. P = new Resampler_table (fr, hl, np);
  83. P->_refc = 1;
  84. P->_next = _list;
  85. _list = P;
  86. _mutex.unlock ();
  87. return P;
  88. }
  89. void Resampler_table::destroy (Resampler_table *T)
  90. {
  91. Resampler_table *P, *Q;
  92. _mutex.lock ();
  93. if (T)
  94. {
  95. T->_refc--;
  96. if (T->_refc == 0)
  97. {
  98. P = _list;
  99. Q = 0;
  100. while (P)
  101. {
  102. if (P == T)
  103. {
  104. if (Q) Q->_next = T->_next;
  105. else _list = T->_next;
  106. break;
  107. }
  108. Q = P;
  109. P = P->_next;
  110. }
  111. delete T;
  112. }
  113. }
  114. _mutex.unlock ();
  115. }
  116. void Resampler_table::print_list (void)
  117. {
  118. Resampler_table *P;
  119. printf ("Resampler table\n----\n");
  120. for (P = _list; P; P = P->_next)
  121. {
  122. printf ("refc = %3d fr = %10.6lf hl = %4d np = %4d\n", P->_refc, P->_fr, P->_hl, P->_np);
  123. }
  124. printf ("----\n\n");
  125. }