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.

2145 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 "CarlaBridgeUtils.hpp"
  22. #include "CarlaMathUtils.hpp"
  23. #include "CarlaShmUtils.hpp"
  24. #include "jackbridge/JackBridge.hpp"
  25. #include <cerrno>
  26. #include <cmath>
  27. #include <ctime>
  28. #define CARLA_BRIDGE_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
  29. /* check argument count */ \
  30. if (argc != argcToCompare) \
  31. { \
  32. carla_stderr("BridgePlugin::%s() - argument count mismatch: %i != %i", __FUNCTION__, argc, argcToCompare); \
  33. return 1; \
  34. } \
  35. if (argc > 0) \
  36. { \
  37. /* check for nullness */ \
  38. if (! (types && typesToCompare)) \
  39. { \
  40. carla_stderr("BridgePlugin::%s() - argument types are null", __FUNCTION__); \
  41. return 1; \
  42. } \
  43. /* check argument types */ \
  44. if (std::strcmp(types, typesToCompare) != 0) \
  45. { \
  46. carla_stderr("BridgePlugin::%s() - argument types mismatch: '%s' != '%s'", __FUNCTION__, types, typesToCompare); \
  47. return 1; \
  48. } \
  49. }
  50. // -------------------------------------------------------------------------------------------------------------------
  51. using juce::File;
  52. using juce::MemoryBlock;
  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. }
  296. // -------------------------------------------------------------------
  297. // Information (base)
  298. BinaryType getBinaryType() const noexcept
  299. {
  300. return fBinaryType;
  301. }
  302. PluginType getType() const noexcept override
  303. {
  304. return fPluginType;
  305. }
  306. PluginCategory getCategory() const noexcept override
  307. {
  308. return fInfo.category;
  309. }
  310. int64_t getUniqueId() const noexcept override
  311. {
  312. return fInfo.uniqueId;
  313. }
  314. // -------------------------------------------------------------------
  315. // Information (count)
  316. uint32_t getMidiInCount() const noexcept override
  317. {
  318. return fInfo.mIns;
  319. }
  320. uint32_t getMidiOutCount() const noexcept override
  321. {
  322. return fInfo.mOuts;
  323. }
  324. // -------------------------------------------------------------------
  325. // Information (current data)
  326. std::size_t getChunkData(void** const dataPtr) noexcept override
  327. {
  328. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  329. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  330. CARLA_SAFE_ASSERT_RETURN(fInfo.chunk.getSize() > 0, 0);
  331. *dataPtr = fInfo.chunk.getData();
  332. return fInfo.chunk.getSize();
  333. }
  334. // -------------------------------------------------------------------
  335. // Information (per-plugin data)
  336. uint getOptionsAvailable() const noexcept override
  337. {
  338. return fInfo.optionsAvailable;
  339. }
  340. float getParameterValue(const uint32_t parameterId) const noexcept override
  341. {
  342. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  343. return fParams[parameterId].value;
  344. }
  345. void getLabel(char* const strBuf) const noexcept override
  346. {
  347. std::strncpy(strBuf, fInfo.label, STR_MAX);
  348. }
  349. void getMaker(char* const strBuf) const noexcept override
  350. {
  351. std::strncpy(strBuf, fInfo.maker, STR_MAX);
  352. }
  353. void getCopyright(char* const strBuf) const noexcept override
  354. {
  355. std::strncpy(strBuf, fInfo.copyright, STR_MAX);
  356. }
  357. void getRealName(char* const strBuf) const noexcept override
  358. {
  359. std::strncpy(strBuf, fInfo.name, STR_MAX);
  360. }
  361. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  362. {
  363. CARLA_ASSERT(parameterId < pData->param.count);
  364. std::strncpy(strBuf, fParams[parameterId].name.buffer(), STR_MAX);
  365. }
  366. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  367. {
  368. CARLA_ASSERT(parameterId < pData->param.count);
  369. std::strncpy(strBuf, fParams[parameterId].unit.buffer(), STR_MAX);
  370. }
  371. // -------------------------------------------------------------------
  372. // Set data (state)
  373. void prepareForSave() override
  374. {
  375. fSaved = false;
  376. {
  377. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  378. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPrepareForSave);
  379. fShmNonRtControl.commitWrite();
  380. }
  381. carla_stdout("BridgePlugin::prepareForSave() - sent, now waiting...");
  382. for (int i=0; i < 200; ++i)
  383. {
  384. if (fSaved)
  385. break;
  386. carla_msleep(30);
  387. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  388. pData->engine->idle();
  389. }
  390. if (! fSaved)
  391. carla_stderr("BridgePlugin::prepareForSave() - Timeout while requesting save state");
  392. else
  393. carla_stdout("BridgePlugin::prepareForSave() - success!");
  394. }
  395. // -------------------------------------------------------------------
  396. // Set data (internal stuff)
  397. void setOption(const uint option, const bool yesNo, const bool sendCallback) override
  398. {
  399. {
  400. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  401. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetOption);
  402. fShmNonRtControl.writeUInt(option);
  403. fShmNonRtControl.writeBool(yesNo);
  404. fShmNonRtControl.commitWrite();
  405. }
  406. CarlaPlugin::setOption(option, yesNo, sendCallback);
  407. }
  408. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  411. {
  412. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  413. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetCtrlChannel);
  414. fShmNonRtControl.writeShort(channel);
  415. fShmNonRtControl.commitWrite();
  416. }
  417. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  418. }
  419. // -------------------------------------------------------------------
  420. // Set data (plugin-specific stuff)
  421. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  422. {
  423. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  424. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  425. fParams[parameterId].value = fixedValue;
  426. {
  427. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  428. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
  429. fShmNonRtControl.writeUInt(parameterId);
  430. fShmNonRtControl.writeFloat(value);
  431. fShmNonRtControl.commitWrite();
  432. }
  433. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  434. }
  435. void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  436. {
  437. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  438. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  439. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  440. {
  441. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  442. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
  443. fShmNonRtControl.writeUInt(parameterId);
  444. fShmNonRtControl.writeByte(channel);
  445. fShmNonRtControl.commitWrite();
  446. }
  447. CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
  448. }
  449. void setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept override
  450. {
  451. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  452. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  453. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F,);
  454. {
  455. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  456. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterMidiCC);
  457. fShmNonRtControl.writeUInt(parameterId);
  458. fShmNonRtControl.writeShort(cc);
  459. fShmNonRtControl.commitWrite();
  460. }
  461. CarlaPlugin::setParameterMidiCC(parameterId, cc, sendOsc, sendCallback);
  462. }
  463. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  464. {
  465. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  466. {
  467. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  468. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetProgram);
  469. fShmNonRtControl.writeInt(index);
  470. fShmNonRtControl.commitWrite();
  471. }
  472. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  473. }
  474. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  475. {
  476. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  477. {
  478. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  479. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetMidiProgram);
  480. fShmNonRtControl.writeInt(index);
  481. fShmNonRtControl.commitWrite();
  482. }
  483. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  484. }
  485. #if 0
  486. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  487. {
  488. CARLA_ASSERT(type);
  489. CARLA_ASSERT(key);
  490. CARLA_ASSERT(value);
  491. if (sendGui)
  492. {
  493. // TODO - if type is chunk|binary, store it in a file and send path instead
  494. QString cData;
  495. cData = type;
  496. cData += "·";
  497. cData += key;
  498. cData += "·";
  499. cData += value;
  500. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CUSTOM, cData.toUtf8().constData());
  501. }
  502. CarlaPlugin::setCustomData(type, key, value, sendGui);
  503. }
  504. #endif
  505. void setChunkData(const void* const data, const std::size_t dataSize) override
  506. {
  507. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  508. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  509. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  510. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  511. filePath += OS_SEP_STR;
  512. filePath += ".CarlaChunk_";
  513. filePath += fShmAudioPool.filename.buffer() + 18;
  514. if (File(filePath).replaceWithData(data, dataSize))
  515. {
  516. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  517. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  518. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetChunkDataFile);
  519. fShmNonRtControl.writeUInt(ulength);
  520. fShmNonRtControl.writeCustomData(filePath.toRawUTF8(), ulength);
  521. fShmNonRtControl.commitWrite();
  522. }
  523. }
  524. // -------------------------------------------------------------------
  525. // Set ui stuff
  526. void showCustomUI(const bool yesNo) override
  527. {
  528. {
  529. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  530. fShmNonRtControl.writeOpcode(yesNo ? kPluginBridgeNonRtShowUI : kPluginBridgeNonRtHideUI);
  531. fShmNonRtControl.commitWrite();
  532. }
  533. if (yesNo)
  534. {
  535. pData->tryTransient();
  536. }
  537. else
  538. {
  539. pData->transientTryCounter = 0;
  540. }
  541. }
  542. void idle() override
  543. {
  544. if (pData->osc.thread.isThreadRunning())
  545. {
  546. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  547. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPing);
  548. fShmNonRtControl.commitWrite();
  549. }
  550. else
  551. carla_stderr2("TESTING: Bridge has closed!");
  552. CarlaPlugin::idle();
  553. }
  554. // -------------------------------------------------------------------
  555. // Plugin state
  556. void reload() override
  557. {
  558. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  559. carla_debug("BridgePlugin::reload() - start");
  560. const EngineProcessMode processMode(pData->engine->getProccessMode());
  561. // Safely disable plugin for reload
  562. const ScopedDisabler sd(this);
  563. bool needsCtrlIn, needsCtrlOut;
  564. needsCtrlIn = needsCtrlOut = false;
  565. if (fInfo.aIns > 0)
  566. {
  567. pData->audioIn.createNew(fInfo.aIns);
  568. }
  569. if (fInfo.aOuts > 0)
  570. {
  571. pData->audioOut.createNew(fInfo.aOuts);
  572. needsCtrlIn = true;
  573. }
  574. if (fInfo.mIns > 0)
  575. needsCtrlIn = true;
  576. if (fInfo.mOuts > 0)
  577. needsCtrlOut = true;
  578. const uint portNameSize(pData->engine->getMaxPortNameSize());
  579. CarlaString portName;
  580. // Audio Ins
  581. for (uint32_t j=0; j < fInfo.aIns; ++j)
  582. {
  583. portName.clear();
  584. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  585. {
  586. portName = pData->name;
  587. portName += ":";
  588. }
  589. if (fInfo.aIns > 1)
  590. {
  591. portName += "input_";
  592. portName += CarlaString(j+1);
  593. }
  594. else
  595. portName += "input";
  596. portName.truncate(portNameSize);
  597. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  598. pData->audioIn.ports[j].rindex = j;
  599. }
  600. // Audio Outs
  601. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  602. {
  603. portName.clear();
  604. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  605. {
  606. portName = pData->name;
  607. portName += ":";
  608. }
  609. if (fInfo.aOuts > 1)
  610. {
  611. portName += "output_";
  612. portName += CarlaString(j+1);
  613. }
  614. else
  615. portName += "output";
  616. portName.truncate(portNameSize);
  617. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  618. pData->audioOut.ports[j].rindex = j;
  619. }
  620. if (needsCtrlIn)
  621. {
  622. portName.clear();
  623. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  624. {
  625. portName = pData->name;
  626. portName += ":";
  627. }
  628. portName += "event-in";
  629. portName.truncate(portNameSize);
  630. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  631. }
  632. if (needsCtrlOut)
  633. {
  634. portName.clear();
  635. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  636. {
  637. portName = pData->name;
  638. portName += ":";
  639. }
  640. portName += "event-out";
  641. portName.truncate(portNameSize);
  642. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  643. }
  644. // extra plugin hints
  645. pData->extraHints = 0x0;
  646. if (fInfo.mIns > 0)
  647. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  648. if (fInfo.mOuts > 0)
  649. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  650. if (fInfo.aIns <= 2 && fInfo.aOuts <= 2 && (fInfo.aIns == fInfo.aOuts || fInfo.aIns == 0 || fInfo.aOuts == 0))
  651. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  652. bufferSizeChanged(pData->engine->getBufferSize());
  653. reloadPrograms(true);
  654. carla_debug("BridgePlugin::reload() - end");
  655. }
  656. // -------------------------------------------------------------------
  657. // Plugin processing
  658. void activate() noexcept override
  659. {
  660. {
  661. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  662. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtActivate);
  663. fShmNonRtControl.commitWrite();
  664. }
  665. bool timedOut = true;
  666. try {
  667. timedOut = waitForServer();
  668. } catch(...) {}
  669. if (! timedOut)
  670. fTimedOut = false;
  671. }
  672. void deactivate() noexcept override
  673. {
  674. {
  675. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  676. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtDeactivate);
  677. fShmNonRtControl.commitWrite();
  678. }
  679. bool timedOut = true;
  680. try {
  681. timedOut = waitForServer();
  682. } catch(...) {}
  683. if (! timedOut)
  684. fTimedOut = false;
  685. }
  686. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  687. {
  688. // --------------------------------------------------------------------------------------------------------
  689. // Check if active
  690. if (fTimedOut || ! pData->active)
  691. {
  692. // disable any output sound
  693. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  694. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  695. return;
  696. }
  697. // --------------------------------------------------------------------------------------------------------
  698. // Check if needs reset
  699. if (pData->needsReset)
  700. {
  701. // TODO
  702. pData->needsReset = false;
  703. }
  704. // --------------------------------------------------------------------------------------------------------
  705. // Event Input
  706. if (pData->event.portIn != nullptr)
  707. {
  708. // ----------------------------------------------------------------------------------------------------
  709. // MIDI Input (External)
  710. if (pData->extNotes.mutex.tryLock())
  711. {
  712. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  713. {
  714. const ExternalMidiNote& note(it.getValue());
  715. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  716. uint8_t data1, data2, data3;
  717. data1 = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  718. data2 = note.note;
  719. data3 = note.velo;
  720. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  721. fShmRtControl.writeUInt(0); // time
  722. fShmRtControl.writeByte(3); // size
  723. fShmRtControl.writeByte(data1);
  724. fShmRtControl.writeByte(data2);
  725. fShmRtControl.writeByte(data3);
  726. fShmRtControl.commitWrite();
  727. }
  728. pData->extNotes.data.clear();
  729. pData->extNotes.mutex.unlock();
  730. } // End of MIDI Input (External)
  731. // ----------------------------------------------------------------------------------------------------
  732. // Event Input (System)
  733. bool allNotesOffSent = false;
  734. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  735. {
  736. const EngineEvent& event(pData->event.portIn->getEvent(i));
  737. // Control change
  738. switch (event.type)
  739. {
  740. case kEngineEventTypeNull:
  741. break;
  742. case kEngineEventTypeControl: {
  743. const EngineControlEvent& ctrlEvent = event.ctrl;
  744. switch (ctrlEvent.type)
  745. {
  746. case kEngineControlEventTypeNull:
  747. break;
  748. case kEngineControlEventTypeParameter:
  749. // Control backend stuff
  750. if (event.channel == pData->ctrlChannel)
  751. {
  752. float value;
  753. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  754. {
  755. value = ctrlEvent.value;
  756. setDryWet(value, false, false);
  757. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  758. break;
  759. }
  760. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  761. {
  762. value = ctrlEvent.value*127.0f/100.0f;
  763. setVolume(value, false, false);
  764. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  765. break;
  766. }
  767. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  768. {
  769. float left, right;
  770. value = ctrlEvent.value/0.5f - 1.0f;
  771. if (value < 0.0f)
  772. {
  773. left = -1.0f;
  774. right = (value*2.0f)+1.0f;
  775. }
  776. else if (value > 0.0f)
  777. {
  778. left = (value*2.0f)-1.0f;
  779. right = 1.0f;
  780. }
  781. else
  782. {
  783. left = -1.0f;
  784. right = 1.0f;
  785. }
  786. setBalanceLeft(left, false, false);
  787. setBalanceRight(right, false, false);
  788. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  789. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  790. break;
  791. }
  792. }
  793. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventParameter);
  794. fShmRtControl.writeUInt(event.time);
  795. fShmRtControl.writeByte(event.channel);
  796. fShmRtControl.writeUShort(event.ctrl.param);
  797. fShmRtControl.writeFloat(event.ctrl.value);
  798. fShmRtControl.commitWrite();
  799. break;
  800. case kEngineControlEventTypeMidiBank:
  801. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  802. {
  803. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventMidiBank);
  804. fShmRtControl.writeUInt(event.time);
  805. fShmRtControl.writeByte(event.channel);
  806. fShmRtControl.writeUShort(event.ctrl.param);
  807. fShmRtControl.commitWrite();
  808. }
  809. break;
  810. case kEngineControlEventTypeMidiProgram:
  811. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  812. {
  813. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventMidiProgram);
  814. fShmRtControl.writeUInt(event.time);
  815. fShmRtControl.writeByte(event.channel);
  816. fShmRtControl.writeUShort(event.ctrl.param);
  817. fShmRtControl.commitWrite();
  818. }
  819. break;
  820. case kEngineControlEventTypeAllSoundOff:
  821. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  822. {
  823. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventAllSoundOff);
  824. fShmRtControl.writeUInt(event.time);
  825. fShmRtControl.writeByte(event.channel);
  826. fShmRtControl.commitWrite();
  827. }
  828. break;
  829. case kEngineControlEventTypeAllNotesOff:
  830. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  831. {
  832. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  833. {
  834. allNotesOffSent = true;
  835. sendMidiAllNotesOffToCallback();
  836. }
  837. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventAllNotesOff);
  838. fShmRtControl.writeUInt(event.time);
  839. fShmRtControl.writeByte(event.channel);
  840. fShmRtControl.commitWrite();
  841. }
  842. break;
  843. } // switch (ctrlEvent.type)
  844. break;
  845. } // case kEngineEventTypeControl
  846. case kEngineEventTypeMidi: {
  847. const EngineMidiEvent& midiEvent(event.midi);
  848. if (midiEvent.size == 0 || midiEvent.size >= MAX_MIDI_VALUE)
  849. continue;
  850. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  851. uint8_t channel = event.channel;
  852. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  853. status = MIDI_STATUS_NOTE_OFF;
  854. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  855. continue;
  856. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  857. continue;
  858. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  859. continue;
  860. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  861. continue;
  862. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  863. fShmRtControl.writeUInt(event.time);
  864. fShmRtControl.writeByte(midiEvent.size);
  865. fShmRtControl.writeByte(static_cast<uint8_t>(status + channel));
  866. for (uint8_t j=1; j < midiEvent.size; ++j)
  867. fShmRtControl.writeByte(midiEvent.data[j]);
  868. fShmRtControl.commitWrite();
  869. if (status == MIDI_STATUS_NOTE_ON)
  870. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  871. else if (status == MIDI_STATUS_NOTE_OFF)
  872. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  873. } break;
  874. }
  875. }
  876. pData->postRtEvents.trySplice();
  877. } // End of Event Input
  878. processSingle(inBuffer, outBuffer, frames);
  879. }
  880. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  881. {
  882. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  883. if (pData->audioIn.count > 0)
  884. {
  885. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  886. }
  887. if (pData->audioOut.count > 0)
  888. {
  889. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  890. }
  891. // --------------------------------------------------------------------------------------------------------
  892. // Try lock, silence otherwise
  893. if (pData->engine->isOffline())
  894. {
  895. pData->singleMutex.lock();
  896. }
  897. else if (! pData->singleMutex.tryLock())
  898. {
  899. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  900. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  901. return false;
  902. }
  903. // --------------------------------------------------------------------------------------------------------
  904. // Reset audio buffers
  905. //std::memset(fShmAudioPool.data, 0, fShmAudioPool.size);
  906. for (uint32_t i=0; i < fInfo.aIns; ++i)
  907. FloatVectorOperations::copy(fShmAudioPool.data + (i * frames), inBuffer[i], static_cast<int>(frames));
  908. // --------------------------------------------------------------------------------------------------------
  909. // TimeInfo
  910. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  911. BridgeTimeInfo& bridgeTimeInfo(fShmRtControl.data->timeInfo);
  912. bridgeTimeInfo.playing = timeInfo.playing;
  913. bridgeTimeInfo.frame = timeInfo.frame;
  914. bridgeTimeInfo.usecs = timeInfo.usecs;
  915. bridgeTimeInfo.valid = timeInfo.valid;
  916. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  917. {
  918. bridgeTimeInfo.bar = timeInfo.bbt.bar;
  919. bridgeTimeInfo.beat = timeInfo.bbt.beat;
  920. bridgeTimeInfo.tick = timeInfo.bbt.tick;
  921. bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
  922. bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
  923. bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  924. bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  925. bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
  926. }
  927. // --------------------------------------------------------------------------------------------------------
  928. // Run plugin
  929. {
  930. fShmRtControl.writeOpcode(kPluginBridgeRtProcess);
  931. fShmRtControl.commitWrite();
  932. }
  933. if (! waitForServer(2))
  934. {
  935. pData->singleMutex.unlock();
  936. return true;
  937. }
  938. for (uint32_t i=0; i < fInfo.aOuts; ++i)
  939. FloatVectorOperations::copy(outBuffer[i], fShmAudioPool.data + ((i + fInfo.aIns) * frames), static_cast<int>(frames));
  940. // --------------------------------------------------------------------------------------------------------
  941. // Post-processing (dry/wet, volume and balance)
  942. {
  943. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && pData->postProc.volume != 1.0f;
  944. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  945. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  946. bool isPair;
  947. float bufValue, oldBufLeft[doBalance ? frames : 1];
  948. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  949. {
  950. // Dry/Wet
  951. if (doDryWet)
  952. {
  953. for (uint32_t k=0; k < frames; ++k)
  954. {
  955. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k];
  956. outBuffer[i][k] = (outBuffer[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  957. }
  958. }
  959. // Balance
  960. if (doBalance)
  961. {
  962. isPair = (i % 2 == 0);
  963. if (isPair)
  964. {
  965. CARLA_ASSERT(i+1 < pData->audioOut.count);
  966. FloatVectorOperations::copy(oldBufLeft, outBuffer[i], static_cast<int>(frames));
  967. }
  968. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  969. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  970. for (uint32_t k=0; k < frames; ++k)
  971. {
  972. if (isPair)
  973. {
  974. // left
  975. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  976. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  977. }
  978. else
  979. {
  980. // right
  981. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  982. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  983. }
  984. }
  985. }
  986. // Volume (and buffer copy)
  987. if (doVolume)
  988. {
  989. for (uint32_t k=0; k < frames; ++k)
  990. outBuffer[i][k] *= pData->postProc.volume;
  991. }
  992. }
  993. } // End of Post-processing
  994. // --------------------------------------------------------------------------------------------------------
  995. pData->singleMutex.unlock();
  996. return true;
  997. }
  998. void bufferSizeChanged(const uint32_t newBufferSize) override
  999. {
  1000. resizeAudioPool(newBufferSize);
  1001. {
  1002. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1003. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
  1004. fShmNonRtControl.writeUInt(newBufferSize);
  1005. fShmNonRtControl.commitWrite();
  1006. }
  1007. fShmRtControl.waitForServer(1);
  1008. }
  1009. void sampleRateChanged(const double newSampleRate) override
  1010. {
  1011. {
  1012. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1013. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
  1014. fShmNonRtControl.writeDouble(newSampleRate);
  1015. fShmNonRtControl.commitWrite();
  1016. }
  1017. fShmRtControl.waitForServer(1);
  1018. }
  1019. void offlineModeChanged(const bool isOffline) override
  1020. {
  1021. {
  1022. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1023. fShmNonRtControl.writeOpcode(isOffline ? kPluginBridgeNonRtSetOffline : kPluginBridgeNonRtSetOnline);
  1024. fShmNonRtControl.commitWrite();
  1025. }
  1026. fShmRtControl.waitForServer(1);
  1027. }
  1028. // -------------------------------------------------------------------
  1029. // Plugin buffers
  1030. void clearBuffers() noexcept override
  1031. {
  1032. if (fParams != nullptr)
  1033. {
  1034. delete[] fParams;
  1035. fParams = nullptr;
  1036. }
  1037. CarlaPlugin::clearBuffers();
  1038. }
  1039. // -------------------------------------------------------------------
  1040. // Post-poned UI Stuff
  1041. // nothing
  1042. // -------------------------------------------------------------------
  1043. int setOscPluginBridgeInfo(const PluginBridgeOscInfoType infoType, const int argc, const lo_arg* const* const argv, const char* const types)
  1044. {
  1045. #ifdef DEBUG
  1046. if (infoType != kPluginBridgeOscPong) {
  1047. carla_debug("BridgePlugin::setOscPluginBridgeInfo(%s, %i, %p, \"%s\")", PluginBridgeOscInfoType2str(infoType), argc, argv, types);
  1048. }
  1049. #endif
  1050. switch (infoType)
  1051. {
  1052. case kPluginBridgeOscNull:
  1053. break;
  1054. case kPluginBridgeOscPong:
  1055. if (fLastPongCounter > 0)
  1056. fLastPongCounter = 0;
  1057. break;
  1058. case kPluginBridgeOscPluginInfo1: {
  1059. CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiih");
  1060. const int32_t category = argv[0]->i;
  1061. const int32_t hints = argv[1]->i;
  1062. const int32_t optionAv = argv[2]->i;
  1063. const int32_t optionEn = argv[3]->i;
  1064. const int64_t uniqueId = argv[4]->h;
  1065. CARLA_SAFE_ASSERT_BREAK(category >= 0);
  1066. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1067. CARLA_SAFE_ASSERT_BREAK(optionAv >= 0);
  1068. CARLA_SAFE_ASSERT_BREAK(optionEn >= 0);
  1069. pData->hints = static_cast<uint>(hints);
  1070. pData->hints |= PLUGIN_IS_BRIDGE;
  1071. pData->options = static_cast<uint>(optionEn);
  1072. fInfo.category = static_cast<PluginCategory>(category);
  1073. fInfo.uniqueId = uniqueId;
  1074. fInfo.optionsAvailable = static_cast<uint>(optionAv);
  1075. break;
  1076. }
  1077. case kPluginBridgeOscPluginInfo2: {
  1078. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ssss");
  1079. const char* const realName = (const char*)&argv[0]->s;
  1080. const char* const label = (const char*)&argv[1]->s;
  1081. const char* const maker = (const char*)&argv[2]->s;
  1082. const char* const copyright = (const char*)&argv[3]->s;
  1083. CARLA_SAFE_ASSERT_BREAK(realName != nullptr);
  1084. CARLA_SAFE_ASSERT_BREAK(label != nullptr);
  1085. CARLA_SAFE_ASSERT_BREAK(maker != nullptr);
  1086. CARLA_SAFE_ASSERT_BREAK(copyright != nullptr);
  1087. fInfo.name = realName;
  1088. fInfo.label = label;
  1089. fInfo.maker = maker;
  1090. fInfo.copyright = copyright;
  1091. if (pData->name == nullptr)
  1092. pData->name = pData->engine->getUniquePluginName(realName);
  1093. break;
  1094. }
  1095. case kPluginBridgeOscAudioCount: {
  1096. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1097. const int32_t ins = argv[0]->i;
  1098. const int32_t outs = argv[1]->i;
  1099. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1100. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1101. fInfo.aIns = static_cast<uint32_t>(ins);
  1102. fInfo.aOuts = static_cast<uint32_t>(outs);
  1103. break;
  1104. }
  1105. case kPluginBridgeOscMidiCount: {
  1106. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1107. const int32_t ins = argv[0]->i;
  1108. const int32_t outs = argv[1]->i;
  1109. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1110. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1111. fInfo.mIns = static_cast<uint32_t>(ins);
  1112. fInfo.mOuts = static_cast<uint32_t>(outs);
  1113. break;
  1114. }
  1115. case kPluginBridgeOscParameterCount: {
  1116. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1117. const int32_t ins = argv[0]->i;
  1118. const int32_t outs = argv[1]->i;
  1119. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1120. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1121. // delete old data
  1122. pData->param.clear();
  1123. if (fParams != nullptr)
  1124. {
  1125. delete[] fParams;
  1126. fParams = nullptr;
  1127. }
  1128. if (int32_t count = ins+outs)
  1129. {
  1130. const int32_t maxParams(static_cast<int32_t>(pData->engine->getOptions().maxParameters));
  1131. if (count > maxParams)
  1132. {
  1133. count = maxParams;
  1134. carla_safe_assert_int2("count <= pData->engine->getOptions().maxParameters", __FILE__, __LINE__, count, maxParams);
  1135. }
  1136. const uint32_t ucount(static_cast<uint32_t>(count));
  1137. pData->param.createNew(ucount, false);
  1138. fParams = new BridgeParamInfo[ucount];
  1139. }
  1140. break;
  1141. }
  1142. case kPluginBridgeOscProgramCount: {
  1143. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1144. const int32_t count = argv[0]->i;
  1145. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1146. pData->prog.clear();
  1147. if (count > 0)
  1148. pData->prog.createNew(static_cast<uint32_t>(count));
  1149. break;
  1150. }
  1151. case kPluginBridgeOscMidiProgramCount: {
  1152. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1153. const int32_t count = argv[0]->i;
  1154. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1155. pData->midiprog.clear();
  1156. if (count > 0)
  1157. pData->midiprog.createNew(static_cast<uint32_t>(count));
  1158. break;
  1159. }
  1160. case kPluginBridgeOscParameterData1: {
  1161. CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiii");
  1162. const int32_t index = argv[0]->i;
  1163. const int32_t rindex = argv[1]->i;
  1164. const int32_t type = argv[2]->i;
  1165. const int32_t hints = argv[3]->i;
  1166. const int32_t midiCC = argv[4]->i;
  1167. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1168. CARLA_SAFE_ASSERT_BREAK(rindex >= 0);
  1169. CARLA_SAFE_ASSERT_BREAK(type >= 0);
  1170. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1171. CARLA_SAFE_ASSERT_BREAK(midiCC >= -1 && midiCC < 0x5F);
  1172. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1173. if (index < static_cast<int32_t>(pData->param.count))
  1174. {
  1175. pData->param.data[index].type = static_cast<ParameterType>(type);
  1176. pData->param.data[index].index = index;
  1177. pData->param.data[index].rindex = rindex;
  1178. pData->param.data[index].hints = static_cast<uint>(hints);
  1179. pData->param.data[index].midiCC = static_cast<int16_t>(midiCC);
  1180. }
  1181. break;
  1182. }
  1183. case kPluginBridgeOscParameterData2: {
  1184. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iss");
  1185. const int32_t index = argv[0]->i;
  1186. const char* const name = (const char*)&argv[1]->s;
  1187. const char* const unit = (const char*)&argv[2]->s;
  1188. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1189. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1190. CARLA_SAFE_ASSERT_BREAK(unit != nullptr);
  1191. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1192. if (index < static_cast<int32_t>(pData->param.count))
  1193. {
  1194. fParams[index].name = name;
  1195. fParams[index].unit = unit;
  1196. }
  1197. break;
  1198. }
  1199. case kPluginBridgeOscParameterRanges1: {
  1200. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1201. const int32_t index = argv[0]->i;
  1202. const float def = argv[1]->f;
  1203. const float min = argv[2]->f;
  1204. const float max = argv[3]->f;
  1205. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1206. CARLA_SAFE_ASSERT_BREAK(min < max);
  1207. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1208. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1209. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1210. if (index < static_cast<int32_t>(pData->param.count))
  1211. {
  1212. pData->param.ranges[index].def = def;
  1213. pData->param.ranges[index].min = min;
  1214. pData->param.ranges[index].max = max;
  1215. }
  1216. break;
  1217. }
  1218. case kPluginBridgeOscParameterRanges2: {
  1219. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1220. const int32_t index = argv[0]->i;
  1221. const float step = argv[1]->f;
  1222. const float stepSmall = argv[2]->f;
  1223. const float stepLarge = argv[3]->f;
  1224. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1225. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1226. if (index < static_cast<int32_t>(pData->param.count))
  1227. {
  1228. pData->param.ranges[index].step = step;
  1229. pData->param.ranges[index].stepSmall = stepSmall;
  1230. pData->param.ranges[index].stepLarge = stepLarge;
  1231. }
  1232. break;
  1233. }
  1234. case kPluginBridgeOscParameterValue: {
  1235. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1236. const int32_t index = argv[0]->i;
  1237. const float value = argv[1]->f;
  1238. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1239. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1240. if (index < static_cast<int32_t>(pData->param.count))
  1241. {
  1242. const uint32_t uindex(static_cast<uint32_t>(index));
  1243. const float fixedValue(pData->param.getFixedValue(uindex, value));
  1244. fParams[uindex].value = fixedValue;
  1245. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1246. }
  1247. break;
  1248. }
  1249. case kPluginBridgeOscDefaultValue: {
  1250. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1251. const int32_t index = argv[0]->i;
  1252. const float value = argv[1]->f;
  1253. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1254. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1255. if (index < static_cast<int32_t>(pData->param.count))
  1256. pData->param.ranges[index].def = value;
  1257. break;
  1258. }
  1259. case kPluginBridgeOscCurrentProgram: {
  1260. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1261. const int32_t index = argv[0]->i;
  1262. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1263. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1264. CarlaPlugin::setProgram(index, false, true, true);
  1265. break;
  1266. }
  1267. case kPluginBridgeOscCurrentMidiProgram: {
  1268. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1269. const int32_t index = argv[0]->i;
  1270. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1271. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1272. CarlaPlugin::setMidiProgram(index, false, true, true);
  1273. break;
  1274. }
  1275. case kPluginBridgeOscProgramName: {
  1276. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "is");
  1277. const int32_t index = argv[0]->i;
  1278. const char* const name = (const char*)&argv[1]->s;
  1279. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1280. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1281. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1282. if (index < static_cast<int32_t>(pData->prog.count))
  1283. {
  1284. if (pData->prog.names[index] != nullptr)
  1285. delete[] pData->prog.names[index];
  1286. pData->prog.names[index] = carla_strdup(name);
  1287. }
  1288. break;
  1289. }
  1290. case kPluginBridgeOscMidiProgramData: {
  1291. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "iiis");
  1292. const int32_t index = argv[0]->i;
  1293. const int32_t bank = argv[1]->i;
  1294. const int32_t program = argv[2]->i;
  1295. const char* const name = (const char*)&argv[3]->s;
  1296. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1297. CARLA_SAFE_ASSERT_BREAK(bank >= 0);
  1298. CARLA_SAFE_ASSERT_BREAK(program >= 0);
  1299. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1300. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1301. if (index < static_cast<int32_t>(pData->midiprog.count))
  1302. {
  1303. if (pData->midiprog.data[index].name != nullptr)
  1304. delete[] pData->midiprog.data[index].name;
  1305. pData->midiprog.data[index].bank = static_cast<uint32_t>(bank);
  1306. pData->midiprog.data[index].program = static_cast<uint32_t>(program);
  1307. pData->midiprog.data[index].name = carla_strdup(name);
  1308. }
  1309. break;
  1310. }
  1311. case kPluginBridgeOscConfigure: {
  1312. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ss");
  1313. const char* const key = (const char*)&argv[0]->s;
  1314. const char* const value = (const char*)&argv[1]->s;
  1315. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1316. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1317. if (std::strcmp(key, CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  1318. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1319. else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVED) == 0)
  1320. fSaved = true;
  1321. break;
  1322. }
  1323. case kPluginBridgeOscSetCustomData: {
  1324. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "sss");
  1325. const char* const type = (const char*)&argv[0]->s;
  1326. const char* const key = (const char*)&argv[1]->s;
  1327. const char* const value = (const char*)&argv[2]->s;
  1328. CARLA_SAFE_ASSERT_BREAK(type != nullptr);
  1329. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1330. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1331. CarlaPlugin::setCustomData(type, key, value, false);
  1332. break;
  1333. }
  1334. case kPluginBridgeOscSetChunkDataFile: {
  1335. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1336. const char* const chunkFilePath = (const char*)&argv[0]->s;
  1337. CARLA_SAFE_ASSERT_BREAK(chunkFilePath != nullptr);
  1338. String realChunkFilePath(chunkFilePath);
  1339. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1340. #ifndef CARLA_OS_WIN
  1341. // Using Wine, fix temp dir
  1342. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1343. {
  1344. // Get WINEPREFIX
  1345. String wineDir;
  1346. if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
  1347. wineDir = String(WINEPREFIX);
  1348. else
  1349. wineDir = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  1350. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1351. realChunkFilePath = wineDir;
  1352. realChunkFilePath += "/drive_";
  1353. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1354. realChunkFilePath += "/";
  1355. realChunkFilePath += driveLetterSplit[1];
  1356. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1357. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1358. }
  1359. #endif
  1360. File chunkFile(realChunkFilePath);
  1361. if (chunkFile.existsAsFile())
  1362. {
  1363. fInfo.chunk.reset();
  1364. chunkFile.loadFileAsData(fInfo.chunk);
  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. MemoryBlock 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. // -------------------------------------------------------------------------------------------------------------------