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.

280 lines
7.0KB

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