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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2019 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. #ifndef CARLA_LIBJACK_HPP_INCLUDED
  18. #define CARLA_LIBJACK_HPP_INCLUDED
  19. // need to include this first
  20. #include "CarlaDefines.h"
  21. // now define as bridge
  22. #define BUILD_BRIDGE 1
  23. // now include a bunch of stuff
  24. #include "CarlaBackendUtils.hpp"
  25. #include "CarlaBridgeUtils.hpp"
  26. #include "CarlaMIDI.h"
  27. #include "CarlaMutex.hpp"
  28. #include "LinkedList.hpp"
  29. #if 0
  30. #include <jack/jack.h>
  31. #include <jack/midiport.h>
  32. #include <jack/transport.h>
  33. #include <jack/session.h>
  34. #include <jack/metadata.h>
  35. #endif
  36. #include <cerrno>
  37. #include <map>
  38. #include <string>
  39. #ifdef __SSE2_MATH__
  40. # include <xmmintrin.h>
  41. #endif
  42. // must be last include
  43. #include "api.hpp"
  44. // small check to not hurt myself
  45. #ifdef JACKBRIDGE_DIRECT
  46. # error "Cannot create custom jack server while linking to libjack directly"
  47. #endif
  48. CARLA_BACKEND_START_NAMESPACE
  49. // --------------------------------------------------------------------------------------------------------------------
  50. class CarlaJackAppClient;
  51. struct JackClientState;
  52. struct JackServerState;
  53. struct JackMidiPortBufferBase {
  54. static const uint8_t kMaxEventSize = 128;
  55. static const size_t kMaxEventCount = 512;
  56. static const size_t kBufferPoolSize = kMaxEventCount*8;
  57. bool isInput;
  58. bool isValid;
  59. };
  60. struct JackMidiPortBufferOnStack : JackMidiPortBufferBase {
  61. size_t bufferPoolPos;
  62. uint16_t count;
  63. jack_midi_event_t events[kMaxEventCount];
  64. jack_midi_data_t bufferPool[kBufferPoolSize];
  65. JackMidiPortBufferOnStack()
  66. : bufferPoolPos(0),
  67. count(0),
  68. events(),
  69. bufferPool()
  70. {
  71. isInput = true;
  72. isValid = true;
  73. }
  74. CARLA_DECLARE_NON_COPY_STRUCT(JackMidiPortBufferOnStack)
  75. };
  76. struct JackMidiPortBufferDummy : JackMidiPortBufferBase {
  77. JackMidiPortBufferDummy(const bool input)
  78. {
  79. isInput = input;
  80. isValid = false;
  81. }
  82. CARLA_DECLARE_NON_COPY_STRUCT(JackMidiPortBufferDummy)
  83. };
  84. struct JackPortState {
  85. const char* name;
  86. const char* fullname;
  87. void* buffer;
  88. uint index;
  89. int flags;
  90. jack_uuid_t uuid;
  91. bool isMidi : 1;
  92. bool isSystem : 1;
  93. bool isConnected : 1;
  94. bool unused : 1;
  95. JackPortState()
  96. : name(nullptr),
  97. fullname(nullptr),
  98. buffer(nullptr),
  99. index(0),
  100. flags(0),
  101. uuid(0),
  102. isMidi(false),
  103. isSystem(false),
  104. isConnected(false),
  105. unused(false) {}
  106. JackPortState(const char* const clientName, const char* const portName, const uint i, const int f,
  107. const bool midi, const bool sys, const bool con)
  108. : name(portName != nullptr ? strdup(portName) : nullptr),
  109. fullname(nullptr),
  110. buffer(nullptr),
  111. index(i),
  112. flags(f),
  113. uuid(0),
  114. isMidi(midi),
  115. isSystem(sys),
  116. isConnected(con),
  117. unused(false)
  118. {
  119. if (clientName != nullptr && portName != nullptr)
  120. {
  121. char strBuf[STR_MAX+1];
  122. snprintf(strBuf, STR_MAX, "%s:%s", clientName, portName);
  123. strBuf[STR_MAX] = '\0';
  124. fullname = strdup(strBuf);
  125. }
  126. }
  127. ~JackPortState()
  128. {
  129. }
  130. CARLA_DECLARE_NON_COPY_STRUCT(JackPortState)
  131. };
  132. struct JackClientState {
  133. const JackServerState& server;
  134. CarlaMutex mutex;
  135. bool activated;
  136. bool deactivated; // activated once, then deactivated
  137. char* name;
  138. LinkedList<JackPortState*> audioIns;
  139. LinkedList<JackPortState*> audioOuts;
  140. LinkedList<JackPortState*> midiIns;
  141. LinkedList<JackPortState*> midiOuts;
  142. std::map<std::string, JackPortState*> portNameMapping;
  143. JackShutdownCallback shutdownCb;
  144. void* shutdownCbPtr;
  145. JackInfoShutdownCallback infoShutdownCb;
  146. void* infoShutdownCbPtr;
  147. JackProcessCallback processCb;
  148. void* processCbPtr;
  149. JackFreewheelCallback freewheelCb;
  150. void* freewheelCbPtr;
  151. JackBufferSizeCallback bufferSizeCb;
  152. void* bufferSizeCbPtr;
  153. JackSampleRateCallback sampleRateCb;
  154. void* sampleRateCbPtr;
  155. JackSyncCallback syncCb;
  156. void* syncCbPtr;
  157. JackThreadInitCallback threadInitCb;
  158. void* threadInitCbPtr;
  159. JackClientState(const JackServerState& s, const char* const n)
  160. : server(s),
  161. mutex(),
  162. activated(false),
  163. deactivated(false),
  164. name(strdup(n)),
  165. audioIns(),
  166. audioOuts(),
  167. midiIns(),
  168. midiOuts(),
  169. portNameMapping(),
  170. shutdownCb(nullptr),
  171. shutdownCbPtr(nullptr),
  172. infoShutdownCb(nullptr),
  173. infoShutdownCbPtr(nullptr),
  174. processCb(nullptr),
  175. processCbPtr(nullptr),
  176. freewheelCb(nullptr),
  177. freewheelCbPtr(nullptr),
  178. bufferSizeCb(nullptr),
  179. bufferSizeCbPtr(nullptr),
  180. sampleRateCb(nullptr),
  181. sampleRateCbPtr(nullptr),
  182. syncCb(nullptr),
  183. syncCbPtr(nullptr),
  184. threadInitCb(nullptr),
  185. threadInitCbPtr(nullptr) {}
  186. ~JackClientState()
  187. {
  188. const CarlaMutexLocker cms(mutex);
  189. for (LinkedList<JackPortState*>::Itenerator it = audioIns.begin2(); it.valid(); it.next())
  190. {
  191. if (JackPortState* const jport = it.getValue(nullptr))
  192. delete jport;
  193. }
  194. for (LinkedList<JackPortState*>::Itenerator it = audioOuts.begin2(); it.valid(); it.next())
  195. {
  196. if (JackPortState* const jport = it.getValue(nullptr))
  197. delete jport;
  198. }
  199. for (LinkedList<JackPortState*>::Itenerator it = midiIns.begin2(); it.valid(); it.next())
  200. {
  201. if (JackPortState* const jport = it.getValue(nullptr))
  202. delete jport;
  203. }
  204. for (LinkedList<JackPortState*>::Itenerator it = midiOuts.begin2(); it.valid(); it.next())
  205. {
  206. if (JackPortState* const jport = it.getValue(nullptr))
  207. delete jport;
  208. }
  209. std::free(name);
  210. name = nullptr;
  211. audioIns.clear();
  212. audioOuts.clear();
  213. midiIns.clear();
  214. midiOuts.clear();
  215. }
  216. CARLA_DECLARE_NON_COPY_STRUCT(JackClientState)
  217. };
  218. struct JackServerState {
  219. CarlaJackAppClient* jackAppPtr;
  220. uint32_t bufferSize;
  221. double sampleRate;
  222. uint8_t numAudioIns;
  223. uint8_t numAudioOuts;
  224. uint8_t numMidiIns;
  225. uint8_t numMidiOuts;
  226. bool playing;
  227. jack_position_t position;
  228. jack_nframes_t monotonic_frame;
  229. JackServerState(CarlaJackAppClient* const app)
  230. : jackAppPtr(app),
  231. bufferSize(0),
  232. sampleRate(0.0),
  233. numAudioIns(0),
  234. numAudioOuts(0),
  235. numMidiIns(0),
  236. numMidiOuts(0),
  237. playing(false),
  238. position(),
  239. monotonic_frame(0)
  240. {
  241. carla_zeroStruct(position);
  242. }
  243. CARLA_DECLARE_NON_COPY_STRUCT(JackServerState)
  244. };
  245. CARLA_BACKEND_END_NAMESPACE
  246. // --------------------------------------------------------------------------------------------------------------------
  247. #endif // CARLA_LIBJACK_HPP_INCLUDED