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.

277 lines
6.9KB

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