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.

246 lines
6.1KB

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