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.

2132 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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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 CarlaCriticalSection::Scope _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. carla_debug("setOscPluginBridgeInfo(%s, %i, %p, \"%s\")", PluginBridgeInfoType2str(infoType), argc, argv, types);
  1020. switch (infoType)
  1021. {
  1022. case kPluginBridgePong:
  1023. if (fLastPongCounter > 0)
  1024. fLastPongCounter = 0;
  1025. break;
  1026. case kPluginBridgePluginInfo1: {
  1027. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iih");
  1028. const int32_t category = argv[0]->i;
  1029. const int32_t hints = argv[1]->i;
  1030. const int64_t uniqueId = argv[2]->h;
  1031. CARLA_SAFE_ASSERT_BREAK(category >= 0);
  1032. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1033. pData->hints = static_cast<uint>(hints);
  1034. pData->hints |= PLUGIN_IS_BRIDGE;
  1035. fInfo.category = static_cast<PluginCategory>(category);
  1036. fInfo.uniqueId = static_cast<long>(uniqueId);
  1037. break;
  1038. }
  1039. case kPluginBridgePluginInfo2: {
  1040. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ssss");
  1041. const char* const realName = (const char*)&argv[0]->s;
  1042. const char* const label = (const char*)&argv[1]->s;
  1043. const char* const maker = (const char*)&argv[2]->s;
  1044. const char* const copyright = (const char*)&argv[3]->s;
  1045. CARLA_SAFE_ASSERT_BREAK(realName != nullptr);
  1046. CARLA_SAFE_ASSERT_BREAK(label != nullptr);
  1047. CARLA_SAFE_ASSERT_BREAK(maker != nullptr);
  1048. CARLA_SAFE_ASSERT_BREAK(copyright != nullptr);
  1049. fInfo.name = realName;
  1050. fInfo.label = label;
  1051. fInfo.maker = maker;
  1052. fInfo.copyright = copyright;
  1053. if (pData->name == nullptr)
  1054. pData->name = pData->engine->getUniquePluginName(realName);
  1055. break;
  1056. }
  1057. case kPluginBridgeAudioCount: {
  1058. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1059. const int32_t ins = argv[0]->i;
  1060. const int32_t outs = argv[1]->i;
  1061. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1062. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1063. fInfo.aIns = static_cast<uint32_t>(ins);
  1064. fInfo.aOuts = static_cast<uint32_t>(outs);
  1065. break;
  1066. }
  1067. case kPluginBridgeMidiCount: {
  1068. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1069. const int32_t ins = argv[0]->i;
  1070. const int32_t outs = argv[1]->i;
  1071. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1072. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1073. fInfo.mIns = static_cast<uint32_t>(ins);
  1074. fInfo.mOuts = static_cast<uint32_t>(outs);
  1075. break;
  1076. }
  1077. case kPluginBridgeParameterCount: {
  1078. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1079. const int32_t ins = argv[0]->i;
  1080. const int32_t outs = argv[1]->i;
  1081. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1082. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1083. // delete old data
  1084. pData->param.clear();
  1085. if (fParams != nullptr)
  1086. {
  1087. delete[] fParams;
  1088. fParams = nullptr;
  1089. }
  1090. CARLA_SAFE_ASSERT_INT2(ins+outs <= static_cast<int32_t>(pData->engine->getOptions().maxParameters), ins+outs, pData->engine->getOptions().maxParameters);
  1091. const uint32_t count(static_cast<uint32_t>(carla_min<int32_t>(ins+outs, static_cast<int32_t>(pData->engine->getOptions().maxParameters), 0)));
  1092. if (count > 0)
  1093. {
  1094. pData->param.createNew(count, false);
  1095. fParams = new BridgeParamInfo[count];
  1096. }
  1097. break;
  1098. }
  1099. case kPluginBridgeProgramCount: {
  1100. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1101. const int32_t count = argv[0]->i;
  1102. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1103. pData->prog.clear();
  1104. if (count > 0)
  1105. pData->prog.createNew(static_cast<uint32_t>(count));
  1106. break;
  1107. }
  1108. case kPluginBridgeMidiProgramCount: {
  1109. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1110. const int32_t count = argv[0]->i;
  1111. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1112. pData->midiprog.clear();
  1113. if (count > 0)
  1114. pData->midiprog.createNew(static_cast<uint32_t>(count));
  1115. break;
  1116. }
  1117. case kPluginBridgeParameterData:
  1118. {
  1119. CARLA_BRIDGE_CHECK_OSC_TYPES(6, "iiiiss");
  1120. const int32_t index = argv[0]->i;
  1121. const int32_t rindex = argv[1]->i;
  1122. const int32_t type = argv[2]->i;
  1123. const int32_t hints = argv[3]->i;
  1124. const char* const name = (const char*)&argv[4]->s;
  1125. const char* const unit = (const char*)&argv[5]->s;
  1126. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1127. CARLA_SAFE_ASSERT_BREAK(rindex >= 0);
  1128. CARLA_SAFE_ASSERT_BREAK(type >= 0);
  1129. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1130. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1131. CARLA_SAFE_ASSERT_BREAK(unit != nullptr);
  1132. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1133. if (index < static_cast<int32_t>(pData->param.count))
  1134. {
  1135. pData->param.data[index].index = index;
  1136. pData->param.data[index].rindex = rindex;
  1137. pData->param.data[index].hints = static_cast<uint>(hints);
  1138. fParams[index].name = name;
  1139. fParams[index].unit = unit;
  1140. }
  1141. break;
  1142. }
  1143. case kPluginBridgeParameterRanges1:
  1144. {
  1145. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1146. const int32_t index = argv[0]->i;
  1147. const float def = argv[1]->f;
  1148. const float min = argv[2]->f;
  1149. const float max = argv[3]->f;
  1150. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1151. CARLA_SAFE_ASSERT_BREAK(min < max);
  1152. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1153. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1154. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1155. if (index < static_cast<int32_t>(pData->param.count))
  1156. {
  1157. pData->param.ranges[index].def = def;
  1158. pData->param.ranges[index].min = min;
  1159. pData->param.ranges[index].max = max;
  1160. }
  1161. break;
  1162. }
  1163. case kPluginBridgeParameterRanges2:
  1164. {
  1165. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1166. const int32_t index = argv[0]->i;
  1167. const float step = argv[1]->f;
  1168. const float stepSmall = argv[2]->f;
  1169. const float stepLarge = argv[3]->f;
  1170. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1171. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1172. if (index < static_cast<int32_t>(pData->param.count))
  1173. {
  1174. pData->param.ranges[index].step = step;
  1175. pData->param.ranges[index].stepSmall = stepSmall;
  1176. pData->param.ranges[index].stepLarge = stepLarge;
  1177. }
  1178. break;
  1179. }
  1180. case kPluginBridgeParameterMidiCC: {
  1181. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1182. const int32_t index = argv[0]->i;
  1183. const int32_t cc = argv[1]->i;
  1184. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1185. CARLA_SAFE_ASSERT_BREAK(cc >= -1 && cc < 0x5F);
  1186. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1187. if (index < static_cast<int32_t>(pData->param.count))
  1188. pData->param.data[index].midiCC = static_cast<int16_t>(cc);
  1189. break;
  1190. }
  1191. case kPluginBridgeParameterMidiChannel: {
  1192. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1193. const int32_t index = argv[0]->i;
  1194. const int32_t channel = argv[1]->i;
  1195. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1196. CARLA_SAFE_ASSERT_BREAK(channel >= 0 && channel < MAX_MIDI_CHANNELS);
  1197. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1198. if (index < static_cast<int32_t>(pData->param.count))
  1199. pData->param.data[index].midiChannel = static_cast<uint8_t>(channel);
  1200. break;
  1201. }
  1202. case kPluginBridgeParameterValue:
  1203. {
  1204. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1205. const int32_t index = argv[0]->i;
  1206. const float value = argv[1]->f;
  1207. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1208. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1209. if (index < static_cast<int32_t>(pData->param.count))
  1210. {
  1211. const uint32_t uindex(static_cast<uint32_t>(index));
  1212. const float fixedValue(pData->param.getFixedValue(uindex, value));
  1213. fParams[uindex].value = fixedValue;
  1214. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1215. }
  1216. break;
  1217. }
  1218. case kPluginBridgeDefaultValue:
  1219. {
  1220. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1221. const int32_t index = argv[0]->i;
  1222. const float value = argv[1]->f;
  1223. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1224. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1225. if (index < static_cast<int32_t>(pData->param.count))
  1226. pData->param.ranges[index].def = value;
  1227. break;
  1228. }
  1229. case kPluginBridgeCurrentProgram: {
  1230. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1231. const int32_t index = argv[0]->i;
  1232. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1233. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1234. CarlaPlugin::setProgram(index, false, true, true);
  1235. break;
  1236. }
  1237. case kPluginBridgeCurrentMidiProgram: {
  1238. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1239. const int32_t index = argv[0]->i;
  1240. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1241. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1242. CarlaPlugin::setMidiProgram(index, false, true, true);
  1243. break;
  1244. }
  1245. case kPluginBridgeProgramName: {
  1246. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "is");
  1247. const int32_t index = argv[0]->i;
  1248. const char* const name = (const char*)&argv[1]->s;
  1249. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1250. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1251. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1252. if (index < static_cast<int32_t>(pData->prog.count))
  1253. {
  1254. if (pData->prog.names[index] != nullptr)
  1255. delete[] pData->prog.names[index];
  1256. pData->prog.names[index] = carla_strdup(name);
  1257. }
  1258. break;
  1259. }
  1260. case kPluginBridgeMidiProgramData: {
  1261. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "iiis");
  1262. const int32_t index = argv[0]->i;
  1263. const int32_t bank = argv[1]->i;
  1264. const int32_t program = argv[2]->i;
  1265. const char* const name = (const char*)&argv[3]->s;
  1266. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1267. CARLA_SAFE_ASSERT_BREAK(bank >= 0);
  1268. CARLA_SAFE_ASSERT_BREAK(program >= 0);
  1269. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1270. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1271. if (index < static_cast<int32_t>(pData->midiprog.count))
  1272. {
  1273. if (pData->midiprog.data[index].name != nullptr)
  1274. delete[] pData->midiprog.data[index].name;
  1275. pData->midiprog.data[index].bank = static_cast<uint32_t>(bank);
  1276. pData->midiprog.data[index].program = static_cast<uint32_t>(program);
  1277. pData->midiprog.data[index].name = carla_strdup(name);
  1278. }
  1279. break;
  1280. }
  1281. case kPluginBridgeConfigure: {
  1282. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ss");
  1283. const char* const key = (const char*)&argv[0]->s;
  1284. const char* const value = (const char*)&argv[1]->s;
  1285. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1286. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1287. if (std::strcmp(key, CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  1288. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1289. else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVED) == 0)
  1290. fSaved = true;
  1291. break;
  1292. }
  1293. case kPluginBridgeSetCustomData: {
  1294. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "sss");
  1295. const char* const type = (const char*)&argv[0]->s;
  1296. const char* const key = (const char*)&argv[1]->s;
  1297. const char* const value = (const char*)&argv[2]->s;
  1298. CARLA_SAFE_ASSERT_BREAK(type != nullptr);
  1299. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1300. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1301. CarlaPlugin::setCustomData(type, key, value, false);
  1302. break;
  1303. }
  1304. case kPluginBridgeSetChunkData: {
  1305. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1306. #if 0
  1307. const char* const chunkFileChar = (const char*)&argv[0]->s;
  1308. CARLA_ASSERT(chunkFileChar);
  1309. QString chunkFileStr(chunkFileChar);
  1310. #ifndef CARLA_OS_WIN
  1311. // Using Wine, fix temp dir
  1312. if (m_binary == BINARY_WIN32 || m_binary == BINARY_WIN64)
  1313. {
  1314. // Get WINEPREFIX
  1315. QString wineDir;
  1316. if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
  1317. wineDir = QString(WINEPREFIX);
  1318. else
  1319. wineDir = QDir::homePath() + "/.wine";
  1320. QStringList chunkFileStrSplit1 = chunkFileStr.split(":/");
  1321. QStringList chunkFileStrSplit2 = chunkFileStrSplit1.at(1).split("\\");
  1322. QString wineDrive = chunkFileStrSplit1.at(0).toLower();
  1323. QString wineTMP = chunkFileStrSplit2.at(0);
  1324. QString baseName = chunkFileStrSplit2.at(1);
  1325. chunkFileStr = wineDir;
  1326. chunkFileStr += "/drive_";
  1327. chunkFileStr += wineDrive;
  1328. chunkFileStr += "/";
  1329. chunkFileStr += wineTMP;
  1330. chunkFileStr += "/";
  1331. chunkFileStr += baseName;
  1332. chunkFileStr = QDir::toNativeSeparators(chunkFileStr);
  1333. }
  1334. #endif
  1335. QFile chunkFile(chunkFileStr);
  1336. if (chunkFile.open(QIODevice::ReadOnly))
  1337. {
  1338. info.chunk = chunkFile.readAll();
  1339. chunkFile.close();
  1340. chunkFile.remove();
  1341. }
  1342. #endif
  1343. break;
  1344. }
  1345. case kPluginBridgeUpdateNow:
  1346. fInitiated = true;
  1347. break;
  1348. case kPluginBridgeError: {
  1349. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1350. const char* const error = (const char*)&argv[0]->s;
  1351. CARLA_ASSERT(error != nullptr);
  1352. pData->engine->setLastError(error);
  1353. fInitError = true;
  1354. fInitiated = true;
  1355. break;
  1356. }
  1357. }
  1358. return 0;
  1359. }
  1360. // -------------------------------------------------------------------
  1361. const void* getExtraStuff() const noexcept override
  1362. {
  1363. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  1364. }
  1365. bool init(const char* const filename, const char* const name, const char* const label, const char* const bridgeBinary)
  1366. {
  1367. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1368. // ---------------------------------------------------------------
  1369. // first checks
  1370. if (pData->client != nullptr)
  1371. {
  1372. pData->engine->setLastError("Plugin client is already registered");
  1373. return false;
  1374. }
  1375. // ---------------------------------------------------------------
  1376. // set info
  1377. if (name != nullptr && name[0] != '\0')
  1378. pData->name = pData->engine->getUniquePluginName(name);
  1379. pData->filename = carla_strdup(filename);
  1380. if (bridgeBinary != nullptr)
  1381. fBridgeBinary = bridgeBinary;
  1382. std::srand(static_cast<uint>(std::time(nullptr)));
  1383. // ---------------------------------------------------------------
  1384. // SHM Audio Pool
  1385. {
  1386. char tmpFileBase[60];
  1387. std::sprintf(tmpFileBase, "/carla-bridge_shm_XXXXXX");
  1388. fShmAudioPool.shm = shm_mkstemp(tmpFileBase);
  1389. if (! carla_is_shm_valid(fShmAudioPool.shm))
  1390. {
  1391. carla_stdout("Failed to open or create shared memory file #1");
  1392. return false;
  1393. }
  1394. fShmAudioPool.filename = tmpFileBase;
  1395. }
  1396. // ---------------------------------------------------------------
  1397. // SHM Control
  1398. {
  1399. char tmpFileBase[60];
  1400. std::sprintf(tmpFileBase, "/carla-bridge_shc_XXXXXX");
  1401. fShmControl.shm = shm_mkstemp(tmpFileBase);
  1402. if (! carla_is_shm_valid(fShmControl.shm))
  1403. {
  1404. carla_stdout("Failed to open or create shared memory file #2");
  1405. // clear
  1406. carla_shm_close(fShmAudioPool.shm);
  1407. return false;
  1408. }
  1409. fShmControl.filename = tmpFileBase;
  1410. if (! fShmControl.mapData())
  1411. {
  1412. carla_stdout("Failed to map shared memory file #2");
  1413. // clear
  1414. carla_shm_close(fShmControl.shm);
  1415. carla_shm_close(fShmAudioPool.shm);
  1416. return false;
  1417. }
  1418. CARLA_ASSERT(fShmControl.data != nullptr);
  1419. if (! jackbridge_sem_init(&fShmControl.data->runServer))
  1420. {
  1421. carla_stdout("Failed to initialize shared memory semaphore #1");
  1422. // clear
  1423. fShmControl.unmapData();
  1424. carla_shm_close(fShmControl.shm);
  1425. carla_shm_close(fShmAudioPool.shm);
  1426. return false;
  1427. }
  1428. if (! jackbridge_sem_init(&fShmControl.data->runClient))
  1429. {
  1430. carla_stdout("Failed to initialize shared memory semaphore #2");
  1431. // clear
  1432. jackbridge_sem_destroy(&fShmControl.data->runServer);
  1433. fShmControl.unmapData();
  1434. carla_shm_close(fShmControl.shm);
  1435. carla_shm_close(fShmAudioPool.shm);
  1436. return false;
  1437. }
  1438. fNeedsSemDestroy = true;
  1439. }
  1440. // ---------------------------------------------------------------
  1441. // SHM TimeInfo
  1442. {
  1443. char tmpFileBase[60];
  1444. std::sprintf(tmpFileBase, "/carla-bridge_sht_XXXXXX");
  1445. fShmTime.shm = shm_mkstemp(tmpFileBase);
  1446. if (! carla_is_shm_valid(fShmTime.shm))
  1447. {
  1448. carla_stdout("Failed to open or create shared memory file #3");
  1449. return false;
  1450. }
  1451. fShmTime.filename = tmpFileBase;
  1452. if (! fShmTime.mapData())
  1453. {
  1454. carla_stdout("Failed to map shared memory file #3");
  1455. // clear
  1456. jackbridge_sem_destroy(&fShmControl.data->runServer);
  1457. fShmControl.unmapData();
  1458. carla_shm_close(fShmTime.shm);
  1459. carla_shm_close(fShmControl.shm);
  1460. carla_shm_close(fShmAudioPool.shm);
  1461. return false;
  1462. }
  1463. }
  1464. // initial values
  1465. fShmControl.writeOpcode(kPluginBridgeOpcodeNull);
  1466. fShmControl.writeInt(static_cast<int32_t>(sizeof(BridgeShmControl)));
  1467. fShmControl.writeOpcode(kPluginBridgeOpcodeSetBufferSize);
  1468. fShmControl.writeInt(static_cast<int32_t>(pData->engine->getBufferSize()));
  1469. fShmControl.writeOpcode(kPluginBridgeOpcodeSetSampleRate);
  1470. fShmControl.writeFloat(float(pData->engine->getSampleRate()));
  1471. fShmControl.commitWrite();
  1472. // register plugin now so we can receive OSC (and wait for it)
  1473. pData->hints |= PLUGIN_IS_BRIDGE;
  1474. pData->engine->registerEnginePlugin(pData->id, this);
  1475. // init OSC
  1476. {
  1477. char shmIdStr[16+1] = { 0 };
  1478. std::strncpy(shmIdStr, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  1479. std::strncat(shmIdStr, &fShmControl.filename[fShmControl.filename.length()-6], 6);
  1480. std::strncat(shmIdStr, &fShmTime.filename[fShmTime.filename.length()-6], 6);
  1481. pData->osc.thread.setOscData(bridgeBinary, label, getPluginTypeAsString(fPluginType), shmIdStr);
  1482. pData->osc.thread.startThread();
  1483. }
  1484. fInitiated = false;
  1485. fLastPongCounter = 0;
  1486. for (; fLastPongCounter < 200; ++fLastPongCounter)
  1487. {
  1488. if (fInitiated || ! pData->osc.thread.isThreadRunning())
  1489. break;
  1490. carla_msleep(30);
  1491. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1492. pData->engine->idle();
  1493. }
  1494. fLastPongCounter = -1;
  1495. if (fInitError || ! fInitiated)
  1496. {
  1497. pData->osc.thread.stopThread(6000);
  1498. if (! fInitError)
  1499. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n(or the plugin crashed on initialization?)");
  1500. return false;
  1501. }
  1502. // ---------------------------------------------------------------
  1503. // register client
  1504. if (pData->name == nullptr)
  1505. {
  1506. if (name != nullptr && name[0] != '\0')
  1507. pData->name = pData->engine->getUniquePluginName(name);
  1508. else if (label != nullptr && label[0] != '\0')
  1509. pData->name = pData->engine->getUniquePluginName(label);
  1510. else
  1511. pData->name = pData->engine->getUniquePluginName("unknown");
  1512. }
  1513. pData->client = pData->engine->addClient(this);
  1514. if (pData->client == nullptr || ! pData->client->isOk())
  1515. {
  1516. pData->engine->setLastError("Failed to register plugin client");
  1517. return false;
  1518. }
  1519. return true;
  1520. }
  1521. private:
  1522. const BinaryType fBinaryType;
  1523. const PluginType fPluginType;
  1524. bool fInitiated;
  1525. bool fInitError;
  1526. bool fSaved;
  1527. bool fNeedsSemDestroy;
  1528. bool fTimedOut;
  1529. volatile int32_t fLastPongCounter;
  1530. CarlaString fBridgeBinary;
  1531. BridgeAudioPool fShmAudioPool;
  1532. BridgeControl fShmControl;
  1533. BridgeTime fShmTime;
  1534. struct Info {
  1535. uint32_t aIns, aOuts;
  1536. uint32_t mIns, mOuts;
  1537. PluginCategory category;
  1538. long uniqueId;
  1539. CarlaString name;
  1540. CarlaString label;
  1541. CarlaString maker;
  1542. CarlaString copyright;
  1543. //QByteArray chunk;
  1544. Info()
  1545. : aIns(0),
  1546. aOuts(0),
  1547. mIns(0),
  1548. mOuts(0),
  1549. category(PLUGIN_CATEGORY_NONE),
  1550. uniqueId(0) {}
  1551. } fInfo;
  1552. BridgeParamInfo* fParams;
  1553. void resizeAudioPool(const uint32_t bufferSize)
  1554. {
  1555. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts);
  1556. fShmControl.writeOpcode(kPluginBridgeOpcodeSetAudioPool);
  1557. fShmControl.writeLong(static_cast<int64_t>(fShmAudioPool.size));
  1558. fShmControl.commitWrite();
  1559. waitForServer();
  1560. }
  1561. bool waitForServer(const int secs = 5)
  1562. {
  1563. CARLA_SAFE_ASSERT_RETURN(! fTimedOut, false);
  1564. if (! fShmControl.waitForServer(secs))
  1565. {
  1566. carla_stderr("waitForServer() timeout here");
  1567. fTimedOut = true;
  1568. return false;
  1569. }
  1570. return true;
  1571. }
  1572. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BridgePlugin)
  1573. };
  1574. CARLA_BACKEND_END_NAMESPACE
  1575. #endif // ! BUILD_BRIDGE
  1576. // -------------------------------------------------------------------------------------------------------------------
  1577. CARLA_BACKEND_START_NAMESPACE
  1578. CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* const bridgeBinary)
  1579. {
  1580. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  1581. #ifndef BUILD_BRIDGE
  1582. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  1583. {
  1584. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  1585. return nullptr;
  1586. }
  1587. BridgePlugin* const plugin(new BridgePlugin(init.engine, init.id, btype, ptype));
  1588. if (! plugin->init(init.filename, init.name, init.label, bridgeBinary))
  1589. {
  1590. delete plugin;
  1591. return nullptr;
  1592. }
  1593. plugin->reload();
  1594. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1595. {
  1596. init.engine->setLastError("Carla's rack mode can only work with Stereo Bridged plugins, sorry!");
  1597. delete plugin;
  1598. return nullptr;
  1599. }
  1600. return plugin;
  1601. #else
  1602. init.engine->setLastError("Plugin bridge support not available");
  1603. return nullptr;
  1604. // unused
  1605. (void)bridgeBinary;
  1606. #endif
  1607. }
  1608. CarlaPlugin* CarlaPlugin::newJACK(const Initializer& init)
  1609. {
  1610. carla_debug("CarlaPlugin::newJACK({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  1611. #ifndef BUILD_BRIDGE
  1612. BridgePlugin* const plugin(new BridgePlugin(init.engine, init.id, BINARY_NATIVE, PLUGIN_JACK));
  1613. if (! plugin->init(init.filename, init.name, init.label, nullptr))
  1614. {
  1615. delete plugin;
  1616. return nullptr;
  1617. }
  1618. plugin->reload();
  1619. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1620. {
  1621. init.engine->setLastError("Carla's rack mode can only work with Stereo bridged apps, sorry!");
  1622. delete plugin;
  1623. return nullptr;
  1624. }
  1625. return plugin;
  1626. #else
  1627. init.engine->setLastError("JACK app bridge support not available");
  1628. return nullptr;
  1629. #endif
  1630. }
  1631. #ifndef BUILD_BRIDGE
  1632. // -------------------------------------------------------------------------------------------------------------------
  1633. // Bridge Helper
  1634. #define bridgePlugin ((BridgePlugin*)plugin)
  1635. extern int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeInfoType type,
  1636. const int argc, const lo_arg* const* const argv, const char* const types);
  1637. int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeInfoType type,
  1638. const int argc, const lo_arg* const* const argv, const char* const types)
  1639. {
  1640. CARLA_ASSERT(plugin != nullptr && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0);
  1641. return bridgePlugin->setOscPluginBridgeInfo(type, argc, argv, types);
  1642. }
  1643. #undef bridgePlugin
  1644. #endif
  1645. CARLA_BACKEND_END_NAMESPACE
  1646. // -------------------------------------------------------------------------------------------------------------------