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.

1952 lines
61KB

  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2011-2013 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. #ifndef BUILD_BRIDGE
  19. #include "CarlaBridgeUtils.hpp"
  20. #include "CarlaShmUtils.hpp"
  21. #include "jackbridge/JackBridge.hpp"
  22. #include <cerrno>
  23. #include <cmath>
  24. #include <ctime>
  25. #define CARLA_BRIDGE_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
  26. /* check argument count */ \
  27. if (argc != argcToCompare) \
  28. { \
  29. carla_stderr("BridgePlugin::%s() - argument count mismatch: %i != %i", __FUNCTION__, argc, argcToCompare); \
  30. return 1; \
  31. } \
  32. if (argc > 0) \
  33. { \
  34. /* check for nullness */ \
  35. if (! (types && typesToCompare)) \
  36. { \
  37. carla_stderr("BridgePlugin::%s() - argument types are null", __FUNCTION__); \
  38. return 1; \
  39. } \
  40. /* check argument types */ \
  41. if (std::strcmp(types, typesToCompare) != 0) \
  42. { \
  43. carla_stderr("BridgePlugin::%s() - argument types mismatch: '%s' != '%s'", __FUNCTION__, types, typesToCompare); \
  44. return 1; \
  45. } \
  46. }
  47. CARLA_BACKEND_START_NAMESPACE
  48. // -------------------------------------------------------------------------------------------------------------------
  49. shm_t shm_mkstemp(char* const fileBase)
  50. {
  51. static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
  52. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  53. "0123456789";
  54. const size_t fileBaseLen((fileBase != nullptr) ? std::strlen(fileBase) : 0);
  55. shm_t fakeShm;
  56. carla_shm_init(fakeShm);
  57. if (fileBaseLen < 6)
  58. {
  59. errno = EINVAL;
  60. return fakeShm;
  61. }
  62. if (std::strcmp(fileBase + fileBaseLen - 6, "XXXXXX") != 0)
  63. {
  64. errno = EINVAL;
  65. return fakeShm;
  66. }
  67. for (;;)
  68. {
  69. for (size_t c = fileBaseLen - 6; c < fileBaseLen; ++c)
  70. {
  71. // Note the -1 to avoid the trailing '\0' in charSet.
  72. fileBase[c] = charSet[std::rand() % (sizeof(charSet) - 1)];
  73. }
  74. shm_t shm = carla_shm_create(fileBase);
  75. if (carla_is_shm_valid(shm) || errno != EEXIST)
  76. return shm;
  77. }
  78. }
  79. // -------------------------------------------------------------------------------------------------------------------
  80. struct BridgeAudioPool {
  81. CarlaString filename;
  82. float* data;
  83. size_t size;
  84. shm_t shm;
  85. BridgeAudioPool()
  86. : data(nullptr),
  87. size(0)
  88. {
  89. carla_shm_init(shm);
  90. }
  91. ~BridgeAudioPool()
  92. {
  93. // should be cleared by now
  94. CARLA_ASSERT(data == nullptr);
  95. clear();
  96. }
  97. void clear()
  98. {
  99. filename.clear();
  100. if (! carla_is_shm_valid(shm))
  101. return;
  102. if (data != nullptr)
  103. {
  104. carla_shm_unmap(shm, data, size);
  105. data = nullptr;
  106. }
  107. size = 0;
  108. carla_shm_close(shm);
  109. }
  110. void resize(const uint32_t bufferSize, const uint32_t portCount)
  111. {
  112. if (data != nullptr)
  113. carla_shm_unmap(shm, data, size);
  114. size = portCount*bufferSize*sizeof(float);
  115. if (size == 0)
  116. size = sizeof(float);
  117. data = (float*)carla_shm_map(shm, size);
  118. }
  119. };
  120. struct BridgeControl : public RingBufferControl {
  121. CarlaString filename;
  122. BridgeShmControl* data;
  123. shm_t shm;
  124. BridgeControl()
  125. : RingBufferControl(nullptr),
  126. data(nullptr)
  127. {
  128. carla_shm_init(shm);
  129. }
  130. ~BridgeControl()
  131. {
  132. // should be cleared by now
  133. CARLA_ASSERT(data == nullptr);
  134. clear();
  135. }
  136. void clear()
  137. {
  138. filename.clear();
  139. if (! carla_is_shm_valid(shm))
  140. return;
  141. if (data != nullptr)
  142. {
  143. carla_shm_unmap(shm, data, sizeof(BridgeShmControl));
  144. data = nullptr;
  145. }
  146. carla_shm_close(shm);
  147. }
  148. bool mapData()
  149. {
  150. CARLA_ASSERT(data == nullptr);
  151. if (carla_shm_map<BridgeShmControl>(shm, data))
  152. {
  153. setRingBuffer(&data->ringBuffer, true);
  154. return true;
  155. }
  156. return false;
  157. }
  158. void unmapData()
  159. {
  160. CARLA_ASSERT(data != nullptr);
  161. if (data == nullptr)
  162. return;
  163. carla_shm_unmap(shm, data, sizeof(BridgeShmControl));
  164. data = nullptr;
  165. setRingBuffer(nullptr, false);
  166. }
  167. bool waitForServer()
  168. {
  169. CARLA_ASSERT(data != nullptr);
  170. if (data == nullptr)
  171. return false;
  172. jackbridge_sem_post(&data->runServer);
  173. return jackbridge_sem_timedwait(&data->runClient, 5);
  174. }
  175. void writeOpcode(const PluginBridgeOpcode opcode)
  176. {
  177. writeInt(static_cast<int>(opcode));
  178. }
  179. };
  180. struct BridgeParamInfo {
  181. float value;
  182. CarlaString name;
  183. CarlaString unit;
  184. BridgeParamInfo()
  185. : value(0.0f) {}
  186. CARLA_DECLARE_NON_COPY_STRUCT(BridgeParamInfo)
  187. };
  188. // -------------------------------------------------------------------------------------------------------------------
  189. class BridgePlugin : public CarlaPlugin
  190. {
  191. public:
  192. BridgePlugin(CarlaEngine* const engine, const unsigned int id, const BinaryType btype, const PluginType ptype)
  193. : CarlaPlugin(engine, id),
  194. fBinaryType(btype),
  195. fPluginType(ptype),
  196. fInitiated(false),
  197. fInitError(false),
  198. fSaved(false),
  199. fNeedsSemDestroy(false),
  200. fParams(nullptr)
  201. {
  202. carla_debug("BridgePlugin::BridgePlugin(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
  203. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_BRIDGE);
  204. fHints |= PLUGIN_IS_BRIDGE;
  205. }
  206. ~BridgePlugin() override
  207. {
  208. carla_debug("BridgePlugin::~BridgePlugin()");
  209. pData->singleMutex.lock();
  210. pData->masterMutex.lock();
  211. if (pData->client != nullptr && pData->client->isActive())
  212. pData->client->deactivate();
  213. if (pData->active)
  214. {
  215. deactivate();
  216. pData->active = false;
  217. }
  218. if (pData->osc.thread.isThreadRunning())
  219. {
  220. fShmControl.writeOpcode(kPluginBridgeOpcodeQuit);
  221. fShmControl.commitWrite();
  222. fShmControl.waitForServer();
  223. }
  224. if (pData->osc.data.target != nullptr)
  225. {
  226. osc_send_hide(pData->osc.data);
  227. osc_send_quit(pData->osc.data);
  228. }
  229. pData->osc.data.free();
  230. pData->osc.thread.stopThread(6000);
  231. if (fNeedsSemDestroy)
  232. {
  233. jackbridge_sem_destroy(&fShmControl.data->runServer);
  234. jackbridge_sem_destroy(&fShmControl.data->runClient);
  235. }
  236. fShmAudioPool.clear();
  237. fShmControl.clear();
  238. clearBuffers();
  239. //info.chunk.clear();
  240. }
  241. // -------------------------------------------------------------------
  242. // Information (base)
  243. BinaryType getBinaryType() const noexcept
  244. {
  245. return fBinaryType;
  246. }
  247. PluginType getType() const noexcept override
  248. {
  249. return fPluginType;
  250. }
  251. PluginCategory getCategory() const override
  252. {
  253. return fInfo.category;
  254. }
  255. long getUniqueId() const noexcept override
  256. {
  257. return fInfo.uniqueId;
  258. }
  259. // -------------------------------------------------------------------
  260. // Information (count)
  261. uint32_t getMidiInCount() const noexcept override
  262. {
  263. return fInfo.mIns;
  264. }
  265. uint32_t getMidiOutCount() const noexcept override
  266. {
  267. return fInfo.mOuts;
  268. }
  269. // -------------------------------------------------------------------
  270. // Information (current data)
  271. int32_t getChunkData(void** const dataPtr) const override
  272. {
  273. CARLA_ASSERT(fOptions & PLUGIN_OPTION_USE_CHUNKS);
  274. CARLA_ASSERT(dataPtr != nullptr);
  275. #if 0
  276. if (! info.chunk.isEmpty())
  277. {
  278. *dataPtr = info.chunk.data();
  279. return info.chunk.size();
  280. }
  281. #endif
  282. return 0;
  283. }
  284. // -------------------------------------------------------------------
  285. // Information (per-plugin data)
  286. unsigned int getAvailableOptions() const override
  287. {
  288. unsigned int options = 0x0;
  289. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  290. options |= PLUGIN_OPTION_USE_CHUNKS;
  291. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  292. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  293. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  294. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  295. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  296. return options;
  297. }
  298. float getParameterValue(const uint32_t parameterId) const override
  299. {
  300. CARLA_ASSERT(parameterId < pData->param.count);
  301. return fParams[parameterId].value;
  302. }
  303. void getLabel(char* const strBuf) const override
  304. {
  305. std::strncpy(strBuf, (const char*)fInfo.label, STR_MAX);
  306. }
  307. void getMaker(char* const strBuf) const override
  308. {
  309. std::strncpy(strBuf, (const char*)fInfo.maker, STR_MAX);
  310. }
  311. void getCopyright(char* const strBuf) const override
  312. {
  313. std::strncpy(strBuf, (const char*)fInfo.copyright, STR_MAX);
  314. }
  315. void getRealName(char* const strBuf) const override
  316. {
  317. std::strncpy(strBuf, (const char*)fInfo.name, STR_MAX);
  318. }
  319. void getParameterName(const uint32_t parameterId, char* const strBuf) const override
  320. {
  321. CARLA_ASSERT(parameterId < pData->param.count);
  322. std::strncpy(strBuf, (const char*)fParams[parameterId].name, STR_MAX);
  323. }
  324. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const override
  325. {
  326. CARLA_ASSERT(parameterId < pData->param.count);
  327. std::strncpy(strBuf, (const char*)fParams[parameterId].unit, STR_MAX);
  328. }
  329. // -------------------------------------------------------------------
  330. // Set data (state)
  331. void prepareForSave() override
  332. {
  333. #if 0
  334. m_saved = false;
  335. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SAVE_NOW, "");
  336. for (int i=0; i < 200; ++i)
  337. {
  338. if (m_saved)
  339. break;
  340. carla_msleep(50);
  341. }
  342. if (! m_saved)
  343. carla_stderr("BridgePlugin::prepareForSave() - Timeout while requesting save state");
  344. else
  345. carla_debug("BridgePlugin::prepareForSave() - success!");
  346. #endif
  347. }
  348. // -------------------------------------------------------------------
  349. // Set data (internal stuff)
  350. // nothing
  351. // -------------------------------------------------------------------
  352. // Set data (plugin-specific stuff)
  353. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  354. {
  355. CARLA_ASSERT(parameterId < pData->param.count);
  356. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  357. fParams[parameterId].value = fixedValue;
  358. const bool doLock(sendGui || sendOsc || sendCallback);
  359. if (doLock)
  360. pData->singleMutex.lock();
  361. fShmControl.writeOpcode(kPluginBridgeOpcodeSetParameter);
  362. fShmControl.writeInt(parameterId);
  363. fShmControl.writeFloat(value);
  364. if (doLock)
  365. {
  366. fShmControl.commitWrite();
  367. pData->singleMutex.unlock();
  368. }
  369. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  370. }
  371. void setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  372. {
  373. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->prog.count));
  374. if (index < -1)
  375. index = -1;
  376. else if (index > static_cast<int32_t>(pData->prog.count))
  377. return;
  378. const bool doLock(sendGui || sendOsc || sendCallback);
  379. if (doLock)
  380. pData->singleMutex.lock();
  381. fShmControl.writeOpcode(kPluginBridgeOpcodeSetProgram);
  382. fShmControl.writeInt(index);
  383. if (doLock)
  384. {
  385. fShmControl.commitWrite();
  386. pData->singleMutex.unlock();
  387. }
  388. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  389. }
  390. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  391. {
  392. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count));
  393. if (index < -1)
  394. index = -1;
  395. else if (index > static_cast<int32_t>(pData->midiprog.count))
  396. return;
  397. const bool doLock(sendGui || sendOsc || sendCallback);
  398. if (doLock)
  399. pData->singleMutex.lock();
  400. fShmControl.writeOpcode(kPluginBridgeOpcodeSetMidiProgram);
  401. fShmControl.writeInt(index);
  402. if (doLock)
  403. {
  404. fShmControl.commitWrite();
  405. pData->singleMutex.unlock();
  406. }
  407. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  408. }
  409. #if 0
  410. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  411. {
  412. CARLA_ASSERT(type);
  413. CARLA_ASSERT(key);
  414. CARLA_ASSERT(value);
  415. if (sendGui)
  416. {
  417. // TODO - if type is chunk|binary, store it in a file and send path instead
  418. QString cData;
  419. cData = type;
  420. cData += "·";
  421. cData += key;
  422. cData += "·";
  423. cData += value;
  424. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CUSTOM, cData.toUtf8().constData());
  425. }
  426. CarlaPlugin::setCustomData(type, key, value, sendGui);
  427. }
  428. void setChunkData(const char* const stringData) override
  429. {
  430. CARLA_ASSERT(m_hints & PLUGIN_USES_CHUNKS);
  431. CARLA_ASSERT(stringData);
  432. QString filePath;
  433. filePath = QDir::tempPath();
  434. filePath += "/.CarlaChunk_";
  435. filePath += m_name;
  436. filePath = QDir::toNativeSeparators(filePath);
  437. QFile file(filePath);
  438. if (file.open(QIODevice::WriteOnly | QIODevice::Text))
  439. {
  440. QTextStream out(&file);
  441. out << stringData;
  442. file.close();
  443. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CHUNK, filePath.toUtf8().constData());
  444. }
  445. }
  446. #endif
  447. // -------------------------------------------------------------------
  448. // Set gui stuff
  449. void showGui(const bool yesNo) override
  450. {
  451. if (yesNo)
  452. osc_send_show(pData->osc.data);
  453. else
  454. osc_send_hide(pData->osc.data);
  455. }
  456. void idleGui() override
  457. {
  458. if (! pData->osc.thread.isThreadRunning())
  459. carla_stderr2("TESTING: Bridge has closed!");
  460. CarlaPlugin::idleGui();
  461. }
  462. // -------------------------------------------------------------------
  463. // Plugin state
  464. void reload() override
  465. {
  466. carla_debug("BridgePlugin::reload() - start");
  467. CARLA_ASSERT(pData->engine != nullptr);
  468. if (pData->engine == nullptr)
  469. return;
  470. const ProcessMode processMode(pData->engine->getProccessMode());
  471. // Safely disable plugin for reload
  472. const ScopedDisabler sd(this);
  473. bool needsCtrlIn, needsCtrlOut;
  474. needsCtrlIn = needsCtrlOut = false;
  475. if (fInfo.aIns > 0)
  476. {
  477. pData->audioIn.createNew(fInfo.aIns);
  478. }
  479. if (fInfo.aOuts > 0)
  480. {
  481. pData->audioOut.createNew(fInfo.aOuts);
  482. needsCtrlIn = true;
  483. }
  484. if (fInfo.mIns > 0)
  485. needsCtrlIn = true;
  486. if (fInfo.mOuts > 0)
  487. needsCtrlOut = true;
  488. const uint portNameSize(pData->engine->getMaxPortNameSize());
  489. CarlaString portName;
  490. // Audio Ins
  491. for (uint32_t j=0; j < fInfo.aIns; ++j)
  492. {
  493. portName.clear();
  494. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  495. {
  496. portName = fName;
  497. portName += ":";
  498. }
  499. if (fInfo.aIns > 1)
  500. {
  501. portName += "input_";
  502. portName += CarlaString(j+1);
  503. }
  504. else
  505. portName += "input";
  506. portName.truncate(portNameSize);
  507. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  508. pData->audioIn.ports[j].rindex = j;
  509. }
  510. // Audio Outs
  511. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  512. {
  513. portName.clear();
  514. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  515. {
  516. portName = fName;
  517. portName += ":";
  518. }
  519. if (fInfo.aOuts > 1)
  520. {
  521. portName += "output_";
  522. portName += CarlaString(j+1);
  523. }
  524. else
  525. portName += "output";
  526. portName.truncate(portNameSize);
  527. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  528. pData->audioOut.ports[j].rindex = j;
  529. }
  530. if (needsCtrlIn)
  531. {
  532. portName.clear();
  533. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  534. {
  535. portName = fName;
  536. portName += ":";
  537. }
  538. portName += "event-in";
  539. portName.truncate(portNameSize);
  540. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  541. }
  542. if (needsCtrlOut)
  543. {
  544. portName.clear();
  545. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  546. {
  547. portName = fName;
  548. portName += ":";
  549. }
  550. portName += "event-out";
  551. portName.truncate(portNameSize);
  552. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  553. }
  554. bufferSizeChanged(pData->engine->getBufferSize());
  555. reloadPrograms(true);
  556. carla_debug("BridgePlugin::reload() - end");
  557. }
  558. // -------------------------------------------------------------------
  559. // Plugin processing
  560. void activate() override
  561. {
  562. // already locked before
  563. fShmControl.writeOpcode(kPluginBridgeOpcodeSetParameter);
  564. fShmControl.writeInt(PARAMETER_ACTIVE);
  565. fShmControl.writeFloat(1.0f);
  566. fShmControl.commitWrite();
  567. waitForServer();
  568. }
  569. void deactivate() override
  570. {
  571. // already locked before
  572. fShmControl.writeOpcode(kPluginBridgeOpcodeSetParameter);
  573. fShmControl.writeInt(PARAMETER_ACTIVE);
  574. fShmControl.writeFloat(0.0f);
  575. fShmControl.commitWrite();
  576. waitForServer();
  577. }
  578. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  579. {
  580. uint32_t i, k;
  581. // --------------------------------------------------------------------------------------------------------
  582. // Check if active
  583. if (! pData->active)
  584. {
  585. // disable any output sound
  586. for (i=0; i < pData->audioOut.count; ++i)
  587. carla_zeroFloat(outBuffer[i], frames);
  588. return;
  589. }
  590. // --------------------------------------------------------------------------------------------------------
  591. // Check if needs reset
  592. if (pData->needsReset)
  593. {
  594. // TODO
  595. pData->needsReset = false;
  596. }
  597. // --------------------------------------------------------------------------------------------------------
  598. // Event Input
  599. if (pData->event.portIn != nullptr)
  600. {
  601. // ----------------------------------------------------------------------------------------------------
  602. // MIDI Input (External)
  603. if (pData->extNotes.mutex.tryLock())
  604. {
  605. while (! pData->extNotes.data.isEmpty())
  606. {
  607. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  608. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  609. char data1, data2, data3;
  610. data1 = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  611. data1 += note.channel;
  612. data2 = note.note;
  613. data3 = note.velo;
  614. fShmControl.writeOpcode(kPluginBridgeOpcodeMidiEvent);
  615. fShmControl.writeLong(0);
  616. fShmControl.writeInt(3);
  617. fShmControl.writeChar(data1);
  618. fShmControl.writeChar(data2);
  619. fShmControl.writeChar(data3);
  620. }
  621. pData->extNotes.mutex.unlock();
  622. } // End of MIDI Input (External)
  623. // ----------------------------------------------------------------------------------------------------
  624. // Event Input (System)
  625. bool allNotesOffSent = false;
  626. uint32_t nEvents = pData->event.portIn->getEventCount();
  627. uint32_t nextBankId = 0;
  628. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  629. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  630. for (i=0; i < nEvents; ++i)
  631. {
  632. const EngineEvent& event(pData->event.portIn->getEvent(i));
  633. // Control change
  634. switch (event.type)
  635. {
  636. case kEngineEventTypeNull:
  637. break;
  638. case kEngineEventTypeControl:
  639. {
  640. const EngineControlEvent& ctrlEvent = event.ctrl;
  641. switch (ctrlEvent.type)
  642. {
  643. case kEngineControlEventTypeNull:
  644. break;
  645. case kEngineControlEventTypeParameter:
  646. {
  647. // Control backend stuff
  648. if (event.channel == pData->ctrlChannel)
  649. {
  650. float value;
  651. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  652. {
  653. value = ctrlEvent.value;
  654. setDryWet(value, false, false);
  655. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  656. }
  657. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  658. {
  659. value = ctrlEvent.value*127.0f/100.0f;
  660. setVolume(value, false, false);
  661. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  662. }
  663. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  664. {
  665. float left, right;
  666. value = ctrlEvent.value/0.5f - 1.0f;
  667. if (value < 0.0f)
  668. {
  669. left = -1.0f;
  670. right = (value*2.0f)+1.0f;
  671. }
  672. else if (value > 0.0f)
  673. {
  674. left = (value*2.0f)-1.0f;
  675. right = 1.0f;
  676. }
  677. else
  678. {
  679. left = -1.0f;
  680. right = 1.0f;
  681. }
  682. setBalanceLeft(left, false, false);
  683. setBalanceRight(right, false, false);
  684. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  685. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  686. }
  687. }
  688. // Control plugin parameters
  689. for (k=0; k < pData->param.count; ++k)
  690. {
  691. if (pData->param.data[k].midiChannel != event.channel)
  692. continue;
  693. if (pData->param.data[k].midiCC != ctrlEvent.param)
  694. continue;
  695. if (pData->param.data[k].type != PARAMETER_INPUT)
  696. continue;
  697. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  698. continue;
  699. float value;
  700. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  701. {
  702. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  703. }
  704. else
  705. {
  706. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  707. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  708. value = std::rint(value);
  709. }
  710. setParameterValue(k, value, false, false, false);
  711. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  712. }
  713. if ((fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  714. {
  715. fShmControl.writeOpcode(kPluginBridgeOpcodeMidiEvent);
  716. fShmControl.writeLong(event.time);
  717. fShmControl.writeInt(3);
  718. fShmControl.writeChar(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  719. fShmControl.writeChar(ctrlEvent.param);
  720. fShmControl.writeChar(ctrlEvent.value*127.0f);
  721. }
  722. break;
  723. }
  724. case kEngineControlEventTypeMidiBank:
  725. if (event.channel == pData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  726. nextBankId = ctrlEvent.param;
  727. break;
  728. case kEngineControlEventTypeMidiProgram:
  729. if (event.channel == pData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  730. {
  731. const uint32_t nextProgramId(ctrlEvent.param);
  732. if (pData->midiprog.count > 0)
  733. {
  734. for (k=0; k < pData->midiprog.count; ++k)
  735. {
  736. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  737. {
  738. setMidiProgram(k, false, false, false);
  739. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  740. break;
  741. }
  742. }
  743. }
  744. else
  745. {
  746. }
  747. }
  748. break;
  749. case kEngineControlEventTypeAllSoundOff:
  750. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  751. {
  752. // TODO
  753. }
  754. break;
  755. case kEngineControlEventTypeAllNotesOff:
  756. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  757. {
  758. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  759. {
  760. allNotesOffSent = true;
  761. sendMidiAllNotesOffToCallback();
  762. }
  763. // TODO
  764. }
  765. break;
  766. }
  767. break;
  768. }
  769. case kEngineEventTypeMidi:
  770. {
  771. const EngineMidiEvent& midiEvent(event.midi);
  772. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  773. uint8_t channel = event.channel;
  774. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  775. continue;
  776. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  777. continue;
  778. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  779. continue;
  780. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  781. continue;
  782. // Fix bad note-off
  783. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  784. status -= 0x10;
  785. char data[4];
  786. data[0] = status + channel;
  787. data[1] = midiEvent.data[1];
  788. data[2] = midiEvent.data[2];
  789. data[3] = midiEvent.data[3];
  790. fShmControl.writeOpcode(kPluginBridgeOpcodeMidiEvent);
  791. fShmControl.writeLong(event.time);
  792. fShmControl.writeInt(midiEvent.size);
  793. for (uint8_t j=0; j < midiEvent.size && j < 4; ++j)
  794. fShmControl.writeChar(data[j]);
  795. if (status == MIDI_STATUS_NOTE_ON)
  796. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  797. else if (status == MIDI_STATUS_NOTE_OFF)
  798. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  799. break;
  800. }
  801. }
  802. }
  803. pData->postRtEvents.trySplice();
  804. } // End of Event Input
  805. processSingle(inBuffer, outBuffer, frames);
  806. }
  807. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  808. {
  809. CARLA_ASSERT(frames > 0);
  810. if (frames == 0)
  811. return false;
  812. if (pData->audioIn.count > 0)
  813. {
  814. CARLA_ASSERT(inBuffer != nullptr);
  815. if (inBuffer == nullptr)
  816. return false;
  817. }
  818. if (pData->audioOut.count > 0)
  819. {
  820. CARLA_ASSERT(outBuffer != nullptr);
  821. if (outBuffer == nullptr)
  822. return false;
  823. }
  824. uint32_t i, k;
  825. // --------------------------------------------------------------------------------------------------------
  826. // Try lock, silence otherwise
  827. if (pData->engine->isOffline())
  828. {
  829. pData->singleMutex.lock();
  830. }
  831. else if (! pData->singleMutex.tryLock())
  832. {
  833. for (i=0; i < pData->audioOut.count; ++i)
  834. carla_zeroFloat(outBuffer[i], frames);
  835. return false;
  836. }
  837. // --------------------------------------------------------------------------------------------------------
  838. // Reset audio buffers
  839. for (i=0; i < fInfo.aIns; ++i)
  840. carla_copyFloat(fShmAudioPool.data + (i * frames), inBuffer[i], frames);
  841. // --------------------------------------------------------------------------------------------------------
  842. // Run plugin
  843. fShmControl.writeOpcode(kPluginBridgeOpcodeProcess);
  844. fShmControl.commitWrite();
  845. if (! waitForServer())
  846. {
  847. pData->singleMutex.unlock();
  848. return true;
  849. }
  850. for (i=0; i < fInfo.aOuts; ++i)
  851. carla_copyFloat(outBuffer[i], fShmAudioPool.data + ((i + fInfo.aIns) * frames), frames);
  852. // --------------------------------------------------------------------------------------------------------
  853. // Post-processing (dry/wet, volume and balance)
  854. {
  855. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) != 0 && pData->postProc.volume != 1.0f;
  856. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  857. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  858. bool isPair;
  859. float bufValue, oldBufLeft[doBalance ? frames : 1];
  860. for (i=0; i < pData->audioOut.count; ++i)
  861. {
  862. // Dry/Wet
  863. if (doDryWet)
  864. {
  865. for (k=0; k < frames; ++k)
  866. {
  867. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k];
  868. outBuffer[i][k] = (outBuffer[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  869. }
  870. }
  871. // Balance
  872. if (doBalance)
  873. {
  874. isPair = (i % 2 == 0);
  875. if (isPair)
  876. {
  877. CARLA_ASSERT(i+1 < pData->audioOut.count);
  878. carla_copyFloat(oldBufLeft, outBuffer[i], frames);
  879. }
  880. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  881. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  882. for (k=0; k < frames; ++k)
  883. {
  884. if (isPair)
  885. {
  886. // left
  887. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  888. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  889. }
  890. else
  891. {
  892. // right
  893. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  894. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  895. }
  896. }
  897. }
  898. // Volume
  899. if (doVolume)
  900. {
  901. for (k=0; k < frames; ++k)
  902. outBuffer[i][k] *= pData->postProc.volume;
  903. }
  904. }
  905. } // End of Post-processing
  906. // --------------------------------------------------------------------------------------------------------
  907. pData->singleMutex.unlock();
  908. return true;
  909. }
  910. void bufferSizeChanged(const uint32_t newBufferSize) override
  911. {
  912. resizeAudioPool(newBufferSize);
  913. fShmControl.writeOpcode(kPluginBridgeOpcodeSetBufferSize);
  914. fShmControl.writeInt(newBufferSize);
  915. fShmControl.commitWrite();
  916. }
  917. void sampleRateChanged(const double newSampleRate) override
  918. {
  919. fShmControl.writeOpcode(kPluginBridgeOpcodeSetSampleRate);
  920. fShmControl.writeFloat(newSampleRate);
  921. fShmControl.commitWrite();
  922. }
  923. // -------------------------------------------------------------------
  924. // Plugin buffers
  925. void clearBuffers() override
  926. {
  927. if (fParams != nullptr)
  928. {
  929. delete[] fParams;
  930. fParams = nullptr;
  931. }
  932. CarlaPlugin::clearBuffers();
  933. }
  934. // -------------------------------------------------------------------
  935. // Post-poned UI Stuff
  936. // nothing
  937. // -------------------------------------------------------------------
  938. int setOscPluginBridgeInfo(const PluginBridgeInfoType type, const int argc, const lo_arg* const* const argv, const char* const types)
  939. {
  940. carla_debug("setOscPluginBridgeInfo(%s, %i, %p, \"%s\")", PluginBridgeInfoType2str(type), argc, argv, types);
  941. switch (type)
  942. {
  943. case kPluginBridgeAudioCount:
  944. {
  945. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iii");
  946. const int32_t aIns = argv[0]->i;
  947. const int32_t aOuts = argv[1]->i;
  948. const int32_t aTotal = argv[2]->i;
  949. CARLA_ASSERT(aIns >= 0);
  950. CARLA_ASSERT(aOuts >= 0);
  951. CARLA_ASSERT(aIns + aOuts == aTotal);
  952. fInfo.aIns = aIns;
  953. fInfo.aOuts = aOuts;
  954. break;
  955. // unused
  956. (void)aTotal;
  957. }
  958. case kPluginBridgeMidiCount:
  959. {
  960. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iii");
  961. const int32_t mIns = argv[0]->i;
  962. const int32_t mOuts = argv[1]->i;
  963. const int32_t mTotal = argv[2]->i;
  964. CARLA_ASSERT(mIns >= 0);
  965. CARLA_ASSERT(mOuts >= 0);
  966. CARLA_ASSERT(mIns + mOuts == mTotal);
  967. fInfo.mIns = mIns;
  968. fInfo.mOuts = mOuts;
  969. break;
  970. // unused
  971. (void)mTotal;
  972. }
  973. case kPluginBridgeParameterCount:
  974. {
  975. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iii");
  976. const int32_t pIns = argv[0]->i;
  977. const int32_t pOuts = argv[1]->i;
  978. const int32_t pTotal = argv[2]->i;
  979. CARLA_ASSERT(pIns >= 0);
  980. CARLA_ASSERT(pOuts >= 0);
  981. CARLA_ASSERT(pIns + pOuts <= pTotal);
  982. // delete old data
  983. pData->param.clear();
  984. if (fParams != nullptr)
  985. {
  986. delete[] fParams;
  987. fParams = nullptr;
  988. }
  989. CARLA_SAFE_ASSERT_INT2(pTotal < static_cast<int32_t>(pData->engine->getOptions().maxParameters), pTotal, pData->engine->getOptions().maxParameters);
  990. const int32_t count(carla_min<int32_t>(pTotal, pData->engine->getOptions().maxParameters, 0));
  991. if (count > 0)
  992. {
  993. pData->param.createNew(count);
  994. fParams = new BridgeParamInfo[count];
  995. }
  996. break;
  997. // unused
  998. (void)pIns;
  999. (void)pOuts;
  1000. }
  1001. case kPluginBridgeProgramCount:
  1002. {
  1003. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1004. const int32_t count = argv[0]->i;
  1005. CARLA_ASSERT(count >= 0);
  1006. pData->prog.clear();
  1007. if (count > 0)
  1008. pData->prog.createNew(count);
  1009. break;
  1010. }
  1011. case kPluginBridgeMidiProgramCount:
  1012. {
  1013. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1014. const int32_t count = argv[0]->i;
  1015. CARLA_ASSERT(count >= 0);
  1016. pData->midiprog.clear();
  1017. if (count > 0)
  1018. pData->midiprog.createNew(count);
  1019. break;
  1020. }
  1021. case kPluginBridgePluginInfo:
  1022. {
  1023. CARLA_BRIDGE_CHECK_OSC_TYPES(7, "iissssh");
  1024. const int32_t category = argv[0]->i;
  1025. const int32_t hints = argv[1]->i;
  1026. const char* const name = (const char*)&argv[2]->s;
  1027. const char* const label = (const char*)&argv[3]->s;
  1028. const char* const maker = (const char*)&argv[4]->s;
  1029. const char* const copyright = (const char*)&argv[5]->s;
  1030. const int64_t uniqueId = argv[6]->h;
  1031. CARLA_ASSERT(category >= 0);
  1032. CARLA_ASSERT(hints >= 0);
  1033. CARLA_ASSERT(name != nullptr);
  1034. CARLA_ASSERT(label != nullptr);
  1035. CARLA_ASSERT(maker != nullptr);
  1036. CARLA_ASSERT(copyright != nullptr);
  1037. fHints = hints | PLUGIN_IS_BRIDGE;
  1038. fInfo.category = static_cast<PluginCategory>(category);
  1039. fInfo.uniqueId = uniqueId;
  1040. fInfo.name = name;
  1041. fInfo.label = label;
  1042. fInfo.maker = maker;
  1043. fInfo.copyright = copyright;
  1044. if (fName.isEmpty())
  1045. fName = name;
  1046. break;
  1047. }
  1048. case kPluginBridgeParameterInfo:
  1049. {
  1050. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iss");
  1051. const int32_t index = argv[0]->i;
  1052. const char* const name = (const char*)&argv[1]->s;
  1053. const char* const unit = (const char*)&argv[2]->s;
  1054. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1055. CARLA_ASSERT(name != nullptr);
  1056. CARLA_ASSERT(unit != nullptr);
  1057. if (index >= 0 && static_cast<int32_t>(pData->param.count))
  1058. {
  1059. fParams[index].name = name;
  1060. fParams[index].unit = unit;
  1061. }
  1062. break;
  1063. }
  1064. case kPluginBridgeParameterData:
  1065. {
  1066. CARLA_BRIDGE_CHECK_OSC_TYPES(6, "iiiiii");
  1067. const int32_t index = argv[0]->i;
  1068. const int32_t type = argv[1]->i;
  1069. const int32_t rindex = argv[2]->i;
  1070. const int32_t hints = argv[3]->i;
  1071. const int32_t channel = argv[4]->i;
  1072. const int32_t cc = argv[5]->i;
  1073. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1074. CARLA_ASSERT(type >= 0);
  1075. CARLA_ASSERT(rindex >= 0);
  1076. CARLA_ASSERT(hints >= 0);
  1077. CARLA_ASSERT(channel >= 0 && channel < 16);
  1078. CARLA_ASSERT(cc >= -1);
  1079. if (index >= 0 && static_cast<int32_t>(pData->param.count))
  1080. {
  1081. pData->param.data[index].type = static_cast<ParameterType>(type);
  1082. pData->param.data[index].index = index;
  1083. pData->param.data[index].rindex = rindex;
  1084. pData->param.data[index].hints = hints;
  1085. pData->param.data[index].midiChannel = channel;
  1086. pData->param.data[index].midiCC = cc;
  1087. }
  1088. break;
  1089. }
  1090. case kPluginBridgeParameterRanges:
  1091. {
  1092. CARLA_BRIDGE_CHECK_OSC_TYPES(7, "iffffff");
  1093. const int32_t index = argv[0]->i;
  1094. const float def = argv[1]->f;
  1095. const float min = argv[2]->f;
  1096. const float max = argv[3]->f;
  1097. const float step = argv[4]->f;
  1098. const float stepSmall = argv[5]->f;
  1099. const float stepLarge = argv[6]->f;
  1100. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1101. CARLA_ASSERT(min < max);
  1102. CARLA_ASSERT(def >= min);
  1103. CARLA_ASSERT(def <= max);
  1104. if (index >= 0 && static_cast<int32_t>(pData->param.count))
  1105. {
  1106. pData->param.ranges[index].def = def;
  1107. pData->param.ranges[index].min = min;
  1108. pData->param.ranges[index].max = max;
  1109. pData->param.ranges[index].step = step;
  1110. pData->param.ranges[index].stepSmall = stepSmall;
  1111. pData->param.ranges[index].stepLarge = stepLarge;
  1112. }
  1113. break;
  1114. }
  1115. case kPluginBridgeProgramInfo:
  1116. {
  1117. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "is");
  1118. const int32_t index = argv[0]->i;
  1119. const char* const name = (const char*)&argv[1]->s;
  1120. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1121. CARLA_ASSERT(name != nullptr);
  1122. if (index >= 0 && index < static_cast<int32_t>(pData->prog.count))
  1123. {
  1124. if (pData->prog.names[index] != nullptr)
  1125. delete[] pData->prog.names[index];
  1126. pData->prog.names[index] = carla_strdup(name);
  1127. }
  1128. break;
  1129. }
  1130. case kPluginBridgeMidiProgramInfo:
  1131. {
  1132. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "iiis");
  1133. const int32_t index = argv[0]->i;
  1134. const int32_t bank = argv[1]->i;
  1135. const int32_t program = argv[2]->i;
  1136. const char* const name = (const char*)&argv[3]->s;
  1137. CARLA_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1138. CARLA_ASSERT(bank >= 0);
  1139. CARLA_ASSERT(program >= 0 && program < 128);
  1140. CARLA_ASSERT(name != nullptr);
  1141. if (index >= 0 && index < static_cast<int32_t>(pData->midiprog.count))
  1142. {
  1143. if (pData->midiprog.data[index].name != nullptr)
  1144. delete[] pData->midiprog.data[index].name;
  1145. pData->midiprog.data[index].bank = bank;
  1146. pData->midiprog.data[index].program = program;
  1147. pData->midiprog.data[index].name = carla_strdup(name);
  1148. }
  1149. break;
  1150. }
  1151. case kPluginBridgeConfigure:
  1152. {
  1153. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ss");
  1154. const char* const key = (const char*)&argv[0]->s;
  1155. const char* const value = (const char*)&argv[1]->s;
  1156. CARLA_ASSERT(key != nullptr);
  1157. CARLA_ASSERT(value != nullptr);
  1158. if (key == nullptr || value == nullptr)
  1159. break;
  1160. if (std::strcmp(key, CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  1161. pData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1162. else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVED) == 0)
  1163. fSaved = true;
  1164. break;
  1165. }
  1166. case kPluginBridgeSetParameterValue:
  1167. {
  1168. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1169. const int32_t index = argv[0]->i;
  1170. const float value = argv[1]->f;
  1171. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1172. if (index >= 0 && static_cast<int32_t>(pData->param.count))
  1173. {
  1174. const float fixedValue(pData->param.getFixedValue(index, value));
  1175. fParams[index].value = fixedValue;
  1176. CarlaPlugin::setParameterValue(index, fixedValue, false, true, true);
  1177. }
  1178. break;
  1179. }
  1180. case kPluginBridgeSetDefaultValue:
  1181. {
  1182. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1183. const int32_t index = argv[0]->i;
  1184. const float value = argv[1]->f;
  1185. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1186. if (index >= 0 && static_cast<int32_t>(pData->param.count))
  1187. pData->param.ranges[index].def = value;
  1188. break;
  1189. }
  1190. case kPluginBridgeSetProgram:
  1191. {
  1192. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1193. const int32_t index = argv[0]->i;
  1194. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1195. setProgram(index, false, true, true);
  1196. break;
  1197. }
  1198. case kPluginBridgeSetMidiProgram:
  1199. {
  1200. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1201. const int32_t index = argv[0]->i;
  1202. CARLA_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1203. setMidiProgram(index, false, true, true);
  1204. break;
  1205. }
  1206. case kPluginBridgeSetCustomData:
  1207. {
  1208. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "sss");
  1209. const char* const type = (const char*)&argv[0]->s;
  1210. const char* const key = (const char*)&argv[1]->s;
  1211. const char* const value = (const char*)&argv[2]->s;
  1212. CARLA_ASSERT(type != nullptr);
  1213. CARLA_ASSERT(key != nullptr);
  1214. CARLA_ASSERT(value != nullptr);
  1215. setCustomData(type, key, value, false);
  1216. break;
  1217. }
  1218. case kPluginBridgeSetChunkData:
  1219. {
  1220. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1221. #if 0
  1222. const char* const chunkFileChar = (const char*)&argv[0]->s;
  1223. CARLA_ASSERT(chunkFileChar);
  1224. QString chunkFileStr(chunkFileChar);
  1225. #ifndef Q_OS_WIN
  1226. // Using Wine, fix temp dir
  1227. if (m_binary == BINARY_WIN32 || m_binary == BINARY_WIN64)
  1228. {
  1229. // Get WINEPREFIX
  1230. QString wineDir;
  1231. if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
  1232. wineDir = QString(WINEPREFIX);
  1233. else
  1234. wineDir = QDir::homePath() + "/.wine";
  1235. QStringList chunkFileStrSplit1 = chunkFileStr.split(":/");
  1236. QStringList chunkFileStrSplit2 = chunkFileStrSplit1.at(1).split("\\");
  1237. QString wineDrive = chunkFileStrSplit1.at(0).toLower();
  1238. QString wineTMP = chunkFileStrSplit2.at(0);
  1239. QString baseName = chunkFileStrSplit2.at(1);
  1240. chunkFileStr = wineDir;
  1241. chunkFileStr += "/drive_";
  1242. chunkFileStr += wineDrive;
  1243. chunkFileStr += "/";
  1244. chunkFileStr += wineTMP;
  1245. chunkFileStr += "/";
  1246. chunkFileStr += baseName;
  1247. chunkFileStr = QDir::toNativeSeparators(chunkFileStr);
  1248. }
  1249. #endif
  1250. QFile chunkFile(chunkFileStr);
  1251. if (chunkFile.open(QIODevice::ReadOnly))
  1252. {
  1253. info.chunk = chunkFile.readAll();
  1254. chunkFile.close();
  1255. chunkFile.remove();
  1256. }
  1257. #endif
  1258. break;
  1259. }
  1260. case kPluginBridgeUpdateNow:
  1261. {
  1262. fInitiated = true;
  1263. break;
  1264. }
  1265. case kPluginBridgeError:
  1266. {
  1267. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1268. const char* const error = (const char*)&argv[0]->s;
  1269. CARLA_ASSERT(error != nullptr);
  1270. pData->engine->setLastError(error);
  1271. fInitError = true;
  1272. fInitiated = true;
  1273. break;
  1274. }
  1275. }
  1276. return 0;
  1277. }
  1278. // -------------------------------------------------------------------
  1279. const void* getExtraStuff() const noexcept override
  1280. {
  1281. return fBridgeBinary.isNotEmpty() ? (const char*)fBridgeBinary : nullptr;
  1282. }
  1283. bool init(const char* const filename, const char* const name, const char* const label, const char* const bridgeBinary)
  1284. {
  1285. CARLA_ASSERT(pData->engine != nullptr);
  1286. CARLA_ASSERT(pData->client == nullptr);
  1287. // ---------------------------------------------------------------
  1288. // first checks
  1289. if (pData->engine == nullptr)
  1290. {
  1291. return false;
  1292. }
  1293. if (pData->client != nullptr)
  1294. {
  1295. pData->engine->setLastError("Plugin client is already registered");
  1296. return false;
  1297. }
  1298. // ---------------------------------------------------------------
  1299. // set info
  1300. if (name != nullptr)
  1301. fName = pData->engine->getUniquePluginName(name);
  1302. fFilename = filename;
  1303. fBridgeBinary = bridgeBinary;
  1304. // ---------------------------------------------------------------
  1305. // SHM Audio Pool
  1306. {
  1307. char tmpFileBase[60];
  1308. std::srand(std::time(NULL));
  1309. std::sprintf(tmpFileBase, "/carla-bridge_shm_XXXXXX");
  1310. fShmAudioPool.shm = shm_mkstemp(tmpFileBase);
  1311. if (! carla_is_shm_valid(fShmAudioPool.shm))
  1312. {
  1313. carla_stdout("Failed to open or create shared memory file #1");
  1314. return false;
  1315. }
  1316. fShmAudioPool.filename = tmpFileBase;
  1317. }
  1318. // ---------------------------------------------------------------
  1319. // SHM Control
  1320. {
  1321. char tmpFileBase[60];
  1322. std::sprintf(tmpFileBase, "/carla-bridge_shc_XXXXXX");
  1323. fShmControl.shm = shm_mkstemp(tmpFileBase);
  1324. if (! carla_is_shm_valid(fShmControl.shm))
  1325. {
  1326. carla_stdout("Failed to open or create shared memory file #2");
  1327. // clear
  1328. carla_shm_close(fShmAudioPool.shm);
  1329. return false;
  1330. }
  1331. fShmControl.filename = tmpFileBase;
  1332. if (! fShmControl.mapData())
  1333. {
  1334. carla_stdout("Failed to mmap shared memory file");
  1335. // clear
  1336. carla_shm_close(fShmControl.shm);
  1337. carla_shm_close(fShmAudioPool.shm);
  1338. return false;
  1339. }
  1340. CARLA_ASSERT(fShmControl.data != nullptr);
  1341. if (! jackbridge_sem_init(&fShmControl.data->runServer))
  1342. {
  1343. carla_stdout("Failed to initialize shared memory semaphore #1");
  1344. // clear
  1345. fShmControl.unmapData();
  1346. carla_shm_close(fShmControl.shm);
  1347. carla_shm_close(fShmAudioPool.shm);
  1348. return false;
  1349. }
  1350. if (! jackbridge_sem_init(&fShmControl.data->runClient))
  1351. {
  1352. carla_stdout("Failed to initialize shared memory semaphore #2");
  1353. // clear
  1354. jackbridge_sem_destroy(&fShmControl.data->runServer);
  1355. fShmControl.unmapData();
  1356. carla_shm_close(fShmControl.shm);
  1357. carla_shm_close(fShmAudioPool.shm);
  1358. return false;
  1359. }
  1360. fNeedsSemDestroy = true;
  1361. }
  1362. // initial values
  1363. fShmControl.writeOpcode(kPluginBridgeOpcodeSetBufferSize);
  1364. fShmControl.writeInt(pData->engine->getBufferSize());
  1365. fShmControl.writeOpcode(kPluginBridgeOpcodeSetSampleRate);
  1366. fShmControl.writeFloat(pData->engine->getSampleRate());
  1367. fShmControl.commitWrite();
  1368. // register plugin now so we can receive OSC (and wait for it)
  1369. fHints |= PLUGIN_IS_BRIDGE;
  1370. pData->engine->registerEnginePlugin(fId, this);
  1371. // init OSC
  1372. {
  1373. char shmIdStr[12+1] = { 0 };
  1374. std::strncpy(shmIdStr, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  1375. std::strncat(shmIdStr, &fShmControl.filename[fShmControl.filename.length()-6], 6);
  1376. pData->osc.thread.setOscData(bridgeBinary, label, getPluginTypeAsString(fPluginType), shmIdStr);
  1377. pData->osc.thread.startThread(7);
  1378. }
  1379. for (int i=0; i < 200; ++i)
  1380. {
  1381. if (fInitiated || ! pData->osc.thread.isThreadRunning())
  1382. break;
  1383. carla_msleep(50);
  1384. }
  1385. if (fInitError || ! fInitiated)
  1386. {
  1387. // unregister so it gets handled properly
  1388. pData->engine->registerEnginePlugin(fId, nullptr);
  1389. pData->osc.thread.stopThread(6000);
  1390. if (! fInitError)
  1391. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n(or the plugin crashed on initialization?)");
  1392. return false;
  1393. }
  1394. // ---------------------------------------------------------------
  1395. // register client
  1396. if (fName.isEmpty())
  1397. {
  1398. if (name != nullptr)
  1399. fName = pData->engine->getUniquePluginName(name);
  1400. else if (label != nullptr)
  1401. fName = pData->engine->getUniquePluginName(label);
  1402. else
  1403. fName = pData->engine->getUniquePluginName("unknown");
  1404. }
  1405. pData->client = pData->engine->addClient(this);
  1406. if (pData->client == nullptr || ! pData->client->isOk())
  1407. {
  1408. pData->engine->setLastError("Failed to register plugin client");
  1409. return false;
  1410. }
  1411. return true;
  1412. }
  1413. private:
  1414. const BinaryType fBinaryType;
  1415. const PluginType fPluginType;
  1416. bool fInitiated;
  1417. bool fInitError;
  1418. bool fSaved;
  1419. bool fNeedsSemDestroy;
  1420. CarlaString fBridgeBinary;
  1421. BridgeAudioPool fShmAudioPool;
  1422. BridgeControl fShmControl;
  1423. struct Info {
  1424. uint32_t aIns, aOuts;
  1425. uint32_t mIns, mOuts;
  1426. PluginCategory category;
  1427. long uniqueId;
  1428. CarlaString name;
  1429. CarlaString label;
  1430. CarlaString maker;
  1431. CarlaString copyright;
  1432. //QByteArray chunk;
  1433. Info()
  1434. : aIns(0),
  1435. aOuts(0),
  1436. mIns(0),
  1437. mOuts(0),
  1438. category(PLUGIN_CATEGORY_NONE),
  1439. uniqueId(0) {}
  1440. } fInfo;
  1441. BridgeParamInfo* fParams;
  1442. void resizeAudioPool(const uint32_t bufferSize)
  1443. {
  1444. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts);
  1445. fShmControl.writeOpcode(kPluginBridgeOpcodeSetAudioPool);
  1446. fShmControl.writeLong(fShmAudioPool.size);
  1447. fShmControl.commitWrite();
  1448. waitForServer();
  1449. }
  1450. bool waitForServer()
  1451. {
  1452. if (! fShmControl.waitForServer())
  1453. {
  1454. carla_stderr("waitForServer() timeout");
  1455. pData->active = false; // TODO
  1456. return false;
  1457. }
  1458. return true;
  1459. }
  1460. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BridgePlugin)
  1461. };
  1462. CARLA_BACKEND_END_NAMESPACE
  1463. #endif // ! BUILD_BRIDGE
  1464. CARLA_BACKEND_START_NAMESPACE
  1465. CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* const bridgeBinary)
  1466. {
  1467. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  1468. #ifndef BUILD_BRIDGE
  1469. if (bridgeBinary == nullptr)
  1470. {
  1471. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  1472. return nullptr;
  1473. }
  1474. BridgePlugin* const plugin(new BridgePlugin(init.engine, init.id, btype, ptype));
  1475. if (! plugin->init(init.filename, init.name, init.label, bridgeBinary))
  1476. {
  1477. delete plugin;
  1478. return nullptr;
  1479. }
  1480. plugin->reload();
  1481. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1482. {
  1483. init.engine->setLastError("Carla's rack mode can only work with Stereo Bridged plugins, sorry!");
  1484. delete plugin;
  1485. return nullptr;
  1486. }
  1487. return plugin;
  1488. #else
  1489. init.engine->setLastError("Plugin bridge support not available");
  1490. return nullptr;
  1491. // unused
  1492. (void)bridgeBinary;
  1493. #endif
  1494. }
  1495. #ifndef BUILD_BRIDGE
  1496. // -------------------------------------------------------------------
  1497. // Bridge Helper
  1498. #define bridgePlugin ((BridgePlugin*)plugin)
  1499. int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeInfoType type,
  1500. const int argc, const lo_arg* const* const argv, const char* const types)
  1501. {
  1502. CARLA_ASSERT(plugin != nullptr && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0);
  1503. return bridgePlugin->setOscPluginBridgeInfo(type, argc, argv, types);
  1504. }
  1505. #undef bridgePlugin
  1506. #endif
  1507. CARLA_BACKEND_END_NAMESPACE