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.

2163 lines
71KB

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