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.

289 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. const char* name;
  81. const 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. }
  125. CARLA_DECLARE_NON_COPY_STRUCT(JackPortState)
  126. };
  127. struct JackClientState {
  128. const JackServerState& server;
  129. CarlaMutex mutex;
  130. bool activated;
  131. bool deactivated; // activated once, then deactivated
  132. char* name;
  133. LinkedList<JackPortState*> audioIns;
  134. LinkedList<JackPortState*> audioOuts;
  135. LinkedList<JackPortState*> midiIns;
  136. LinkedList<JackPortState*> midiOuts;
  137. std::map<std::string, JackPortState*> portNameMapping;
  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. portNameMapping(),
  163. shutdownCb(nullptr),
  164. shutdownCbPtr(nullptr),
  165. infoShutdownCb(nullptr),
  166. infoShutdownCbPtr(nullptr),
  167. processCb(nullptr),
  168. processCbPtr(nullptr),
  169. freewheelCb(nullptr),
  170. freewheelCbPtr(nullptr),
  171. bufferSizeCb(nullptr),
  172. bufferSizeCbPtr(nullptr),
  173. sampleRateCb(nullptr),
  174. sampleRateCbPtr(nullptr),
  175. syncCb(nullptr),
  176. syncCbPtr(nullptr) {}
  177. ~JackClientState()
  178. {
  179. const CarlaMutexLocker cms(mutex);
  180. for (LinkedList<JackPortState*>::Itenerator it = audioIns.begin2(); it.valid(); it.next())
  181. {
  182. if (JackPortState* const jport = it.getValue(nullptr))
  183. delete jport;
  184. }
  185. for (LinkedList<JackPortState*>::Itenerator it = audioOuts.begin2(); it.valid(); it.next())
  186. {
  187. if (JackPortState* const jport = it.getValue(nullptr))
  188. delete jport;
  189. }
  190. for (LinkedList<JackPortState*>::Itenerator it = midiIns.begin2(); it.valid(); it.next())
  191. {
  192. if (JackPortState* const jport = it.getValue(nullptr))
  193. delete jport;
  194. }
  195. for (LinkedList<JackPortState*>::Itenerator it = midiOuts.begin2(); it.valid(); it.next())
  196. {
  197. if (JackPortState* const jport = it.getValue(nullptr))
  198. delete jport;
  199. }
  200. std::free(name);
  201. name = nullptr;
  202. audioIns.clear();
  203. audioOuts.clear();
  204. midiIns.clear();
  205. midiOuts.clear();
  206. }
  207. CARLA_DECLARE_NON_COPY_STRUCT(JackClientState)
  208. };
  209. struct JackServerState {
  210. CarlaJackAppClient* jackAppPtr;
  211. uint32_t bufferSize;
  212. double sampleRate;
  213. uint8_t numAudioIns;
  214. uint8_t numAudioOuts;
  215. uint8_t numMidiIns;
  216. uint8_t numMidiOuts;
  217. bool playing;
  218. jack_position_t position;
  219. JackServerState(CarlaJackAppClient* const app)
  220. : jackAppPtr(app),
  221. bufferSize(0),
  222. sampleRate(0.0),
  223. numAudioIns(0),
  224. numAudioOuts(0),
  225. numMidiIns(0),
  226. numMidiOuts(0),
  227. playing(false),
  228. position()
  229. {
  230. carla_zeroStruct(position);
  231. }
  232. CARLA_DECLARE_NON_COPY_STRUCT(JackServerState)
  233. };
  234. CARLA_BACKEND_END_NAMESPACE
  235. // --------------------------------------------------------------------------------------------------------------------