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.

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