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.

2133 lines
72KB

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