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.

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