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.

libjack.hpp 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2017 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. // need to include this first
  18. #include "CarlaDefines.h"
  19. // now define as bridge
  20. #define BUILD_BRIDGE 1
  21. // now include a bunch of stuff
  22. #include "CarlaBackendUtils.hpp"
  23. #include "CarlaBridgeUtils.hpp"
  24. #include "CarlaMIDI.h"
  25. #include "CarlaMutex.hpp"
  26. #include "LinkedList.hpp"
  27. #if 0
  28. #include <jack/jack.h>
  29. #include <jack/midiport.h>
  30. #include <jack/transport.h>
  31. #include <jack/session.h>
  32. #include <jack/metadata.h>
  33. #endif
  34. #include <cerrno>
  35. #ifdef __SSE2_MATH__
  36. # include <xmmintrin.h>
  37. #endif
  38. // must be last include
  39. #include "jackbridge/JackBridge.hpp"
  40. // small check to not hurt myself
  41. #ifdef JACKBRIDGE_DIRECT
  42. # error "Cannot create custom jack server while linking to libjack directly"
  43. #endif
  44. CARLA_BACKEND_START_NAMESPACE
  45. // --------------------------------------------------------------------------------------------------------------------
  46. class CarlaJackAppClient;
  47. struct JackClientState;
  48. struct JackServerState;
  49. struct JackMidiPortBuffer {
  50. static const uint8_t kMaxEventSize = 128;
  51. static const size_t kMaxEventCount = 512;
  52. static const size_t kBufferPoolSize = kMaxEventCount*8;
  53. uint16_t count;
  54. bool isInput;
  55. jack_midi_event_t* events;
  56. size_t bufferPoolPos;
  57. jack_midi_data_t* bufferPool;
  58. JackMidiPortBuffer()
  59. : count(0),
  60. isInput(true),
  61. events(new jack_midi_event_t[kMaxEventCount]),
  62. bufferPoolPos(0),
  63. bufferPool(new jack_midi_data_t[kBufferPoolSize]) {}
  64. // for unused ports
  65. JackMidiPortBuffer(const bool input, const char*)
  66. : count(0),
  67. isInput(input),
  68. events(nullptr),
  69. bufferPoolPos(kBufferPoolSize),
  70. bufferPool(nullptr) {}
  71. ~JackMidiPortBuffer()
  72. {
  73. delete[] events;
  74. delete[] bufferPool;
  75. }
  76. CARLA_DECLARE_NON_COPY_STRUCT(JackMidiPortBuffer)
  77. };
  78. struct JackPortState {
  79. char* name;
  80. char* fullname;
  81. void* buffer;
  82. uint index;
  83. uint flags;
  84. bool isMidi : 1;
  85. bool isSystem : 1;
  86. bool isConnected : 1;
  87. bool unused : 1;
  88. JackPortState()
  89. : name(nullptr),
  90. fullname(nullptr),
  91. buffer(nullptr),
  92. index(0),
  93. flags(0),
  94. isMidi(false),
  95. isSystem(false),
  96. isConnected(false),
  97. unused(false) {}
  98. JackPortState(const char* const cn, const char* const pn, const uint i, const uint f,
  99. const bool midi, const bool sys, const bool con)
  100. : name(strdup(pn)),
  101. fullname(nullptr),
  102. buffer(nullptr),
  103. index(i),
  104. flags(f),
  105. isMidi(midi),
  106. isSystem(sys),
  107. isConnected(con),
  108. unused(false)
  109. {
  110. char strBuf[STR_MAX+1];
  111. snprintf(strBuf, STR_MAX, "%s:%s", cn, pn);
  112. strBuf[STR_MAX] = '\0';
  113. fullname = strdup(strBuf);
  114. }
  115. ~JackPortState()
  116. {
  117. free(name);
  118. free(fullname);
  119. }
  120. CARLA_DECLARE_NON_COPY_STRUCT(JackPortState)
  121. };
  122. struct JackClientState {
  123. const JackServerState& server;
  124. CarlaMutex mutex;
  125. bool activated;
  126. bool deactivated; // activated once, then deactivated
  127. char* name;
  128. LinkedList<JackPortState*> audioIns;
  129. LinkedList<JackPortState*> audioOuts;
  130. LinkedList<JackPortState*> midiIns;
  131. LinkedList<JackPortState*> midiOuts;
  132. JackShutdownCallback shutdownCb;
  133. void* shutdownCbPtr;
  134. JackInfoShutdownCallback infoShutdownCb;
  135. void* infoShutdownCbPtr;
  136. JackProcessCallback processCb;
  137. void* processCbPtr;
  138. JackFreewheelCallback freewheelCb;
  139. void* freewheelCbPtr;
  140. JackBufferSizeCallback bufferSizeCb;
  141. void* bufferSizeCbPtr;
  142. JackSampleRateCallback sampleRateCb;
  143. void* sampleRateCbPtr;
  144. JackSyncCallback syncCb;
  145. void* syncCbPtr;
  146. JackClientState(const JackServerState& s, const char* const n)
  147. : server(s),
  148. mutex(),
  149. activated(false),
  150. deactivated(false),
  151. name(strdup(n)),
  152. audioIns(),
  153. audioOuts(),
  154. midiIns(),
  155. midiOuts(),
  156. shutdownCb(nullptr),
  157. shutdownCbPtr(nullptr),
  158. infoShutdownCb(nullptr),
  159. infoShutdownCbPtr(nullptr),
  160. processCb(nullptr),
  161. processCbPtr(nullptr),
  162. freewheelCb(nullptr),
  163. freewheelCbPtr(nullptr),
  164. bufferSizeCb(nullptr),
  165. bufferSizeCbPtr(nullptr),
  166. sampleRateCb(nullptr),
  167. sampleRateCbPtr(nullptr),
  168. syncCb(nullptr),
  169. syncCbPtr(nullptr) {}
  170. ~JackClientState()
  171. {
  172. const CarlaMutexLocker cms(mutex);
  173. for (LinkedList<JackPortState*>::Itenerator it = audioIns.begin2(); it.valid(); it.next())
  174. {
  175. if (JackPortState* const jport = it.getValue(nullptr))
  176. delete jport;
  177. }
  178. for (LinkedList<JackPortState*>::Itenerator it = audioOuts.begin2(); it.valid(); it.next())
  179. {
  180. if (JackPortState* const jport = it.getValue(nullptr))
  181. delete jport;
  182. }
  183. for (LinkedList<JackPortState*>::Itenerator it = midiIns.begin2(); it.valid(); it.next())
  184. {
  185. if (JackPortState* const jport = it.getValue(nullptr))
  186. delete jport;
  187. }
  188. for (LinkedList<JackPortState*>::Itenerator it = midiOuts.begin2(); it.valid(); it.next())
  189. {
  190. if (JackPortState* const jport = it.getValue(nullptr))
  191. delete jport;
  192. }
  193. free(name);
  194. name = nullptr;
  195. audioIns.clear();
  196. audioOuts.clear();
  197. }
  198. CARLA_DECLARE_NON_COPY_STRUCT(JackClientState)
  199. };
  200. struct JackServerState {
  201. CarlaJackAppClient* jackAppPtr;
  202. uint32_t bufferSize;
  203. double sampleRate;
  204. uint8_t numAudioIns;
  205. uint8_t numAudioOuts;
  206. uint8_t numMidiIns;
  207. uint8_t numMidiOuts;
  208. bool playing;
  209. jack_position_t position;
  210. JackServerState(CarlaJackAppClient* const app)
  211. : jackAppPtr(app),
  212. bufferSize(0),
  213. sampleRate(0.0),
  214. numAudioIns(0),
  215. numAudioOuts(0),
  216. numMidiIns(0),
  217. numMidiOuts(0),
  218. playing(false),
  219. position()
  220. {
  221. carla_zeroStruct(position);
  222. }
  223. CARLA_DECLARE_NON_COPY_STRUCT(JackServerState)
  224. };
  225. CARLA_BACKEND_END_NAMESPACE
  226. // --------------------------------------------------------------------------------------------------------------------