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.

1974 lines
65KB

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