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.

287 lines
7.2KB

  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. jack_uuid_t uuid;
  85. bool isMidi : 1;
  86. bool isSystem : 1;
  87. bool isConnected : 1;
  88. bool unused : 1;
  89. JackPortState()
  90. : name(nullptr),
  91. fullname(nullptr),
  92. buffer(nullptr),
  93. index(0),
  94. flags(0),
  95. uuid(0),
  96. isMidi(false),
  97. isSystem(false),
  98. isConnected(false),
  99. unused(false) {}
  100. JackPortState(const char* const clientName, const char* const portName, const uint i, const uint f,
  101. const bool midi, const bool sys, const bool con)
  102. : name(portName != nullptr ? strdup(portName) : nullptr),
  103. fullname(nullptr),
  104. buffer(nullptr),
  105. index(i),
  106. flags(f),
  107. uuid(0),
  108. isMidi(midi),
  109. isSystem(sys),
  110. isConnected(con),
  111. unused(false)
  112. {
  113. if (clientName != nullptr && portName != nullptr)
  114. {
  115. char strBuf[STR_MAX+1];
  116. snprintf(strBuf, STR_MAX, "%s:%s", clientName, portName);
  117. strBuf[STR_MAX] = '\0';
  118. fullname = strdup(strBuf);
  119. }
  120. }
  121. ~JackPortState()
  122. {
  123. std::free(name);
  124. std::free(fullname);
  125. }
  126. CARLA_DECLARE_NON_COPY_STRUCT(JackPortState)
  127. };
  128. struct JackClientState {
  129. const JackServerState& server;
  130. CarlaMutex mutex;
  131. bool activated;
  132. bool deactivated; // activated once, then deactivated
  133. char* name;
  134. LinkedList<JackPortState*> audioIns;
  135. LinkedList<JackPortState*> audioOuts;
  136. LinkedList<JackPortState*> midiIns;
  137. LinkedList<JackPortState*> midiOuts;
  138. JackShutdownCallback shutdownCb;
  139. void* shutdownCbPtr;
  140. JackInfoShutdownCallback infoShutdownCb;
  141. void* infoShutdownCbPtr;
  142. JackProcessCallback processCb;
  143. void* processCbPtr;
  144. JackFreewheelCallback freewheelCb;
  145. void* freewheelCbPtr;
  146. JackBufferSizeCallback bufferSizeCb;
  147. void* bufferSizeCbPtr;
  148. JackSampleRateCallback sampleRateCb;
  149. void* sampleRateCbPtr;
  150. JackSyncCallback syncCb;
  151. void* syncCbPtr;
  152. JackClientState(const JackServerState& s, const char* const n)
  153. : server(s),
  154. mutex(),
  155. activated(false),
  156. deactivated(false),
  157. name(strdup(n)),
  158. audioIns(),
  159. audioOuts(),
  160. midiIns(),
  161. midiOuts(),
  162. shutdownCb(nullptr),
  163. shutdownCbPtr(nullptr),
  164. infoShutdownCb(nullptr),
  165. infoShutdownCbPtr(nullptr),
  166. processCb(nullptr),
  167. processCbPtr(nullptr),
  168. freewheelCb(nullptr),
  169. freewheelCbPtr(nullptr),
  170. bufferSizeCb(nullptr),
  171. bufferSizeCbPtr(nullptr),
  172. sampleRateCb(nullptr),
  173. sampleRateCbPtr(nullptr),
  174. syncCb(nullptr),
  175. syncCbPtr(nullptr) {}
  176. ~JackClientState()
  177. {
  178. const CarlaMutexLocker cms(mutex);
  179. for (LinkedList<JackPortState*>::Itenerator it = audioIns.begin2(); it.valid(); it.next())
  180. {
  181. if (JackPortState* const jport = it.getValue(nullptr))
  182. delete jport;
  183. }
  184. for (LinkedList<JackPortState*>::Itenerator it = audioOuts.begin2(); it.valid(); it.next())
  185. {
  186. if (JackPortState* const jport = it.getValue(nullptr))
  187. delete jport;
  188. }
  189. for (LinkedList<JackPortState*>::Itenerator it = midiIns.begin2(); it.valid(); it.next())
  190. {
  191. if (JackPortState* const jport = it.getValue(nullptr))
  192. delete jport;
  193. }
  194. for (LinkedList<JackPortState*>::Itenerator it = midiOuts.begin2(); it.valid(); it.next())
  195. {
  196. if (JackPortState* const jport = it.getValue(nullptr))
  197. delete jport;
  198. }
  199. std::free(name);
  200. name = nullptr;
  201. audioIns.clear();
  202. audioOuts.clear();
  203. midiIns.clear();
  204. midiOuts.clear();
  205. }
  206. CARLA_DECLARE_NON_COPY_STRUCT(JackClientState)
  207. };
  208. struct JackServerState {
  209. CarlaJackAppClient* jackAppPtr;
  210. uint32_t bufferSize;
  211. double sampleRate;
  212. uint8_t numAudioIns;
  213. uint8_t numAudioOuts;
  214. uint8_t numMidiIns;
  215. uint8_t numMidiOuts;
  216. bool playing;
  217. jack_position_t position;
  218. JackServerState(CarlaJackAppClient* const app)
  219. : jackAppPtr(app),
  220. bufferSize(0),
  221. sampleRate(0.0),
  222. numAudioIns(0),
  223. numAudioOuts(0),
  224. numMidiIns(0),
  225. numMidiOuts(0),
  226. playing(false),
  227. position()
  228. {
  229. carla_zeroStruct(position);
  230. }
  231. CARLA_DECLARE_NON_COPY_STRUCT(JackServerState)
  232. };
  233. CARLA_BACKEND_END_NAMESPACE
  234. // --------------------------------------------------------------------------------------------------------------------