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.

vresampler.cc 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // ----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2006-2013 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 "vresampler.h"
  24. VResampler::VResampler (void) :
  25. _table (0),
  26. _nchan (0),
  27. _buff (0),
  28. _c1 (0),
  29. _c2 (0)
  30. {
  31. reset ();
  32. }
  33. VResampler::~VResampler (void)
  34. {
  35. clear ();
  36. }
  37. int VResampler::setup (double ratio,
  38. unsigned int nchan,
  39. unsigned int hlen)
  40. {
  41. if ((hlen < 8) || (hlen > 96) || (16 * ratio < 1) || (ratio > 256)) return 1;
  42. return setup (ratio, nchan, hlen, 1.0 - 2.6 / hlen);
  43. }
  44. int VResampler::setup (double ratio,
  45. unsigned int nchan,
  46. unsigned int hlen,
  47. double frel)
  48. {
  49. unsigned int h, k, n;
  50. double s;
  51. Resampler_table *T = 0;
  52. if (! nchan) return 1;
  53. n = NPHASE;
  54. s = n / ratio;
  55. h = hlen;
  56. k = 250;
  57. if (ratio < 1)
  58. {
  59. frel *= ratio;
  60. h = (unsigned int)(ceil (h / ratio));
  61. k = (unsigned int)(ceil (k / ratio));
  62. }
  63. T = Resampler_table::create (frel, h, n);
  64. clear ();
  65. if (T)
  66. {
  67. _table = T;
  68. _buff = new float [nchan * (2 * h - 1 + k)];
  69. _c1 = new float [2 * h];
  70. _c2 = new float [2 * h];
  71. _nchan = nchan;
  72. _inmax = k;
  73. _ratio = ratio;
  74. _pstep = s;
  75. _qstep = s;
  76. _wstep = 1;
  77. return reset ();
  78. }
  79. else return 1;
  80. }
  81. void VResampler::clear (void)
  82. {
  83. Resampler_table::destroy (_table);
  84. delete[] _buff;
  85. delete[] _c1;
  86. delete[] _c2;
  87. _buff = 0;
  88. _c1 = 0;
  89. _c2 = 0;
  90. _table = 0;
  91. _nchan = 0;
  92. _inmax = 0;
  93. _pstep = 0;
  94. _qstep = 0;
  95. _wstep = 1;
  96. reset ();
  97. }
  98. void VResampler::set_phase (double p)
  99. {
  100. if (!_table) return;
  101. _phase = (p - floor (p)) * _table->_np;
  102. }
  103. void VResampler::set_rrfilt (double t)
  104. {
  105. if (!_table) return;
  106. _wstep = (t < 1) ? 1 : 1 - exp (-1 / t);
  107. }
  108. void VResampler::set_rratio (double r)
  109. {
  110. if (!_table) return;
  111. if (r > 16.0) r = 16.0;
  112. if (r < 0.95) r = 0.95;
  113. _qstep = _table->_np / (_ratio * r);
  114. }
  115. double VResampler::inpdist (void) const
  116. {
  117. if (!_table) return 0;
  118. return (int)(_table->_hl + 1 - _nread) - _phase / _table->_np;
  119. }
  120. int VResampler::inpsize (void) const
  121. {
  122. if (!_table) return 0;
  123. return 2 * _table->_hl;
  124. }
  125. int VResampler::reset (void)
  126. {
  127. if (!_table) return 1;
  128. inp_count = 0;
  129. out_count = 0;
  130. inp_data = 0;
  131. out_data = 0;
  132. _index = 0;
  133. _phase = 0;
  134. _nread = 2 * _table->_hl;
  135. _nzero = 0;
  136. return 0;
  137. }
  138. int VResampler::process (void)
  139. {
  140. unsigned int k, np, in, nr, n, c;
  141. int i, hl, nz;
  142. double ph, dp, dd;
  143. float a, b, *p1, *p2, *q1, *q2;
  144. if (!_table) return 1;
  145. hl = _table->_hl;
  146. np = _table->_np;
  147. in = _index;
  148. nr = _nread;
  149. nz = _nzero;
  150. ph = _phase;
  151. dp = _pstep;
  152. n = (2 * hl - nr) * _nchan;
  153. p1 = _buff + in * _nchan;
  154. p2 = p1 + n;
  155. while (out_count)
  156. {
  157. if (nr)
  158. {
  159. if (inp_count == 0) break;
  160. if (inp_data)
  161. {
  162. for (c = 0; c < _nchan; c++) p2 [c] = inp_data [c];
  163. inp_data += _nchan;
  164. nz = 0;
  165. }
  166. else
  167. {
  168. for (c = 0; c < _nchan; c++) p2 [c] = 0;
  169. if (nz < 2 * hl) nz++;
  170. }
  171. nr--;
  172. p2 += _nchan;
  173. inp_count--;
  174. }
  175. else
  176. {
  177. if (out_data)
  178. {
  179. if (nz < 2 * hl)
  180. {
  181. k = (unsigned int) ph;
  182. b = (float)(ph - k);
  183. a = 1.0f - b;
  184. q1 = _table->_ctab + hl * k;
  185. q2 = _table->_ctab + hl * (np - k);
  186. for (i = 0; i < hl; i++)
  187. {
  188. _c1 [i] = a * q1 [i] + b * q1 [i + hl];
  189. _c2 [i] = a * q2 [i] + b * q2 [i - hl];
  190. }
  191. for (c = 0; c < _nchan; c++)
  192. {
  193. q1 = p1 + c;
  194. q2 = p2 + c;
  195. a = 1e-25f;
  196. for (i = 0; i < hl; i++)
  197. {
  198. q2 -= _nchan;
  199. a += *q1 * _c1 [i] + *q2 * _c2 [i];
  200. q1 += _nchan;
  201. }
  202. *out_data++ = a - 1e-25f;
  203. }
  204. }
  205. else
  206. {
  207. for (c = 0; c < _nchan; c++) *out_data++ = 0;
  208. }
  209. }
  210. out_count--;
  211. dd = _qstep - dp;
  212. if (fabs (dd) < 1e-30) dp = _qstep;
  213. else dp += _wstep * dd;
  214. ph += dp;
  215. if (ph >= np)
  216. {
  217. nr = (unsigned int) floor( ph / np);
  218. ph -= nr * np;;
  219. in += nr;
  220. p1 += nr * _nchan;;
  221. if (in >= _inmax)
  222. {
  223. n = (2 * hl - nr) * _nchan;
  224. memcpy (_buff, p1, n * sizeof (float));
  225. in = 0;
  226. p1 = _buff;
  227. p2 = p1 + n;
  228. }
  229. }
  230. }
  231. }
  232. _index = in;
  233. _nread = nr;
  234. _phase = ph;
  235. _pstep = dp;
  236. _nzero = nz;
  237. return 0;
  238. }