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.

jclient.cc 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // ----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2011 Fons Adriaensen <fons@linuxaudio.org>
  4. // Modified by falkTX on Jan-Apr 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 <string.h>
  22. #include "jclient.h"
  23. namespace BLS1 {
  24. Jclient::Jclient (jack_client_t *jclient) :
  25. A_thread ("Jclient"),
  26. _jack_client (jclient),
  27. _active (false),
  28. _inpbal0 (0),
  29. _inpbal1 (0),
  30. _ga (1.0f),
  31. _gb (1.0f),
  32. _da (0.0f),
  33. _db (0.0f)
  34. {
  35. init_jack ();
  36. }
  37. Jclient::~Jclient (void)
  38. {
  39. if (_jack_client) close_jack ();
  40. }
  41. void Jclient::init_jack (void)
  42. {
  43. jack_set_process_callback (_jack_client, jack_static_process, (void *) this);
  44. jack_on_shutdown (_jack_client, jack_static_shutdown, (void *) this);
  45. jack_activate (_jack_client);
  46. _fsamp = jack_get_sample_rate (_jack_client);
  47. _psize = jack_get_buffer_size (_jack_client);
  48. if (_psize > 4096)
  49. {
  50. fprintf (stderr, "Period size can't be more than 4096.\n");
  51. return;
  52. }
  53. if (_psize & (_psize - 1))
  54. {
  55. fprintf (stderr, "Period size must be a power of 2.\n");
  56. return;
  57. }
  58. _inpports [0] = jack_port_register (_jack_client, "inp.L", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  59. _inpports [1] = jack_port_register (_jack_client, "inp.R", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  60. _outports [0] = jack_port_register (_jack_client, "out.L", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  61. _outports [1] = jack_port_register (_jack_client, "out.R", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  62. _hpfilt.setfsamp (_fsamp);
  63. _lshelf.setfsamp (_fsamp);
  64. _lshelf.bypass (false);
  65. _shuffl.init (_fsamp, _psize);
  66. for (int k = _fsamp, _fragm = 1024; k > 56000; k >>= 1, _fragm <<= 1);
  67. _nsamp = 0;
  68. _active = true;
  69. }
  70. void Jclient::close_jack ()
  71. {
  72. jack_deactivate (_jack_client);
  73. jack_client_close (_jack_client);
  74. }
  75. void Jclient::jack_static_shutdown (void *arg)
  76. {
  77. ((Jclient *) arg)->jack_shutdown ();
  78. }
  79. int Jclient::jack_static_process (jack_nframes_t nframes, void *arg)
  80. {
  81. return ((Jclient *) arg)->jack_process (nframes);
  82. }
  83. void Jclient::jack_shutdown (void)
  84. {
  85. send_event (EV_EXIT, 1);
  86. }
  87. int Jclient::jack_process (int frames)
  88. {
  89. int i, k, n;
  90. float a, b, t;
  91. float *inp [2];
  92. float *tmp [2];
  93. float *out [2];
  94. if (!_active) return 0;
  95. inp [0] = (float *) jack_port_get_buffer (_inpports [0], frames);
  96. inp [1] = (float *) jack_port_get_buffer (_inpports [1], frames);
  97. out [0] = tmp [0] = (float *) jack_port_get_buffer (_outports [0], frames);
  98. out [1] = tmp [1] = (float *) jack_port_get_buffer (_outports [1], frames);
  99. a = _ga;
  100. b = _gb;
  101. n = frames;
  102. while (n)
  103. {
  104. if (!_nsamp)
  105. {
  106. if (fabsf (_inpbal0 -_inpbal1) > 0.01f)
  107. {
  108. _inpbal1 = _inpbal0;
  109. t = powf (10.0f, 0.05f * _inpbal0);
  110. _db = (t - b) / _fragm;
  111. t = 1.0f / t;
  112. _da = (t - a) / _fragm;
  113. }
  114. else
  115. {
  116. _da = 0.0f;
  117. _db = 0.0f;
  118. }
  119. _hpfilt.prepare (_fragm);
  120. _lshelf.prepare (_fragm);
  121. _nsamp = _fragm;
  122. }
  123. k = (n < _nsamp) ? n: _nsamp;
  124. for (i = 0; i < k; i++)
  125. {
  126. a += _da;
  127. b += _db;
  128. tmp [0][i] = a * inp [0][i];
  129. tmp [1][i] = b * inp [1][i];
  130. }
  131. _hpfilt.process (k, 2, tmp);
  132. _lshelf.process (k, 2, tmp);
  133. inp [0] += k;
  134. inp [1] += k;
  135. tmp [0] += k;
  136. tmp [1] += k;
  137. _nsamp -= k;
  138. n -= k;
  139. }
  140. _ga = a;
  141. _gb = b;
  142. _shuffl.process (frames, out, out);
  143. return 0;
  144. }
  145. }