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.

268 lines
6.7KB

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