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

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