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.

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