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.

2131 lines
72KB

  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2011-2014 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. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifndef BUILD_BRIDGE
  20. #include "CarlaBackendUtils.hpp"
  21. #include "CarlaBase64Utils.hpp"
  22. #include "CarlaBridgeUtils.hpp"
  23. #include "CarlaMathUtils.hpp"
  24. #include "CarlaShmUtils.hpp"
  25. #include "jackbridge/JackBridge.hpp"
  26. #include <cerrno>
  27. #include <cmath>
  28. #include <ctime>
  29. #define CARLA_BRIDGE_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
  30. /* check argument count */ \
  31. if (argc != argcToCompare) \
  32. { \
  33. carla_stderr("BridgePlugin::%s() - argument count mismatch: %i != %i", __FUNCTION__, argc, argcToCompare); \
  34. return 1; \
  35. } \
  36. if (argc > 0) \
  37. { \
  38. /* check for nullness */ \
  39. if (! (types && typesToCompare)) \
  40. { \
  41. carla_stderr("BridgePlugin::%s() - argument types are null", __FUNCTION__); \
  42. return 1; \
  43. } \
  44. /* check argument types */ \
  45. if (std::strcmp(types, typesToCompare) != 0) \
  46. { \
  47. carla_stderr("BridgePlugin::%s() - argument types mismatch: '%s' != '%s'", __FUNCTION__, types, typesToCompare); \
  48. return 1; \
  49. } \
  50. }
  51. // -------------------------------------------------------------------------------------------------------------------
  52. using juce::File;
  53. using juce::MemoryBlock;
  54. using juce::String;
  55. using juce::StringArray;
  56. CARLA_BACKEND_START_NAMESPACE
  57. // -------------------------------------------------------------------------------------------------------------------
  58. // call carla_shm_create with for a XXXXXX temp filename
  59. static shm_t shm_mkstemp(char* const fileBase)
  60. {
  61. CARLA_SAFE_ASSERT_RETURN(fileBase != nullptr, gNullCarlaShm);
  62. const size_t fileBaseLen(std::strlen(fileBase));
  63. CARLA_SAFE_ASSERT_RETURN(fileBaseLen > 6, gNullCarlaShm);
  64. CARLA_SAFE_ASSERT_RETURN(std::strcmp(fileBase + fileBaseLen - 6, "XXXXXX") == 0, gNullCarlaShm);
  65. static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
  66. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  67. "0123456789";
  68. static const int charSetLen = static_cast<int>(std::strlen(charSet) - 1); // -1 to avoid trailing '\0'
  69. // try until getting a valid shm or an error occurs
  70. for (;;)
  71. {
  72. for (size_t c = fileBaseLen - 6; c < fileBaseLen; ++c)
  73. fileBase[c] = charSet[std::rand() % charSetLen];
  74. const shm_t shm = carla_shm_create(fileBase);
  75. if (carla_is_shm_valid(shm))
  76. return shm;
  77. if (errno != EEXIST)
  78. return gNullCarlaShm;
  79. }
  80. }
  81. // -------------------------------------------------------------------------------------------------------------------
  82. struct BridgeAudioPool {
  83. CarlaString filename;
  84. float* data;
  85. size_t size;
  86. shm_t shm;
  87. BridgeAudioPool()
  88. : data(nullptr),
  89. size(0)
  90. {
  91. carla_shm_init(shm);
  92. }
  93. ~BridgeAudioPool()
  94. {
  95. // should be cleared by now
  96. CARLA_SAFE_ASSERT(data == nullptr);
  97. clear();
  98. }
  99. void clear()
  100. {
  101. filename.clear();
  102. if (! carla_is_shm_valid(shm))
  103. return;
  104. if (data != nullptr)
  105. {
  106. carla_shm_unmap(shm, data, size);
  107. data = nullptr;
  108. }
  109. size = 0;
  110. carla_shm_close(shm);
  111. }
  112. void resize(const uint32_t bufferSize, const uint32_t portCount)
  113. {
  114. if (data != nullptr)
  115. carla_shm_unmap(shm, data, size);
  116. size = portCount*bufferSize*sizeof(float);
  117. if (size == 0)
  118. size = sizeof(float);
  119. data = (float*)carla_shm_map(shm, size);
  120. }
  121. CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
  122. };
  123. // -------------------------------------------------------------------------------------------------------------------
  124. struct BridgeRtControl : public CarlaRingBuffer<StackBuffer> {
  125. CarlaString filename;
  126. BridgeRtData* data;
  127. shm_t shm;
  128. BridgeRtControl()
  129. : CarlaRingBuffer<StackBuffer>(),
  130. data(nullptr)
  131. {
  132. carla_shm_init(shm);
  133. }
  134. ~BridgeRtControl()
  135. {
  136. // should be cleared by now
  137. CARLA_SAFE_ASSERT(data == nullptr);
  138. clear();
  139. }
  140. void clear()
  141. {
  142. filename.clear();
  143. if (! carla_is_shm_valid(shm))
  144. return;
  145. if (data != nullptr)
  146. {
  147. carla_shm_unmap(shm, data, sizeof(BridgeRtData));
  148. data = nullptr;
  149. }
  150. carla_shm_close(shm);
  151. }
  152. bool mapData()
  153. {
  154. CARLA_ASSERT(data == nullptr);
  155. if (carla_shm_map<BridgeRtData>(shm, data))
  156. {
  157. setRingBuffer(&data->ringBuffer, true);
  158. return true;
  159. }
  160. return false;
  161. }
  162. void unmapData()
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  165. carla_shm_unmap(shm, data, sizeof(BridgeRtData));
  166. data = nullptr;
  167. setRingBuffer(nullptr, false);
  168. }
  169. bool waitForServer(const int secs)
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  172. jackbridge_sem_post(&data->sem.server);
  173. return jackbridge_sem_timedwait(&data->sem.client, secs);
  174. }
  175. void writeOpcode(const PluginBridgeRtOpcode opcode) noexcept
  176. {
  177. writeInt(static_cast<int32_t>(opcode));
  178. }
  179. CARLA_DECLARE_NON_COPY_STRUCT(BridgeRtControl)
  180. };
  181. // -------------------------------------------------------------------------------------------------------------------
  182. struct BridgeNonRtControl : public CarlaRingBuffer<BigStackBuffer> {
  183. CarlaMutex mutex;
  184. CarlaString filename;
  185. BridgeNonRtData* data;
  186. shm_t shm;
  187. BridgeNonRtControl()
  188. : CarlaRingBuffer<BigStackBuffer>(),
  189. data(nullptr)
  190. {
  191. carla_shm_init(shm);
  192. }
  193. ~BridgeNonRtControl()
  194. {
  195. // should be cleared by now
  196. CARLA_SAFE_ASSERT(data == nullptr);
  197. clear();
  198. }
  199. void clear()
  200. {
  201. filename.clear();
  202. if (! carla_is_shm_valid(shm))
  203. return;
  204. if (data != nullptr)
  205. {
  206. carla_shm_unmap(shm, data, sizeof(BridgeNonRtData));
  207. data = nullptr;
  208. }
  209. carla_shm_close(shm);
  210. }
  211. bool mapData()
  212. {
  213. CARLA_SAFE_ASSERT(data == nullptr);
  214. if (carla_shm_map<BridgeNonRtData>(shm, data))
  215. {
  216. setRingBuffer(&data->ringBuffer, true);
  217. return true;
  218. }
  219. return false;
  220. }
  221. void unmapData()
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  224. carla_shm_unmap(shm, data, sizeof(BridgeNonRtData));
  225. data = nullptr;
  226. setRingBuffer(nullptr, false);
  227. }
  228. void writeOpcode(const PluginBridgeNonRtOpcode opcode) noexcept
  229. {
  230. writeInt(static_cast<int32_t>(opcode));
  231. }
  232. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtControl)
  233. };
  234. // -------------------------------------------------------------------------------------------------------------------
  235. struct BridgeParamInfo {
  236. float value;
  237. CarlaString name;
  238. CarlaString unit;
  239. BridgeParamInfo() noexcept
  240. : value(0.0f) {}
  241. CARLA_DECLARE_NON_COPY_STRUCT(BridgeParamInfo)
  242. };
  243. // -------------------------------------------------------------------------------------------------------------------
  244. class BridgePlugin : public CarlaPlugin
  245. {
  246. public:
  247. BridgePlugin(CarlaEngine* const engine, const uint id, const BinaryType btype, const PluginType ptype)
  248. : CarlaPlugin(engine, id),
  249. fBinaryType(btype),
  250. fPluginType(ptype),
  251. fInitiated(false),
  252. fInitError(false),
  253. fSaved(false),
  254. fNeedsSemDestroy(false),
  255. fTimedOut(false),
  256. fLastPongCounter(-1),
  257. fParams(nullptr)
  258. {
  259. carla_debug("BridgePlugin::BridgePlugin(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
  260. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_BRIDGE);
  261. pData->hints |= PLUGIN_IS_BRIDGE;
  262. }
  263. ~BridgePlugin() override
  264. {
  265. carla_debug("BridgePlugin::~BridgePlugin()");
  266. // close UI
  267. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  268. pData->transientTryCounter = 0;
  269. pData->singleMutex.lock();
  270. pData->masterMutex.lock();
  271. if (pData->client != nullptr && pData->client->isActive())
  272. pData->client->deactivate();
  273. if (pData->active)
  274. {
  275. deactivate();
  276. pData->active = false;
  277. }
  278. if (pData->osc.thread.isThreadRunning())
  279. {
  280. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtQuit);
  281. fShmNonRtControl.commitWrite();
  282. if (! fTimedOut)
  283. fShmRtControl.waitForServer(3);
  284. }
  285. pData->osc.data.clear();
  286. pData->osc.thread.stopThread(3000);
  287. if (fNeedsSemDestroy)
  288. {
  289. jackbridge_sem_destroy(&fShmRtControl.data->sem.server);
  290. jackbridge_sem_destroy(&fShmRtControl.data->sem.client);
  291. }
  292. fShmAudioPool.clear();
  293. fShmRtControl.clear();
  294. fShmNonRtControl.clear();
  295. clearBuffers();
  296. fInfo.chunk.clear();
  297. }
  298. // -------------------------------------------------------------------
  299. // Information (base)
  300. BinaryType getBinaryType() const noexcept
  301. {
  302. return fBinaryType;
  303. }
  304. PluginType getType() const noexcept override
  305. {
  306. return fPluginType;
  307. }
  308. PluginCategory getCategory() const noexcept override
  309. {
  310. return fInfo.category;
  311. }
  312. int64_t getUniqueId() const noexcept override
  313. {
  314. return fInfo.uniqueId;
  315. }
  316. // -------------------------------------------------------------------
  317. // Information (count)
  318. uint32_t getMidiInCount() const noexcept override
  319. {
  320. return fInfo.mIns;
  321. }
  322. uint32_t getMidiOutCount() const noexcept override
  323. {
  324. return fInfo.mOuts;
  325. }
  326. // -------------------------------------------------------------------
  327. // Information (current data)
  328. std::size_t getChunkData(void** const dataPtr) noexcept override
  329. {
  330. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  331. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  332. CARLA_SAFE_ASSERT_RETURN(fInfo.chunk.size() > 0, 0);
  333. *dataPtr = fInfo.chunk.data();
  334. return fInfo.chunk.size();
  335. }
  336. // -------------------------------------------------------------------
  337. // Information (per-plugin data)
  338. uint getOptionsAvailable() const noexcept override
  339. {
  340. return fInfo.optionsAvailable;
  341. }
  342. float getParameterValue(const uint32_t parameterId) const noexcept override
  343. {
  344. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  345. return fParams[parameterId].value;
  346. }
  347. void getLabel(char* const strBuf) const noexcept override
  348. {
  349. std::strncpy(strBuf, fInfo.label, STR_MAX);
  350. }
  351. void getMaker(char* const strBuf) const noexcept override
  352. {
  353. std::strncpy(strBuf, fInfo.maker, STR_MAX);
  354. }
  355. void getCopyright(char* const strBuf) const noexcept override
  356. {
  357. std::strncpy(strBuf, fInfo.copyright, STR_MAX);
  358. }
  359. void getRealName(char* const strBuf) const noexcept override
  360. {
  361. std::strncpy(strBuf, fInfo.name, STR_MAX);
  362. }
  363. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  364. {
  365. CARLA_ASSERT(parameterId < pData->param.count);
  366. std::strncpy(strBuf, fParams[parameterId].name.buffer(), STR_MAX);
  367. }
  368. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  369. {
  370. CARLA_ASSERT(parameterId < pData->param.count);
  371. std::strncpy(strBuf, fParams[parameterId].unit.buffer(), STR_MAX);
  372. }
  373. // -------------------------------------------------------------------
  374. // Set data (state)
  375. void prepareForSave() override
  376. {
  377. fSaved = false;
  378. {
  379. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  380. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPrepareForSave);
  381. fShmNonRtControl.commitWrite();
  382. }
  383. carla_stdout("BridgePlugin::prepareForSave() - sent, now waiting...");
  384. for (int i=0; i < 200; ++i)
  385. {
  386. if (fSaved)
  387. break;
  388. carla_msleep(30);
  389. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  390. pData->engine->idle();
  391. }
  392. if (! fSaved)
  393. carla_stderr("BridgePlugin::prepareForSave() - Timeout while requesting save state");
  394. else
  395. carla_stdout("BridgePlugin::prepareForSave() - success!");
  396. }
  397. // -------------------------------------------------------------------
  398. // Set data (internal stuff)
  399. // nothing
  400. // -------------------------------------------------------------------
  401. // Set data (plugin-specific stuff)
  402. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  403. {
  404. CARLA_ASSERT(parameterId < pData->param.count);
  405. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  406. fParams[parameterId].value = fixedValue;
  407. {
  408. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  409. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
  410. fShmNonRtControl.writeInt(static_cast<int32_t>(parameterId));
  411. fShmNonRtControl.writeFloat(value);
  412. fShmNonRtControl.commitWrite();
  413. }
  414. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  415. }
  416. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  417. {
  418. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  419. {
  420. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  421. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetProgram);
  422. fShmNonRtControl.writeInt(index);
  423. fShmNonRtControl.commitWrite();
  424. }
  425. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  426. }
  427. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  428. {
  429. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  430. {
  431. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  432. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetMidiProgram);
  433. fShmNonRtControl.writeInt(index);
  434. fShmNonRtControl.commitWrite();
  435. }
  436. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  437. }
  438. #if 0
  439. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  440. {
  441. CARLA_ASSERT(type);
  442. CARLA_ASSERT(key);
  443. CARLA_ASSERT(value);
  444. if (sendGui)
  445. {
  446. // TODO - if type is chunk|binary, store it in a file and send path instead
  447. QString cData;
  448. cData = type;
  449. cData += "·";
  450. cData += key;
  451. cData += "·";
  452. cData += value;
  453. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CUSTOM, cData.toUtf8().constData());
  454. }
  455. CarlaPlugin::setCustomData(type, key, value, sendGui);
  456. }
  457. #endif
  458. void setChunkData(const char* const stringData) override
  459. {
  460. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  461. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr,);
  462. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  463. filePath += OS_SEP_STR;
  464. filePath += ".CarlaChunk_";
  465. filePath += fShmAudioPool.filename.buffer() + 18;
  466. if (File(filePath).replaceWithText(stringData))
  467. {
  468. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  469. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetChunkDataFile);
  470. fShmNonRtControl.writeInt(filePath.length());
  471. fShmNonRtControl.writeCustomData(filePath.toRawUTF8(), static_cast<uint32_t>(filePath.length()));
  472. fShmNonRtControl.commitWrite();
  473. }
  474. }
  475. // -------------------------------------------------------------------
  476. // Set ui stuff
  477. void showCustomUI(const bool yesNo) override
  478. {
  479. {
  480. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  481. fShmNonRtControl.writeOpcode(yesNo ? kPluginBridgeNonRtShowUI : kPluginBridgeNonRtHideUI);
  482. fShmNonRtControl.commitWrite();
  483. }
  484. if (yesNo)
  485. {
  486. pData->tryTransient();
  487. }
  488. else
  489. {
  490. pData->transientTryCounter = 0;
  491. }
  492. }
  493. void idle() override
  494. {
  495. if (pData->osc.thread.isThreadRunning())
  496. {
  497. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  498. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPing);
  499. fShmNonRtControl.commitWrite();
  500. }
  501. else
  502. carla_stderr2("TESTING: Bridge has closed!");
  503. CarlaPlugin::idle();
  504. }
  505. // -------------------------------------------------------------------
  506. // Plugin state
  507. void reload() override
  508. {
  509. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  510. carla_debug("BridgePlugin::reload() - start");
  511. const EngineProcessMode processMode(pData->engine->getProccessMode());
  512. // Safely disable plugin for reload
  513. const ScopedDisabler sd(this);
  514. bool needsCtrlIn, needsCtrlOut;
  515. needsCtrlIn = needsCtrlOut = false;
  516. if (fInfo.aIns > 0)
  517. {
  518. pData->audioIn.createNew(fInfo.aIns);
  519. }
  520. if (fInfo.aOuts > 0)
  521. {
  522. pData->audioOut.createNew(fInfo.aOuts);
  523. needsCtrlIn = true;
  524. }
  525. if (fInfo.mIns > 0)
  526. needsCtrlIn = true;
  527. if (fInfo.mOuts > 0)
  528. needsCtrlOut = true;
  529. const uint portNameSize(pData->engine->getMaxPortNameSize());
  530. CarlaString portName;
  531. // Audio Ins
  532. for (uint32_t j=0; j < fInfo.aIns; ++j)
  533. {
  534. portName.clear();
  535. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  536. {
  537. portName = pData->name;
  538. portName += ":";
  539. }
  540. if (fInfo.aIns > 1)
  541. {
  542. portName += "input_";
  543. portName += CarlaString(j+1);
  544. }
  545. else
  546. portName += "input";
  547. portName.truncate(portNameSize);
  548. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  549. pData->audioIn.ports[j].rindex = j;
  550. }
  551. // Audio Outs
  552. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  553. {
  554. portName.clear();
  555. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  556. {
  557. portName = pData->name;
  558. portName += ":";
  559. }
  560. if (fInfo.aOuts > 1)
  561. {
  562. portName += "output_";
  563. portName += CarlaString(j+1);
  564. }
  565. else
  566. portName += "output";
  567. portName.truncate(portNameSize);
  568. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  569. pData->audioOut.ports[j].rindex = j;
  570. }
  571. if (needsCtrlIn)
  572. {
  573. portName.clear();
  574. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  575. {
  576. portName = pData->name;
  577. portName += ":";
  578. }
  579. portName += "event-in";
  580. portName.truncate(portNameSize);
  581. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  582. }
  583. if (needsCtrlOut)
  584. {
  585. portName.clear();
  586. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  587. {
  588. portName = pData->name;
  589. portName += ":";
  590. }
  591. portName += "event-out";
  592. portName.truncate(portNameSize);
  593. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  594. }
  595. // extra plugin hints
  596. pData->extraHints = 0x0;
  597. if (fInfo.mIns > 0)
  598. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  599. if (fInfo.mOuts > 0)
  600. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  601. if (fInfo.aIns <= 2 && fInfo.aOuts <= 2 && (fInfo.aIns == fInfo.aOuts || fInfo.aIns == 0 || fInfo.aOuts == 0))
  602. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  603. bufferSizeChanged(pData->engine->getBufferSize());
  604. reloadPrograms(true);
  605. carla_debug("BridgePlugin::reload() - end");
  606. }
  607. // -------------------------------------------------------------------
  608. // Plugin processing
  609. void activate() noexcept override
  610. {
  611. {
  612. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  613. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtActivate);
  614. fShmNonRtControl.commitWrite();
  615. }
  616. bool timedOut = true;
  617. try {
  618. timedOut = waitForServer();
  619. } catch(...) {}
  620. if (! timedOut)
  621. fTimedOut = false;
  622. }
  623. void deactivate() noexcept override
  624. {
  625. {
  626. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  627. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtDeactivate);
  628. fShmNonRtControl.commitWrite();
  629. }
  630. bool timedOut = true;
  631. try {
  632. timedOut = waitForServer();
  633. } catch(...) {}
  634. if (! timedOut)
  635. fTimedOut = false;
  636. }
  637. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  638. {
  639. // --------------------------------------------------------------------------------------------------------
  640. // Check if active
  641. if (fTimedOut || ! pData->active)
  642. {
  643. // disable any output sound
  644. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  645. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  646. return;
  647. }
  648. // --------------------------------------------------------------------------------------------------------
  649. // Check if needs reset
  650. if (pData->needsReset)
  651. {
  652. // TODO
  653. pData->needsReset = false;
  654. }
  655. // --------------------------------------------------------------------------------------------------------
  656. // Event Input
  657. if (pData->event.portIn != nullptr)
  658. {
  659. // ----------------------------------------------------------------------------------------------------
  660. // MIDI Input (External)
  661. if (pData->extNotes.mutex.tryLock())
  662. {
  663. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  664. {
  665. const ExternalMidiNote& note(it.getValue());
  666. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  667. uint8_t data1, data2, data3;
  668. data1 = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  669. data2 = note.note;
  670. data3 = note.velo;
  671. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  672. fShmRtControl.writeInt(0);
  673. fShmRtControl.writeInt(3);
  674. fShmRtControl.writeByte(data1);
  675. fShmRtControl.writeByte(data2);
  676. fShmRtControl.writeByte(data3);
  677. fShmRtControl.commitWrite();
  678. }
  679. pData->extNotes.data.clear();
  680. pData->extNotes.mutex.unlock();
  681. } // End of MIDI Input (External)
  682. // ----------------------------------------------------------------------------------------------------
  683. // Event Input (System)
  684. bool allNotesOffSent = false;
  685. uint32_t numEvents = pData->event.portIn->getEventCount();
  686. uint32_t nextBankId;
  687. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  688. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  689. else
  690. nextBankId = 0;
  691. for (uint32_t i=0; i < numEvents; ++i)
  692. {
  693. const EngineEvent& event(pData->event.portIn->getEvent(i));
  694. // Control change
  695. switch (event.type)
  696. {
  697. case kEngineEventTypeNull:
  698. break;
  699. case kEngineEventTypeControl: {
  700. const EngineControlEvent& ctrlEvent = event.ctrl;
  701. switch (ctrlEvent.type)
  702. {
  703. case kEngineControlEventTypeNull:
  704. break;
  705. case kEngineControlEventTypeParameter:
  706. {
  707. // Control backend stuff
  708. if (event.channel == pData->ctrlChannel)
  709. {
  710. float value;
  711. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  712. {
  713. value = ctrlEvent.value;
  714. setDryWet(value, false, false);
  715. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  716. break;
  717. }
  718. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  719. {
  720. value = ctrlEvent.value*127.0f/100.0f;
  721. setVolume(value, false, false);
  722. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  723. break;
  724. }
  725. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  726. {
  727. float left, right;
  728. value = ctrlEvent.value/0.5f - 1.0f;
  729. if (value < 0.0f)
  730. {
  731. left = -1.0f;
  732. right = (value*2.0f)+1.0f;
  733. }
  734. else if (value > 0.0f)
  735. {
  736. left = (value*2.0f)-1.0f;
  737. right = 1.0f;
  738. }
  739. else
  740. {
  741. left = -1.0f;
  742. right = 1.0f;
  743. }
  744. setBalanceLeft(left, false, false);
  745. setBalanceRight(right, false, false);
  746. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  747. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  748. break;
  749. }
  750. }
  751. // Control plugin parameters
  752. uint32_t k;
  753. for (k=0; k < pData->param.count; ++k)
  754. {
  755. if (pData->param.data[k].midiChannel != event.channel)
  756. continue;
  757. if (pData->param.data[k].midiCC != ctrlEvent.param)
  758. continue;
  759. if (pData->param.data[k].type != PARAMETER_INPUT)
  760. continue;
  761. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  762. continue;
  763. float value;
  764. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  765. {
  766. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  767. }
  768. else
  769. {
  770. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  771. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  772. value = std::rint(value);
  773. }
  774. fShmRtControl.writeOpcode(kPluginBridgeRtSetParameter);
  775. fShmRtControl.writeInt(static_cast<int32_t>(k));
  776. fShmRtControl.writeFloat(value);
  777. fShmRtControl.commitWrite();
  778. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  779. break;
  780. }
  781. // check if event is already handled
  782. if (k != pData->param.count)
  783. break;
  784. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  785. {
  786. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  787. fShmRtControl.writeInt(static_cast<int32_t>(event.time));
  788. fShmRtControl.writeInt(3);
  789. fShmRtControl.writeByte(static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel));
  790. fShmRtControl.writeByte(static_cast<uint8_t>(ctrlEvent.param));
  791. fShmRtControl.writeByte(static_cast<uint8_t>(ctrlEvent.value*127.0f));
  792. fShmRtControl.commitWrite();
  793. }
  794. break;
  795. } // case kEngineControlEventTypeParameter
  796. case kEngineControlEventTypeMidiBank:
  797. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  798. nextBankId = ctrlEvent.param;
  799. break;
  800. case kEngineControlEventTypeMidiProgram:
  801. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  802. {
  803. const uint32_t nextProgramId(ctrlEvent.param);
  804. if (pData->midiprog.count > 0)
  805. {
  806. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  807. {
  808. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  809. {
  810. const int32_t index(static_cast<int32_t>(k));
  811. fShmRtControl.writeOpcode(kPluginBridgeRtSetMidiProgram);
  812. fShmRtControl.writeInt(index);
  813. fShmRtControl.commitWrite();
  814. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  815. break;
  816. }
  817. }
  818. }
  819. else
  820. {
  821. }
  822. }
  823. break;
  824. case kEngineControlEventTypeAllSoundOff:
  825. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  826. {
  827. fShmRtControl.writeOpcode(kPluginBridgeRtAllSoundOff);
  828. fShmRtControl.writeInt(static_cast<int32_t>(event.time));
  829. fShmRtControl.commitWrite();
  830. }
  831. break;
  832. case kEngineControlEventTypeAllNotesOff:
  833. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  834. {
  835. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  836. {
  837. allNotesOffSent = true;
  838. sendMidiAllNotesOffToCallback();
  839. }
  840. fShmRtControl.writeOpcode(kPluginBridgeRtAllNotesOff);
  841. fShmRtControl.writeInt(static_cast<int32_t>(event.time));
  842. fShmRtControl.commitWrite();
  843. }
  844. break;
  845. } // switch (ctrlEvent.type)
  846. break;
  847. } // case kEngineEventTypeControl
  848. case kEngineEventTypeMidi:
  849. {
  850. const EngineMidiEvent& midiEvent(event.midi);
  851. if (midiEvent.size == 0 || midiEvent.size > 4)
  852. continue;
  853. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  854. uint8_t channel = event.channel;
  855. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  856. status = MIDI_STATUS_NOTE_OFF;
  857. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  858. continue;
  859. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  860. continue;
  861. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  862. continue;
  863. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  864. continue;
  865. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  866. fShmRtControl.writeInt(static_cast<int32_t>(event.time));
  867. fShmRtControl.writeInt(midiEvent.size);
  868. fShmRtControl.writeByte(static_cast<uint8_t>(status + channel));
  869. for (uint8_t j=1; j < midiEvent.size; ++j)
  870. fShmRtControl.writeByte(midiEvent.data[j]);
  871. fShmRtControl.commitWrite();
  872. if (status == MIDI_STATUS_NOTE_ON)
  873. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  874. else if (status == MIDI_STATUS_NOTE_OFF)
  875. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  876. break;
  877. }
  878. }
  879. }
  880. pData->postRtEvents.trySplice();
  881. } // End of Event Input
  882. processSingle(inBuffer, outBuffer, frames);
  883. }
  884. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  885. {
  886. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  887. if (pData->audioIn.count > 0)
  888. {
  889. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  890. }
  891. if (pData->audioOut.count > 0)
  892. {
  893. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  894. }
  895. // --------------------------------------------------------------------------------------------------------
  896. // Try lock, silence otherwise
  897. if (pData->engine->isOffline())
  898. {
  899. pData->singleMutex.lock();
  900. }
  901. else if (! pData->singleMutex.tryLock())
  902. {
  903. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  904. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  905. return false;
  906. }
  907. // --------------------------------------------------------------------------------------------------------
  908. // Reset audio buffers
  909. //std::memset(fShmAudioPool.data, 0, fShmAudioPool.size);
  910. for (uint32_t i=0; i < fInfo.aIns; ++i)
  911. FloatVectorOperations::copy(fShmAudioPool.data + (i * frames), inBuffer[i], static_cast<int>(frames));
  912. // --------------------------------------------------------------------------------------------------------
  913. // TimeInfo
  914. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  915. BridgeTimeInfo& bridgeTimeInfo(fShmRtControl.data->timeInfo);
  916. bridgeTimeInfo.playing = timeInfo.playing;
  917. bridgeTimeInfo.frame = timeInfo.frame;
  918. bridgeTimeInfo.usecs = timeInfo.usecs;
  919. bridgeTimeInfo.valid = timeInfo.valid;
  920. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  921. {
  922. bridgeTimeInfo.bar = timeInfo.bbt.bar;
  923. bridgeTimeInfo.beat = timeInfo.bbt.beat;
  924. bridgeTimeInfo.tick = timeInfo.bbt.tick;
  925. bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
  926. bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
  927. bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  928. bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  929. bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
  930. }
  931. // --------------------------------------------------------------------------------------------------------
  932. // Run plugin
  933. {
  934. fShmRtControl.writeOpcode(kPluginBridgeRtProcess);
  935. fShmRtControl.commitWrite();
  936. }
  937. if (! waitForServer(2))
  938. {
  939. pData->singleMutex.unlock();
  940. return true;
  941. }
  942. for (uint32_t i=0; i < fInfo.aOuts; ++i)
  943. FloatVectorOperations::copy(outBuffer[i], fShmAudioPool.data + ((i + fInfo.aIns) * frames), static_cast<int>(frames));
  944. // --------------------------------------------------------------------------------------------------------
  945. // Post-processing (dry/wet, volume and balance)
  946. {
  947. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && pData->postProc.volume != 1.0f;
  948. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  949. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  950. bool isPair;
  951. float bufValue, oldBufLeft[doBalance ? frames : 1];
  952. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  953. {
  954. // Dry/Wet
  955. if (doDryWet)
  956. {
  957. for (uint32_t k=0; k < frames; ++k)
  958. {
  959. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k];
  960. outBuffer[i][k] = (outBuffer[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  961. }
  962. }
  963. // Balance
  964. if (doBalance)
  965. {
  966. isPair = (i % 2 == 0);
  967. if (isPair)
  968. {
  969. CARLA_ASSERT(i+1 < pData->audioOut.count);
  970. FloatVectorOperations::copy(oldBufLeft, outBuffer[i], static_cast<int>(frames));
  971. }
  972. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  973. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  974. for (uint32_t k=0; k < frames; ++k)
  975. {
  976. if (isPair)
  977. {
  978. // left
  979. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  980. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  981. }
  982. else
  983. {
  984. // right
  985. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  986. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  987. }
  988. }
  989. }
  990. // Volume (and buffer copy)
  991. if (doVolume)
  992. {
  993. for (uint32_t k=0; k < frames; ++k)
  994. outBuffer[i][k] *= pData->postProc.volume;
  995. }
  996. }
  997. } // End of Post-processing
  998. // --------------------------------------------------------------------------------------------------------
  999. pData->singleMutex.unlock();
  1000. return true;
  1001. }
  1002. void bufferSizeChanged(const uint32_t newBufferSize) override
  1003. {
  1004. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1005. resizeAudioPool(newBufferSize);
  1006. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
  1007. fShmNonRtControl.writeInt(static_cast<int32_t>(newBufferSize));
  1008. fShmNonRtControl.commitWrite();
  1009. }
  1010. void sampleRateChanged(const double newSampleRate) override
  1011. {
  1012. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1013. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
  1014. fShmNonRtControl.writeDouble(newSampleRate);
  1015. fShmNonRtControl.commitWrite();
  1016. }
  1017. // -------------------------------------------------------------------
  1018. // Plugin buffers
  1019. void clearBuffers() noexcept override
  1020. {
  1021. if (fParams != nullptr)
  1022. {
  1023. delete[] fParams;
  1024. fParams = nullptr;
  1025. }
  1026. CarlaPlugin::clearBuffers();
  1027. }
  1028. // -------------------------------------------------------------------
  1029. // Post-poned UI Stuff
  1030. // nothing
  1031. // -------------------------------------------------------------------
  1032. int setOscPluginBridgeInfo(const PluginBridgeOscInfoType infoType, const int argc, const lo_arg* const* const argv, const char* const types)
  1033. {
  1034. #ifdef DEBUG
  1035. if (infoType != kPluginBridgeOscPong) {
  1036. carla_debug("BridgePlugin::setOscPluginBridgeInfo(%s, %i, %p, \"%s\")", PluginBridgeOscInfoType2str(infoType), argc, argv, types);
  1037. }
  1038. #endif
  1039. switch (infoType)
  1040. {
  1041. case kPluginBridgeOscNull:
  1042. break;
  1043. case kPluginBridgeOscPong:
  1044. if (fLastPongCounter > 0)
  1045. fLastPongCounter = 0;
  1046. break;
  1047. case kPluginBridgeOscPluginInfo1: {
  1048. CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiih");
  1049. const int32_t category = argv[0]->i;
  1050. const int32_t hints = argv[1]->i;
  1051. const int32_t optionAv = argv[2]->i;
  1052. const int32_t optionEn = argv[3]->i;
  1053. const int64_t uniqueId = argv[4]->h;
  1054. CARLA_SAFE_ASSERT_BREAK(category >= 0);
  1055. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1056. CARLA_SAFE_ASSERT_BREAK(optionAv >= 0);
  1057. CARLA_SAFE_ASSERT_BREAK(optionEn >= 0);
  1058. pData->hints = static_cast<uint>(hints);
  1059. pData->hints |= PLUGIN_IS_BRIDGE;
  1060. pData->options = static_cast<uint>(optionEn);
  1061. fInfo.category = static_cast<PluginCategory>(category);
  1062. fInfo.uniqueId = uniqueId;
  1063. fInfo.optionsAvailable = static_cast<uint>(optionAv);
  1064. break;
  1065. }
  1066. case kPluginBridgeOscPluginInfo2: {
  1067. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ssss");
  1068. const char* const realName = (const char*)&argv[0]->s;
  1069. const char* const label = (const char*)&argv[1]->s;
  1070. const char* const maker = (const char*)&argv[2]->s;
  1071. const char* const copyright = (const char*)&argv[3]->s;
  1072. CARLA_SAFE_ASSERT_BREAK(realName != nullptr);
  1073. CARLA_SAFE_ASSERT_BREAK(label != nullptr);
  1074. CARLA_SAFE_ASSERT_BREAK(maker != nullptr);
  1075. CARLA_SAFE_ASSERT_BREAK(copyright != nullptr);
  1076. fInfo.name = realName;
  1077. fInfo.label = label;
  1078. fInfo.maker = maker;
  1079. fInfo.copyright = copyright;
  1080. if (pData->name == nullptr)
  1081. pData->name = pData->engine->getUniquePluginName(realName);
  1082. break;
  1083. }
  1084. case kPluginBridgeOscAudioCount: {
  1085. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1086. const int32_t ins = argv[0]->i;
  1087. const int32_t outs = argv[1]->i;
  1088. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1089. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1090. fInfo.aIns = static_cast<uint32_t>(ins);
  1091. fInfo.aOuts = static_cast<uint32_t>(outs);
  1092. break;
  1093. }
  1094. case kPluginBridgeOscMidiCount: {
  1095. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1096. const int32_t ins = argv[0]->i;
  1097. const int32_t outs = argv[1]->i;
  1098. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1099. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1100. fInfo.mIns = static_cast<uint32_t>(ins);
  1101. fInfo.mOuts = static_cast<uint32_t>(outs);
  1102. break;
  1103. }
  1104. case kPluginBridgeOscParameterCount: {
  1105. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1106. const int32_t ins = argv[0]->i;
  1107. const int32_t outs = argv[1]->i;
  1108. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1109. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1110. // delete old data
  1111. pData->param.clear();
  1112. if (fParams != nullptr)
  1113. {
  1114. delete[] fParams;
  1115. fParams = nullptr;
  1116. }
  1117. if (int32_t count = ins+outs)
  1118. {
  1119. const int32_t maxParams(static_cast<int32_t>(pData->engine->getOptions().maxParameters));
  1120. if (count > maxParams)
  1121. {
  1122. count = maxParams;
  1123. carla_safe_assert_int2("count <= pData->engine->getOptions().maxParameters", __FILE__, __LINE__, count, maxParams);
  1124. }
  1125. const uint32_t ucount(static_cast<uint32_t>(count));
  1126. pData->param.createNew(ucount, false);
  1127. fParams = new BridgeParamInfo[ucount];
  1128. }
  1129. break;
  1130. }
  1131. case kPluginBridgeOscProgramCount: {
  1132. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1133. const int32_t count = argv[0]->i;
  1134. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1135. pData->prog.clear();
  1136. if (count > 0)
  1137. pData->prog.createNew(static_cast<uint32_t>(count));
  1138. break;
  1139. }
  1140. case kPluginBridgeOscMidiProgramCount: {
  1141. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1142. const int32_t count = argv[0]->i;
  1143. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1144. pData->midiprog.clear();
  1145. if (count > 0)
  1146. pData->midiprog.createNew(static_cast<uint32_t>(count));
  1147. break;
  1148. }
  1149. case kPluginBridgeOscParameterData1: {
  1150. CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiii");
  1151. const int32_t index = argv[0]->i;
  1152. const int32_t rindex = argv[1]->i;
  1153. const int32_t type = argv[2]->i;
  1154. const int32_t hints = argv[3]->i;
  1155. const int32_t midiCC = argv[4]->i;
  1156. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1157. CARLA_SAFE_ASSERT_BREAK(rindex >= 0);
  1158. CARLA_SAFE_ASSERT_BREAK(type >= 0);
  1159. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1160. CARLA_SAFE_ASSERT_BREAK(midiCC >= -1 && midiCC < 0x5F);
  1161. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1162. if (index < static_cast<int32_t>(pData->param.count))
  1163. {
  1164. pData->param.data[index].type = static_cast<ParameterType>(type);
  1165. pData->param.data[index].index = index;
  1166. pData->param.data[index].rindex = rindex;
  1167. pData->param.data[index].hints = static_cast<uint>(hints);
  1168. pData->param.data[index].midiCC = static_cast<int16_t>(midiCC);
  1169. }
  1170. break;
  1171. }
  1172. case kPluginBridgeOscParameterData2: {
  1173. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iss");
  1174. const int32_t index = argv[0]->i;
  1175. const char* const name = (const char*)&argv[1]->s;
  1176. const char* const unit = (const char*)&argv[2]->s;
  1177. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1178. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1179. CARLA_SAFE_ASSERT_BREAK(unit != nullptr);
  1180. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1181. if (index < static_cast<int32_t>(pData->param.count))
  1182. {
  1183. fParams[index].name = name;
  1184. fParams[index].unit = unit;
  1185. }
  1186. break;
  1187. }
  1188. case kPluginBridgeOscParameterRanges1: {
  1189. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1190. const int32_t index = argv[0]->i;
  1191. const float def = argv[1]->f;
  1192. const float min = argv[2]->f;
  1193. const float max = argv[3]->f;
  1194. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1195. CARLA_SAFE_ASSERT_BREAK(min < max);
  1196. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1197. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1198. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1199. if (index < static_cast<int32_t>(pData->param.count))
  1200. {
  1201. pData->param.ranges[index].def = def;
  1202. pData->param.ranges[index].min = min;
  1203. pData->param.ranges[index].max = max;
  1204. }
  1205. break;
  1206. }
  1207. case kPluginBridgeOscParameterRanges2: {
  1208. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1209. const int32_t index = argv[0]->i;
  1210. const float step = argv[1]->f;
  1211. const float stepSmall = argv[2]->f;
  1212. const float stepLarge = argv[3]->f;
  1213. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1214. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1215. if (index < static_cast<int32_t>(pData->param.count))
  1216. {
  1217. pData->param.ranges[index].step = step;
  1218. pData->param.ranges[index].stepSmall = stepSmall;
  1219. pData->param.ranges[index].stepLarge = stepLarge;
  1220. }
  1221. break;
  1222. }
  1223. case kPluginBridgeOscParameterValue: {
  1224. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1225. const int32_t index = argv[0]->i;
  1226. const float value = argv[1]->f;
  1227. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1228. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1229. if (index < static_cast<int32_t>(pData->param.count))
  1230. {
  1231. const uint32_t uindex(static_cast<uint32_t>(index));
  1232. const float fixedValue(pData->param.getFixedValue(uindex, value));
  1233. fParams[uindex].value = fixedValue;
  1234. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1235. }
  1236. break;
  1237. }
  1238. case kPluginBridgeOscDefaultValue: {
  1239. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1240. const int32_t index = argv[0]->i;
  1241. const float value = argv[1]->f;
  1242. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1243. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1244. if (index < static_cast<int32_t>(pData->param.count))
  1245. pData->param.ranges[index].def = value;
  1246. break;
  1247. }
  1248. case kPluginBridgeOscCurrentProgram: {
  1249. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1250. const int32_t index = argv[0]->i;
  1251. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1252. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1253. CarlaPlugin::setProgram(index, false, true, true);
  1254. break;
  1255. }
  1256. case kPluginBridgeOscCurrentMidiProgram: {
  1257. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1258. const int32_t index = argv[0]->i;
  1259. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1260. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1261. CarlaPlugin::setMidiProgram(index, false, true, true);
  1262. break;
  1263. }
  1264. case kPluginBridgeOscProgramName: {
  1265. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "is");
  1266. const int32_t index = argv[0]->i;
  1267. const char* const name = (const char*)&argv[1]->s;
  1268. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1269. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1270. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1271. if (index < static_cast<int32_t>(pData->prog.count))
  1272. {
  1273. if (pData->prog.names[index] != nullptr)
  1274. delete[] pData->prog.names[index];
  1275. pData->prog.names[index] = carla_strdup(name);
  1276. }
  1277. break;
  1278. }
  1279. case kPluginBridgeOscMidiProgramData: {
  1280. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "iiis");
  1281. const int32_t index = argv[0]->i;
  1282. const int32_t bank = argv[1]->i;
  1283. const int32_t program = argv[2]->i;
  1284. const char* const name = (const char*)&argv[3]->s;
  1285. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1286. CARLA_SAFE_ASSERT_BREAK(bank >= 0);
  1287. CARLA_SAFE_ASSERT_BREAK(program >= 0);
  1288. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1289. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1290. if (index < static_cast<int32_t>(pData->midiprog.count))
  1291. {
  1292. if (pData->midiprog.data[index].name != nullptr)
  1293. delete[] pData->midiprog.data[index].name;
  1294. pData->midiprog.data[index].bank = static_cast<uint32_t>(bank);
  1295. pData->midiprog.data[index].program = static_cast<uint32_t>(program);
  1296. pData->midiprog.data[index].name = carla_strdup(name);
  1297. }
  1298. break;
  1299. }
  1300. case kPluginBridgeOscConfigure: {
  1301. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ss");
  1302. const char* const key = (const char*)&argv[0]->s;
  1303. const char* const value = (const char*)&argv[1]->s;
  1304. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1305. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1306. if (std::strcmp(key, CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  1307. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1308. else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVED) == 0)
  1309. fSaved = true;
  1310. break;
  1311. }
  1312. case kPluginBridgeOscSetCustomData: {
  1313. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "sss");
  1314. const char* const type = (const char*)&argv[0]->s;
  1315. const char* const key = (const char*)&argv[1]->s;
  1316. const char* const value = (const char*)&argv[2]->s;
  1317. CARLA_SAFE_ASSERT_BREAK(type != nullptr);
  1318. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1319. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1320. CarlaPlugin::setCustomData(type, key, value, false);
  1321. break;
  1322. }
  1323. case kPluginBridgeOscSetChunkDataFile: {
  1324. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1325. const char* const chunkFilePath = (const char*)&argv[0]->s;
  1326. CARLA_SAFE_ASSERT_BREAK(chunkFilePath != nullptr);
  1327. String realChunkFilePath(chunkFilePath);
  1328. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1329. #ifndef CARLA_OS_WIN
  1330. // Using Wine, fix temp dir
  1331. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1332. {
  1333. // Get WINEPREFIX
  1334. String wineDir;
  1335. if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
  1336. wineDir = String(WINEPREFIX);
  1337. else
  1338. wineDir = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  1339. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1340. realChunkFilePath = wineDir;
  1341. realChunkFilePath += "/drive_";
  1342. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1343. realChunkFilePath += "/";
  1344. realChunkFilePath += driveLetterSplit[1];
  1345. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1346. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1347. }
  1348. #endif
  1349. File chunkFile(realChunkFilePath);
  1350. if (chunkFile.existsAsFile())
  1351. {
  1352. fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
  1353. chunkFile.deleteFile();
  1354. }
  1355. break;
  1356. }
  1357. case kPluginBridgeOscLatency:
  1358. // TODO
  1359. break;
  1360. case kPluginBridgeOscReady:
  1361. fInitiated = true;
  1362. break;
  1363. case kPluginBridgeOscError: {
  1364. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1365. const char* const error = (const char*)&argv[0]->s;
  1366. CARLA_ASSERT(error != nullptr);
  1367. pData->engine->setLastError(error);
  1368. fInitError = true;
  1369. fInitiated = true;
  1370. break;
  1371. }
  1372. }
  1373. return 0;
  1374. }
  1375. // -------------------------------------------------------------------
  1376. const void* getExtraStuff() const noexcept override
  1377. {
  1378. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  1379. }
  1380. bool init(const char* const filename, const char* const name, const char* const label, const char* const bridgeBinary)
  1381. {
  1382. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1383. // ---------------------------------------------------------------
  1384. // first checks
  1385. if (pData->client != nullptr)
  1386. {
  1387. pData->engine->setLastError("Plugin client is already registered");
  1388. return false;
  1389. }
  1390. // ---------------------------------------------------------------
  1391. // set info
  1392. if (name != nullptr && name[0] != '\0')
  1393. pData->name = pData->engine->getUniquePluginName(name);
  1394. pData->filename = carla_strdup(filename);
  1395. if (bridgeBinary != nullptr)
  1396. fBridgeBinary = bridgeBinary;
  1397. std::srand(static_cast<uint>(std::time(nullptr)));
  1398. // ---------------------------------------------------------------
  1399. // SHM Audio Pool
  1400. {
  1401. char tmpFileBase[64];
  1402. std::sprintf(tmpFileBase, "/carla-bridge_shm_ap_XXXXXX");
  1403. fShmAudioPool.shm = shm_mkstemp(tmpFileBase);
  1404. if (! carla_is_shm_valid(fShmAudioPool.shm))
  1405. {
  1406. carla_stdout("Failed to open or create shared memory file #1");
  1407. return false;
  1408. }
  1409. fShmAudioPool.filename = tmpFileBase;
  1410. }
  1411. // ---------------------------------------------------------------
  1412. // SHM RT Control
  1413. {
  1414. char tmpFileBase[64];
  1415. std::sprintf(tmpFileBase, "/carla-bridge_shm_rt_XXXXXX");
  1416. fShmRtControl.shm = shm_mkstemp(tmpFileBase);
  1417. if (! carla_is_shm_valid(fShmRtControl.shm))
  1418. {
  1419. carla_stdout("Failed to open or create shared memory file #2");
  1420. // clear
  1421. carla_shm_close(fShmAudioPool.shm);
  1422. return false;
  1423. }
  1424. fShmRtControl.filename = tmpFileBase;
  1425. if (! fShmRtControl.mapData())
  1426. {
  1427. carla_stdout("Failed to map shared memory file #2");
  1428. // clear
  1429. carla_shm_close(fShmRtControl.shm);
  1430. carla_shm_close(fShmAudioPool.shm);
  1431. return false;
  1432. }
  1433. CARLA_SAFE_ASSERT(fShmRtControl.data != nullptr);
  1434. if (! jackbridge_sem_init(&fShmRtControl.data->sem.server))
  1435. {
  1436. carla_stdout("Failed to initialize shared memory semaphore #1");
  1437. // clear
  1438. fShmRtControl.unmapData();
  1439. carla_shm_close(fShmRtControl.shm);
  1440. carla_shm_close(fShmAudioPool.shm);
  1441. return false;
  1442. }
  1443. if (! jackbridge_sem_init(&fShmRtControl.data->sem.client))
  1444. {
  1445. carla_stdout("Failed to initialize shared memory semaphore #2");
  1446. // clear
  1447. jackbridge_sem_destroy(&fShmRtControl.data->sem.server);
  1448. fShmRtControl.unmapData();
  1449. carla_shm_close(fShmRtControl.shm);
  1450. carla_shm_close(fShmAudioPool.shm);
  1451. return false;
  1452. }
  1453. fNeedsSemDestroy = true;
  1454. }
  1455. // ---------------------------------------------------------------
  1456. // SHM Non-RT Control
  1457. {
  1458. char tmpFileBase[64];
  1459. std::sprintf(tmpFileBase, "/carla-bridge_shm_nonrt_XXXXXX");
  1460. fShmNonRtControl.shm = shm_mkstemp(tmpFileBase);
  1461. if (! carla_is_shm_valid(fShmNonRtControl.shm))
  1462. {
  1463. carla_stdout("Failed to open or create shared memory file #3");
  1464. return false;
  1465. }
  1466. fShmNonRtControl.filename = tmpFileBase;
  1467. if (! fShmNonRtControl.mapData())
  1468. {
  1469. carla_stdout("Failed to map shared memory file #3");
  1470. // clear
  1471. fShmNonRtControl.unmapData();
  1472. carla_shm_close(fShmNonRtControl.shm);
  1473. carla_shm_close(fShmRtControl.shm);
  1474. carla_shm_close(fShmAudioPool.shm);
  1475. return false;
  1476. }
  1477. }
  1478. carla_stdout("Carla Server Info:");
  1479. carla_stdout(" sizeof(BridgeRtData): " P_SIZE, sizeof(BridgeRtData));
  1480. carla_stdout(" sizeof(BridgeNonRtData): " P_SIZE, sizeof(BridgeNonRtData));
  1481. // initial values
  1482. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtNull);
  1483. fShmNonRtControl.writeInt(static_cast<int32_t>(sizeof(BridgeRtData)));
  1484. fShmNonRtControl.writeInt(static_cast<int32_t>(sizeof(BridgeNonRtData)));
  1485. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
  1486. fShmNonRtControl.writeInt(static_cast<int32_t>(pData->engine->getBufferSize()));
  1487. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
  1488. fShmNonRtControl.writeDouble(pData->engine->getSampleRate());
  1489. fShmNonRtControl.commitWrite();
  1490. // register plugin now so we can receive OSC (and wait for it)
  1491. pData->hints |= PLUGIN_IS_BRIDGE;
  1492. pData->engine->registerEnginePlugin(pData->id, this);
  1493. // init OSC
  1494. {
  1495. char shmIdStr[18+1] = { 0 };
  1496. std::strncpy(shmIdStr, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  1497. std::strncat(shmIdStr, &fShmRtControl.filename[fShmRtControl.filename.length()-6], 6);
  1498. std::strncat(shmIdStr, &fShmNonRtControl.filename[fShmNonRtControl.filename.length()-6], 6);
  1499. pData->osc.thread.setOscData(bridgeBinary, label, getPluginTypeAsString(fPluginType), shmIdStr);
  1500. pData->osc.thread.startThread();
  1501. }
  1502. fInitiated = false;
  1503. fLastPongCounter = 0;
  1504. for (; fLastPongCounter < 200; ++fLastPongCounter)
  1505. {
  1506. if (fInitiated || ! pData->osc.thread.isThreadRunning())
  1507. break;
  1508. carla_msleep(30);
  1509. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1510. pData->engine->idle();
  1511. }
  1512. fLastPongCounter = -1;
  1513. if (fInitError || ! fInitiated)
  1514. {
  1515. pData->osc.thread.stopThread(6000);
  1516. if (! fInitError)
  1517. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n(or the plugin crashed on initialization?)");
  1518. return false;
  1519. }
  1520. // ---------------------------------------------------------------
  1521. // register client
  1522. if (pData->name == nullptr)
  1523. {
  1524. if (name != nullptr && name[0] != '\0')
  1525. pData->name = pData->engine->getUniquePluginName(name);
  1526. else if (label != nullptr && label[0] != '\0')
  1527. pData->name = pData->engine->getUniquePluginName(label);
  1528. else
  1529. pData->name = pData->engine->getUniquePluginName("unknown");
  1530. }
  1531. pData->client = pData->engine->addClient(this);
  1532. if (pData->client == nullptr || ! pData->client->isOk())
  1533. {
  1534. pData->engine->setLastError("Failed to register plugin client");
  1535. return false;
  1536. }
  1537. return true;
  1538. }
  1539. private:
  1540. const BinaryType fBinaryType;
  1541. const PluginType fPluginType;
  1542. bool fInitiated;
  1543. bool fInitError;
  1544. bool fSaved;
  1545. bool fNeedsSemDestroy;
  1546. bool fTimedOut;
  1547. volatile int32_t fLastPongCounter;
  1548. CarlaString fBridgeBinary;
  1549. BridgeAudioPool fShmAudioPool;
  1550. BridgeRtControl fShmRtControl;
  1551. BridgeNonRtControl fShmNonRtControl;
  1552. struct Info {
  1553. uint32_t aIns, aOuts;
  1554. uint32_t mIns, mOuts;
  1555. PluginCategory category;
  1556. uint optionsAvailable;
  1557. int64_t uniqueId;
  1558. CarlaString name;
  1559. CarlaString label;
  1560. CarlaString maker;
  1561. CarlaString copyright;
  1562. std::vector<uint8_t> chunk;
  1563. Info()
  1564. : aIns(0),
  1565. aOuts(0),
  1566. mIns(0),
  1567. mOuts(0),
  1568. category(PLUGIN_CATEGORY_NONE),
  1569. optionsAvailable(0),
  1570. uniqueId(0) {}
  1571. } fInfo;
  1572. BridgeParamInfo* fParams;
  1573. void resizeAudioPool(const uint32_t bufferSize)
  1574. {
  1575. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts);
  1576. fShmRtControl.writeOpcode(kPluginBridgeRtSetAudioPool);
  1577. fShmRtControl.writeLong(static_cast<int64_t>(fShmAudioPool.size));
  1578. fShmRtControl.commitWrite();
  1579. waitForServer();
  1580. }
  1581. bool waitForServer(const int secs = 5)
  1582. {
  1583. CARLA_SAFE_ASSERT_RETURN(! fTimedOut, false);
  1584. if (! fShmRtControl.waitForServer(secs))
  1585. {
  1586. carla_stderr("waitForServer() timeout here");
  1587. fTimedOut = true;
  1588. return false;
  1589. }
  1590. return true;
  1591. }
  1592. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BridgePlugin)
  1593. };
  1594. CARLA_BACKEND_END_NAMESPACE
  1595. #endif // ! BUILD_BRIDGE
  1596. // -------------------------------------------------------------------------------------------------------------------
  1597. CARLA_BACKEND_START_NAMESPACE
  1598. CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* const bridgeBinary)
  1599. {
  1600. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  1601. #ifndef BUILD_BRIDGE
  1602. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  1603. {
  1604. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  1605. return nullptr;
  1606. }
  1607. BridgePlugin* const plugin(new BridgePlugin(init.engine, init.id, btype, ptype));
  1608. if (! plugin->init(init.filename, init.name, init.label, bridgeBinary))
  1609. {
  1610. init.engine->registerEnginePlugin(init.id, nullptr);
  1611. delete plugin;
  1612. return nullptr;
  1613. }
  1614. plugin->reload();
  1615. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1616. {
  1617. init.engine->setLastError("Carla's rack mode can only work with Stereo Bridged plugins, sorry!");
  1618. delete plugin;
  1619. return nullptr;
  1620. }
  1621. return plugin;
  1622. #else
  1623. init.engine->setLastError("Plugin bridge support not available");
  1624. return nullptr;
  1625. // unused
  1626. (void)bridgeBinary;
  1627. #endif
  1628. }
  1629. #ifndef BUILD_BRIDGE
  1630. // -------------------------------------------------------------------------------------------------------------------
  1631. // Bridge Helper
  1632. #define bridgePlugin ((BridgePlugin*)plugin)
  1633. extern int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
  1634. const int argc, const lo_arg* const* const argv, const char* const types);
  1635. int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
  1636. const int argc, const lo_arg* const* const argv, const char* const types)
  1637. {
  1638. CARLA_SAFE_ASSERT(plugin != nullptr && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0);
  1639. return bridgePlugin->setOscPluginBridgeInfo(type, argc, argv, types);
  1640. }
  1641. #undef bridgePlugin
  1642. #endif
  1643. CARLA_BACKEND_END_NAMESPACE
  1644. // -------------------------------------------------------------------------------------------------------------------