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.

373 lines
11KB

  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 <cerrno>
  25. #include <ctime>
  26. CARLA_BACKEND_START_NAMESPACE
  27. #if 0
  28. } // Fix editor indentation
  29. #endif
  30. // -------------------------------------------------------------------
  31. class CarlaEngineBridge : public CarlaEngine,
  32. public QThread
  33. {
  34. public:
  35. CarlaEngineBridge(const char* const audioBaseName, const char* const controlBaseName)
  36. : CarlaEngine(),
  37. fIsRunning(false),
  38. fQuitNow(false)
  39. {
  40. carla_debug("CarlaEngineBridge::CarlaEngineBridge()");
  41. fShmAudioPool.filename = "/carla-bridge_shm_";
  42. fShmAudioPool.filename += audioBaseName;
  43. fShmControl.filename = "/carla-bridge_shc_";
  44. fShmControl.filename += controlBaseName;
  45. }
  46. ~CarlaEngineBridge()
  47. {
  48. carla_debug("CarlaEngineBridge::~CarlaEngineBridge()");
  49. }
  50. // -------------------------------------
  51. // CarlaEngine virtual calls
  52. bool init(const char* const clientName)
  53. {
  54. carla_debug("CarlaEngineBridge::init(\"%s\")", clientName);
  55. // SHM Audio Pool
  56. {
  57. #ifdef CARLA_OS_WIN
  58. // TESTING!
  59. fShmAudioPool.shm = carla_shm_attach_linux((const char*)fShmAudioPool.filename);
  60. #else
  61. fShmAudioPool.shm = carla_shm_attach((const char*)fShmAudioPool.filename);
  62. #endif
  63. if (! carla_is_shm_valid(fShmAudioPool.shm))
  64. {
  65. _cleanup();
  66. carla_stdout("Failed to open or create shared memory file #1");
  67. return false;
  68. }
  69. }
  70. // SHM Control
  71. {
  72. #ifdef CARLA_OS_WIN
  73. // TESTING!
  74. fShmControl.shm = carla_shm_attach_linux((const char*)fShmControl.filename);
  75. #else
  76. fShmControl.shm = carla_shm_attach((const char*)fShmControl.filename);
  77. #endif
  78. if (! carla_is_shm_valid(fShmControl.shm))
  79. {
  80. _cleanup();
  81. carla_stdout("Failed to open or create shared memory file #2");
  82. return false;
  83. }
  84. if (! carla_shm_map<BridgeShmControl>(fShmControl.shm, fShmControl.data))
  85. {
  86. _cleanup();
  87. carla_stdout("Failed to mmap shared memory file");
  88. return false;
  89. }
  90. }
  91. // Read values from memory
  92. PluginBridgeOpcode opcode;
  93. opcode = rdwr_readOpcode(&fShmControl.data->ringBuffer);
  94. CARLA_ASSERT(opcode == kPluginBridgeOpcodeSetBufferSize);
  95. fBufferSize = rdwr_readInt(&fShmControl.data->ringBuffer);
  96. carla_stderr("BufferSize: %i", fBufferSize);
  97. opcode = rdwr_readOpcode(&fShmControl.data->ringBuffer);
  98. CARLA_ASSERT(opcode == kPluginBridgeOpcodeSetSampleRate);
  99. fSampleRate = rdwr_readFloat(&fShmControl.data->ringBuffer);
  100. carla_stderr("SampleRate: %f", fSampleRate);
  101. fQuitNow = false;
  102. fIsRunning = true;
  103. QThread::start(QThread::TimeCriticalPriority);
  104. CarlaEngine::init(clientName);
  105. return true;
  106. }
  107. bool close()
  108. {
  109. carla_debug("CarlaEnginePlugin::close()");
  110. CarlaEngine::close();
  111. fQuitNow = true;
  112. QThread::wait();
  113. _cleanup();
  114. return true;
  115. }
  116. bool isRunning() const
  117. {
  118. return fIsRunning;
  119. }
  120. bool isOffline() const
  121. {
  122. return false;
  123. }
  124. EngineType type() const
  125. {
  126. return kEngineTypeBridge;
  127. }
  128. // -------------------------------------
  129. // CarlaThread virtual calls
  130. void run()
  131. {
  132. // TODO - set RT permissions
  133. while (! fQuitNow)
  134. {
  135. if (! jackbridge_sem_timedwait(&fShmControl.data->runServer, 5))
  136. {
  137. if (errno != ETIMEDOUT)
  138. continue;
  139. fQuitNow = true;
  140. return;
  141. }
  142. while (rdwr_dataAvailable(&fShmControl.data->ringBuffer))
  143. {
  144. const PluginBridgeOpcode opcode(rdwr_readOpcode(&fShmControl.data->ringBuffer));
  145. switch (opcode)
  146. {
  147. case kPluginBridgeOpcodeNull:
  148. break;
  149. case kPluginBridgeOpcodeSetAudioPool:
  150. {
  151. const int poolSize(rdwr_readInt(&fShmControl.data->ringBuffer));
  152. fShmAudioPool.data = (float*)carla_shm_map(fShmAudioPool.shm, poolSize);
  153. break;
  154. }
  155. case kPluginBridgeOpcodeSetBufferSize:
  156. {
  157. const int bufferSize(rdwr_readInt(&fShmControl.data->ringBuffer));
  158. bufferSizeChanged(bufferSize);
  159. break;
  160. }
  161. case kPluginBridgeOpcodeSetSampleRate:
  162. {
  163. const float sampleRate(rdwr_readFloat(&fShmControl.data->ringBuffer));
  164. sampleRateChanged(sampleRate);
  165. break;
  166. }
  167. case kPluginBridgeOpcodeSetParameter:
  168. {
  169. const int index(rdwr_readInt(&fShmControl.data->ringBuffer));
  170. const float value(rdwr_readFloat(&fShmControl.data->ringBuffer));
  171. CarlaPlugin* const plugin(getPluginUnchecked(0));
  172. if (plugin != nullptr && plugin->enabled())
  173. {
  174. plugin->setParameterValueByRealIndex(index, value, false, false, false);
  175. plugin->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, value);
  176. }
  177. break;
  178. }
  179. case kPluginBridgeOpcodeSetProgram:
  180. {
  181. const int index(rdwr_readInt(&fShmControl.data->ringBuffer));
  182. CarlaPlugin* const plugin(getPluginUnchecked(0));
  183. if (plugin != nullptr && plugin->enabled())
  184. {
  185. plugin->setProgram(index, false, false, false);
  186. plugin->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0.0f);
  187. }
  188. break;
  189. }
  190. case kPluginBridgeOpcodeSetMidiProgram:
  191. {
  192. const int index(rdwr_readInt(&fShmControl.data->ringBuffer));
  193. CarlaPlugin* const plugin(getPluginUnchecked(0));
  194. if (plugin != nullptr && plugin->enabled())
  195. {
  196. plugin->setMidiProgram(index, false, false, false);
  197. plugin->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  198. }
  199. break;
  200. }
  201. case kPluginBridgeOpcodeMidiEvent:
  202. {
  203. uint8_t data[4] = { 0 };
  204. const long time(rdwr_readLong(&fShmControl.data->ringBuffer));
  205. const int dataSize(rdwr_readInt(&fShmControl.data->ringBuffer));
  206. CARLA_ASSERT_INT(dataSize >= 1 && dataSize <= 4, dataSize);
  207. for (int i=0; i < dataSize && i < 4; ++i)
  208. data[i] = rdwr_readChar(&fShmControl.data->ringBuffer);
  209. CARLA_ASSERT(kData->bufEvents.in != nullptr);
  210. if (kData->bufEvents.in != nullptr)
  211. {
  212. // TODO
  213. }
  214. break;
  215. }
  216. case kPluginBridgeOpcodeProcess:
  217. {
  218. CARLA_ASSERT(fShmAudioPool.data != nullptr);
  219. CarlaPlugin* const plugin(getPluginUnchecked(0));
  220. if (plugin != nullptr && plugin->enabled() && plugin->tryLock())
  221. {
  222. const uint32_t inCount(plugin->audioInCount());
  223. const uint32_t outCount(plugin->audioOutCount());
  224. float* inBuffer[inCount];
  225. float* outBuffer[outCount];
  226. for (uint32_t i=0; i < inCount; ++i)
  227. inBuffer[i] = fShmAudioPool.data + i*fBufferSize;
  228. for (uint32_t i=0; i < outCount; ++i)
  229. outBuffer[i] = fShmAudioPool.data + (i+inCount)*fBufferSize;
  230. plugin->initBuffers();
  231. plugin->process(inBuffer, outBuffer, fBufferSize);
  232. plugin->unlock();
  233. }
  234. break;
  235. }
  236. case kPluginBridgeOpcodeQuit:
  237. fQuitNow = true;
  238. break;
  239. }
  240. }
  241. if (jackbridge_sem_post(&fShmControl.data->runClient) != 0)
  242. pass(); //carla_stderr2("Could not post to semaphore");
  243. }
  244. fIsRunning = false;
  245. }
  246. private:
  247. struct BridgeAudioPool {
  248. CarlaString filename;
  249. float* data;
  250. shm_t shm;
  251. BridgeAudioPool()
  252. : data(nullptr)
  253. {
  254. carla_shm_init(shm);
  255. }
  256. } fShmAudioPool;
  257. struct BridgeControl {
  258. CarlaString filename;
  259. BridgeShmControl* data;
  260. shm_t shm;
  261. BridgeControl()
  262. : data(nullptr)
  263. {
  264. carla_shm_init(shm);
  265. }
  266. } fShmControl;
  267. bool fIsRunning;
  268. bool fQuitNow;
  269. void _cleanup()
  270. {
  271. if (fShmAudioPool.filename.isNotEmpty())
  272. fShmAudioPool.filename.clear();
  273. if (fShmControl.filename.isNotEmpty())
  274. fShmControl.filename.clear();
  275. fShmAudioPool.data = nullptr;
  276. fShmControl.data = nullptr;
  277. if (carla_is_shm_valid(fShmAudioPool.shm))
  278. carla_shm_close(fShmAudioPool.shm);
  279. if (carla_is_shm_valid(fShmControl.shm))
  280. carla_shm_close(fShmControl.shm);
  281. }
  282. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  283. };
  284. // -----------------------------------------
  285. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName)
  286. {
  287. return new CarlaEngineBridge(audioBaseName, controlBaseName);
  288. }
  289. CARLA_BACKEND_END_NAMESPACE
  290. #endif // BUILD_BRIDGE