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.

196 lines
4.5KB

  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. struct JackPortState {
  48. char* name;
  49. char* fullname;
  50. void* buffer;
  51. uint index;
  52. uint flags;
  53. bool isSystem;
  54. JackPortState()
  55. : name(nullptr),
  56. fullname(nullptr),
  57. buffer(nullptr),
  58. index(0),
  59. flags(0),
  60. isSystem(false) {}
  61. JackPortState(const char* const cn, const char* const pn, const uint i, const uint f, const bool sys)
  62. : name(strdup(pn)),
  63. fullname(nullptr),
  64. buffer(nullptr),
  65. index(i),
  66. flags(f),
  67. isSystem(sys)
  68. {
  69. char strBuf[STR_MAX+1];
  70. snprintf(strBuf, STR_MAX, "%s:%s", cn, pn);
  71. strBuf[STR_MAX] = '\0';
  72. fullname = strdup(strBuf);
  73. }
  74. ~JackPortState()
  75. {
  76. free(name);
  77. free(fullname);
  78. }
  79. };
  80. struct JackClientState {
  81. bool activated;
  82. bool prematurelyActivated;
  83. char* name;
  84. uint32_t bufferSize;
  85. double sampleRate;
  86. bool playing;
  87. jack_position_t position;
  88. LinkedList<JackPortState*> audioIns;
  89. LinkedList<JackPortState*> audioOuts;
  90. uint32_t fakeIns, fakeOuts;
  91. LinkedList<JackPortState> midiIns;
  92. LinkedList<JackPortState> midiOuts;
  93. JackProcessCallback process;
  94. void* processPtr;
  95. JackShutdownCallback shutdown;
  96. void* shutdownPtr;
  97. JackClientState()
  98. : activated(false),
  99. prematurelyActivated(false),
  100. name(nullptr),
  101. bufferSize(0),
  102. sampleRate(0.0),
  103. playing(false),
  104. audioIns(),
  105. audioOuts(),
  106. fakeIns(0),
  107. fakeOuts(0),
  108. midiIns(),
  109. midiOuts(),
  110. process(nullptr),
  111. processPtr(nullptr),
  112. shutdown(nullptr),
  113. shutdownPtr(nullptr)
  114. {
  115. carla_zeroStruct(position);
  116. }
  117. ~JackClientState()
  118. {
  119. free(name);
  120. }
  121. };
  122. class CarlaJackClient : public juce::Thread
  123. {
  124. public:
  125. JackClientState fState;
  126. CarlaJackClient();
  127. ~CarlaJackClient() noexcept override;
  128. bool initIfNeeded(const char* const clientName);
  129. void clear() noexcept;
  130. bool isValid() const noexcept;
  131. void activate();
  132. void deactivate();
  133. void handleNonRtData();
  134. // -------------------------------------------------------------------
  135. protected:
  136. void run() override;
  137. private:
  138. BridgeAudioPool fShmAudioPool;
  139. BridgeRtClientControl fShmRtClientControl;
  140. BridgeNonRtClientControl fShmNonRtClientControl;
  141. BridgeNonRtServerControl fShmNonRtServerControl;
  142. char fBaseNameAudioPool[6+1];
  143. char fBaseNameRtClientControl[6+1];
  144. char fBaseNameNonRtClientControl[6+1];
  145. char fBaseNameNonRtServerControl[6+1];
  146. bool fIsValid;
  147. bool fIsOffline;
  148. bool fFirstIdle;
  149. int64_t fLastPingTime;
  150. uint32_t fAudioIns;
  151. uint32_t fAudioOuts;
  152. CarlaMutex fRealtimeThreadMutex;
  153. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaJackClient)
  154. };
  155. CARLA_BACKEND_END_NAMESPACE
  156. // --------------------------------------------------------------------------------------------------------------------