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.

1570 lines
58KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2015 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. #ifndef BUILD_BRIDGE
  18. # error This file should not be compiled if not building bridge
  19. #endif
  20. #include "CarlaEngineInternal.hpp"
  21. #include "CarlaPlugin.hpp"
  22. #include "CarlaBackendUtils.hpp"
  23. #include "CarlaBase64Utils.hpp"
  24. #include "CarlaBridgeUtils.hpp"
  25. #include "CarlaMIDI.h"
  26. #include "jackbridge/JackBridge.hpp"
  27. using juce::File;
  28. using juce::MemoryBlock;
  29. using juce::String;
  30. using juce::Time;
  31. using juce::Thread;
  32. template<typename T>
  33. bool jackbridge_shm_map2(void* shm, T*& value) noexcept
  34. {
  35. value = (T*)jackbridge_shm_map(shm, sizeof(T));
  36. return (value != nullptr);
  37. }
  38. CARLA_BACKEND_START_NAMESPACE
  39. // -------------------------------------------------------------------
  40. struct BridgeAudioPool {
  41. CarlaString filename;
  42. float* data;
  43. char shm[64];
  44. BridgeAudioPool() noexcept
  45. : filename(),
  46. data(nullptr)
  47. {
  48. carla_zeroChars(shm, 64);
  49. jackbridge_shm_init(shm);
  50. }
  51. ~BridgeAudioPool() noexcept
  52. {
  53. // should be cleared by now
  54. CARLA_SAFE_ASSERT(data == nullptr);
  55. clear();
  56. }
  57. void clear() noexcept
  58. {
  59. filename.clear();
  60. if (! jackbridge_shm_is_valid(shm))
  61. {
  62. CARLA_SAFE_ASSERT(data == nullptr);
  63. return;
  64. }
  65. data = nullptr;
  66. jackbridge_shm_close(shm);
  67. jackbridge_shm_init(shm);
  68. }
  69. bool attach() noexcept
  70. {
  71. // must be invalid right now
  72. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  73. jackbridge_shm_attach(shm, filename);
  74. return jackbridge_shm_is_valid(shm);
  75. }
  76. CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
  77. };
  78. // -------------------------------------------------------------------
  79. struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {
  80. CarlaString filename;
  81. BridgeRtClientData* data;
  82. char shm[64];
  83. BridgeRtClientControl() noexcept
  84. : filename(),
  85. data(nullptr)
  86. {
  87. carla_zeroChars(shm, 64);
  88. jackbridge_shm_init(shm);
  89. }
  90. ~BridgeRtClientControl() noexcept override
  91. {
  92. // should be cleared by now
  93. CARLA_SAFE_ASSERT(data == nullptr);
  94. clear();
  95. }
  96. void clear() noexcept
  97. {
  98. filename.clear();
  99. if (data != nullptr)
  100. unmapData();
  101. if (! jackbridge_shm_is_valid(shm))
  102. return;
  103. jackbridge_shm_close(shm);
  104. jackbridge_shm_init(shm);
  105. }
  106. bool attach() noexcept
  107. {
  108. // must be invalid right now
  109. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  110. jackbridge_shm_attach(shm, filename);
  111. return jackbridge_shm_is_valid(shm);
  112. }
  113. bool mapData() noexcept
  114. {
  115. CARLA_SAFE_ASSERT(data == nullptr);
  116. if (jackbridge_shm_map2<BridgeRtClientData>(shm, data))
  117. {
  118. CARLA_SAFE_ASSERT(data->midiOut[0] == 0);
  119. setRingBuffer(&data->ringBuffer, false);
  120. return true;
  121. }
  122. return false;
  123. }
  124. void unmapData() noexcept
  125. {
  126. data = nullptr;
  127. setRingBuffer(nullptr, false);
  128. }
  129. PluginBridgeRtClientOpcode readOpcode() noexcept
  130. {
  131. return static_cast<PluginBridgeRtClientOpcode>(readUInt());
  132. }
  133. // helper class that automatically posts semaphore on destructor
  134. struct WaitHelper {
  135. BridgeRtClientData* const data;
  136. const bool ok;
  137. WaitHelper(BridgeRtClientControl& c) noexcept
  138. : data(c.data),
  139. ok(jackbridge_sem_timedwait(&data->sem.server, 5000)) {}
  140. ~WaitHelper() noexcept
  141. {
  142. if (ok)
  143. jackbridge_sem_post(&data->sem.client);
  144. }
  145. CARLA_DECLARE_NON_COPY_STRUCT(WaitHelper)
  146. };
  147. CARLA_DECLARE_NON_COPY_STRUCT(BridgeRtClientControl)
  148. };
  149. // -------------------------------------------------------------------
  150. struct BridgeNonRtClientControl : public CarlaRingBufferControl<BigStackBuffer> {
  151. CarlaString filename;
  152. BridgeNonRtClientData* data;
  153. char shm[64];
  154. BridgeNonRtClientControl() noexcept
  155. : filename(),
  156. data(nullptr)
  157. {
  158. carla_zeroChars(shm, 64);
  159. jackbridge_shm_init(shm);
  160. }
  161. ~BridgeNonRtClientControl() noexcept override
  162. {
  163. // should be cleared by now
  164. CARLA_SAFE_ASSERT(data == nullptr);
  165. clear();
  166. }
  167. void clear() noexcept
  168. {
  169. filename.clear();
  170. if (data != nullptr)
  171. unmapData();
  172. if (! jackbridge_shm_is_valid(shm))
  173. {
  174. CARLA_SAFE_ASSERT(data == nullptr);
  175. return;
  176. }
  177. jackbridge_shm_close(shm);
  178. jackbridge_shm_init(shm);
  179. }
  180. bool attach() noexcept
  181. {
  182. // must be invalid right now
  183. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  184. jackbridge_shm_attach(shm, filename);
  185. return jackbridge_shm_is_valid(shm);
  186. }
  187. bool mapData() noexcept
  188. {
  189. CARLA_SAFE_ASSERT(data == nullptr);
  190. if (jackbridge_shm_map2<BridgeNonRtClientData>(shm, data))
  191. {
  192. setRingBuffer(&data->ringBuffer, false);
  193. return true;
  194. }
  195. return false;
  196. }
  197. void unmapData() noexcept
  198. {
  199. data = nullptr;
  200. setRingBuffer(nullptr, false);
  201. }
  202. PluginBridgeNonRtClientOpcode readOpcode() noexcept
  203. {
  204. return static_cast<PluginBridgeNonRtClientOpcode>(readUInt());
  205. }
  206. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtClientControl)
  207. };
  208. // -------------------------------------------------------------------
  209. struct BridgeNonRtServerControl : public CarlaRingBufferControl<HugeStackBuffer> {
  210. CarlaMutex mutex;
  211. CarlaString filename;
  212. BridgeNonRtServerData* data;
  213. char shm[64];
  214. BridgeNonRtServerControl() noexcept
  215. : mutex(),
  216. filename(),
  217. data(nullptr)
  218. {
  219. carla_zeroChars(shm, 64);
  220. jackbridge_shm_init(shm);
  221. }
  222. ~BridgeNonRtServerControl() noexcept override
  223. {
  224. // should be cleared by now
  225. CARLA_SAFE_ASSERT(data == nullptr);
  226. clear();
  227. }
  228. void clear() noexcept
  229. {
  230. filename.clear();
  231. if (data != nullptr)
  232. unmapData();
  233. if (! jackbridge_shm_is_valid(shm))
  234. {
  235. CARLA_SAFE_ASSERT(data == nullptr);
  236. return;
  237. }
  238. jackbridge_shm_close(shm);
  239. jackbridge_shm_init(shm);
  240. }
  241. bool attach() noexcept
  242. {
  243. // must be invalid right now
  244. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  245. jackbridge_shm_attach(shm, filename);
  246. return jackbridge_shm_is_valid(shm);
  247. }
  248. bool mapData() noexcept
  249. {
  250. CARLA_SAFE_ASSERT(data == nullptr);
  251. if (jackbridge_shm_map2<BridgeNonRtServerData>(shm, data))
  252. {
  253. setRingBuffer(&data->ringBuffer, false);
  254. return true;
  255. }
  256. return false;
  257. }
  258. void unmapData() noexcept
  259. {
  260. data = nullptr;
  261. setRingBuffer(nullptr, false);
  262. }
  263. void writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept
  264. {
  265. writeUInt(static_cast<uint32_t>(opcode));
  266. }
  267. void waitIfDataIsReachingLimit() noexcept
  268. {
  269. if (getAvailableDataSize() < HugeStackBuffer::size/4)
  270. return;
  271. for (int i=50; --i >= 0;)
  272. {
  273. if (getAvailableDataSize() >= HugeStackBuffer::size*3/4)
  274. {
  275. writeOpcode(kPluginBridgeNonRtServerPong);
  276. commitWrite();
  277. return;
  278. }
  279. carla_msleep(20);
  280. }
  281. carla_stderr("Client waitIfDataIsReachingLimit() reached and failed");
  282. }
  283. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtServerControl)
  284. };
  285. // -------------------------------------------------------------------
  286. class CarlaEngineBridge : public CarlaEngine,
  287. public Thread
  288. {
  289. public:
  290. CarlaEngineBridge(const char* const audioPoolBaseName, const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
  291. : CarlaEngine(),
  292. Thread("CarlaEngineBridge"),
  293. fShmAudioPool(),
  294. fShmRtClientControl(),
  295. fShmNonRtClientControl(),
  296. fShmNonRtServerControl(),
  297. fIsOffline(false),
  298. fFirstIdle(true),
  299. fLastPingTime(-1)
  300. {
  301. carla_debug("CarlaEngineBridge::CarlaEngineBridge(\"%s\", \"%s\", \"%s\", \"%s\")", audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  302. fShmAudioPool.filename = PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL;
  303. fShmAudioPool.filename += audioPoolBaseName;
  304. fShmRtClientControl.filename = PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT;
  305. fShmRtClientControl.filename += rtClientBaseName;
  306. fShmNonRtClientControl.filename = PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT;
  307. fShmNonRtClientControl.filename += nonRtClientBaseName;
  308. fShmNonRtServerControl.filename = PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER;
  309. fShmNonRtServerControl.filename += nonRtServerBaseName;
  310. }
  311. ~CarlaEngineBridge() noexcept override
  312. {
  313. carla_debug("CarlaEngineBridge::~CarlaEngineBridge()");
  314. clear();
  315. }
  316. // -------------------------------------
  317. // CarlaEngine virtual calls
  318. bool init(const char* const clientName) override
  319. {
  320. carla_debug("CarlaEngineBridge::init(\"%s\")", clientName);
  321. if (! pData->init(clientName))
  322. {
  323. setLastError("Failed to init internal data");
  324. return false;
  325. }
  326. if (! fShmAudioPool.attach())
  327. {
  328. carla_stderr("Failed to attach to audio pool shared memory");
  329. return false;
  330. }
  331. if (! fShmRtClientControl.attach())
  332. {
  333. clear();
  334. carla_stderr("Failed to attach to rt client control shared memory");
  335. return false;
  336. }
  337. if (! fShmRtClientControl.mapData())
  338. {
  339. clear();
  340. carla_stderr("Failed to map rt client control shared memory");
  341. return false;
  342. }
  343. if (! fShmNonRtClientControl.attach())
  344. {
  345. clear();
  346. carla_stderr("Failed to attach to non-rt client control shared memory");
  347. return false;
  348. }
  349. if (! fShmNonRtClientControl.mapData())
  350. {
  351. clear();
  352. carla_stderr("Failed to map non-rt control client shared memory");
  353. return false;
  354. }
  355. if (! fShmNonRtServerControl.attach())
  356. {
  357. clear();
  358. carla_stderr("Failed to attach to non-rt server control shared memory");
  359. return false;
  360. }
  361. if (! fShmNonRtServerControl.mapData())
  362. {
  363. clear();
  364. carla_stderr("Failed to map non-rt control server shared memory");
  365. return false;
  366. }
  367. PluginBridgeNonRtClientOpcode opcode;
  368. opcode = fShmNonRtClientControl.readOpcode();
  369. CARLA_SAFE_ASSERT_INT(opcode == kPluginBridgeNonRtClientNull, opcode);
  370. const uint32_t shmRtClientDataSize = fShmNonRtClientControl.readUInt();
  371. CARLA_SAFE_ASSERT_INT2(shmRtClientDataSize == sizeof(BridgeRtClientData), shmRtClientDataSize, sizeof(BridgeRtClientData));
  372. const uint32_t shmNonRtClientDataSize = fShmNonRtClientControl.readUInt();
  373. CARLA_SAFE_ASSERT_INT2(shmNonRtClientDataSize == sizeof(BridgeNonRtClientData), shmNonRtClientDataSize, sizeof(BridgeNonRtClientData));
  374. const uint32_t shmNonRtServerDataSize = fShmNonRtClientControl.readUInt();
  375. CARLA_SAFE_ASSERT_INT2(shmNonRtServerDataSize == sizeof(BridgeNonRtServerData), shmNonRtServerDataSize, sizeof(BridgeNonRtServerData));
  376. opcode = fShmNonRtClientControl.readOpcode();
  377. CARLA_SAFE_ASSERT_INT(opcode == kPluginBridgeNonRtClientSetBufferSize, opcode);
  378. pData->bufferSize = fShmNonRtClientControl.readUInt();
  379. opcode = fShmNonRtClientControl.readOpcode();
  380. CARLA_SAFE_ASSERT_INT(opcode == kPluginBridgeNonRtClientSetSampleRate, opcode);
  381. pData->sampleRate = fShmNonRtClientControl.readDouble();
  382. carla_stdout("Carla Client Info:");
  383. carla_stdout(" BufferSize: %i", pData->bufferSize);
  384. carla_stdout(" SampleRate: %g", pData->sampleRate);
  385. carla_stdout(" sizeof(BridgeRtClientData): %i/" P_SIZE, shmRtClientDataSize, sizeof(BridgeRtClientData));
  386. carla_stdout(" sizeof(BridgeNonRtClientData): %i/" P_SIZE, shmNonRtClientDataSize, sizeof(BridgeNonRtClientData));
  387. carla_stdout(" sizeof(BridgeNonRtServerData): %i/" P_SIZE, shmNonRtServerDataSize, sizeof(BridgeNonRtServerData));
  388. if (shmRtClientDataSize != sizeof(BridgeRtClientData) || shmNonRtClientDataSize != sizeof(BridgeNonRtClientData) || shmNonRtServerDataSize != sizeof(BridgeNonRtServerData))
  389. return false;
  390. // tell backend we're live
  391. {
  392. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  393. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPong);
  394. fShmNonRtServerControl.commitWrite();
  395. }
  396. startThread(10);
  397. return true;
  398. }
  399. bool close() override
  400. {
  401. carla_debug("CarlaEnginePlugin::close()");
  402. fLastPingTime = -1;
  403. CarlaEngine::close();
  404. stopThread(5000);
  405. clear();
  406. return true;
  407. }
  408. bool isRunning() const noexcept override
  409. {
  410. return isThreadRunning() || ! fFirstIdle;
  411. }
  412. bool isOffline() const noexcept override
  413. {
  414. return fIsOffline;
  415. }
  416. EngineType getType() const noexcept override
  417. {
  418. return kEngineTypeBridge;
  419. }
  420. const char* getCurrentDriverName() const noexcept
  421. {
  422. return "Bridge";
  423. }
  424. void idle() noexcept override
  425. {
  426. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  427. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  428. const bool wasFirstIdle(fFirstIdle);
  429. if (fFirstIdle)
  430. {
  431. fFirstIdle = false;
  432. fLastPingTime = Time::currentTimeMillis();
  433. CARLA_SAFE_ASSERT(fLastPingTime > 0);
  434. char bufStr[STR_MAX+1];
  435. uint32_t bufStrSize;
  436. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  437. // kPluginBridgeNonRtServerPluginInfo1
  438. {
  439. // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
  440. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPluginInfo1);
  441. fShmNonRtServerControl.writeUInt(plugin->getCategory());
  442. fShmNonRtServerControl.writeUInt(plugin->getHints());
  443. fShmNonRtServerControl.writeUInt(plugin->getOptionsAvailable());
  444. fShmNonRtServerControl.writeUInt(plugin->getOptionsEnabled());
  445. fShmNonRtServerControl.writeLong(plugin->getUniqueId());
  446. fShmNonRtServerControl.commitWrite();
  447. }
  448. // kPluginBridgeNonRtServerPluginInfo2
  449. {
  450. // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
  451. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPluginInfo2);
  452. carla_zeroChars(bufStr, STR_MAX);
  453. plugin->getRealName(bufStr);
  454. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  455. fShmNonRtServerControl.writeUInt(bufStrSize);
  456. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  457. carla_zeroChars(bufStr, STR_MAX);
  458. plugin->getLabel(bufStr);
  459. bufStrSize = carla_fixedValue(1U, 256U, static_cast<uint32_t>(std::strlen(bufStr)));
  460. fShmNonRtServerControl.writeUInt(bufStrSize);
  461. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  462. carla_zeroChars(bufStr, STR_MAX);
  463. plugin->getMaker(bufStr);
  464. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  465. fShmNonRtServerControl.writeUInt(bufStrSize);
  466. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  467. carla_zeroChars(bufStr, STR_MAX);
  468. plugin->getCopyright(bufStr);
  469. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  470. fShmNonRtServerControl.writeUInt(bufStrSize);
  471. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  472. fShmNonRtServerControl.commitWrite();
  473. }
  474. // kPluginBridgeNonRtServerAudioCount
  475. {
  476. // uint/ins, uint/outs
  477. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerAudioCount);
  478. fShmNonRtServerControl.writeUInt(plugin->getAudioInCount());
  479. fShmNonRtServerControl.writeUInt(plugin->getAudioOutCount());
  480. fShmNonRtServerControl.commitWrite();
  481. }
  482. // kPluginBridgeNonRtServerMidiCount
  483. {
  484. // uint/ins, uint/outs
  485. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiCount);
  486. fShmNonRtServerControl.writeUInt(plugin->getMidiInCount());
  487. fShmNonRtServerControl.writeUInt(plugin->getMidiOutCount());
  488. fShmNonRtServerControl.commitWrite();
  489. }
  490. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  491. // kPluginBridgeNonRtServerParameter*
  492. if (const uint32_t count = plugin->getParameterCount())
  493. {
  494. // uint/count
  495. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterCount);
  496. fShmNonRtServerControl.writeUInt(count);
  497. fShmNonRtServerControl.commitWrite();
  498. for (uint32_t i=0; i<count; ++i)
  499. {
  500. const ParameterData& paramData(plugin->getParameterData(i));
  501. if (paramData.type != PARAMETER_INPUT && paramData.type != PARAMETER_OUTPUT)
  502. continue;
  503. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  504. continue;
  505. // kPluginBridgeNonRtServerParameterData1
  506. {
  507. // uint/index, int/rindex, uint/type, uint/hints, short/cc
  508. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterData1);
  509. fShmNonRtServerControl.writeUInt(i);
  510. fShmNonRtServerControl.writeInt(paramData.rindex);
  511. fShmNonRtServerControl.writeUInt(paramData.type);
  512. fShmNonRtServerControl.writeUInt(paramData.hints);
  513. fShmNonRtServerControl.writeShort(paramData.midiCC);
  514. fShmNonRtServerControl.commitWrite();
  515. }
  516. // kPluginBridgeNonRtServerParameterData2
  517. {
  518. // uint/index, uint/size, str[] (name), uint/size, str[] (symbol), uint/size, str[] (unit)
  519. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterData2);
  520. fShmNonRtServerControl.writeUInt(i);
  521. carla_zeroChars(bufStr, STR_MAX);
  522. plugin->getParameterName(i, bufStr);
  523. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  524. fShmNonRtServerControl.writeUInt(bufStrSize);
  525. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  526. carla_zeroChars(bufStr, STR_MAX);
  527. plugin->getParameterSymbol(i, bufStr);
  528. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  529. fShmNonRtServerControl.writeUInt(bufStrSize);
  530. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  531. carla_zeroChars(bufStr, STR_MAX);
  532. plugin->getParameterUnit(i, bufStr);
  533. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  534. fShmNonRtServerControl.writeUInt(bufStrSize);
  535. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  536. fShmNonRtServerControl.commitWrite();
  537. }
  538. // kPluginBridgeNonRtServerParameterRanges
  539. {
  540. const ParameterRanges& paramRanges(plugin->getParameterRanges(i));
  541. // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
  542. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterRanges);
  543. fShmNonRtServerControl.writeUInt(i);
  544. fShmNonRtServerControl.writeFloat(paramRanges.def);
  545. fShmNonRtServerControl.writeFloat(paramRanges.min);
  546. fShmNonRtServerControl.writeFloat(paramRanges.max);
  547. fShmNonRtServerControl.writeFloat(paramRanges.step);
  548. fShmNonRtServerControl.writeFloat(paramRanges.stepSmall);
  549. fShmNonRtServerControl.writeFloat(paramRanges.stepLarge);
  550. fShmNonRtServerControl.commitWrite();
  551. }
  552. // kPluginBridgeNonRtServerParameterValue2
  553. {
  554. // uint/index float/value (used for init/output parameters only, don't resend values)
  555. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue2);
  556. fShmNonRtServerControl.writeUInt(i);
  557. fShmNonRtServerControl.writeFloat(plugin->getParameterValue(i));
  558. fShmNonRtServerControl.commitWrite();
  559. }
  560. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  561. }
  562. }
  563. // kPluginBridgeNonRtServerProgram*
  564. if (const uint32_t count = plugin->getProgramCount())
  565. {
  566. // uint/count
  567. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerProgramCount);
  568. fShmNonRtServerControl.writeUInt(count);
  569. fShmNonRtServerControl.commitWrite();
  570. for (uint32_t i=0; i < count; ++i)
  571. {
  572. // uint/index, uint/size, str[] (name)
  573. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerProgramName);
  574. fShmNonRtServerControl.writeUInt(i);
  575. carla_zeroChars(bufStr, STR_MAX);
  576. plugin->getProgramName(i, bufStr);
  577. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  578. fShmNonRtServerControl.writeUInt(bufStrSize);
  579. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  580. fShmNonRtServerControl.commitWrite();
  581. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  582. }
  583. }
  584. // kPluginBridgeNonRtServerMidiProgram*
  585. if (const uint32_t count = plugin->getMidiProgramCount())
  586. {
  587. // uint/count
  588. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiProgramCount);
  589. fShmNonRtServerControl.writeUInt(count);
  590. fShmNonRtServerControl.commitWrite();
  591. for (uint32_t i=0; i < count; ++i)
  592. {
  593. const MidiProgramData& mpData(plugin->getMidiProgramData(i));
  594. CARLA_SAFE_ASSERT_CONTINUE(mpData.name != nullptr);
  595. // uint/index, uint/bank, uint/program, uint/size, str[] (name)
  596. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiProgramData);
  597. fShmNonRtServerControl.writeUInt(i);
  598. fShmNonRtServerControl.writeUInt(mpData.bank);
  599. fShmNonRtServerControl.writeUInt(mpData.program);
  600. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(mpData.name)));
  601. fShmNonRtServerControl.writeUInt(bufStrSize);
  602. fShmNonRtServerControl.writeCustomData(mpData.name, bufStrSize);
  603. fShmNonRtServerControl.commitWrite();
  604. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  605. }
  606. }
  607. // ready!
  608. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerReady);
  609. fShmNonRtServerControl.commitWrite();
  610. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  611. carla_stdout("Carla Client Ready!");
  612. fLastPingTime = Time::currentTimeMillis();
  613. }
  614. // send parameter outputs
  615. if (const uint32_t count = plugin->getParameterCount())
  616. {
  617. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  618. for (uint32_t i=0; i < count; ++i)
  619. {
  620. if (! plugin->isParameterOutput(i))
  621. continue;
  622. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue2);
  623. fShmNonRtServerControl.writeUInt(i);
  624. fShmNonRtServerControl.writeFloat(plugin->getParameterValue(i));
  625. // parameter outputs are not that important, we can skip some
  626. if (! fShmNonRtServerControl.commitWrite())
  627. break;
  628. }
  629. }
  630. CarlaEngine::idle();
  631. try {
  632. handleNonRtData();
  633. } CARLA_SAFE_EXCEPTION("handleNonRtData");
  634. if (fLastPingTime > 0 && Time::currentTimeMillis() > fLastPingTime + 30000 && ! wasFirstIdle)
  635. {
  636. carla_stderr("Did not receive ping message from server for 30 secs, closing...");
  637. threadShouldExit();
  638. callback(ENGINE_CALLBACK_QUIT, 0, 0, 0, 0.0f, nullptr);
  639. }
  640. }
  641. void callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept override
  642. {
  643. CarlaEngine::callback(action, pluginId, value1, value2, value3, valueStr);
  644. if (fLastPingTime < 0)
  645. return;
  646. switch (action)
  647. {
  648. // uint/index float/value
  649. case ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED: {
  650. CARLA_SAFE_ASSERT_BREAK(value1 >= 0);
  651. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  652. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue);
  653. fShmNonRtServerControl.writeUInt(static_cast<uint>(value1));
  654. fShmNonRtServerControl.writeFloat(value3);
  655. fShmNonRtServerControl.commitWrite();
  656. } break;
  657. // uint/index float/value
  658. case ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED: {
  659. CARLA_SAFE_ASSERT_BREAK(value1 >= 0);
  660. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  661. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerDefaultValue);
  662. fShmNonRtServerControl.writeUInt(static_cast<uint>(value1));
  663. fShmNonRtServerControl.writeFloat(value3);
  664. fShmNonRtServerControl.commitWrite();
  665. } break;
  666. // int/index
  667. case ENGINE_CALLBACK_PROGRAM_CHANGED: {
  668. CARLA_SAFE_ASSERT_BREAK(value1 >= -1);
  669. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  670. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerCurrentProgram);
  671. fShmNonRtServerControl.writeInt(value1);
  672. fShmNonRtServerControl.commitWrite();
  673. } break;
  674. // int/index
  675. case ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED: {
  676. CARLA_SAFE_ASSERT_BREAK(value1 >= -1);
  677. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  678. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerCurrentMidiProgram);
  679. fShmNonRtServerControl.writeInt(value1);
  680. fShmNonRtServerControl.commitWrite();
  681. } break;
  682. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  683. if (value1 != 1)
  684. {
  685. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  686. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerUiClosed);
  687. fShmNonRtServerControl.commitWrite();
  688. }
  689. break;
  690. default:
  691. break;
  692. }
  693. }
  694. // -------------------------------------------------------------------
  695. void clear() noexcept
  696. {
  697. fShmAudioPool.clear();
  698. fShmRtClientControl.clear();
  699. fShmNonRtClientControl.clear();
  700. fShmNonRtServerControl.clear();
  701. }
  702. void handleNonRtData()
  703. {
  704. for (; fShmNonRtClientControl.isDataAvailableForReading();)
  705. {
  706. const PluginBridgeNonRtClientOpcode opcode(fShmNonRtClientControl.readOpcode());
  707. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  708. #ifdef DEBUG
  709. if (opcode != kPluginBridgeNonRtClientPing) {
  710. carla_debug("CarlaEngineBridge::handleNonRtData() - got opcode: %s", PluginBridgeNonRtClientOpcode2str(opcode));
  711. }
  712. #endif
  713. if (opcode != kPluginBridgeNonRtClientNull && opcode != kPluginBridgeNonRtClientPingOnOff && fLastPingTime > 0)
  714. fLastPingTime = Time::currentTimeMillis();
  715. switch (opcode)
  716. {
  717. case kPluginBridgeNonRtClientNull:
  718. break;
  719. case kPluginBridgeNonRtClientPing: {
  720. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  721. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPong);
  722. fShmNonRtServerControl.commitWrite();
  723. } break;
  724. case kPluginBridgeNonRtClientPingOnOff: {
  725. const uint32_t onOff(fShmNonRtClientControl.readBool());
  726. fLastPingTime = onOff ? Time::currentTimeMillis() : -1;
  727. } break;
  728. case kPluginBridgeNonRtClientActivate:
  729. if (plugin != nullptr && plugin->isEnabled())
  730. plugin->setActive(true, false, false);
  731. break;
  732. case kPluginBridgeNonRtClientDeactivate:
  733. if (plugin != nullptr && plugin->isEnabled())
  734. plugin->setActive(false, false, false);
  735. break;
  736. case kPluginBridgeNonRtClientSetBufferSize: {
  737. const uint32_t bufferSize(fShmNonRtClientControl.readUInt());
  738. pData->bufferSize = bufferSize;
  739. bufferSizeChanged(bufferSize);
  740. break;
  741. }
  742. case kPluginBridgeNonRtClientSetSampleRate: {
  743. const double sampleRate(fShmNonRtClientControl.readDouble());
  744. pData->sampleRate = sampleRate;
  745. sampleRateChanged(sampleRate);
  746. break;
  747. }
  748. case kPluginBridgeNonRtClientSetOffline:
  749. fIsOffline = true;
  750. offlineModeChanged(true);
  751. break;
  752. case kPluginBridgeNonRtClientSetOnline:
  753. fIsOffline = false;
  754. offlineModeChanged(false);
  755. break;
  756. case kPluginBridgeNonRtClientSetParameterValue: {
  757. const uint32_t index(fShmNonRtClientControl.readUInt());
  758. const float value(fShmNonRtClientControl.readFloat());
  759. if (plugin != nullptr && plugin->isEnabled())
  760. plugin->setParameterValue(index, value, false, false, false);
  761. break;
  762. }
  763. case kPluginBridgeNonRtClientSetParameterMidiChannel: {
  764. const uint32_t index(fShmNonRtClientControl.readUInt());
  765. const uint8_t channel(fShmNonRtClientControl.readByte());
  766. if (plugin != nullptr && plugin->isEnabled())
  767. plugin->setParameterMidiChannel(index, channel, false, false);
  768. break;
  769. }
  770. case kPluginBridgeNonRtClientSetParameterMidiCC: {
  771. const uint32_t index(fShmNonRtClientControl.readUInt());
  772. const int16_t cc(fShmNonRtClientControl.readShort());
  773. if (plugin != nullptr && plugin->isEnabled())
  774. plugin->setParameterMidiCC(index, cc, false, false);
  775. break;
  776. }
  777. case kPluginBridgeNonRtClientSetProgram: {
  778. const int32_t index(fShmNonRtClientControl.readInt());
  779. if (plugin != nullptr && plugin->isEnabled())
  780. plugin->setProgram(index, false, false, false);
  781. break;
  782. }
  783. case kPluginBridgeNonRtClientSetMidiProgram: {
  784. const int32_t index(fShmNonRtClientControl.readInt());
  785. if (plugin != nullptr && plugin->isEnabled())
  786. plugin->setMidiProgram(index, false, false, false);
  787. break;
  788. }
  789. case kPluginBridgeNonRtClientSetCustomData: {
  790. // type
  791. const uint32_t typeSize(fShmNonRtClientControl.readUInt());
  792. char typeStr[typeSize+1];
  793. carla_zeroChars(typeStr, typeSize+1);
  794. fShmNonRtClientControl.readCustomData(typeStr, typeSize);
  795. // key
  796. const uint32_t keySize(fShmNonRtClientControl.readUInt());
  797. char keyStr[keySize+1];
  798. carla_zeroChars(keyStr, keySize+1);
  799. fShmNonRtClientControl.readCustomData(keyStr, keySize);
  800. // value
  801. const uint32_t valueSize(fShmNonRtClientControl.readUInt());
  802. char valueStr[valueSize+1];
  803. carla_zeroChars(valueStr, valueSize+1);
  804. fShmNonRtClientControl.readCustomData(valueStr, valueSize);
  805. if (plugin != nullptr && plugin->isEnabled())
  806. plugin->setCustomData(typeStr, keyStr, valueStr, true);
  807. break;
  808. }
  809. case kPluginBridgeNonRtClientSetChunkDataFile: {
  810. const uint32_t size(fShmNonRtClientControl.readUInt());
  811. CARLA_SAFE_ASSERT_BREAK(size > 0);
  812. char chunkFilePathTry[size+1];
  813. carla_zeroChars(chunkFilePathTry, size+1);
  814. fShmNonRtClientControl.readCustomData(chunkFilePathTry, size);
  815. CARLA_SAFE_ASSERT_BREAK(chunkFilePathTry[0] != '\0');
  816. if (plugin == nullptr || ! plugin->isEnabled()) break;
  817. String chunkFilePath(chunkFilePathTry);
  818. #ifdef CARLA_OS_WIN
  819. // check if running under Wine
  820. if (chunkFilePath.startsWith("/"))
  821. chunkFilePath = chunkFilePath.replaceSection(0, 1, "Z:\\").replace("/", "\\");
  822. #endif
  823. File chunkFile(chunkFilePath);
  824. CARLA_SAFE_ASSERT_BREAK(chunkFile.existsAsFile());
  825. String chunkDataBase64(chunkFile.loadFileAsString());
  826. chunkFile.deleteFile();
  827. CARLA_SAFE_ASSERT_BREAK(chunkDataBase64.isNotEmpty());
  828. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(chunkDataBase64.toRawUTF8()));
  829. plugin->setChunkData(chunk.data(), chunk.size());
  830. break;
  831. }
  832. case kPluginBridgeNonRtClientSetCtrlChannel: {
  833. const int16_t channel(fShmNonRtClientControl.readShort());
  834. CARLA_SAFE_ASSERT_BREAK(channel >= -1 && channel < MAX_MIDI_CHANNELS);
  835. if (plugin != nullptr && plugin->isEnabled())
  836. plugin->setCtrlChannel(static_cast<int8_t>(channel), false, false);
  837. break;
  838. }
  839. case kPluginBridgeNonRtClientSetOption: {
  840. const uint32_t option(fShmNonRtClientControl.readUInt());
  841. const bool yesNo(fShmNonRtClientControl.readBool());
  842. if (plugin != nullptr && plugin->isEnabled())
  843. plugin->setOption(option, yesNo, false);
  844. break;
  845. }
  846. case kPluginBridgeNonRtClientPrepareForSave: {
  847. if (plugin == nullptr || ! plugin->isEnabled()) break;
  848. plugin->prepareForSave();
  849. for (uint32_t i=0, count=plugin->getCustomDataCount(); i<count; ++i)
  850. {
  851. const CustomData& cdata(plugin->getCustomData(i));
  852. if (std::strcmp(cdata.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cdata.key, "CarlaLoadLv2StateNow") == 0 && std::strcmp(cdata.value, "true") == 0)
  853. continue;
  854. const uint32_t typeLen(static_cast<uint32_t>(std::strlen(cdata.type)));
  855. const uint32_t keyLen(static_cast<uint32_t>(std::strlen(cdata.key)));
  856. const uint32_t valueLen(static_cast<uint32_t>(std::strlen(cdata.value)));
  857. {
  858. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  859. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetCustomData);
  860. fShmNonRtServerControl.writeUInt(typeLen);
  861. fShmNonRtServerControl.writeCustomData(cdata.type, typeLen);
  862. fShmNonRtServerControl.writeUInt(keyLen);
  863. fShmNonRtServerControl.writeCustomData(cdata.key, keyLen);
  864. fShmNonRtServerControl.writeUInt(valueLen);
  865. fShmNonRtServerControl.writeCustomData(cdata.value, valueLen);
  866. fShmNonRtServerControl.commitWrite();
  867. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  868. }
  869. }
  870. if (plugin->getOptionsEnabled() & PLUGIN_OPTION_USE_CHUNKS)
  871. {
  872. void* data = nullptr;
  873. if (const std::size_t dataSize = plugin->getChunkData(&data))
  874. {
  875. CARLA_SAFE_ASSERT_BREAK(data != nullptr);
  876. CarlaString dataBase64 = CarlaString::asBase64(data, dataSize);
  877. CARLA_SAFE_ASSERT_BREAK(dataBase64.length() > 0);
  878. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  879. filePath += CARLA_OS_SEP_STR;
  880. filePath += ".CarlaChunk_";
  881. filePath += fShmNonRtClientControl.filename.buffer() + 24;
  882. if (File(filePath).replaceWithText(dataBase64.buffer()))
  883. {
  884. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  885. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  886. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetChunkDataFile);
  887. fShmNonRtServerControl.writeUInt(ulength);
  888. fShmNonRtServerControl.writeCustomData(filePath.toRawUTF8(), ulength);
  889. fShmNonRtServerControl.commitWrite();
  890. }
  891. }
  892. }
  893. {
  894. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  895. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSaved);
  896. fShmNonRtServerControl.commitWrite();
  897. }
  898. break;
  899. }
  900. case kPluginBridgeNonRtClientShowUI:
  901. if (plugin != nullptr && plugin->isEnabled())
  902. plugin->showCustomUI(true);
  903. break;
  904. case kPluginBridgeNonRtClientHideUI:
  905. if (plugin != nullptr && plugin->isEnabled())
  906. plugin->showCustomUI(false);
  907. break;
  908. case kPluginBridgeNonRtClientUiParameterChange: {
  909. const uint32_t index(fShmNonRtClientControl.readUInt());
  910. const float value(fShmNonRtClientControl.readFloat());
  911. if (plugin != nullptr && plugin->isEnabled())
  912. plugin->uiParameterChange(index, value);
  913. break;
  914. }
  915. case kPluginBridgeNonRtClientUiProgramChange: {
  916. const uint32_t index(fShmNonRtClientControl.readUInt());
  917. if (plugin != nullptr && plugin->isEnabled())
  918. plugin->uiProgramChange(index);
  919. break;
  920. }
  921. case kPluginBridgeNonRtClientUiMidiProgramChange: {
  922. const uint32_t index(fShmNonRtClientControl.readUInt());
  923. if (plugin != nullptr && plugin->isEnabled())
  924. plugin->uiMidiProgramChange(index);
  925. break;
  926. }
  927. case kPluginBridgeNonRtClientUiNoteOn: {
  928. const uint8_t chnl(fShmNonRtClientControl.readByte());
  929. const uint8_t note(fShmNonRtClientControl.readByte());
  930. const uint8_t velo(fShmNonRtClientControl.readByte());
  931. if (plugin != nullptr && plugin->isEnabled())
  932. plugin->uiNoteOn(chnl, note, velo);
  933. break;
  934. }
  935. case kPluginBridgeNonRtClientUiNoteOff: {
  936. const uint8_t chnl(fShmNonRtClientControl.readByte());
  937. const uint8_t note(fShmNonRtClientControl.readByte());
  938. if (plugin != nullptr && plugin->isEnabled())
  939. plugin->uiNoteOff(chnl, note);
  940. break;
  941. }
  942. case kPluginBridgeNonRtClientQuit:
  943. signalThreadShouldExit();
  944. callback(ENGINE_CALLBACK_QUIT, 0, 0, 0, 0.0f, nullptr);
  945. break;
  946. }
  947. }
  948. }
  949. // -------------------------------------------------------------------
  950. protected:
  951. void run() override
  952. {
  953. bool quitReceived = false;
  954. for (; ! threadShouldExit();)
  955. {
  956. const BridgeRtClientControl::WaitHelper helper(fShmRtClientControl);
  957. if (! helper.ok)
  958. continue;
  959. for (; fShmRtClientControl.isDataAvailableForReading();)
  960. {
  961. const PluginBridgeRtClientOpcode opcode(fShmRtClientControl.readOpcode());
  962. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  963. #ifdef DEBUG
  964. if (opcode != kPluginBridgeRtClientProcess && opcode != kPluginBridgeRtClientMidiEvent) {
  965. carla_debug("CarlaEngineBridgeRtThread::run() - got opcode: %s", PluginBridgeRtClientOpcode2str(opcode));
  966. }
  967. #endif
  968. switch (opcode)
  969. {
  970. case kPluginBridgeRtClientNull:
  971. break;
  972. case kPluginBridgeRtClientSetAudioPool: {
  973. if (fShmAudioPool.data != nullptr)
  974. {
  975. jackbridge_shm_unmap(fShmAudioPool.shm, fShmAudioPool.data);
  976. fShmAudioPool.data = nullptr;
  977. }
  978. const uint64_t poolSize(fShmRtClientControl.readULong());
  979. CARLA_SAFE_ASSERT_BREAK(poolSize > 0);
  980. fShmAudioPool.data = (float*)jackbridge_shm_map(fShmAudioPool.shm, static_cast<size_t>(poolSize));
  981. break;
  982. }
  983. case kPluginBridgeRtClientControlEventParameter: {
  984. const uint32_t time(fShmRtClientControl.readUInt());
  985. const uint8_t channel(fShmRtClientControl.readByte());
  986. const uint16_t param(fShmRtClientControl.readUShort());
  987. const float value(fShmRtClientControl.readFloat());
  988. if (EngineEvent* const event = getNextFreeInputEvent())
  989. {
  990. event->type = kEngineEventTypeControl;
  991. event->time = time;
  992. event->channel = channel;
  993. event->ctrl.type = kEngineControlEventTypeParameter;
  994. event->ctrl.param = param;
  995. event->ctrl.value = value;
  996. }
  997. break;
  998. }
  999. case kPluginBridgeRtClientControlEventMidiBank: {
  1000. const uint32_t time(fShmRtClientControl.readUInt());
  1001. const uint8_t channel(fShmRtClientControl.readByte());
  1002. const uint16_t index(fShmRtClientControl.readUShort());
  1003. if (EngineEvent* const event = getNextFreeInputEvent())
  1004. {
  1005. event->type = kEngineEventTypeControl;
  1006. event->time = time;
  1007. event->channel = channel;
  1008. event->ctrl.type = kEngineControlEventTypeMidiBank;
  1009. event->ctrl.param = index;
  1010. event->ctrl.value = 0.0f;
  1011. }
  1012. break;
  1013. }
  1014. case kPluginBridgeRtClientControlEventMidiProgram: {
  1015. const uint32_t time(fShmRtClientControl.readUInt());
  1016. const uint8_t channel(fShmRtClientControl.readByte());
  1017. const uint16_t index(fShmRtClientControl.readUShort());
  1018. if (EngineEvent* const event = getNextFreeInputEvent())
  1019. {
  1020. event->type = kEngineEventTypeControl;
  1021. event->time = time;
  1022. event->channel = channel;
  1023. event->ctrl.type = kEngineControlEventTypeMidiProgram;
  1024. event->ctrl.param = index;
  1025. event->ctrl.value = 0.0f;
  1026. }
  1027. break;
  1028. }
  1029. case kPluginBridgeRtClientControlEventAllSoundOff: {
  1030. const uint32_t time(fShmRtClientControl.readUInt());
  1031. const uint8_t channel(fShmRtClientControl.readByte());
  1032. if (EngineEvent* const event = getNextFreeInputEvent())
  1033. {
  1034. event->type = kEngineEventTypeControl;
  1035. event->time = time;
  1036. event->channel = channel;
  1037. event->ctrl.type = kEngineControlEventTypeAllSoundOff;
  1038. event->ctrl.param = 0;
  1039. event->ctrl.value = 0.0f;
  1040. }
  1041. } break;
  1042. case kPluginBridgeRtClientControlEventAllNotesOff: {
  1043. const uint32_t time(fShmRtClientControl.readUInt());
  1044. const uint8_t channel(fShmRtClientControl.readByte());
  1045. if (EngineEvent* const event = getNextFreeInputEvent())
  1046. {
  1047. event->type = kEngineEventTypeControl;
  1048. event->time = time;
  1049. event->channel = channel;
  1050. event->ctrl.type = kEngineControlEventTypeAllNotesOff;
  1051. event->ctrl.param = 0;
  1052. event->ctrl.value = 0.0f;
  1053. }
  1054. } break;
  1055. case kPluginBridgeRtClientMidiEvent: {
  1056. const uint32_t time(fShmRtClientControl.readUInt());
  1057. const uint8_t port(fShmRtClientControl.readByte());
  1058. const uint8_t size(fShmRtClientControl.readByte());
  1059. CARLA_SAFE_ASSERT_BREAK(size > 0);
  1060. uint8_t data[size];
  1061. for (uint8_t i=0; i<size; ++i)
  1062. data[i] = fShmRtClientControl.readByte();
  1063. if (EngineEvent* const event = getNextFreeInputEvent())
  1064. {
  1065. event->type = kEngineEventTypeMidi;
  1066. event->time = time;
  1067. event->channel = MIDI_GET_CHANNEL_FROM_DATA(data);
  1068. event->midi.port = port;
  1069. event->midi.size = size;
  1070. if (size > EngineMidiEvent::kDataSize)
  1071. {
  1072. event->midi.dataExt = data;
  1073. std::memset(event->midi.data, 0, sizeof(uint8_t)*EngineMidiEvent::kDataSize);
  1074. }
  1075. else
  1076. {
  1077. event->midi.data[0] = MIDI_GET_STATUS_FROM_DATA(data);
  1078. uint8_t i=1;
  1079. for (; i < size; ++i)
  1080. event->midi.data[i] = data[i];
  1081. for (; i < EngineMidiEvent::kDataSize; ++i)
  1082. event->midi.data[i] = 0;
  1083. event->midi.dataExt = nullptr;
  1084. }
  1085. }
  1086. break;
  1087. }
  1088. case kPluginBridgeRtClientProcess: {
  1089. CARLA_SAFE_ASSERT_BREAK(fShmAudioPool.data != nullptr);
  1090. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(false))
  1091. {
  1092. const BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
  1093. const uint32_t audioInCount(plugin->getAudioInCount());
  1094. const uint32_t audioOutCount(plugin->getAudioOutCount());
  1095. const uint32_t cvInCount(plugin->getCVInCount());
  1096. const uint32_t cvOutCount(plugin->getCVOutCount());
  1097. const float* audioIn[audioInCount];
  1098. /* */ float* audioOut[audioOutCount];
  1099. const float* cvIn[cvInCount];
  1100. /* */ float* cvOut[cvOutCount];
  1101. float* fdata = fShmAudioPool.data;
  1102. for (uint32_t i=0; i < audioInCount; ++i, fdata += pData->bufferSize)
  1103. audioIn[i] = fdata;
  1104. for (uint32_t i=0; i < audioOutCount; ++i, fdata += pData->bufferSize)
  1105. audioOut[i] = fdata;
  1106. for (uint32_t i=0; i < cvInCount; ++i, fdata += pData->bufferSize)
  1107. cvIn[i] = fdata;
  1108. for (uint32_t i=0; i < cvOutCount; ++i, fdata += pData->bufferSize)
  1109. cvOut[i] = fdata;
  1110. EngineTimeInfo& timeInfo(pData->timeInfo);
  1111. timeInfo.playing = bridgeTimeInfo.playing;
  1112. timeInfo.frame = bridgeTimeInfo.frame;
  1113. timeInfo.usecs = bridgeTimeInfo.usecs;
  1114. timeInfo.valid = bridgeTimeInfo.valid;
  1115. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1116. {
  1117. timeInfo.bbt.bar = bridgeTimeInfo.bar;
  1118. timeInfo.bbt.beat = bridgeTimeInfo.beat;
  1119. timeInfo.bbt.tick = bridgeTimeInfo.tick;
  1120. timeInfo.bbt.beatsPerBar = bridgeTimeInfo.beatsPerBar;
  1121. timeInfo.bbt.beatType = bridgeTimeInfo.beatType;
  1122. timeInfo.bbt.ticksPerBeat = bridgeTimeInfo.ticksPerBeat;
  1123. timeInfo.bbt.beatsPerMinute = bridgeTimeInfo.beatsPerMinute;
  1124. timeInfo.bbt.barStartTick = bridgeTimeInfo.barStartTick;
  1125. }
  1126. plugin->initBuffers();
  1127. plugin->process(audioIn, audioOut, cvIn, cvOut, pData->bufferSize);
  1128. plugin->unlock();
  1129. }
  1130. uint8_t* midiData(fShmRtClientControl.data->midiOut);
  1131. carla_zeroBytes(midiData, kBridgeRtClientDataMidiOutSize);
  1132. std::size_t curMidiDataPos = 0;
  1133. if (pData->events.in[0].type != kEngineEventTypeNull)
  1134. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  1135. if (pData->events.out[0].type != kEngineEventTypeNull)
  1136. {
  1137. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  1138. {
  1139. const EngineEvent& event(pData->events.out[i]);
  1140. if (event.type == kEngineEventTypeNull)
  1141. break;
  1142. if (event.type == kEngineEventTypeControl)
  1143. {
  1144. uint8_t size;
  1145. uint8_t data[3];
  1146. event.ctrl.convertToMidiData(event.channel, size, data);
  1147. CARLA_SAFE_ASSERT_CONTINUE(size > 0 && size <= 3);
  1148. if (curMidiDataPos + 1U /* size*/ + 4U /* time */ + size >= kBridgeRtClientDataMidiOutSize)
  1149. break;
  1150. // set size
  1151. *midiData++ = size;
  1152. // set time
  1153. *(uint32_t*)midiData = event.time;
  1154. midiData = midiData + 4;
  1155. // set data
  1156. for (uint8_t j=0; j<size; ++j)
  1157. *midiData++ = data[j];
  1158. curMidiDataPos += 1U /* size*/ + 4U /* time */ + size;
  1159. }
  1160. else if (event.type == kEngineEventTypeMidi)
  1161. {
  1162. const EngineMidiEvent& _midiEvent(event.midi);
  1163. if (curMidiDataPos + 1 /* size*/ + 4 /* time */ + _midiEvent.size >= kBridgeRtClientDataMidiOutSize)
  1164. break;
  1165. const uint8_t* const _midiData(_midiEvent.dataExt != nullptr ? _midiEvent.dataExt : _midiEvent.data);
  1166. // set size
  1167. *midiData++ = _midiEvent.size;
  1168. // set time
  1169. *(uint32_t*)midiData = event.time;
  1170. midiData = midiData + 4;
  1171. // set data
  1172. *midiData++ = uint8_t(_midiData[0] | (event.channel & MIDI_CHANNEL_BIT));
  1173. for (uint8_t j=1; j<_midiEvent.size; ++j)
  1174. *midiData++ = _midiData[j];
  1175. curMidiDataPos += 1U /* size*/ + 4U /* time */ + _midiEvent.size;
  1176. }
  1177. }
  1178. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  1179. }
  1180. } break;
  1181. case kPluginBridgeRtClientQuit: {
  1182. quitReceived = true;
  1183. signalThreadShouldExit();
  1184. } break;
  1185. }
  1186. }
  1187. }
  1188. callback(ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0.0f, nullptr);
  1189. if (! quitReceived)
  1190. {
  1191. const char* const message("Plugin bridge error, process thread has stopped");
  1192. const std::size_t messageSize(std::strlen(message));
  1193. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  1194. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerError);
  1195. fShmNonRtServerControl.writeUInt(messageSize);
  1196. fShmNonRtServerControl.writeCustomData(message, messageSize);
  1197. fShmNonRtServerControl.commitWrite();
  1198. }
  1199. }
  1200. // called from process thread above
  1201. EngineEvent* getNextFreeInputEvent() const noexcept
  1202. {
  1203. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  1204. {
  1205. EngineEvent* const event(&pData->events.in[i]);
  1206. if (event->type == kEngineEventTypeNull)
  1207. return event;
  1208. }
  1209. return nullptr;
  1210. }
  1211. // -------------------------------------------------------------------
  1212. private:
  1213. BridgeAudioPool fShmAudioPool;
  1214. BridgeRtClientControl fShmRtClientControl;
  1215. BridgeNonRtClientControl fShmNonRtClientControl;
  1216. BridgeNonRtServerControl fShmNonRtServerControl;
  1217. bool fIsOffline;
  1218. bool fFirstIdle;
  1219. int64_t fLastPingTime;
  1220. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  1221. };
  1222. // -----------------------------------------------------------------------
  1223. CarlaEngine* CarlaEngine::newBridge(const char* const audioPoolBaseName, const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
  1224. {
  1225. return new CarlaEngineBridge(audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  1226. }
  1227. // -----------------------------------------------------------------------
  1228. #ifdef BRIDGE_PLUGIN
  1229. CarlaPlugin* CarlaPlugin::newNative(const CarlaPlugin::Initializer&) { return nullptr; }
  1230. CarlaPlugin* CarlaPlugin::newFileGIG(const CarlaPlugin::Initializer&, const bool) { return nullptr; }
  1231. CarlaPlugin* CarlaPlugin::newFileSF2(const CarlaPlugin::Initializer&, const bool) { return nullptr; }
  1232. CarlaPlugin* CarlaPlugin::newFileSFZ(const CarlaPlugin::Initializer&) { return nullptr; }
  1233. #endif
  1234. CARLA_BACKEND_END_NAMESPACE
  1235. // -----------------------------------------------------------------------
  1236. #if defined(CARLA_OS_WIN) && ! defined(__WINE__)
  1237. extern "C" __declspec (dllexport)
  1238. #else
  1239. extern "C" __attribute__ ((visibility("default")))
  1240. #endif
  1241. void carla_register_native_plugin_carla();
  1242. void carla_register_native_plugin_carla(){}
  1243. // -----------------------------------------------------------------------