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.6KB

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