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.

2009 lines
67KB

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