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.

shuffler.cc 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // ----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 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 <stdio.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include "shuffler.h"
  26. namespace BLS1 {
  27. Shuffler::Shuffler (void) :
  28. _fsamp (0),
  29. _quant (0),
  30. _minpt (0),
  31. _iplen (0),
  32. _delay (0),
  33. _fft_time (0),
  34. _fft_freq (0),
  35. _fft_plan (0),
  36. _del_size (0),
  37. _del_wind (0),
  38. _del_data (0),
  39. _count (0),
  40. _touch0 (0),
  41. _touch1 (0)
  42. {
  43. }
  44. Shuffler::~Shuffler (void)
  45. {
  46. fini ();
  47. }
  48. void Shuffler::init (int fsamp, int quant, int abspri, int policy)
  49. {
  50. int k;
  51. _fsamp = fsamp;
  52. _quant = quant;
  53. _minpt = 256;
  54. for (k = _fsamp, _iplen = 1024; k > 56000; k >>= 1, _iplen <<= 1);
  55. if (_quant > _iplen) _quant = _iplen;
  56. _delay = _iplen / 2;
  57. if (_quant < _minpt) _delay += 2 * _minpt - _quant;
  58. else _minpt = _quant;
  59. _del_size = 3 * _iplen;
  60. _del_wind = 0;
  61. _del_data = new float [3 * _iplen];
  62. memset (_del_data, 0, 3 * _iplen * sizeof (float));
  63. _fft_time = (float *) fftwf_malloc (_iplen * sizeof (float));
  64. _fft_freq = (fftwf_complex *) fftwf_malloc ((_iplen / 2 + 1) * sizeof (fftwf_complex));
  65. _fft_plan = fftwf_plan_dft_c2r_1d (_iplen, _fft_freq, _fft_time, FFTW_ESTIMATE);
  66. memset (_fft_time, 0, _iplen * sizeof (float));
  67. _fft_time [_iplen / 2] = 1.0f;
  68. _convproc.configure (1, 1, _iplen, _quant, _minpt, _minpt);
  69. _convproc.impdata_create (0, 0, 1, _fft_time, 0, _iplen);
  70. _convproc.start_process (abspri, policy);
  71. }
  72. void Shuffler::fini (void)
  73. {
  74. fftwf_free (_fft_time);
  75. fftwf_free (_fft_freq);
  76. fftwf_destroy_plan (_fft_plan);
  77. delete[] _del_data;
  78. _convproc.stop_process ();
  79. _convproc.cleanup ();
  80. }
  81. void Shuffler::reset (void)
  82. {
  83. }
  84. void Shuffler::prepare (float gain, float freq)
  85. {
  86. int i, h;
  87. float f, g, t;
  88. g = powf (10.0f, 0.05f * gain);
  89. g = sqrtf (g * g - 1.0f);
  90. freq /= g;
  91. h = _iplen / 2;
  92. for (i = 0; i < h; i++)
  93. {
  94. f = i * _fsamp / _iplen;
  95. t = 1.0f / hypotf (1.0f, f / freq);
  96. _fft_freq [i][0] = 0;
  97. _fft_freq [i][1] = (i & 1) ? t : -t;
  98. }
  99. _fft_freq [h][0] = 0;
  100. _fft_freq [h][1] = 0;
  101. fftwf_execute(_fft_plan);
  102. g /= _iplen;
  103. for (i = 1; i < h; i++)
  104. {
  105. t = g * (0.6f + 0.4f * cosf (i * M_PI / h));
  106. _fft_time [h + i] *= t;
  107. _fft_time [h - i] *= t;
  108. }
  109. _fft_time [0] = 0;
  110. _fft_time [h] = 1;
  111. _convproc.impdata_update (0, 0, 1, _fft_time, 0, _iplen);
  112. _touch0++;
  113. }
  114. void Shuffler::process (int nsamp, float *inp [], float *out [])
  115. {
  116. int i, k, im, ri, wi;
  117. float a, b, *p0, *p1, *q;
  118. im = _del_size;
  119. wi = _del_wind;
  120. ri = _del_wind - _delay;
  121. if (ri < 0) ri += im;
  122. for (k = 0; k < nsamp; k += _quant)
  123. {
  124. if ((_count == 0) && (_touch0 != _touch1)) _count = 2 * _iplen;
  125. p0 = inp [0] + k;
  126. p1 = inp [1] + k;
  127. q = _convproc.inpdata (0);
  128. for (i = 0; i < _quant; i++)
  129. {
  130. a = p0 [i] / 2;
  131. b = p1 [i] / 2;
  132. _del_data [wi++] = a + b;
  133. if (wi == im) wi = 0;
  134. *q++ = a - b;
  135. }
  136. _convproc.process ();
  137. p0 = out [0] + k;
  138. p1 = out [1] + k;
  139. q = _convproc.outdata (0);
  140. for (i = 0; i < _quant; i++)
  141. {
  142. a = _del_data [ri++];
  143. if (ri == im) ri = 0;
  144. b = *q++;
  145. p0 [i] = a + b;
  146. p1 [i] = a - b;
  147. }
  148. if (_count)
  149. {
  150. _count -= _quant;
  151. if (_count == 0) _touch1 = _touch0;
  152. }
  153. }
  154. _del_wind = wi;
  155. }
  156. }