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.

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