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.

CarlaEngineBridge.cpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Carla Engine Bridge
  3. * Copyright (C) 2013 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 GPL.txt file
  16. */
  17. #ifdef BUILD_BRIDGE
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "CarlaBridgeUtils.hpp"
  22. #include "CarlaShmUtils.hpp"
  23. #include "jackbridge/JackBridge.hpp"
  24. #include <ctime>
  25. CARLA_BACKEND_START_NAMESPACE
  26. #if 0
  27. } // Fix editor indentation
  28. #endif
  29. // -----------------------------------------
  30. class CarlaEngineBridge : public CarlaEngine,
  31. public QThread
  32. {
  33. public:
  34. CarlaEngineBridge(const char* const audioBaseName, const char* const controlBaseName)
  35. : CarlaEngine(),
  36. fIsRunning(false),
  37. fQuitNow(false)
  38. {
  39. carla_debug("CarlaEngineBridge::CarlaEngineBridge()");
  40. fShmAudioPool.filename = "/carla-bridge_shm_";
  41. fShmAudioPool.filename += audioBaseName;
  42. fShmControl.filename = "/carla-bridge_shc_";
  43. fShmControl.filename += controlBaseName;
  44. }
  45. ~CarlaEngineBridge()
  46. {
  47. carla_debug("CarlaEngineBridge::~CarlaEngineBridge()");
  48. }
  49. // -------------------------------------
  50. // CarlaEngine virtual calls
  51. bool init(const char* const clientName)
  52. {
  53. carla_debug("CarlaEngineBridge::init(\"%s\")", clientName);
  54. // SHM Audio Pool
  55. {
  56. #ifdef CARLA_OS_WIN
  57. // TESTING!
  58. fShmAudioPool.shm = carla_shm_attach_linux((const char*)fShmAudioPool.filename);
  59. #else
  60. fShmAudioPool.shm = carla_shm_attach((const char*)fShmAudioPool.filename);
  61. #endif
  62. if (! carla_is_shm_valid(fShmAudioPool.shm))
  63. {
  64. _cleanup();
  65. carla_stdout("Failed to open or create shared memory file #1");
  66. return false;
  67. }
  68. }
  69. // SHM Control
  70. {
  71. #ifdef CARLA_OS_WIN
  72. // TESTING!
  73. fShmControl.shm = carla_shm_attach_linux((const char*)fShmControl.filename);
  74. #else
  75. fShmControl.shm = carla_shm_attach((const char*)fShmControl.filename);
  76. #endif
  77. if (! carla_is_shm_valid(fShmControl.shm))
  78. {
  79. _cleanup();
  80. carla_stdout("Failed to open or create shared memory file #2");
  81. return false;
  82. }
  83. if (! carla_shm_map<BridgeShmControl>(fShmControl.shm, fShmControl.data))
  84. {
  85. _cleanup();
  86. carla_stdout("Failed to mmap shared memory file");
  87. return false;
  88. }
  89. }
  90. // Read values from memory
  91. CARLA_ASSERT(rdwr_readOpcode(&fShmControl.data->ringBuffer) == kPluginBridgeOpcodeBufferSize);
  92. fBufferSize = rdwr_readInt(&fShmControl.data->ringBuffer);
  93. carla_stderr("BufferSize: %i", fBufferSize);
  94. CARLA_ASSERT(rdwr_readOpcode(&fShmControl.data->ringBuffer) == kPluginBridgeOpcodeSampleRate);
  95. fSampleRate = rdwr_readFloat(&fShmControl.data->ringBuffer);
  96. carla_stderr("SampleRate: %f", fSampleRate);
  97. fQuitNow = false;
  98. fIsRunning = true;
  99. QThread::start();
  100. CarlaEngine::init(clientName);
  101. return true;
  102. }
  103. bool close()
  104. {
  105. carla_debug("CarlaEnginePlugin::close()");
  106. CarlaEngine::close();
  107. fQuitNow = true;
  108. QThread::wait();
  109. _cleanup();
  110. return true;
  111. }
  112. bool isRunning() const
  113. {
  114. return fIsRunning;
  115. }
  116. bool isOffline() const
  117. {
  118. return false;
  119. }
  120. EngineType type() const
  121. {
  122. return kEngineTypeBridge;
  123. }
  124. // -------------------------------------
  125. // CarlaThread virtual calls
  126. void run()
  127. {
  128. // TODO - set RT permissions
  129. while (! fQuitNow)
  130. {
  131. if (! jackbridge_sem_timedwait(&fShmControl.data->runServer, 5))
  132. {
  133. if (errno != ETIMEDOUT)
  134. {
  135. fQuitNow = true;
  136. break;
  137. }
  138. }
  139. while (rdwr_dataAvailable(&fShmControl.data->ringBuffer))
  140. {
  141. const PluginBridgeOpcode opcode(rdwr_readOpcode(&fShmControl.data->ringBuffer));
  142. switch (opcode)
  143. {
  144. case kPluginBridgeOpcodeNull:
  145. break;
  146. case kPluginBridgeOpcodeReadyWait:
  147. fShmAudioPool.data = (float*)carla_shm_map(fShmAudioPool.shm, rdwr_readInt(&fShmControl.data->ringBuffer));
  148. break;
  149. case kPluginBridgeOpcodeBufferSize:
  150. bufferSizeChanged(rdwr_readInt(&fShmControl.data->ringBuffer));
  151. break;
  152. case kPluginBridgeOpcodeSampleRate:
  153. sampleRateChanged(rdwr_readFloat(&fShmControl.data->ringBuffer));
  154. break;
  155. case kPluginBridgeOpcodeProcess:
  156. {
  157. CARLA_ASSERT(fShmAudioPool.data != nullptr);
  158. CarlaPlugin* const plugin(getPluginUnchecked(0));
  159. if (plugin != nullptr && plugin->enabled() && plugin->tryLock())
  160. {
  161. const uint32_t inCount = plugin->audioInCount();
  162. const uint32_t outCount = plugin->audioOutCount();
  163. float* inBuffer[inCount];
  164. float* outBuffer[outCount];
  165. for (uint32_t i=0; i < inCount; ++i)
  166. inBuffer[i] = fShmAudioPool.data + i*fBufferSize;
  167. for (uint32_t i=0; i < outCount; ++i)
  168. outBuffer[i] = fShmAudioPool.data + (i+inCount)*fBufferSize;
  169. plugin->initBuffers();
  170. plugin->setActive(true, false, false);
  171. plugin->process(inBuffer, outBuffer, fBufferSize);
  172. plugin->unlock();
  173. }
  174. break;
  175. }
  176. }
  177. }
  178. if (jackbridge_sem_post(&fShmControl.data->runClient) != 0)
  179. carla_stderr2("Could not post to semaphore");
  180. }
  181. fIsRunning = false;
  182. }
  183. private:
  184. struct BridgeAudioPool {
  185. CarlaString filename;
  186. float* data;
  187. shm_t shm;
  188. BridgeAudioPool()
  189. : data(nullptr)
  190. {
  191. carla_shm_init(shm);
  192. }
  193. } fShmAudioPool;
  194. struct BridgeControl {
  195. CarlaString filename;
  196. BridgeShmControl* data;
  197. shm_t shm;
  198. BridgeControl()
  199. : data(nullptr)
  200. {
  201. carla_shm_init(shm);
  202. }
  203. } fShmControl;
  204. bool fIsRunning;
  205. bool fQuitNow;
  206. void _cleanup()
  207. {
  208. if (fShmAudioPool.filename.isNotEmpty())
  209. fShmAudioPool.filename.clear();
  210. if (fShmControl.filename.isNotEmpty())
  211. fShmControl.filename.clear();
  212. fShmAudioPool.data = nullptr;
  213. fShmControl.data = nullptr;
  214. if (carla_is_shm_valid(fShmAudioPool.shm))
  215. carla_shm_close(fShmAudioPool.shm);
  216. if (carla_is_shm_valid(fShmControl.shm))
  217. carla_shm_close(fShmControl.shm);
  218. }
  219. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  220. };
  221. // -----------------------------------------
  222. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName)
  223. {
  224. return new CarlaEngineBridge(audioBaseName, controlBaseName);
  225. }
  226. CARLA_BACKEND_END_NAMESPACE
  227. #endif // BUILD_BRIDGE