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.

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