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.

372 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 <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. PluginBridgeOpcode opcode;
  92. opcode = rdwr_readOpcode(&fShmControl.data->ringBuffer);
  93. CARLA_ASSERT(opcode == kPluginBridgeOpcodeSetBufferSize);
  94. fBufferSize = rdwr_readInt(&fShmControl.data->ringBuffer);
  95. carla_stderr("BufferSize: %i", fBufferSize);
  96. opcode = rdwr_readOpcode(&fShmControl.data->ringBuffer);
  97. CARLA_ASSERT(opcode == kPluginBridgeOpcodeSetSampleRate);
  98. fSampleRate = rdwr_readFloat(&fShmControl.data->ringBuffer);
  99. carla_stderr("SampleRate: %f", fSampleRate);
  100. fQuitNow = false;
  101. fIsRunning = true;
  102. QThread::start(QThread::TimeCriticalPriority);
  103. CarlaEngine::init(clientName);
  104. return true;
  105. }
  106. bool close()
  107. {
  108. carla_debug("CarlaEnginePlugin::close()");
  109. CarlaEngine::close();
  110. fQuitNow = true;
  111. QThread::wait();
  112. _cleanup();
  113. return true;
  114. }
  115. bool isRunning() const
  116. {
  117. return fIsRunning;
  118. }
  119. bool isOffline() const
  120. {
  121. return false;
  122. }
  123. EngineType type() const
  124. {
  125. return kEngineTypeBridge;
  126. }
  127. // -------------------------------------
  128. // CarlaThread virtual calls
  129. void run()
  130. {
  131. // TODO - set RT permissions
  132. while (! fQuitNow)
  133. {
  134. if (! jackbridge_sem_timedwait(&fShmControl.data->runServer, 5))
  135. {
  136. if (errno != ETIMEDOUT)
  137. continue;
  138. fQuitNow = true;
  139. return;
  140. }
  141. while (rdwr_dataAvailable(&fShmControl.data->ringBuffer))
  142. {
  143. const PluginBridgeOpcode opcode(rdwr_readOpcode(&fShmControl.data->ringBuffer));
  144. switch (opcode)
  145. {
  146. case kPluginBridgeOpcodeNull:
  147. break;
  148. case kPluginBridgeOpcodeSetAudioPool:
  149. {
  150. const int poolSize(rdwr_readInt(&fShmControl.data->ringBuffer));
  151. fShmAudioPool.data = (float*)carla_shm_map(fShmAudioPool.shm, poolSize);
  152. break;
  153. }
  154. case kPluginBridgeOpcodeSetBufferSize:
  155. {
  156. const int bufferSize(rdwr_readInt(&fShmControl.data->ringBuffer));
  157. bufferSizeChanged(bufferSize);
  158. break;
  159. }
  160. case kPluginBridgeOpcodeSetSampleRate:
  161. {
  162. const float sampleRate(rdwr_readFloat(&fShmControl.data->ringBuffer));
  163. sampleRateChanged(sampleRate);
  164. break;
  165. }
  166. case kPluginBridgeOpcodeSetParameter:
  167. {
  168. const int index(rdwr_readInt(&fShmControl.data->ringBuffer));
  169. const float value(rdwr_readFloat(&fShmControl.data->ringBuffer));
  170. CarlaPlugin* const plugin(getPluginUnchecked(0));
  171. if (plugin != nullptr && plugin->enabled())
  172. {
  173. plugin->setParameterValueByRealIndex(index, value, false, false, false);
  174. plugin->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, value);
  175. }
  176. break;
  177. }
  178. case kPluginBridgeOpcodeSetProgram:
  179. {
  180. const int index(rdwr_readInt(&fShmControl.data->ringBuffer));
  181. CarlaPlugin* const plugin(getPluginUnchecked(0));
  182. if (plugin != nullptr && plugin->enabled())
  183. {
  184. plugin->setProgram(index, false, false, false);
  185. plugin->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0.0f);
  186. }
  187. break;
  188. }
  189. case kPluginBridgeOpcodeSetMidiProgram:
  190. {
  191. const int index(rdwr_readInt(&fShmControl.data->ringBuffer));
  192. CarlaPlugin* const plugin(getPluginUnchecked(0));
  193. if (plugin != nullptr && plugin->enabled())
  194. {
  195. plugin->setMidiProgram(index, false, false, false);
  196. plugin->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  197. }
  198. break;
  199. }
  200. case kPluginBridgeOpcodeMidiEvent:
  201. {
  202. uint8_t data[4] = { 0 };
  203. const long time(rdwr_readLong(&fShmControl.data->ringBuffer));
  204. const int dataSize(rdwr_readInt(&fShmControl.data->ringBuffer));
  205. CARLA_ASSERT_INT(dataSize >= 1 && dataSize <= 4, dataSize);
  206. for (int i=0; i < dataSize && i < 4; ++i)
  207. data[i] = rdwr_readChar(&fShmControl.data->ringBuffer);
  208. CARLA_ASSERT(kData->bufEvent.in != nullptr);
  209. if (kData->bufEvent.in != nullptr)
  210. {
  211. // TODO
  212. }
  213. break;
  214. }
  215. case kPluginBridgeOpcodeProcess:
  216. {
  217. CARLA_ASSERT(fShmAudioPool.data != nullptr);
  218. CarlaPlugin* const plugin(getPluginUnchecked(0));
  219. if (plugin != nullptr && plugin->enabled() && plugin->tryLock())
  220. {
  221. const uint32_t inCount(plugin->audioInCount());
  222. const uint32_t outCount(plugin->audioOutCount());
  223. float* inBuffer[inCount];
  224. float* outBuffer[outCount];
  225. for (uint32_t i=0; i < inCount; ++i)
  226. inBuffer[i] = fShmAudioPool.data + i*fBufferSize;
  227. for (uint32_t i=0; i < outCount; ++i)
  228. outBuffer[i] = fShmAudioPool.data + (i+inCount)*fBufferSize;
  229. plugin->initBuffers();
  230. plugin->process(inBuffer, outBuffer, fBufferSize);
  231. plugin->unlock();
  232. }
  233. break;
  234. }
  235. case kPluginBridgeOpcodeQuit:
  236. fQuitNow = true;
  237. break;
  238. }
  239. }
  240. if (jackbridge_sem_post(&fShmControl.data->runClient) != 0)
  241. pass(); //carla_stderr2("Could not post to semaphore");
  242. }
  243. fIsRunning = false;
  244. }
  245. private:
  246. struct BridgeAudioPool {
  247. CarlaString filename;
  248. float* data;
  249. shm_t shm;
  250. BridgeAudioPool()
  251. : data(nullptr)
  252. {
  253. carla_shm_init(shm);
  254. }
  255. } fShmAudioPool;
  256. struct BridgeControl {
  257. CarlaString filename;
  258. BridgeShmControl* data;
  259. shm_t shm;
  260. BridgeControl()
  261. : data(nullptr)
  262. {
  263. carla_shm_init(shm);
  264. }
  265. } fShmControl;
  266. bool fIsRunning;
  267. bool fQuitNow;
  268. void _cleanup()
  269. {
  270. if (fShmAudioPool.filename.isNotEmpty())
  271. fShmAudioPool.filename.clear();
  272. if (fShmControl.filename.isNotEmpty())
  273. fShmControl.filename.clear();
  274. fShmAudioPool.data = nullptr;
  275. fShmControl.data = nullptr;
  276. if (carla_is_shm_valid(fShmAudioPool.shm))
  277. carla_shm_close(fShmAudioPool.shm);
  278. if (carla_is_shm_valid(fShmControl.shm))
  279. carla_shm_close(fShmControl.shm);
  280. }
  281. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  282. };
  283. // -----------------------------------------
  284. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName)
  285. {
  286. return new CarlaEngineBridge(audioBaseName, controlBaseName);
  287. }
  288. CARLA_BACKEND_END_NAMESPACE
  289. #endif // BUILD_BRIDGE