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.

158 lines
3.7KB

  1. // -----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2010 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 <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "jclient.h"
  25. #include "global.h"
  26. namespace AT1 {
  27. Jclient::Jclient (jack_client_t *jclient) :
  28. A_thread ("jclient"),
  29. _jack_client (jclient),
  30. _active (false)
  31. {
  32. init_jack ();
  33. }
  34. Jclient::~Jclient (void)
  35. {
  36. if (_jack_client) close_jack ();
  37. }
  38. void Jclient::init_jack (void)
  39. {
  40. jack_on_shutdown (_jack_client, jack_static_shutdown, (void *) this);
  41. jack_set_process_callback (_jack_client, jack_static_process, (void *) this);
  42. jack_activate (_jack_client);
  43. _fsamp = jack_get_sample_rate (_jack_client);
  44. _fsize = jack_get_buffer_size (_jack_client);
  45. _ainp_port = jack_port_register (_jack_client, "in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  46. _aout_port = jack_port_register (_jack_client, "out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  47. _midi_port = jack_port_register (_jack_client, "pitch", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  48. _retuner = new Retuner (_fsamp);
  49. _notemask = 0xFFF;
  50. _midichan = -1;
  51. clr_midimask ();
  52. _active = true;
  53. }
  54. void Jclient::close_jack ()
  55. {
  56. jack_deactivate (_jack_client);
  57. jack_client_close (_jack_client);
  58. delete _retuner;
  59. }
  60. void Jclient::jack_static_shutdown (void *arg)
  61. {
  62. ((Jclient *) arg)->jack_shutdown ();
  63. }
  64. int Jclient::jack_static_process (jack_nframes_t nframes, void *arg)
  65. {
  66. return ((Jclient *) arg)->jack_process (nframes);
  67. }
  68. void Jclient::jack_shutdown (void)
  69. {
  70. send_event (EV_EXIT, 1);
  71. }
  72. void Jclient::clr_midimask (void)
  73. {
  74. int i;
  75. for (i = 0; i < 12; i++) _notes [i] = 0;
  76. _midimask = 0;
  77. }
  78. void Jclient::midi_process (int nframes)
  79. {
  80. int i, b, n, t, v;
  81. void *p;
  82. jack_midi_event_t E;
  83. p = jack_port_get_buffer (_midi_port, nframes);
  84. i = 0;
  85. while (jack_midi_event_get (&E, p, i) == 0 && E.size == 3)
  86. {
  87. t = E.buffer [0];
  88. n = E.buffer [1];
  89. v = E.buffer [2];
  90. if ((_midichan < 0) || ((t & 0x0F) == _midichan))
  91. {
  92. const int n12 = n % 12;
  93. switch (t & 0xF0)
  94. {
  95. case 0x80:
  96. case 0x90:
  97. if (v && (t & 0x10))_notes [n12] += 1;
  98. else if (_notes [n12] > 0) _notes [n12] -= 1;
  99. break;
  100. }
  101. }
  102. i++;
  103. }
  104. _midimask = 0;
  105. for (i = 0, b = 1; i < 12; i++, b <<= 1)
  106. {
  107. if (_notes [i]) _midimask |= b;
  108. }
  109. }
  110. int Jclient::jack_process (int nframes)
  111. {
  112. float *inpp;
  113. float *outp;
  114. if (!_active) return 0;
  115. inpp = (float *) jack_port_get_buffer (_ainp_port, nframes);
  116. outp = (float *) jack_port_get_buffer (_aout_port, nframes);
  117. midi_process (nframes);
  118. _retuner->set_notemask (_midimask ? _midimask : _notemask);
  119. _retuner->process (nframes, inpp, outp);
  120. return 0;
  121. }
  122. }