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.

163 lines
3.8KB

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