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.

2403 lines
78KB

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