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.

retuner.cc 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // -----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2009-2011 Fons Adriaensen <fons@linuxaudio.org>
  4. // Modified by falkTX on Jan 2015 for inclusion in Carla
  5. //
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 2 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. //
  20. // -----------------------------------------------------------------------
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include "retuner.h"
  26. namespace AT1 {
  27. Retuner::Retuner (int fsamp) :
  28. _fsamp (fsamp),
  29. _refpitch (440.0f),
  30. _notebias (0.0f),
  31. _corrfilt (1.0f),
  32. _corrgain (1.0f),
  33. _corroffs (0.0f),
  34. _notemask (0xFFF)
  35. {
  36. int i, h;
  37. float t, x, y;
  38. if (_fsamp < 64000)
  39. {
  40. // At 44.1 and 48 kHz resample to double rate.
  41. _upsamp = true;
  42. _ipsize = 4096;
  43. _fftlen = 2048;
  44. _frsize = 128;
  45. _resampler.setup (1, 2, 1, 32); // 32 is medium quality.
  46. // Prefeed some input samples to remove delay.
  47. _resampler.inp_count = _resampler.filtlen () - 1;
  48. _resampler.inp_data = 0;
  49. _resampler.out_count = 0;
  50. _resampler.out_data = 0;
  51. _resampler.process ();
  52. }
  53. else if (_fsamp < 128000)
  54. {
  55. // 88.2 or 96 kHz.
  56. _upsamp = false;
  57. _ipsize = 4096;
  58. _fftlen = 4096;
  59. _frsize = 256;
  60. }
  61. else
  62. {
  63. // 192 kHz, double time domain buffers sizes.
  64. _upsamp = false;
  65. _ipsize = 8192;
  66. _fftlen = 8192;
  67. _frsize = 512;
  68. }
  69. // Accepted correlation peak range, corresponding to 60..1200 Hz.
  70. _ifmin = _fsamp / 1200;
  71. _ifmax = _fsamp / 60;
  72. // Various buffers
  73. _ipbuff = new float[_ipsize + 3]; // Resampled or filtered input
  74. _xffunc = new float[_frsize]; // Crossfade function
  75. _fftTwind = (float *) fftwf_malloc (_fftlen * sizeof (float)); // Window function
  76. _fftWcorr = (float *) fftwf_malloc (_fftlen * sizeof (float)); // Autocorrelation of window
  77. _fftTdata = (float *) fftwf_malloc (_fftlen * sizeof (float)); // Time domain data for FFT
  78. _fftFdata = (fftwf_complex *) fftwf_malloc ((_fftlen / 2 + 1) * sizeof (fftwf_complex));
  79. // FFTW3 plans
  80. _fwdplan = fftwf_plan_dft_r2c_1d (_fftlen, _fftTdata, _fftFdata, FFTW_ESTIMATE);
  81. _invplan = fftwf_plan_dft_c2r_1d (_fftlen, _fftFdata, _fftTdata, FFTW_ESTIMATE);
  82. // Clear input buffer.
  83. memset (_ipbuff, 0, (_ipsize + 1) * sizeof (float));
  84. // Create crossfade function, half of raised cosine.
  85. for (i = 0; i < _frsize; i++)
  86. {
  87. _xffunc [i] = 0.5 * (1 - cosf (M_PI * i / _frsize));
  88. }
  89. // Create window, raised cosine.
  90. for (i = 0; i < _fftlen; i++)
  91. {
  92. _fftTwind [i] = 0.5 * (1 - cosf (2 * M_PI * i / _fftlen));
  93. }
  94. // Compute window autocorrelation and normalise it.
  95. fftwf_execute_dft_r2c (_fwdplan, _fftTwind, _fftFdata);
  96. h = _fftlen / 2;
  97. for (i = 0; i < h; i++)
  98. {
  99. x = _fftFdata [i][0];
  100. y = _fftFdata [i][1];
  101. _fftFdata [i][0] = x * x + y * y;
  102. _fftFdata [i][1] = 0;
  103. }
  104. _fftFdata [h][0] = 0;
  105. _fftFdata [h][1] = 0;
  106. fftwf_execute_dft_c2r (_invplan, _fftFdata, _fftWcorr);
  107. t = _fftWcorr [0];
  108. for (i = 0; i < _fftlen; i++)
  109. {
  110. _fftWcorr [i] /= t;
  111. }
  112. // Initialise all counters and other state.
  113. _notebits = 0;
  114. _lastnote = -1;
  115. _count = 0;
  116. _cycle = _frsize;
  117. _error = 0.0f;
  118. _ratio = 1.0f;
  119. _xfade = false;
  120. _ipindex = 0;
  121. _frindex = 0;
  122. _frcount = 0;
  123. _rindex1 = _ipsize / 2;
  124. _rindex2 = 0;
  125. }
  126. Retuner::~Retuner (void)
  127. {
  128. delete[] _ipbuff;
  129. delete[] _xffunc;
  130. fftwf_free (_fftTwind);
  131. fftwf_free (_fftWcorr);
  132. fftwf_free (_fftTdata);
  133. fftwf_free (_fftFdata);
  134. fftwf_destroy_plan (_fwdplan);
  135. fftwf_destroy_plan (_invplan);
  136. }
  137. int Retuner::process (int nfram, float *inp, float *out)
  138. {
  139. int i, k, fi;
  140. float ph, dp, r1, r2, dr, u1, u2, v;
  141. // Pitch shifting is done by resampling the input at the
  142. // required ratio, and eventually jumping forward or back
  143. // by one or more pitch period(s). Processing is done in
  144. // fragments of '_frsize' frames, and the decision to jump
  145. // forward or back is taken at the start of each fragment.
  146. // If a jump happens we crossfade over one fragment size.
  147. // Every 4 fragments a new pitch estimate is made. Since
  148. // _fftsize = 16 * _frsize, the estimation window moves
  149. // by 1/4 of the FFT length.
  150. fi = _frindex; // Write index in current fragment.
  151. r1 = _rindex1; // Read index for current input frame.
  152. r2 = _rindex2; // Second read index while crossfading.
  153. // No assumptions are made about fragments being aligned
  154. // with process() calls, so we may be in the middle of
  155. // a fragment here.
  156. while (nfram)
  157. {
  158. // Don't go past the end of the current fragment.
  159. k = _frsize - fi;
  160. if (nfram < k) k = nfram;
  161. nfram -= k;
  162. // At 44.1 and 48 kHz upsample by 2.
  163. if (_upsamp)
  164. {
  165. _resampler.inp_count = k;
  166. _resampler.inp_data = inp;
  167. _resampler.out_count = 2 * k;
  168. _resampler.out_data = _ipbuff + _ipindex;
  169. _resampler.process ();
  170. _ipindex += 2 * k;
  171. }
  172. // At higher sample rates apply lowpass filter.
  173. else
  174. {
  175. // Not implemented yet, just copy.
  176. memcpy (_ipbuff + _ipindex, inp, k * sizeof (float));
  177. _ipindex += k;
  178. }
  179. // Extra samples for interpolation.
  180. _ipbuff [_ipsize + 0] = _ipbuff [0];
  181. _ipbuff [_ipsize + 1] = _ipbuff [1];
  182. _ipbuff [_ipsize + 2] = _ipbuff [2];
  183. inp += k;
  184. if (_ipindex == _ipsize) _ipindex = 0;
  185. // Process available samples.
  186. dr = _ratio;
  187. if (_upsamp) dr *= 2;
  188. if (_xfade)
  189. {
  190. // Interpolate and crossfade.
  191. while (k--)
  192. {
  193. i = (int) r1;
  194. u1 = cubic (_ipbuff + i, r1 - i);
  195. i = (int) r2;
  196. u2 = cubic (_ipbuff + i, r2 - i);
  197. v = _xffunc [fi++];
  198. *out++ = (1 - v) * u1 + v * u2;
  199. r1 += dr;
  200. if (r1 >= _ipsize) r1 -= _ipsize;
  201. r2 += dr;
  202. if (r2 >= _ipsize) r2 -= _ipsize;
  203. }
  204. }
  205. else
  206. {
  207. // Interpolation only.
  208. fi += k;
  209. while (k--)
  210. {
  211. i = (int) r1;
  212. *out++ = cubic (_ipbuff + i, r1 - i);
  213. r1 += dr;
  214. if (r1 >= _ipsize) r1 -= _ipsize;
  215. }
  216. }
  217. // If at end of fragment check for jump.
  218. if (fi == _frsize)
  219. {
  220. fi = 0;
  221. // Estimate the pitch every 4th fragment.
  222. if (++_frcount == 4)
  223. {
  224. _frcount = 0;
  225. findcycle ();
  226. if (_cycle)
  227. {
  228. // If the pitch estimate succeeds, find the
  229. // nearest note and required resampling ratio.
  230. _count = 0;
  231. finderror ();
  232. }
  233. else if (++_count > 5)
  234. {
  235. // If the pitch estimate fails, the current
  236. // ratio is kept for 5 fragments. After that
  237. // the signal is considered unvoiced and the
  238. // pitch error is reset.
  239. _count = 5;
  240. _cycle = _frsize;
  241. _error = 0;
  242. }
  243. else if (_count == 2)
  244. {
  245. // Bias is removed after two unvoiced fragments.
  246. _lastnote = -1;
  247. }
  248. _ratio = powf (2.0f, _corroffs / 12.0f - _error * _corrgain);
  249. }
  250. // If the previous fragment was crossfading,
  251. // the end of the new fragment that was faded
  252. // in becomes the current read position.
  253. if (_xfade) r1 = r2;
  254. // A jump must correspond to an integer number
  255. // of pitch periods, and to avoid reading outside
  256. // the circular input buffer limits it must be at
  257. // least one fragment size.
  258. dr = _cycle * (int)(ceilf (_frsize / _cycle));
  259. dp = dr / _frsize;
  260. ph = r1 - _ipindex;
  261. if (ph < 0) ph += _ipsize;
  262. if (_upsamp)
  263. {
  264. ph /= 2;
  265. dr *= 2;
  266. }
  267. ph = ph / _frsize + 2 * _ratio - 10;
  268. if (ph > 0.5f)
  269. {
  270. // Jump back by 'dr' frames and crossfade.
  271. _xfade = true;
  272. r2 = r1 - dr;
  273. if (r2 < 0) r2 += _ipsize;
  274. }
  275. else if (ph + dp < 0.5f)
  276. {
  277. // Jump forward by 'dr' frames and crossfade.
  278. _xfade = true;
  279. r2 = r1 + dr;
  280. if (r2 >= _ipsize) r2 -= _ipsize;
  281. }
  282. else _xfade = false;
  283. }
  284. }
  285. // Save local state.
  286. _frindex = fi;
  287. _rindex1 = r1;
  288. _rindex2 = r2;
  289. return 0;
  290. }
  291. void Retuner::findcycle (void)
  292. {
  293. int d, h, i, j, k;
  294. float f, m, t, x, y, z;
  295. d = _upsamp ? 2 : 1;
  296. h = _fftlen / 2;
  297. j = _ipindex;
  298. k = _ipsize - 1;
  299. for (i = 0; i < _fftlen; i++)
  300. {
  301. _fftTdata [i] = _fftTwind [i] * _ipbuff [j & k];
  302. j += d;
  303. }
  304. fftwf_execute_dft_r2c (_fwdplan, _fftTdata, _fftFdata);
  305. f = _fsamp / (_fftlen * 3e3f);
  306. for (i = 0; i < h; i++)
  307. {
  308. x = _fftFdata [i][0];
  309. y = _fftFdata [i][1];
  310. m = i * f;
  311. _fftFdata [i][0] = (x * x + y * y) / (1 + m * m);
  312. _fftFdata [i][1] = 0;
  313. }
  314. _fftFdata [h][0] = 0;
  315. _fftFdata [h][1] = 0;
  316. fftwf_execute_dft_c2r (_invplan, _fftFdata, _fftTdata);
  317. t = _fftTdata [0] + 0.1f;
  318. for (i = 0; i < h; i++) _fftTdata [i] /= (t * _fftWcorr [i]);
  319. x = _fftTdata [0];
  320. for (i = 4; i < _ifmax; i += 4)
  321. {
  322. y = _fftTdata [i];
  323. if (y > x) break;
  324. x = y;
  325. }
  326. i -= 4;
  327. _cycle = 0;
  328. if (i >= _ifmax) return;
  329. if (i < _ifmin) i = _ifmin;
  330. x = _fftTdata [--i];
  331. y = _fftTdata [++i];
  332. m = 0;
  333. j = 0;
  334. while (i <= _ifmax)
  335. {
  336. t = y * _fftWcorr [i];
  337. z = _fftTdata [++i];
  338. if ((t > m) && (y >= x) && (y >= z) && (y > 0.8f))
  339. {
  340. j = i - 1;
  341. m = t;
  342. }
  343. x = y;
  344. y = z;
  345. }
  346. if (j)
  347. {
  348. x = _fftTdata [j - 1];
  349. y = _fftTdata [j];
  350. z = _fftTdata [j + 1];
  351. _cycle = j + 0.5f * (x - z) / (z - 2 * y + x - 1e-9f);
  352. }
  353. }
  354. void Retuner::finderror (void)
  355. {
  356. int i, m, im;
  357. float a, am, d, dm, f;
  358. if (!_notemask)
  359. {
  360. _error = 0;
  361. _lastnote = -1;
  362. return;
  363. }
  364. f = log2f (_fsamp / (_cycle * _refpitch));
  365. dm = 0;
  366. am = 1;
  367. im = -1;
  368. for (i = 0, m = 1; i < 12; i++, m <<= 1)
  369. {
  370. if (_notemask & m)
  371. {
  372. d = f - (i - 9) / 12.0f;
  373. d -= floorf (d + 0.5f);
  374. a = fabsf (d);
  375. if (i == _lastnote) a -= _notebias;
  376. if (a < am)
  377. {
  378. am = a;
  379. dm = d;
  380. im = i;
  381. }
  382. }
  383. }
  384. if (_lastnote == im)
  385. {
  386. _error += _corrfilt * (dm - _error);
  387. }
  388. else
  389. {
  390. _error = dm;
  391. _lastnote = im;
  392. }
  393. // For display only.
  394. _notebits |= 1 << im;
  395. }
  396. float Retuner::cubic (float *v, float a)
  397. {
  398. float b, c;
  399. b = 1 - a;
  400. c = a * b;
  401. return (1.0f + 1.5f * c) * (v[1] * b + v[2] * a)
  402. - 0.5f * c * (v[0] * b + v[1] + v[2] + v[3] * a);
  403. }
  404. }