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.

2142 lines
71KB

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