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.

150 lines
3.3KB

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