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.

2146 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. void setOption(const uint option, const bool yesNo, const bool sendCallback) override
  400. {
  401. {
  402. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  403. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetOption);
  404. fShmNonRtControl.writeUInt(option);
  405. fShmNonRtControl.writeBool(yesNo);
  406. fShmNonRtControl.commitWrite();
  407. }
  408. CarlaPlugin::setOption(option, yesNo, sendCallback);
  409. }
  410. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  411. {
  412. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  413. {
  414. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  415. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetCtrlChannel);
  416. fShmNonRtControl.writeShort(channel);
  417. fShmNonRtControl.commitWrite();
  418. }
  419. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  420. }
  421. // -------------------------------------------------------------------
  422. // Set data (plugin-specific stuff)
  423. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  424. {
  425. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  426. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  427. fParams[parameterId].value = fixedValue;
  428. {
  429. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  430. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
  431. fShmNonRtControl.writeUInt(parameterId);
  432. fShmNonRtControl.writeFloat(value);
  433. fShmNonRtControl.commitWrite();
  434. }
  435. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  436. }
  437. void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  438. {
  439. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  440. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  441. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  442. {
  443. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  444. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
  445. fShmNonRtControl.writeUInt(parameterId);
  446. fShmNonRtControl.writeByte(channel);
  447. fShmNonRtControl.commitWrite();
  448. }
  449. CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
  450. }
  451. void setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept override
  452. {
  453. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  454. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  455. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F,);
  456. {
  457. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  458. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterMidiCC);
  459. fShmNonRtControl.writeUInt(parameterId);
  460. fShmNonRtControl.writeShort(cc);
  461. fShmNonRtControl.commitWrite();
  462. }
  463. CarlaPlugin::setParameterMidiCC(parameterId, cc, sendOsc, sendCallback);
  464. }
  465. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  466. {
  467. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  468. {
  469. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  470. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetProgram);
  471. fShmNonRtControl.writeInt(index);
  472. fShmNonRtControl.commitWrite();
  473. }
  474. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  475. }
  476. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  477. {
  478. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  479. {
  480. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  481. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetMidiProgram);
  482. fShmNonRtControl.writeInt(index);
  483. fShmNonRtControl.commitWrite();
  484. }
  485. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  486. }
  487. #if 0
  488. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  489. {
  490. CARLA_ASSERT(type);
  491. CARLA_ASSERT(key);
  492. CARLA_ASSERT(value);
  493. if (sendGui)
  494. {
  495. // TODO - if type is chunk|binary, store it in a file and send path instead
  496. QString cData;
  497. cData = type;
  498. cData += "·";
  499. cData += key;
  500. cData += "·";
  501. cData += value;
  502. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CUSTOM, cData.toUtf8().constData());
  503. }
  504. CarlaPlugin::setCustomData(type, key, value, sendGui);
  505. }
  506. #endif
  507. void setChunkData(const char* const stringData) override
  508. {
  509. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  510. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr,);
  511. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  512. filePath += OS_SEP_STR;
  513. filePath += ".CarlaChunk_";
  514. filePath += fShmAudioPool.filename.buffer() + 18;
  515. if (File(filePath).replaceWithText(stringData))
  516. {
  517. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  518. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  519. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetChunkDataFile);
  520. fShmNonRtControl.writeUInt(ulength);
  521. fShmNonRtControl.writeCustomData(filePath.toRawUTF8(), ulength);
  522. fShmNonRtControl.commitWrite();
  523. }
  524. }
  525. // -------------------------------------------------------------------
  526. // Set ui stuff
  527. void showCustomUI(const bool yesNo) override
  528. {
  529. {
  530. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  531. fShmNonRtControl.writeOpcode(yesNo ? kPluginBridgeNonRtShowUI : kPluginBridgeNonRtHideUI);
  532. fShmNonRtControl.commitWrite();
  533. }
  534. if (yesNo)
  535. {
  536. pData->tryTransient();
  537. }
  538. else
  539. {
  540. pData->transientTryCounter = 0;
  541. }
  542. }
  543. void idle() override
  544. {
  545. if (pData->osc.thread.isThreadRunning())
  546. {
  547. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  548. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPing);
  549. fShmNonRtControl.commitWrite();
  550. }
  551. else
  552. carla_stderr2("TESTING: Bridge has closed!");
  553. CarlaPlugin::idle();
  554. }
  555. // -------------------------------------------------------------------
  556. // Plugin state
  557. void reload() override
  558. {
  559. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  560. carla_debug("BridgePlugin::reload() - start");
  561. const EngineProcessMode processMode(pData->engine->getProccessMode());
  562. // Safely disable plugin for reload
  563. const ScopedDisabler sd(this);
  564. bool needsCtrlIn, needsCtrlOut;
  565. needsCtrlIn = needsCtrlOut = false;
  566. if (fInfo.aIns > 0)
  567. {
  568. pData->audioIn.createNew(fInfo.aIns);
  569. }
  570. if (fInfo.aOuts > 0)
  571. {
  572. pData->audioOut.createNew(fInfo.aOuts);
  573. needsCtrlIn = true;
  574. }
  575. if (fInfo.mIns > 0)
  576. needsCtrlIn = true;
  577. if (fInfo.mOuts > 0)
  578. needsCtrlOut = true;
  579. const uint portNameSize(pData->engine->getMaxPortNameSize());
  580. CarlaString portName;
  581. // Audio Ins
  582. for (uint32_t j=0; j < fInfo.aIns; ++j)
  583. {
  584. portName.clear();
  585. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  586. {
  587. portName = pData->name;
  588. portName += ":";
  589. }
  590. if (fInfo.aIns > 1)
  591. {
  592. portName += "input_";
  593. portName += CarlaString(j+1);
  594. }
  595. else
  596. portName += "input";
  597. portName.truncate(portNameSize);
  598. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  599. pData->audioIn.ports[j].rindex = j;
  600. }
  601. // Audio Outs
  602. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  603. {
  604. portName.clear();
  605. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  606. {
  607. portName = pData->name;
  608. portName += ":";
  609. }
  610. if (fInfo.aOuts > 1)
  611. {
  612. portName += "output_";
  613. portName += CarlaString(j+1);
  614. }
  615. else
  616. portName += "output";
  617. portName.truncate(portNameSize);
  618. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  619. pData->audioOut.ports[j].rindex = j;
  620. }
  621. if (needsCtrlIn)
  622. {
  623. portName.clear();
  624. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  625. {
  626. portName = pData->name;
  627. portName += ":";
  628. }
  629. portName += "event-in";
  630. portName.truncate(portNameSize);
  631. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  632. }
  633. if (needsCtrlOut)
  634. {
  635. portName.clear();
  636. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  637. {
  638. portName = pData->name;
  639. portName += ":";
  640. }
  641. portName += "event-out";
  642. portName.truncate(portNameSize);
  643. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  644. }
  645. // extra plugin hints
  646. pData->extraHints = 0x0;
  647. if (fInfo.mIns > 0)
  648. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  649. if (fInfo.mOuts > 0)
  650. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  651. if (fInfo.aIns <= 2 && fInfo.aOuts <= 2 && (fInfo.aIns == fInfo.aOuts || fInfo.aIns == 0 || fInfo.aOuts == 0))
  652. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  653. bufferSizeChanged(pData->engine->getBufferSize());
  654. reloadPrograms(true);
  655. carla_debug("BridgePlugin::reload() - end");
  656. }
  657. // -------------------------------------------------------------------
  658. // Plugin processing
  659. void activate() noexcept override
  660. {
  661. {
  662. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  663. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtActivate);
  664. fShmNonRtControl.commitWrite();
  665. }
  666. bool timedOut = true;
  667. try {
  668. timedOut = waitForServer();
  669. } catch(...) {}
  670. if (! timedOut)
  671. fTimedOut = false;
  672. }
  673. void deactivate() noexcept override
  674. {
  675. {
  676. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  677. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtDeactivate);
  678. fShmNonRtControl.commitWrite();
  679. }
  680. bool timedOut = true;
  681. try {
  682. timedOut = waitForServer();
  683. } catch(...) {}
  684. if (! timedOut)
  685. fTimedOut = false;
  686. }
  687. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  688. {
  689. // --------------------------------------------------------------------------------------------------------
  690. // Check if active
  691. if (fTimedOut || ! pData->active)
  692. {
  693. // disable any output sound
  694. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  695. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  696. return;
  697. }
  698. // --------------------------------------------------------------------------------------------------------
  699. // Check if needs reset
  700. if (pData->needsReset)
  701. {
  702. // TODO
  703. pData->needsReset = false;
  704. }
  705. // --------------------------------------------------------------------------------------------------------
  706. // Event Input
  707. if (pData->event.portIn != nullptr)
  708. {
  709. // ----------------------------------------------------------------------------------------------------
  710. // MIDI Input (External)
  711. if (pData->extNotes.mutex.tryLock())
  712. {
  713. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  714. {
  715. const ExternalMidiNote& note(it.getValue());
  716. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  717. uint8_t data1, data2, data3;
  718. data1 = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  719. data2 = note.note;
  720. data3 = note.velo;
  721. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  722. fShmRtControl.writeUInt(0); // time
  723. fShmRtControl.writeByte(3); // size
  724. fShmRtControl.writeByte(data1);
  725. fShmRtControl.writeByte(data2);
  726. fShmRtControl.writeByte(data3);
  727. fShmRtControl.commitWrite();
  728. }
  729. pData->extNotes.data.clear();
  730. pData->extNotes.mutex.unlock();
  731. } // End of MIDI Input (External)
  732. // ----------------------------------------------------------------------------------------------------
  733. // Event Input (System)
  734. bool allNotesOffSent = false;
  735. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  736. {
  737. const EngineEvent& event(pData->event.portIn->getEvent(i));
  738. // Control change
  739. switch (event.type)
  740. {
  741. case kEngineEventTypeNull:
  742. break;
  743. case kEngineEventTypeControl: {
  744. const EngineControlEvent& ctrlEvent = event.ctrl;
  745. switch (ctrlEvent.type)
  746. {
  747. case kEngineControlEventTypeNull:
  748. break;
  749. case kEngineControlEventTypeParameter:
  750. // Control backend stuff
  751. if (event.channel == pData->ctrlChannel)
  752. {
  753. float value;
  754. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  755. {
  756. value = ctrlEvent.value;
  757. setDryWet(value, false, false);
  758. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  759. break;
  760. }
  761. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  762. {
  763. value = ctrlEvent.value*127.0f/100.0f;
  764. setVolume(value, false, false);
  765. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  766. break;
  767. }
  768. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  769. {
  770. float left, right;
  771. value = ctrlEvent.value/0.5f - 1.0f;
  772. if (value < 0.0f)
  773. {
  774. left = -1.0f;
  775. right = (value*2.0f)+1.0f;
  776. }
  777. else if (value > 0.0f)
  778. {
  779. left = (value*2.0f)-1.0f;
  780. right = 1.0f;
  781. }
  782. else
  783. {
  784. left = -1.0f;
  785. right = 1.0f;
  786. }
  787. setBalanceLeft(left, false, false);
  788. setBalanceRight(right, false, false);
  789. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  790. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  791. break;
  792. }
  793. }
  794. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventParameter);
  795. fShmRtControl.writeUInt(event.time);
  796. fShmRtControl.writeByte(event.channel);
  797. fShmRtControl.writeUShort(event.ctrl.param);
  798. fShmRtControl.writeFloat(event.ctrl.value);
  799. fShmRtControl.commitWrite();
  800. break;
  801. case kEngineControlEventTypeMidiBank:
  802. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  803. {
  804. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventMidiBank);
  805. fShmRtControl.writeUInt(event.time);
  806. fShmRtControl.writeByte(event.channel);
  807. fShmRtControl.writeUShort(event.ctrl.param);
  808. fShmRtControl.commitWrite();
  809. }
  810. break;
  811. case kEngineControlEventTypeMidiProgram:
  812. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  813. {
  814. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventMidiProgram);
  815. fShmRtControl.writeUInt(event.time);
  816. fShmRtControl.writeByte(event.channel);
  817. fShmRtControl.writeUShort(event.ctrl.param);
  818. fShmRtControl.commitWrite();
  819. }
  820. break;
  821. case kEngineControlEventTypeAllSoundOff:
  822. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  823. {
  824. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventAllSoundOff);
  825. fShmRtControl.writeUInt(event.time);
  826. fShmRtControl.writeByte(event.channel);
  827. fShmRtControl.commitWrite();
  828. }
  829. break;
  830. case kEngineControlEventTypeAllNotesOff:
  831. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  832. {
  833. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  834. {
  835. allNotesOffSent = true;
  836. sendMidiAllNotesOffToCallback();
  837. }
  838. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventAllNotesOff);
  839. fShmRtControl.writeUInt(event.time);
  840. fShmRtControl.writeByte(event.channel);
  841. fShmRtControl.commitWrite();
  842. }
  843. break;
  844. } // switch (ctrlEvent.type)
  845. break;
  846. } // case kEngineEventTypeControl
  847. case kEngineEventTypeMidi: {
  848. const EngineMidiEvent& midiEvent(event.midi);
  849. if (midiEvent.size == 0 || midiEvent.size >= MAX_MIDI_VALUE)
  850. continue;
  851. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  852. uint8_t channel = event.channel;
  853. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  854. status = MIDI_STATUS_NOTE_OFF;
  855. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  856. continue;
  857. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  858. continue;
  859. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  860. continue;
  861. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  862. continue;
  863. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  864. fShmRtControl.writeUInt(event.time);
  865. fShmRtControl.writeByte(midiEvent.size);
  866. fShmRtControl.writeByte(static_cast<uint8_t>(status + channel));
  867. for (uint8_t j=1; j < midiEvent.size; ++j)
  868. fShmRtControl.writeByte(midiEvent.data[j]);
  869. fShmRtControl.commitWrite();
  870. if (status == MIDI_STATUS_NOTE_ON)
  871. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  872. else if (status == MIDI_STATUS_NOTE_OFF)
  873. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  874. } break;
  875. }
  876. }
  877. pData->postRtEvents.trySplice();
  878. } // End of Event Input
  879. processSingle(inBuffer, outBuffer, frames);
  880. }
  881. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  882. {
  883. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  884. if (pData->audioIn.count > 0)
  885. {
  886. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  887. }
  888. if (pData->audioOut.count > 0)
  889. {
  890. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  891. }
  892. // --------------------------------------------------------------------------------------------------------
  893. // Try lock, silence otherwise
  894. if (pData->engine->isOffline())
  895. {
  896. pData->singleMutex.lock();
  897. }
  898. else if (! pData->singleMutex.tryLock())
  899. {
  900. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  901. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  902. return false;
  903. }
  904. // --------------------------------------------------------------------------------------------------------
  905. // Reset audio buffers
  906. //std::memset(fShmAudioPool.data, 0, fShmAudioPool.size);
  907. for (uint32_t i=0; i < fInfo.aIns; ++i)
  908. FloatVectorOperations::copy(fShmAudioPool.data + (i * frames), inBuffer[i], static_cast<int>(frames));
  909. // --------------------------------------------------------------------------------------------------------
  910. // TimeInfo
  911. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  912. BridgeTimeInfo& bridgeTimeInfo(fShmRtControl.data->timeInfo);
  913. bridgeTimeInfo.playing = timeInfo.playing;
  914. bridgeTimeInfo.frame = timeInfo.frame;
  915. bridgeTimeInfo.usecs = timeInfo.usecs;
  916. bridgeTimeInfo.valid = timeInfo.valid;
  917. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  918. {
  919. bridgeTimeInfo.bar = timeInfo.bbt.bar;
  920. bridgeTimeInfo.beat = timeInfo.bbt.beat;
  921. bridgeTimeInfo.tick = timeInfo.bbt.tick;
  922. bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
  923. bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
  924. bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  925. bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  926. bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
  927. }
  928. // --------------------------------------------------------------------------------------------------------
  929. // Run plugin
  930. {
  931. fShmRtControl.writeOpcode(kPluginBridgeRtProcess);
  932. fShmRtControl.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. FloatVectorOperations::copy(outBuffer[i], fShmAudioPool.data + ((i + fInfo.aIns) * frames), static_cast<int>(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. FloatVectorOperations::copy(oldBufLeft, outBuffer[i], static_cast<int>(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. resizeAudioPool(newBufferSize);
  1002. {
  1003. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1004. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
  1005. fShmNonRtControl.writeUInt(newBufferSize);
  1006. fShmNonRtControl.commitWrite();
  1007. }
  1008. fShmRtControl.waitForServer(1);
  1009. }
  1010. void sampleRateChanged(const double newSampleRate) override
  1011. {
  1012. {
  1013. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1014. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
  1015. fShmNonRtControl.writeDouble(newSampleRate);
  1016. fShmNonRtControl.commitWrite();
  1017. }
  1018. fShmRtControl.waitForServer(1);
  1019. }
  1020. void offlineModeChanged(const bool isOffline) override
  1021. {
  1022. {
  1023. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1024. fShmNonRtControl.writeOpcode(isOffline ? kPluginBridgeNonRtSetOffline : kPluginBridgeNonRtSetOnline);
  1025. fShmNonRtControl.commitWrite();
  1026. }
  1027. fShmRtControl.waitForServer(1);
  1028. }
  1029. // -------------------------------------------------------------------
  1030. // Plugin buffers
  1031. void clearBuffers() noexcept override
  1032. {
  1033. if (fParams != nullptr)
  1034. {
  1035. delete[] fParams;
  1036. fParams = nullptr;
  1037. }
  1038. CarlaPlugin::clearBuffers();
  1039. }
  1040. // -------------------------------------------------------------------
  1041. // Post-poned UI Stuff
  1042. // nothing
  1043. // -------------------------------------------------------------------
  1044. int setOscPluginBridgeInfo(const PluginBridgeOscInfoType infoType, const int argc, const lo_arg* const* const argv, const char* const types)
  1045. {
  1046. #ifdef DEBUG
  1047. if (infoType != kPluginBridgeOscPong) {
  1048. carla_debug("BridgePlugin::setOscPluginBridgeInfo(%s, %i, %p, \"%s\")", PluginBridgeOscInfoType2str(infoType), argc, argv, types);
  1049. }
  1050. #endif
  1051. switch (infoType)
  1052. {
  1053. case kPluginBridgeOscNull:
  1054. break;
  1055. case kPluginBridgeOscPong:
  1056. if (fLastPongCounter > 0)
  1057. fLastPongCounter = 0;
  1058. break;
  1059. case kPluginBridgeOscPluginInfo1: {
  1060. CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiih");
  1061. const int32_t category = argv[0]->i;
  1062. const int32_t hints = argv[1]->i;
  1063. const int32_t optionAv = argv[2]->i;
  1064. const int32_t optionEn = argv[3]->i;
  1065. const int64_t uniqueId = argv[4]->h;
  1066. CARLA_SAFE_ASSERT_BREAK(category >= 0);
  1067. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1068. CARLA_SAFE_ASSERT_BREAK(optionAv >= 0);
  1069. CARLA_SAFE_ASSERT_BREAK(optionEn >= 0);
  1070. pData->hints = static_cast<uint>(hints);
  1071. pData->hints |= PLUGIN_IS_BRIDGE;
  1072. pData->options = static_cast<uint>(optionEn);
  1073. fInfo.category = static_cast<PluginCategory>(category);
  1074. fInfo.uniqueId = uniqueId;
  1075. fInfo.optionsAvailable = static_cast<uint>(optionAv);
  1076. break;
  1077. }
  1078. case kPluginBridgeOscPluginInfo2: {
  1079. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ssss");
  1080. const char* const realName = (const char*)&argv[0]->s;
  1081. const char* const label = (const char*)&argv[1]->s;
  1082. const char* const maker = (const char*)&argv[2]->s;
  1083. const char* const copyright = (const char*)&argv[3]->s;
  1084. CARLA_SAFE_ASSERT_BREAK(realName != nullptr);
  1085. CARLA_SAFE_ASSERT_BREAK(label != nullptr);
  1086. CARLA_SAFE_ASSERT_BREAK(maker != nullptr);
  1087. CARLA_SAFE_ASSERT_BREAK(copyright != nullptr);
  1088. fInfo.name = realName;
  1089. fInfo.label = label;
  1090. fInfo.maker = maker;
  1091. fInfo.copyright = copyright;
  1092. if (pData->name == nullptr)
  1093. pData->name = pData->engine->getUniquePluginName(realName);
  1094. break;
  1095. }
  1096. case kPluginBridgeOscAudioCount: {
  1097. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1098. const int32_t ins = argv[0]->i;
  1099. const int32_t outs = argv[1]->i;
  1100. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1101. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1102. fInfo.aIns = static_cast<uint32_t>(ins);
  1103. fInfo.aOuts = static_cast<uint32_t>(outs);
  1104. break;
  1105. }
  1106. case kPluginBridgeOscMidiCount: {
  1107. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1108. const int32_t ins = argv[0]->i;
  1109. const int32_t outs = argv[1]->i;
  1110. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1111. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1112. fInfo.mIns = static_cast<uint32_t>(ins);
  1113. fInfo.mOuts = static_cast<uint32_t>(outs);
  1114. break;
  1115. }
  1116. case kPluginBridgeOscParameterCount: {
  1117. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1118. const int32_t ins = argv[0]->i;
  1119. const int32_t outs = argv[1]->i;
  1120. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1121. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1122. // delete old data
  1123. pData->param.clear();
  1124. if (fParams != nullptr)
  1125. {
  1126. delete[] fParams;
  1127. fParams = nullptr;
  1128. }
  1129. if (int32_t count = ins+outs)
  1130. {
  1131. const int32_t maxParams(static_cast<int32_t>(pData->engine->getOptions().maxParameters));
  1132. if (count > maxParams)
  1133. {
  1134. count = maxParams;
  1135. carla_safe_assert_int2("count <= pData->engine->getOptions().maxParameters", __FILE__, __LINE__, count, maxParams);
  1136. }
  1137. const uint32_t ucount(static_cast<uint32_t>(count));
  1138. pData->param.createNew(ucount, false);
  1139. fParams = new BridgeParamInfo[ucount];
  1140. }
  1141. break;
  1142. }
  1143. case kPluginBridgeOscProgramCount: {
  1144. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1145. const int32_t count = argv[0]->i;
  1146. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1147. pData->prog.clear();
  1148. if (count > 0)
  1149. pData->prog.createNew(static_cast<uint32_t>(count));
  1150. break;
  1151. }
  1152. case kPluginBridgeOscMidiProgramCount: {
  1153. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1154. const int32_t count = argv[0]->i;
  1155. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1156. pData->midiprog.clear();
  1157. if (count > 0)
  1158. pData->midiprog.createNew(static_cast<uint32_t>(count));
  1159. break;
  1160. }
  1161. case kPluginBridgeOscParameterData1: {
  1162. CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiii");
  1163. const int32_t index = argv[0]->i;
  1164. const int32_t rindex = argv[1]->i;
  1165. const int32_t type = argv[2]->i;
  1166. const int32_t hints = argv[3]->i;
  1167. const int32_t midiCC = argv[4]->i;
  1168. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1169. CARLA_SAFE_ASSERT_BREAK(rindex >= 0);
  1170. CARLA_SAFE_ASSERT_BREAK(type >= 0);
  1171. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1172. CARLA_SAFE_ASSERT_BREAK(midiCC >= -1 && midiCC < 0x5F);
  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. pData->param.data[index].type = static_cast<ParameterType>(type);
  1177. pData->param.data[index].index = index;
  1178. pData->param.data[index].rindex = rindex;
  1179. pData->param.data[index].hints = static_cast<uint>(hints);
  1180. pData->param.data[index].midiCC = static_cast<int16_t>(midiCC);
  1181. }
  1182. break;
  1183. }
  1184. case kPluginBridgeOscParameterData2: {
  1185. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iss");
  1186. const int32_t index = argv[0]->i;
  1187. const char* const name = (const char*)&argv[1]->s;
  1188. const char* const unit = (const char*)&argv[2]->s;
  1189. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1190. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1191. CARLA_SAFE_ASSERT_BREAK(unit != nullptr);
  1192. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1193. if (index < static_cast<int32_t>(pData->param.count))
  1194. {
  1195. fParams[index].name = name;
  1196. fParams[index].unit = unit;
  1197. }
  1198. break;
  1199. }
  1200. case kPluginBridgeOscParameterRanges1: {
  1201. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1202. const int32_t index = argv[0]->i;
  1203. const float def = argv[1]->f;
  1204. const float min = argv[2]->f;
  1205. const float max = argv[3]->f;
  1206. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1207. CARLA_SAFE_ASSERT_BREAK(min < max);
  1208. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1209. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1210. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1211. if (index < static_cast<int32_t>(pData->param.count))
  1212. {
  1213. pData->param.ranges[index].def = def;
  1214. pData->param.ranges[index].min = min;
  1215. pData->param.ranges[index].max = max;
  1216. }
  1217. break;
  1218. }
  1219. case kPluginBridgeOscParameterRanges2: {
  1220. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1221. const int32_t index = argv[0]->i;
  1222. const float step = argv[1]->f;
  1223. const float stepSmall = argv[2]->f;
  1224. const float stepLarge = argv[3]->f;
  1225. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1226. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1227. if (index < static_cast<int32_t>(pData->param.count))
  1228. {
  1229. pData->param.ranges[index].step = step;
  1230. pData->param.ranges[index].stepSmall = stepSmall;
  1231. pData->param.ranges[index].stepLarge = stepLarge;
  1232. }
  1233. break;
  1234. }
  1235. case kPluginBridgeOscParameterValue: {
  1236. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1237. const int32_t index = argv[0]->i;
  1238. const float value = argv[1]->f;
  1239. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1240. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1241. if (index < static_cast<int32_t>(pData->param.count))
  1242. {
  1243. const uint32_t uindex(static_cast<uint32_t>(index));
  1244. const float fixedValue(pData->param.getFixedValue(uindex, value));
  1245. fParams[uindex].value = fixedValue;
  1246. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1247. }
  1248. break;
  1249. }
  1250. case kPluginBridgeOscDefaultValue: {
  1251. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1252. const int32_t index = argv[0]->i;
  1253. const float value = argv[1]->f;
  1254. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1255. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1256. if (index < static_cast<int32_t>(pData->param.count))
  1257. pData->param.ranges[index].def = value;
  1258. break;
  1259. }
  1260. case kPluginBridgeOscCurrentProgram: {
  1261. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1262. const int32_t index = argv[0]->i;
  1263. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1264. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1265. CarlaPlugin::setProgram(index, false, true, true);
  1266. break;
  1267. }
  1268. case kPluginBridgeOscCurrentMidiProgram: {
  1269. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1270. const int32_t index = argv[0]->i;
  1271. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1272. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1273. CarlaPlugin::setMidiProgram(index, false, true, true);
  1274. break;
  1275. }
  1276. case kPluginBridgeOscProgramName: {
  1277. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "is");
  1278. const int32_t index = argv[0]->i;
  1279. const char* const name = (const char*)&argv[1]->s;
  1280. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1281. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1282. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1283. if (index < static_cast<int32_t>(pData->prog.count))
  1284. {
  1285. if (pData->prog.names[index] != nullptr)
  1286. delete[] pData->prog.names[index];
  1287. pData->prog.names[index] = carla_strdup(name);
  1288. }
  1289. break;
  1290. }
  1291. case kPluginBridgeOscMidiProgramData: {
  1292. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "iiis");
  1293. const int32_t index = argv[0]->i;
  1294. const int32_t bank = argv[1]->i;
  1295. const int32_t program = argv[2]->i;
  1296. const char* const name = (const char*)&argv[3]->s;
  1297. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1298. CARLA_SAFE_ASSERT_BREAK(bank >= 0);
  1299. CARLA_SAFE_ASSERT_BREAK(program >= 0);
  1300. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1301. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1302. if (index < static_cast<int32_t>(pData->midiprog.count))
  1303. {
  1304. if (pData->midiprog.data[index].name != nullptr)
  1305. delete[] pData->midiprog.data[index].name;
  1306. pData->midiprog.data[index].bank = static_cast<uint32_t>(bank);
  1307. pData->midiprog.data[index].program = static_cast<uint32_t>(program);
  1308. pData->midiprog.data[index].name = carla_strdup(name);
  1309. }
  1310. break;
  1311. }
  1312. case kPluginBridgeOscConfigure: {
  1313. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ss");
  1314. const char* const key = (const char*)&argv[0]->s;
  1315. const char* const value = (const char*)&argv[1]->s;
  1316. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1317. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1318. if (std::strcmp(key, CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  1319. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1320. else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVED) == 0)
  1321. fSaved = true;
  1322. break;
  1323. }
  1324. case kPluginBridgeOscSetCustomData: {
  1325. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "sss");
  1326. const char* const type = (const char*)&argv[0]->s;
  1327. const char* const key = (const char*)&argv[1]->s;
  1328. const char* const value = (const char*)&argv[2]->s;
  1329. CARLA_SAFE_ASSERT_BREAK(type != nullptr);
  1330. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1331. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1332. CarlaPlugin::setCustomData(type, key, value, false);
  1333. break;
  1334. }
  1335. case kPluginBridgeOscSetChunkDataFile: {
  1336. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1337. const char* const chunkFilePath = (const char*)&argv[0]->s;
  1338. CARLA_SAFE_ASSERT_BREAK(chunkFilePath != nullptr);
  1339. String realChunkFilePath(chunkFilePath);
  1340. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1341. #ifndef CARLA_OS_WIN
  1342. // Using Wine, fix temp dir
  1343. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1344. {
  1345. // Get WINEPREFIX
  1346. String wineDir;
  1347. if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
  1348. wineDir = String(WINEPREFIX);
  1349. else
  1350. wineDir = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  1351. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1352. realChunkFilePath = wineDir;
  1353. realChunkFilePath += "/drive_";
  1354. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1355. realChunkFilePath += "/";
  1356. realChunkFilePath += driveLetterSplit[1];
  1357. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1358. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1359. }
  1360. #endif
  1361. File chunkFile(realChunkFilePath);
  1362. if (chunkFile.existsAsFile())
  1363. {
  1364. fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
  1365. chunkFile.deleteFile();
  1366. }
  1367. break;
  1368. }
  1369. case kPluginBridgeOscLatency:
  1370. // TODO
  1371. break;
  1372. case kPluginBridgeOscReady:
  1373. fInitiated = true;
  1374. break;
  1375. case kPluginBridgeOscError: {
  1376. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1377. const char* const error = (const char*)&argv[0]->s;
  1378. CARLA_ASSERT(error != nullptr);
  1379. pData->engine->setLastError(error);
  1380. fInitError = true;
  1381. fInitiated = true;
  1382. break;
  1383. }
  1384. }
  1385. return 0;
  1386. }
  1387. // -------------------------------------------------------------------
  1388. const void* getExtraStuff() const noexcept override
  1389. {
  1390. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  1391. }
  1392. bool init(const char* const filename, const char* const name, const char* const label, const char* const bridgeBinary)
  1393. {
  1394. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1395. // ---------------------------------------------------------------
  1396. // first checks
  1397. if (pData->client != nullptr)
  1398. {
  1399. pData->engine->setLastError("Plugin client is already registered");
  1400. return false;
  1401. }
  1402. // ---------------------------------------------------------------
  1403. // set info
  1404. if (name != nullptr && name[0] != '\0')
  1405. pData->name = pData->engine->getUniquePluginName(name);
  1406. pData->filename = carla_strdup(filename);
  1407. if (bridgeBinary != nullptr)
  1408. fBridgeBinary = bridgeBinary;
  1409. std::srand(static_cast<uint>(std::time(nullptr)));
  1410. // ---------------------------------------------------------------
  1411. // SHM Audio Pool
  1412. {
  1413. char tmpFileBase[64];
  1414. std::sprintf(tmpFileBase, "/carla-bridge_shm_ap_XXXXXX");
  1415. fShmAudioPool.shm = shm_mkstemp(tmpFileBase);
  1416. if (! carla_is_shm_valid(fShmAudioPool.shm))
  1417. {
  1418. carla_stdout("Failed to open or create shared memory file #1");
  1419. return false;
  1420. }
  1421. fShmAudioPool.filename = tmpFileBase;
  1422. }
  1423. // ---------------------------------------------------------------
  1424. // SHM RT Control
  1425. {
  1426. char tmpFileBase[64];
  1427. std::sprintf(tmpFileBase, "/carla-bridge_shm_rt_XXXXXX");
  1428. fShmRtControl.shm = shm_mkstemp(tmpFileBase);
  1429. if (! carla_is_shm_valid(fShmRtControl.shm))
  1430. {
  1431. carla_stdout("Failed to open or create shared memory file #2");
  1432. // clear
  1433. carla_shm_close(fShmAudioPool.shm);
  1434. return false;
  1435. }
  1436. fShmRtControl.filename = tmpFileBase;
  1437. if (! fShmRtControl.mapData())
  1438. {
  1439. carla_stdout("Failed to map shared memory file #2");
  1440. // clear
  1441. carla_shm_close(fShmRtControl.shm);
  1442. carla_shm_close(fShmAudioPool.shm);
  1443. return false;
  1444. }
  1445. CARLA_SAFE_ASSERT(fShmRtControl.data != nullptr);
  1446. if (! jackbridge_sem_init(&fShmRtControl.data->sem.server))
  1447. {
  1448. carla_stdout("Failed to initialize shared memory semaphore #1");
  1449. // clear
  1450. fShmRtControl.unmapData();
  1451. carla_shm_close(fShmRtControl.shm);
  1452. carla_shm_close(fShmAudioPool.shm);
  1453. return false;
  1454. }
  1455. if (! jackbridge_sem_init(&fShmRtControl.data->sem.client))
  1456. {
  1457. carla_stdout("Failed to initialize shared memory semaphore #2");
  1458. // clear
  1459. jackbridge_sem_destroy(&fShmRtControl.data->sem.server);
  1460. fShmRtControl.unmapData();
  1461. carla_shm_close(fShmRtControl.shm);
  1462. carla_shm_close(fShmAudioPool.shm);
  1463. return false;
  1464. }
  1465. fNeedsSemDestroy = true;
  1466. }
  1467. // ---------------------------------------------------------------
  1468. // SHM Non-RT Control
  1469. {
  1470. char tmpFileBase[64];
  1471. std::sprintf(tmpFileBase, "/carla-bridge_shm_nonrt_XXXXXX");
  1472. fShmNonRtControl.shm = shm_mkstemp(tmpFileBase);
  1473. if (! carla_is_shm_valid(fShmNonRtControl.shm))
  1474. {
  1475. carla_stdout("Failed to open or create shared memory file #3");
  1476. return false;
  1477. }
  1478. fShmNonRtControl.filename = tmpFileBase;
  1479. if (! fShmNonRtControl.mapData())
  1480. {
  1481. carla_stdout("Failed to map shared memory file #3");
  1482. // clear
  1483. fShmNonRtControl.unmapData();
  1484. carla_shm_close(fShmNonRtControl.shm);
  1485. carla_shm_close(fShmRtControl.shm);
  1486. carla_shm_close(fShmAudioPool.shm);
  1487. return false;
  1488. }
  1489. }
  1490. carla_stdout("Carla Server Info:");
  1491. carla_stdout(" sizeof(BridgeRtData): " P_SIZE, sizeof(BridgeRtData));
  1492. carla_stdout(" sizeof(BridgeNonRtData): " P_SIZE, sizeof(BridgeNonRtData));
  1493. // initial values
  1494. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtNull);
  1495. fShmNonRtControl.writeInt(static_cast<int32_t>(sizeof(BridgeRtData)));
  1496. fShmNonRtControl.writeInt(static_cast<int32_t>(sizeof(BridgeNonRtData)));
  1497. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
  1498. fShmNonRtControl.writeInt(static_cast<int32_t>(pData->engine->getBufferSize()));
  1499. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
  1500. fShmNonRtControl.writeDouble(pData->engine->getSampleRate());
  1501. fShmNonRtControl.commitWrite();
  1502. // register plugin now so we can receive OSC (and wait for it)
  1503. pData->hints |= PLUGIN_IS_BRIDGE;
  1504. pData->engine->registerEnginePlugin(pData->id, this);
  1505. // init OSC
  1506. {
  1507. char shmIdStr[18+1] = { 0 };
  1508. std::strncpy(shmIdStr, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  1509. std::strncat(shmIdStr, &fShmRtControl.filename[fShmRtControl.filename.length()-6], 6);
  1510. std::strncat(shmIdStr, &fShmNonRtControl.filename[fShmNonRtControl.filename.length()-6], 6);
  1511. pData->osc.thread.setOscData(bridgeBinary, label, getPluginTypeAsString(fPluginType), shmIdStr);
  1512. pData->osc.thread.startThread();
  1513. }
  1514. fInitiated = false;
  1515. fLastPongCounter = 0;
  1516. for (; fLastPongCounter < 200; ++fLastPongCounter)
  1517. {
  1518. if (fInitiated || ! pData->osc.thread.isThreadRunning())
  1519. break;
  1520. carla_msleep(30);
  1521. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1522. pData->engine->idle();
  1523. }
  1524. fLastPongCounter = -1;
  1525. if (fInitError || ! fInitiated)
  1526. {
  1527. pData->osc.thread.stopThread(6000);
  1528. if (! fInitError)
  1529. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n(or the plugin crashed on initialization?)");
  1530. return false;
  1531. }
  1532. // ---------------------------------------------------------------
  1533. // register client
  1534. if (pData->name == nullptr)
  1535. {
  1536. if (name != nullptr && name[0] != '\0')
  1537. pData->name = pData->engine->getUniquePluginName(name);
  1538. else if (label != nullptr && label[0] != '\0')
  1539. pData->name = pData->engine->getUniquePluginName(label);
  1540. else
  1541. pData->name = pData->engine->getUniquePluginName("unknown");
  1542. }
  1543. pData->client = pData->engine->addClient(this);
  1544. if (pData->client == nullptr || ! pData->client->isOk())
  1545. {
  1546. pData->engine->setLastError("Failed to register plugin client");
  1547. return false;
  1548. }
  1549. return true;
  1550. }
  1551. private:
  1552. const BinaryType fBinaryType;
  1553. const PluginType fPluginType;
  1554. bool fInitiated;
  1555. bool fInitError;
  1556. bool fSaved;
  1557. bool fNeedsSemDestroy;
  1558. bool fTimedOut;
  1559. volatile int32_t fLastPongCounter;
  1560. CarlaString fBridgeBinary;
  1561. BridgeAudioPool fShmAudioPool;
  1562. BridgeRtControl fShmRtControl;
  1563. BridgeNonRtControl fShmNonRtControl;
  1564. struct Info {
  1565. uint32_t aIns, aOuts;
  1566. uint32_t mIns, mOuts;
  1567. PluginCategory category;
  1568. uint optionsAvailable;
  1569. int64_t uniqueId;
  1570. CarlaString name;
  1571. CarlaString label;
  1572. CarlaString maker;
  1573. CarlaString copyright;
  1574. std::vector<uint8_t> chunk;
  1575. Info()
  1576. : aIns(0),
  1577. aOuts(0),
  1578. mIns(0),
  1579. mOuts(0),
  1580. category(PLUGIN_CATEGORY_NONE),
  1581. optionsAvailable(0),
  1582. uniqueId(0) {}
  1583. } fInfo;
  1584. BridgeParamInfo* fParams;
  1585. void resizeAudioPool(const uint32_t bufferSize)
  1586. {
  1587. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts);
  1588. fShmRtControl.writeOpcode(kPluginBridgeRtSetAudioPool);
  1589. fShmRtControl.writeLong(static_cast<int64_t>(fShmAudioPool.size));
  1590. fShmRtControl.commitWrite();
  1591. waitForServer();
  1592. }
  1593. bool waitForServer(const int secs = 5)
  1594. {
  1595. CARLA_SAFE_ASSERT_RETURN(! fTimedOut, false);
  1596. if (! fShmRtControl.waitForServer(secs))
  1597. {
  1598. carla_stderr("waitForServer() timeout here");
  1599. fTimedOut = true;
  1600. return false;
  1601. }
  1602. return true;
  1603. }
  1604. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BridgePlugin)
  1605. };
  1606. CARLA_BACKEND_END_NAMESPACE
  1607. #endif // ! BUILD_BRIDGE
  1608. // -------------------------------------------------------------------------------------------------------------------
  1609. CARLA_BACKEND_START_NAMESPACE
  1610. CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* const bridgeBinary)
  1611. {
  1612. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  1613. #ifndef BUILD_BRIDGE
  1614. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  1615. {
  1616. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  1617. return nullptr;
  1618. }
  1619. BridgePlugin* const plugin(new BridgePlugin(init.engine, init.id, btype, ptype));
  1620. if (! plugin->init(init.filename, init.name, init.label, bridgeBinary))
  1621. {
  1622. init.engine->registerEnginePlugin(init.id, nullptr);
  1623. delete plugin;
  1624. return nullptr;
  1625. }
  1626. plugin->reload();
  1627. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1628. {
  1629. init.engine->setLastError("Carla's rack mode can only work with Stereo Bridged plugins, sorry!");
  1630. delete plugin;
  1631. return nullptr;
  1632. }
  1633. return plugin;
  1634. #else
  1635. init.engine->setLastError("Plugin bridge support not available");
  1636. return nullptr;
  1637. // unused
  1638. (void)bridgeBinary;
  1639. #endif
  1640. }
  1641. #ifndef BUILD_BRIDGE
  1642. // -------------------------------------------------------------------------------------------------------------------
  1643. // Bridge Helper
  1644. #define bridgePlugin ((BridgePlugin*)plugin)
  1645. extern int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
  1646. const int argc, const lo_arg* const* const argv, const char* const types);
  1647. int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
  1648. const int argc, const lo_arg* const* const argv, const char* const types)
  1649. {
  1650. CARLA_SAFE_ASSERT(plugin != nullptr && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0);
  1651. return bridgePlugin->setOscPluginBridgeInfo(type, argc, argv, types);
  1652. }
  1653. #undef bridgePlugin
  1654. #endif
  1655. CARLA_BACKEND_END_NAMESPACE
  1656. // -------------------------------------------------------------------------------------------------------------------