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.

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