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.

2133 lines
70KB

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