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.

291 lines
7.3KB

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