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.cc 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.h"
  24. #undef ENABLE_VEC4
  25. #ifndef CARLA_OS_WIN
  26. # if defined(__SSE2_MATH__)
  27. # define ENABLE_VEC4
  28. # include <xmmintrin.h>
  29. # elif defined(__ARM_NEON) || defined(__ARM_NEON__)
  30. # define ENABLE_VEC4
  31. # include <arm_neon.h>
  32. # endif
  33. #endif
  34. static unsigned int gcd (unsigned int a, unsigned int b)
  35. {
  36. if (a == 0) return b;
  37. if (b == 0) return a;
  38. while (1)
  39. {
  40. if (a > b)
  41. {
  42. a = a % b;
  43. if (a == 0) return b;
  44. if (a == 1) return 1;
  45. }
  46. else
  47. {
  48. b = b % a;
  49. if (b == 0) return a;
  50. if (b == 1) return 1;
  51. }
  52. }
  53. return 1;
  54. }
  55. Resampler::Resampler (void) noexcept :
  56. _table (0),
  57. _nchan (0),
  58. _buff (0)
  59. {
  60. reset ();
  61. }
  62. Resampler::~Resampler (void)
  63. {
  64. clear ();
  65. }
  66. bool Resampler::setup (unsigned int fs_inp,
  67. unsigned int fs_out,
  68. unsigned int nchan,
  69. unsigned int hlen)
  70. {
  71. return setup (fs_inp, fs_out, nchan, hlen, 1.0 - 2.6 / hlen);
  72. }
  73. bool Resampler::setup (unsigned int fs_inp,
  74. unsigned int fs_out,
  75. unsigned int nchan,
  76. unsigned int hlen,
  77. double frel)
  78. {
  79. unsigned int np, dp, mi, hl, n;
  80. double r;
  81. Resampler_table *T = 0;
  82. if (!nchan || (hlen < 8) || (hlen > 96))
  83. {
  84. clear ();
  85. return false;
  86. }
  87. r = (double) fs_out / (double) fs_inp;
  88. n = gcd (fs_out, fs_inp);
  89. np = fs_out / n;
  90. dp = fs_inp / n;
  91. if ((64 * r < 1.0) || (np > 1000))
  92. {
  93. clear ();
  94. return false;
  95. }
  96. hl = hlen;
  97. mi = 32;
  98. if (r < 1.0)
  99. {
  100. frel *= r;
  101. hl = (unsigned int)(ceil (hl / r));
  102. mi = (unsigned int)(ceil (mi / r));
  103. }
  104. #ifdef ENABLE_VEC4
  105. hl = (hl + 3) & ~3;
  106. #endif
  107. T = Resampler_table::create (frel, hl, np);
  108. clear ();
  109. if (T)
  110. {
  111. _table = T;
  112. n = nchan * (2 * hl + mi);
  113. #ifdef ENABLE_VEC4
  114. posix_memalign ((void **)(&_buff), 16, n * sizeof (float));
  115. memset (_buff, 0, n * sizeof (float));
  116. #else
  117. _buff = new float [n];
  118. #endif
  119. _nchan = nchan;
  120. _inmax = mi;
  121. _pstep = dp;
  122. return reset ();
  123. }
  124. else return false;
  125. }
  126. void Resampler::clear (void)
  127. {
  128. Resampler_table::destroy (_table);
  129. #ifdef ENABLE_VEC4
  130. free (_buff);
  131. #else
  132. delete[] _buff;
  133. #endif
  134. _buff = 0;
  135. _table = 0;
  136. _nchan = 0;
  137. _inmax = 0;
  138. _pstep = 0;
  139. reset ();
  140. }
  141. double Resampler::inpdist (void) const noexcept
  142. {
  143. if (!_table) return 0;
  144. return (int)(_table->_hl + 1 - _nread) - (double)_phase / _table->_np;
  145. }
  146. int Resampler::inpsize (void) const noexcept
  147. {
  148. if (!_table) return 0;
  149. return 2 * _table->_hl;
  150. }
  151. bool Resampler::reset (void) noexcept
  152. {
  153. if (!_table) return false;
  154. inp_count = 0;
  155. out_count = 0;
  156. inp_data = 0;
  157. out_data = 0;
  158. _index = 0;
  159. _nread = 0;
  160. _nzero = 0;
  161. _phase = 0;
  162. if (_table)
  163. {
  164. _nread = 2 * _table->_hl;
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool Resampler::process (void)
  170. {
  171. unsigned int hl, np, ph, dp, in, nr, nz, di, i, j, n;
  172. float *c1, *c2, *p1, *p2, *q1, *q2;
  173. if (!_table) return false;
  174. hl = _table->_hl;
  175. np = _table->_np;
  176. dp = _pstep;
  177. in = _index;
  178. nr = _nread;
  179. nz = _nzero;
  180. ph = _phase;
  181. p1 = _buff + in;
  182. p2 = p1 + 2 * hl - nr;
  183. di = 2 * hl + _inmax;
  184. while (out_count)
  185. {
  186. while (nr && inp_count)
  187. {
  188. if (inp_data)
  189. {
  190. for (j = 0; j < _nchan; j++) p2 [j * di] = inp_data [j];
  191. inp_data += _nchan;
  192. nz = 0;
  193. }
  194. else
  195. {
  196. for (j = 0; j < _nchan; j++) p2 [j * di] = 0;
  197. if (nz < 2 * hl) nz++;
  198. }
  199. p2++;
  200. nr--;
  201. inp_count--;
  202. }
  203. if (nr) break;
  204. if (out_data)
  205. {
  206. if (nz < 2 * hl)
  207. {
  208. c1 = _table->_ctab + hl * ph;
  209. c2 = _table->_ctab + hl * (np - ph);
  210. #if defined(__SSE2_MATH__) && !defined(CARLA_OS_WIN)
  211. __m128 C1, C2, Q1, Q2, S;
  212. for (j = 0; j < _nchan; j++)
  213. {
  214. q1 = p1 + j * di;
  215. q2 = p2 + j * di;
  216. S = _mm_setzero_ps ();
  217. for (i = 0; i < hl; i += 4)
  218. {
  219. C1 = _mm_load_ps (c1 + i);
  220. Q1 = _mm_loadu_ps (q1);
  221. q2 -= 4;
  222. S = _mm_add_ps (S, _mm_mul_ps (C1, Q1));
  223. C2 = _mm_loadr_ps (c2 + i);
  224. Q2 = _mm_loadu_ps (q2);
  225. q1 += 4;
  226. S = _mm_add_ps (S, _mm_mul_ps (C2, Q2));
  227. }
  228. *out_data++ = S [0] + S [1] + S [2] + S [3];
  229. }
  230. #elif (defined(__ARM_NEON) || defined(__ARM_NEON__)) && !defined(CARLA_OS_WIN)
  231. // ARM64 version by Nicolas Belin <nbelin@baylibre.com>
  232. float32x4_t *C1 = (float32x4_t *)c1;
  233. float32x4_t *C2 = (float32x4_t *)c2;
  234. float32x4_t S, T;
  235. for (j = 0; j < _nchan; j++)
  236. {
  237. q1 = p1 + j * di;
  238. q2 = p2 + j * di - 4;
  239. T = vrev64q_f32 (vld1q_f32 (q2));
  240. S = vmulq_f32 (vextq_f32 (T, T, 2), C2 [0]);
  241. S = vmlaq_f32 (S, vld1q_f32(q1), C1 [0]);
  242. for (i = 1; i < (hl>>2); i++)
  243. {
  244. q2 -= 4;
  245. q1 += 4;
  246. T = vrev64q_f32 (vld1q_f32 (q2));
  247. S = vmlaq_f32 (S, vextq_f32 (T, T, 2), C2 [i]);
  248. S = vmlaq_f32 (S, vld1q_f32 (q1), C1 [i]);
  249. }
  250. *out_data++ = S [0] + S [1] + S [2] + S [3];
  251. }
  252. #else
  253. float s;
  254. for (j = 0; j < _nchan; j++)
  255. {
  256. q1 = p1 + j * di;
  257. q2 = p2 + j * di;
  258. s = 1e-30f;
  259. for (i = 0; i < hl; i++)
  260. {
  261. q2--;
  262. s += *q1 * c1 [i] + *q2 * c2 [i];
  263. q1++;
  264. }
  265. *out_data++ = s - 1e-30f;
  266. }
  267. #endif
  268. }
  269. else
  270. {
  271. for (j = 0; j < _nchan; j++) *out_data++ = 0;
  272. }
  273. }
  274. out_count--;
  275. ph += dp;
  276. if (ph >= np)
  277. {
  278. nr = ph / np;
  279. ph -= nr * np;
  280. in += nr;
  281. p1 += nr;
  282. if (in >= _inmax)
  283. {
  284. n = 2 * hl - nr;
  285. p2 = _buff;
  286. for (j = 0; j < _nchan; j++)
  287. {
  288. memmove (p2 + j * di, p1 + j * di, n * sizeof (float));
  289. }
  290. in = 0;
  291. p1 = _buff;
  292. p2 = p1 + n;
  293. }
  294. }
  295. }
  296. _index = in;
  297. _nread = nr;
  298. _phase = ph;
  299. _nzero = nz;
  300. return true;
  301. }