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.

2132 lines
67KB

  1. /*
  2. * Carla 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. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaPluginUi.hpp"
  22. #include <ctime>
  23. #include <QtCore/QFile>
  24. #include <QtCore/QTextStream>
  25. #include <QtXml/QDomNode>
  26. CARLA_BACKEND_START_NAMESPACE
  27. // -------------------------------------------------------------------
  28. // Fallback data
  29. static const ParameterData kParameterDataNull = { PARAMETER_UNKNOWN, 0x0, PARAMETER_NULL, -1, -1, 0 };
  30. static const ParameterRanges kParameterRangesNull = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
  31. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  32. static const CustomData kCustomDataNull = { nullptr, nullptr, nullptr };
  33. static bool gIsLoadingProject = false;
  34. // -------------------------------------------------------------------
  35. // ParamSymbol struct, needed for CarlaPlugin::loadSaveState()
  36. struct ParamSymbol {
  37. int32_t index;
  38. const char* symbol;
  39. ParamSymbol(uint32_t i, const char* s)
  40. : index(static_cast<int32_t>(i)),
  41. symbol(carla_strdup(s)) {}
  42. ~ParamSymbol()
  43. {
  44. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
  45. delete[] symbol;
  46. symbol = nullptr;
  47. }
  48. #ifdef CARLA_PROPER_CPP11_SUPPORT
  49. ParamSymbol() = delete;
  50. CARLA_DECLARE_NON_COPY_STRUCT(ParamSymbol)
  51. #endif
  52. };
  53. // -----------------------------------------------------------------------
  54. void CarlaPluginProtectedData::tryTransient()
  55. {
  56. if (engine->getOptions().frontendWinId != 0)
  57. transientTryCounter = 1;
  58. }
  59. // -----------------------------------------------------------------------
  60. CarlaPlugin* CarlaPlugin::newFileGIG(const Initializer& init, const bool use16Outs)
  61. {
  62. carla_debug("CarlaPlugin::newFileGIG({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  63. #ifdef WANT_LINUXSAMPLER
  64. return newLinuxSampler(init, "GIG", use16Outs);
  65. #else
  66. init.engine->setLastError("GIG support not available");
  67. return nullptr;
  68. // unused
  69. (void)use16Outs;
  70. #endif
  71. }
  72. CarlaPlugin* CarlaPlugin::newFileSF2(const Initializer& init, const bool use16Outs)
  73. {
  74. carla_debug("CarlaPlugin::newFileSF2({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  75. #if defined(WANT_FLUIDSYNTH)
  76. return newFluidSynth(init, use16Outs);
  77. #elif defined(WANT_LINUXSAMPLER)
  78. return newLinuxSampler(init, "SF2", use16Outs);
  79. #else
  80. init.engine->setLastError("SF2 support not available");
  81. return nullptr;
  82. // unused
  83. (void)use16Outs;
  84. #endif
  85. }
  86. CarlaPlugin* CarlaPlugin::newFileSFZ(const Initializer& init)
  87. {
  88. carla_debug("CarlaPlugin::newFileSFZ({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  89. #ifdef WANT_LINUXSAMPLER
  90. return newLinuxSampler(init, "SFZ", false);
  91. #else
  92. init.engine->setLastError("SFZ support not available");
  93. return nullptr;
  94. #endif
  95. }
  96. // -------------------------------------------------------------------
  97. // Constructor and destructor
  98. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const unsigned int id)
  99. : pData(new CarlaPluginProtectedData(engine, id, this))
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  102. CARLA_SAFE_ASSERT(id < engine->getMaxPluginNumber());
  103. carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  104. switch (engine->getProccessMode())
  105. {
  106. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  107. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  108. CARLA_SAFE_ASSERT(id < MAX_DEFAULT_PLUGINS);
  109. break;
  110. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  111. CARLA_SAFE_ASSERT(id < MAX_RACK_PLUGINS);
  112. break;
  113. case ENGINE_PROCESS_MODE_PATCHBAY:
  114. CARLA_SAFE_ASSERT(id < MAX_PATCHBAY_PLUGINS);
  115. break;
  116. case ENGINE_PROCESS_MODE_BRIDGE:
  117. CARLA_SAFE_ASSERT(id == 0);
  118. break;
  119. }
  120. }
  121. CarlaPlugin::~CarlaPlugin()
  122. {
  123. carla_debug("CarlaPlugin::~CarlaPlugin()");
  124. delete pData;
  125. }
  126. // -------------------------------------------------------------------
  127. // Information (base)
  128. unsigned int CarlaPlugin::getId() const noexcept
  129. {
  130. return pData->id;
  131. }
  132. unsigned int CarlaPlugin::getHints() const noexcept
  133. {
  134. return pData->hints;
  135. }
  136. unsigned int CarlaPlugin::getOptionsEnabled() const noexcept
  137. {
  138. return pData->options;
  139. }
  140. bool CarlaPlugin::isEnabled() const noexcept
  141. {
  142. return pData->enabled;
  143. }
  144. const char* CarlaPlugin::getName() const noexcept
  145. {
  146. return pData->name;
  147. }
  148. const char* CarlaPlugin::getFilename() const noexcept
  149. {
  150. return pData->filename;
  151. }
  152. const char* CarlaPlugin::getIconName() const noexcept
  153. {
  154. return pData->iconName;
  155. }
  156. PluginCategory CarlaPlugin::getCategory() const noexcept
  157. {
  158. PluginCategory category = PLUGIN_CATEGORY_NONE;
  159. try {
  160. category = getPluginCategoryFromName(pData->name);
  161. } catch(...) {}
  162. return category;
  163. }
  164. int64_t CarlaPlugin::getUniqueId() const noexcept
  165. {
  166. return 0;
  167. }
  168. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  169. {
  170. return pData->latency;
  171. }
  172. // -------------------------------------------------------------------
  173. // Information (count)
  174. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  175. {
  176. return pData->audioIn.count;
  177. }
  178. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  179. {
  180. return pData->audioOut.count;
  181. }
  182. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  183. {
  184. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  185. }
  186. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  187. {
  188. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  189. }
  190. uint32_t CarlaPlugin::getParameterCount() const noexcept
  191. {
  192. return pData->param.count;
  193. }
  194. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
  195. {
  196. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  197. return 0;
  198. }
  199. uint32_t CarlaPlugin::getProgramCount() const noexcept
  200. {
  201. return pData->prog.count;
  202. }
  203. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  204. {
  205. return pData->midiprog.count;
  206. }
  207. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  208. {
  209. return static_cast<uint32_t>(pData->custom.count());
  210. }
  211. // -------------------------------------------------------------------
  212. // Information (current data)
  213. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  214. {
  215. return pData->prog.current;
  216. }
  217. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  218. {
  219. return pData->midiprog.current;
  220. }
  221. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  224. return pData->param.data[parameterId];
  225. }
  226. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
  227. {
  228. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  229. return pData->param.ranges[parameterId];
  230. }
  231. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
  232. {
  233. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  234. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  235. }
  236. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
  237. {
  238. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  239. return pData->midiprog.data[index];
  240. }
  241. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
  242. {
  243. CARLA_SAFE_ASSERT_RETURN(index < pData->custom.count(), kCustomDataNull);
  244. return pData->custom.getAt(index);
  245. }
  246. int32_t CarlaPlugin::getChunkData(void** const dataPtr) const noexcept
  247. {
  248. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  249. CARLA_SAFE_ASSERT(false); // this should never happen
  250. return 0;
  251. }
  252. // -------------------------------------------------------------------
  253. // Information (per-plugin data)
  254. unsigned int CarlaPlugin::getOptionsAvailable() const noexcept
  255. {
  256. CARLA_SAFE_ASSERT(false); // this should never happen
  257. return 0x0;
  258. }
  259. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  262. CARLA_SAFE_ASSERT(false); // this should never happen
  263. return 0.0f;
  264. }
  265. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  268. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  269. CARLA_SAFE_ASSERT(false); // this should never happen
  270. return 0.0f;
  271. }
  272. void CarlaPlugin::getLabel(char* const strBuf) const noexcept
  273. {
  274. strBuf[0] = '\0';
  275. }
  276. void CarlaPlugin::getMaker(char* const strBuf) const noexcept
  277. {
  278. strBuf[0] = '\0';
  279. }
  280. void CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  281. {
  282. strBuf[0] = '\0';
  283. }
  284. void CarlaPlugin::getRealName(char* const strBuf) const noexcept
  285. {
  286. strBuf[0] = '\0';
  287. }
  288. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  289. {
  290. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  291. CARLA_SAFE_ASSERT(false); // this should never happen
  292. strBuf[0] = '\0';
  293. }
  294. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  295. {
  296. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  297. strBuf[0] = '\0';
  298. }
  299. void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) const noexcept
  300. {
  301. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  302. CARLA_SAFE_ASSERT(false); // this should never happen
  303. strBuf[0] = '\0';
  304. }
  305. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  306. {
  307. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  308. strBuf[0] = '\0';
  309. }
  310. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  311. {
  312. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  313. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  314. CARLA_SAFE_ASSERT(false); // this should never happen
  315. strBuf[0] = '\0';
  316. }
  317. float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
  318. {
  319. CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
  320. switch (parameterId)
  321. {
  322. case PARAMETER_ACTIVE:
  323. return pData->active;
  324. case PARAMETER_CTRL_CHANNEL:
  325. return pData->ctrlChannel;
  326. #ifndef BUILD_BRIDGE
  327. case PARAMETER_DRYWET:
  328. return pData->postProc.dryWet;
  329. case PARAMETER_VOLUME:
  330. return pData->postProc.volume;
  331. case PARAMETER_BALANCE_LEFT:
  332. return pData->postProc.balanceLeft;
  333. case PARAMETER_BALANCE_RIGHT:
  334. return pData->postProc.balanceRight;
  335. case PARAMETER_PANNING:
  336. return pData->postProc.panning;
  337. #endif
  338. };
  339. CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
  340. return getParameterValue(static_cast<uint32_t>(parameterId));
  341. }
  342. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  343. {
  344. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  345. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr,);
  346. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  347. }
  348. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  349. {
  350. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  351. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr,);
  352. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  353. }
  354. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  355. {
  356. ins = 0;
  357. outs = 0;
  358. for (uint32_t i=0; i < pData->param.count; ++i)
  359. {
  360. if (pData->param.data[i].type == PARAMETER_INPUT)
  361. ++ins;
  362. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  363. ++outs;
  364. }
  365. }
  366. // -------------------------------------------------------------------
  367. // Set data (state)
  368. void CarlaPlugin::prepareForSave()
  369. {
  370. }
  371. void CarlaPlugin::resetParameters() noexcept
  372. {
  373. for (uint i=0; i < pData->param.count; ++i)
  374. {
  375. const ParameterData& paramData(pData->param.data[i]);
  376. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  377. if (paramData.type != PARAMETER_INPUT)
  378. continue;
  379. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  380. continue;
  381. setParameterValue(i, paramRanges.def, true, true, true);
  382. }
  383. }
  384. void CarlaPlugin::randomizeParameters() noexcept
  385. {
  386. float value, random;
  387. char strBuf[STR_MAX+1];
  388. strBuf[STR_MAX] = '\0';
  389. std::srand(static_cast<uint>(std::time(nullptr)));
  390. for (uint i=0; i < pData->param.count; ++i)
  391. {
  392. const ParameterData& paramData(pData->param.data[i]);
  393. if (paramData.type != PARAMETER_INPUT)
  394. continue;
  395. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  396. continue;
  397. getParameterName(i, strBuf);
  398. if (std::strstr(strBuf, "olume") != nullptr)
  399. continue;
  400. if (std::strstr(strBuf, "Master") != nullptr)
  401. continue;
  402. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  403. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  404. {
  405. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  406. value = random > 0.5 ? paramRanges.max : paramRanges.min;
  407. }
  408. else
  409. {
  410. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  411. value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
  412. if (paramData.hints & PARAMETER_IS_INTEGER)
  413. value = std::rint(value);
  414. }
  415. setParameterValue(i, value, true, true, true);
  416. }
  417. }
  418. const SaveState& CarlaPlugin::getSaveState()
  419. {
  420. pData->saveState.reset();
  421. prepareForSave();
  422. char strBuf[STR_MAX+1];
  423. // ---------------------------------------------------------------
  424. // Basic info
  425. getLabel(strBuf);
  426. pData->saveState.type = carla_strdup(getPluginTypeAsString(getType()));
  427. pData->saveState.name = carla_strdup(pData->name);
  428. pData->saveState.label = carla_strdup(strBuf);
  429. pData->saveState.binary = carla_strdup(pData->filename);
  430. pData->saveState.uniqueId = getUniqueId();
  431. // ---------------------------------------------------------------
  432. // Internals
  433. pData->saveState.active = pData->active;
  434. #ifndef BUILD_BRIDGE
  435. pData->saveState.dryWet = pData->postProc.dryWet;
  436. pData->saveState.volume = pData->postProc.volume;
  437. pData->saveState.balanceLeft = pData->postProc.balanceLeft;
  438. pData->saveState.balanceRight = pData->postProc.balanceRight;
  439. pData->saveState.panning = pData->postProc.panning;
  440. pData->saveState.ctrlChannel = pData->ctrlChannel;
  441. #endif
  442. // ---------------------------------------------------------------
  443. // Chunk
  444. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  445. {
  446. void* data = nullptr;
  447. const int32_t dataSize(getChunkData(&data));
  448. if (data != nullptr && dataSize > 0)
  449. {
  450. pData->saveState.chunk = carla_strdup(QByteArray((char*)data, dataSize).toBase64().constData());
  451. // Don't save anything else if using chunks
  452. return pData->saveState;
  453. }
  454. }
  455. // ---------------------------------------------------------------
  456. // Current Program
  457. if (pData->prog.current >= 0 && getType() != PLUGIN_LV2)
  458. {
  459. pData->saveState.currentProgramIndex = pData->prog.current;
  460. pData->saveState.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  461. }
  462. // ---------------------------------------------------------------
  463. // Current MIDI Program
  464. if (pData->midiprog.current >= 0 && getType() != PLUGIN_LV2)
  465. {
  466. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  467. pData->saveState.currentMidiBank = static_cast<int32_t>(mpData.bank);
  468. pData->saveState.currentMidiProgram = static_cast<int32_t>(mpData.program);
  469. }
  470. // ---------------------------------------------------------------
  471. // Parameters
  472. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  473. for (uint32_t i=0; i < pData->param.count; ++i)
  474. {
  475. const ParameterData& paramData(pData->param.data[i]);
  476. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  477. continue;
  478. StateParameter* const stateParameter(new StateParameter());
  479. stateParameter->isInput = (paramData.type == PARAMETER_INPUT);
  480. stateParameter->index = paramData.index;
  481. stateParameter->midiCC = paramData.midiCC;
  482. stateParameter->midiChannel = paramData.midiChannel;
  483. getParameterName(i, strBuf);
  484. stateParameter->name = carla_strdup(strBuf);
  485. getParameterSymbol(i, strBuf);
  486. stateParameter->symbol = carla_strdup(strBuf);;
  487. stateParameter->value = getParameterValue(i);
  488. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  489. stateParameter->value /= sampleRate;
  490. pData->saveState.parameters.append(stateParameter);
  491. }
  492. // ---------------------------------------------------------------
  493. // Custom Data
  494. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  495. {
  496. const CustomData& cData(it.getValue());
  497. StateCustomData* stateCustomData(new StateCustomData());
  498. stateCustomData->type = carla_strdup(cData.type);
  499. stateCustomData->key = carla_strdup(cData.key);
  500. stateCustomData->value = carla_strdup(cData.value);
  501. pData->saveState.customData.append(stateCustomData);
  502. }
  503. return pData->saveState;
  504. }
  505. void CarlaPlugin::loadSaveState(const SaveState& saveState)
  506. {
  507. char strBuf[STR_MAX+1];
  508. const bool usesMultiProgs(pData->extraHints & PLUGIN_EXTRA_HINT_USES_MULTI_PROGS);
  509. gIsLoadingProject = true;
  510. ScopedValueSetter<bool>(gIsLoadingProject, false);
  511. // ---------------------------------------------------------------
  512. // Part 1 - PRE-set custom data (only that which reload programs)
  513. for (LinkedList<StateCustomData*>::Itenerator it = saveState.customData.begin(); it.valid(); it.next())
  514. {
  515. const StateCustomData* const stateCustomData(it.getValue());
  516. const char* const key(stateCustomData->key);
  517. bool wantData = false;
  518. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  519. wantData = true;
  520. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  521. wantData = true;
  522. if (wantData)
  523. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  524. }
  525. // ---------------------------------------------------------------
  526. // Part 2 - set program
  527. if (saveState.currentProgramIndex >= 0 && saveState.currentProgramName != nullptr)
  528. {
  529. int32_t programId = -1;
  530. // index < count
  531. if (saveState.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  532. {
  533. programId = saveState.currentProgramIndex;
  534. }
  535. // index not valid, try to find by name
  536. else
  537. {
  538. for (uint32_t i=0; i < pData->prog.count; ++i)
  539. {
  540. strBuf[0] = '\0';
  541. getProgramName(i, strBuf);
  542. if (strBuf[0] != '\0' && std::strcmp(saveState.currentProgramName, strBuf) == 0)
  543. {
  544. programId = static_cast<int32_t>(i);
  545. break;
  546. }
  547. }
  548. }
  549. // set program now, if valid
  550. if (programId >= 0)
  551. setProgram(programId, true, true, true);
  552. }
  553. // ---------------------------------------------------------------
  554. // Part 3 - set midi program
  555. if (saveState.currentMidiBank >= 0 && saveState.currentMidiProgram >= 0 && ! usesMultiProgs)
  556. setMidiProgramById(static_cast<uint32_t>(saveState.currentMidiBank), static_cast<uint32_t>(saveState.currentMidiProgram), true, true, true);
  557. // ---------------------------------------------------------------
  558. // Part 4a - get plugin parameter symbols
  559. LinkedList<ParamSymbol*> paramSymbols;
  560. if (getType() == PLUGIN_LADSPA || getType() == PLUGIN_LV2)
  561. {
  562. for (uint32_t i=0; i < pData->param.count; ++i)
  563. {
  564. strBuf[0] = '\0';
  565. getParameterSymbol(i, strBuf);
  566. if (strBuf[0] != '\0')
  567. {
  568. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  569. paramSymbols.append(paramSymbol);
  570. }
  571. }
  572. }
  573. // ---------------------------------------------------------------
  574. // Part 4b - set parameter values (carefully)
  575. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  576. for (LinkedList<StateParameter*>::Itenerator it = saveState.parameters.begin(); it.valid(); it.next())
  577. {
  578. StateParameter* const stateParameter(it.getValue());
  579. int32_t index = -1;
  580. if (getType() == PLUGIN_LADSPA)
  581. {
  582. // Try to set by symbol, otherwise use index
  583. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  584. {
  585. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  586. {
  587. ParamSymbol* const paramSymbol(it2.getValue());
  588. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  589. {
  590. index = paramSymbol->index;
  591. break;
  592. }
  593. }
  594. if (index == -1)
  595. index = stateParameter->index;
  596. }
  597. else
  598. index = stateParameter->index;
  599. }
  600. else if (getType() == PLUGIN_LV2)
  601. {
  602. // Symbol only
  603. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  604. {
  605. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  606. {
  607. ParamSymbol* const paramSymbol(it2.getValue());
  608. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  609. {
  610. index = paramSymbol->index;
  611. break;
  612. }
  613. }
  614. if (index == -1)
  615. carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
  616. }
  617. else
  618. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  619. }
  620. else
  621. {
  622. // Index only
  623. index = stateParameter->index;
  624. }
  625. // Now set parameter
  626. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  627. {
  628. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  629. if (stateParameter->isInput)
  630. {
  631. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  632. stateParameter->value *= sampleRate;
  633. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  634. }
  635. #ifndef BUILD_BRIDGE
  636. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  637. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  638. #endif
  639. }
  640. else
  641. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  642. }
  643. // ---------------------------------------------------------------
  644. // Part 4c - clear
  645. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin(); it.valid(); it.next())
  646. {
  647. ParamSymbol* const paramSymbol(it.getValue());
  648. delete paramSymbol;
  649. }
  650. paramSymbols.clear();
  651. // ---------------------------------------------------------------
  652. // Part 5 - set custom data
  653. for (LinkedList<StateCustomData*>::Itenerator it = saveState.customData.begin(); it.valid(); it.next())
  654. {
  655. const StateCustomData* const stateCustomData(it.getValue());
  656. const char* const key(stateCustomData->key);
  657. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  658. continue;
  659. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  660. continue;
  661. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  662. }
  663. // ---------------------------------------------------------------
  664. // Part 5x - set lv2 state
  665. if (getType() == PLUGIN_LV2 && pData->custom.count() > 0)
  666. setCustomData(CUSTOM_DATA_TYPE_STRING, "CarlaLoadLv2StateNow", "true", true);
  667. // ---------------------------------------------------------------
  668. // Part 6 - set chunk
  669. if (saveState.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  670. setChunkData(saveState.chunk);
  671. // ---------------------------------------------------------------
  672. // Part 6 - set internal stuff
  673. #ifndef BUILD_BRIDGE
  674. setDryWet(saveState.dryWet, true, true);
  675. setVolume(saveState.volume, true, true);
  676. setBalanceLeft(saveState.balanceLeft, true, true);
  677. setBalanceRight(saveState.balanceRight, true, true);
  678. setPanning(saveState.panning, true, true);
  679. setCtrlChannel(saveState.ctrlChannel, true, true);
  680. #endif
  681. setActive(saveState.active, true, true);
  682. }
  683. bool CarlaPlugin::saveStateToFile(const char* const filename)
  684. {
  685. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  686. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  687. QFile file(filename);
  688. if (! file.open(QIODevice::WriteOnly | QIODevice::Text))
  689. return false;
  690. QString content;
  691. fillXmlStringFromSaveState(content, getSaveState());
  692. QTextStream out(&file);
  693. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  694. out << "<!DOCTYPE CARLA-PRESET>\n";
  695. out << "<CARLA-PRESET VERSION='2.0'>\n";
  696. out << content;
  697. out << "</CARLA-PRESET>\n";
  698. file.close();
  699. return true;
  700. }
  701. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  702. {
  703. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  704. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  705. QFile file(filename);
  706. if (! file.open(QIODevice::ReadOnly | QIODevice::Text))
  707. return false;
  708. QDomDocument xml;
  709. xml.setContent(file.readAll());
  710. file.close();
  711. QDomNode xmlNode(xml.documentElement());
  712. if (xmlNode.toElement().tagName().compare("carla-preset", Qt::CaseInsensitive) == 0)
  713. {
  714. pData->engine->setLastError("Not a valid Carla preset file");
  715. return false;
  716. }
  717. pData->saveState.reset();
  718. fillSaveStateFromXmlNode(pData->saveState, xmlNode);
  719. loadSaveState(pData->saveState);
  720. return true;
  721. }
  722. // -------------------------------------------------------------------
  723. // Set data (internal stuff)
  724. void CarlaPlugin::setId(const unsigned int newId) noexcept
  725. {
  726. pData->id = newId;
  727. }
  728. void CarlaPlugin::setName(const char* const newName)
  729. {
  730. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  731. if (pData->name != nullptr)
  732. delete[] pData->name;
  733. pData->name = carla_strdup(newName);
  734. }
  735. void CarlaPlugin::setOption(const unsigned int option, const bool yesNo)
  736. {
  737. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  738. if (yesNo)
  739. pData->options |= option;
  740. else
  741. pData->options &= ~option;
  742. pData->saveSetting(option, yesNo);
  743. }
  744. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  745. {
  746. if (pData->enabled == yesNo)
  747. return;
  748. pData->enabled = yesNo;
  749. pData->masterMutex.lock();
  750. pData->masterMutex.unlock();
  751. }
  752. // -------------------------------------------------------------------
  753. // Set data (internal stuff)
  754. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  755. {
  756. #ifndef BUILD_BRIDGE
  757. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  758. #endif
  759. if (pData->active == active)
  760. return;
  761. {
  762. const ScopedSingleProcessLocker spl(this, true);
  763. if (active)
  764. activate();
  765. else
  766. deactivate();
  767. }
  768. pData->active = active;
  769. #ifndef BUILD_BRIDGE
  770. const float value(active ? 1.0f : 0.0f);
  771. if (sendOsc && pData->engine->isOscControlRegistered())
  772. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  773. if (sendCallback)
  774. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  775. #else
  776. return;
  777. // unused
  778. (void)sendOsc;
  779. (void)sendCallback;
  780. #endif
  781. }
  782. #ifndef BUILD_BRIDGE
  783. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  784. {
  785. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  786. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  787. if (pData->postProc.dryWet == fixedValue)
  788. return;
  789. pData->postProc.dryWet = fixedValue;
  790. if (sendOsc && pData->engine->isOscControlRegistered())
  791. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  792. if (sendCallback)
  793. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  794. }
  795. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  796. {
  797. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  798. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  799. if (pData->postProc.volume == fixedValue)
  800. return;
  801. pData->postProc.volume = fixedValue;
  802. if (sendOsc && pData->engine->isOscControlRegistered())
  803. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  804. if (sendCallback)
  805. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  806. }
  807. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  808. {
  809. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  810. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  811. if (pData->postProc.balanceLeft == fixedValue)
  812. return;
  813. pData->postProc.balanceLeft = fixedValue;
  814. if (sendOsc && pData->engine->isOscControlRegistered())
  815. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  816. if (sendCallback)
  817. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  818. }
  819. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  820. {
  821. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  822. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  823. if (pData->postProc.balanceRight == fixedValue)
  824. return;
  825. pData->postProc.balanceRight = fixedValue;
  826. if (sendOsc && pData->engine->isOscControlRegistered())
  827. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  828. if (sendCallback)
  829. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  830. }
  831. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  832. {
  833. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  834. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  835. if (pData->postProc.panning == fixedValue)
  836. return;
  837. pData->postProc.panning = fixedValue;
  838. if (sendOsc && pData->engine->isOscControlRegistered())
  839. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  840. if (sendCallback)
  841. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  842. }
  843. #endif
  844. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  845. {
  846. #ifndef BUILD_BRIDGE
  847. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  848. #endif
  849. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  850. if (pData->ctrlChannel == channel)
  851. return;
  852. pData->ctrlChannel = channel;
  853. #ifndef BUILD_BRIDGE
  854. const float ctrlf(channel);
  855. if (sendOsc && pData->engine->isOscControlRegistered())
  856. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, ctrlf);
  857. if (sendCallback)
  858. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, ctrlf, nullptr);
  859. if (pData->hints & PLUGIN_IS_BRIDGE)
  860. osc_send_control(pData->osc.data, PARAMETER_CTRL_CHANNEL, ctrlf);
  861. #else
  862. return;
  863. // unused
  864. (void)sendOsc;
  865. (void)sendCallback;
  866. #endif
  867. }
  868. // -------------------------------------------------------------------
  869. // Set data (plugin-specific stuff)
  870. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  871. {
  872. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  873. #ifdef BUILD_BRIDGE
  874. if (! gIsLoadingProject)
  875. {
  876. //CARLA_ASSERT(! sendGui); // this should never happen
  877. }
  878. #endif
  879. #ifdef BUILD_BRIDGE
  880. if (sendGui == sendOsc && sendOsc == sendCallback && ! sendCallback) {
  881. //pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(parameterId), 1, value);
  882. }
  883. #else
  884. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  885. uiParameterChange(parameterId, value);
  886. if (sendOsc && pData->engine->isOscControlRegistered())
  887. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  888. #endif
  889. if (sendCallback)
  890. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  891. }
  892. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  893. {
  894. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  895. switch (rindex)
  896. {
  897. case PARAMETER_ACTIVE:
  898. return setActive((value > 0.0f), sendOsc, sendCallback);
  899. case PARAMETER_CTRL_CHANNEL:
  900. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  901. #ifndef BUILD_BRIDGE
  902. case PARAMETER_DRYWET:
  903. return setDryWet(value, sendOsc, sendCallback);
  904. case PARAMETER_VOLUME:
  905. return setVolume(value, sendOsc, sendCallback);
  906. case PARAMETER_BALANCE_LEFT:
  907. return setBalanceLeft(value, sendOsc, sendCallback);
  908. case PARAMETER_BALANCE_RIGHT:
  909. return setBalanceRight(value, sendOsc, sendCallback);
  910. case PARAMETER_PANNING:
  911. return setPanning(value, sendOsc, sendCallback);
  912. #endif
  913. }
  914. for (uint32_t i=0; i < pData->param.count; ++i)
  915. {
  916. if (pData->param.data[i].rindex == rindex)
  917. {
  918. if (getParameterValue(i) != value)
  919. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  920. break;
  921. }
  922. }
  923. }
  924. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  925. {
  926. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  927. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  928. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  929. pData->param.data[parameterId].midiChannel = channel;
  930. #ifndef BUILD_BRIDGE
  931. if (sendOsc && pData->engine->isOscControlRegistered())
  932. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  933. if (sendCallback)
  934. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  935. if (pData->hints & PLUGIN_IS_BRIDGE)
  936. {} // TODO
  937. #else
  938. return;
  939. // unused
  940. (void)sendOsc;
  941. (void)sendCallback;
  942. #endif
  943. }
  944. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  945. {
  946. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  947. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  948. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F,);
  949. pData->param.data[parameterId].midiCC = cc;
  950. #ifndef BUILD_BRIDGE
  951. if (sendOsc && pData->engine->isOscControlRegistered())
  952. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  953. if (sendCallback)
  954. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  955. if (pData->hints & PLUGIN_IS_BRIDGE)
  956. {} // TODO
  957. #else
  958. return;
  959. // unused
  960. (void)sendOsc;
  961. (void)sendCallback;
  962. #endif
  963. }
  964. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  965. {
  966. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  967. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  968. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  969. #ifdef BUILD_BRIDGE
  970. if (! gIsLoadingProject) {
  971. CARLA_SAFE_ASSERT_RETURN(! sendGui,); // this should never happen
  972. }
  973. #else
  974. // unused
  975. (void)sendGui;
  976. #endif
  977. bool saveData = true;
  978. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  979. {
  980. // Ignore some keys
  981. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  982. saveData = false;
  983. //else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVE_NOW) == 0 || std::strcmp(key, CARLA_BRIDGE_MSG_SET_CHUNK) == 0 || std::strcmp(key, CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  984. // saveData = false;
  985. }
  986. if (! saveData)
  987. return;
  988. // Check if we already have this key
  989. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  990. {
  991. CustomData& cData(it.getValue());
  992. CARLA_SAFE_ASSERT_CONTINUE(cData.type != nullptr && cData.type[0] != '\0');
  993. CARLA_SAFE_ASSERT_CONTINUE(cData.key != nullptr && cData.key[0] != '\0');
  994. CARLA_SAFE_ASSERT_CONTINUE(cData.value != nullptr);
  995. if (std::strcmp(cData.key, key) == 0)
  996. {
  997. if (cData.value != nullptr)
  998. delete[] cData.value;
  999. cData.value = carla_strdup(value);
  1000. return;
  1001. }
  1002. }
  1003. // Otherwise store it
  1004. CustomData newData;
  1005. newData.type = carla_strdup(type);
  1006. newData.key = carla_strdup(key);
  1007. newData.value = carla_strdup(value);
  1008. pData->custom.append(newData);
  1009. }
  1010. void CarlaPlugin::setChunkData(const char* const stringData)
  1011. {
  1012. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr && stringData[0] != '\0',);
  1013. CARLA_SAFE_ASSERT(false); // this should never happen
  1014. }
  1015. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1016. {
  1017. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1018. #ifdef BUILD_BRIDGE
  1019. if (! gIsLoadingProject) {
  1020. CARLA_ASSERT(! sendGui); // this should never happen
  1021. }
  1022. #endif
  1023. pData->prog.current = index;
  1024. #ifndef BUILD_BRIDGE
  1025. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1026. if (reallySendOsc)
  1027. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1028. #endif
  1029. if (sendCallback)
  1030. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1031. // Change default parameter values
  1032. if (index >= 0)
  1033. {
  1034. #ifndef BUILD_BRIDGE
  1035. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1036. uiProgramChange(static_cast<uint32_t>(index));
  1037. #endif
  1038. if (getType() == PLUGIN_FILE_CSD || getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || getType() == PLUGIN_FILE_SFZ)
  1039. return;
  1040. for (uint32_t i=0; i < pData->param.count; ++i)
  1041. {
  1042. const float value(pData->param.ranges[i].getFixedValue(getParameterValue(i)));
  1043. pData->param.ranges[i].def = value;
  1044. #ifndef BUILD_BRIDGE
  1045. if (reallySendOsc)
  1046. {
  1047. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), value);
  1048. pData->engine->oscSend_control_set_default_value(pData->id, i, value);
  1049. }
  1050. #endif
  1051. if (sendCallback)
  1052. {
  1053. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1054. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1055. }
  1056. }
  1057. }
  1058. #ifdef BUILD_BRIDGE
  1059. return;
  1060. // unused
  1061. (void)sendGui;
  1062. (void)sendOsc;
  1063. #endif
  1064. }
  1065. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1066. {
  1067. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1068. #ifdef BUILD_BRIDGE
  1069. if (! gIsLoadingProject) {
  1070. CARLA_ASSERT(! sendGui); // this should never happen
  1071. }
  1072. #endif
  1073. pData->midiprog.current = index;
  1074. #ifndef BUILD_BRIDGE
  1075. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1076. if (reallySendOsc)
  1077. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1078. #endif
  1079. if (sendCallback)
  1080. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1081. if (index >= 0)
  1082. {
  1083. #ifndef BUILD_BRIDGE
  1084. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1085. uiMidiProgramChange(static_cast<uint32_t>(index));
  1086. #endif
  1087. if (getType() == PLUGIN_FILE_CSD || getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || getType() == PLUGIN_FILE_SFZ)
  1088. return;
  1089. for (uint32_t i=0; i < pData->param.count; ++i)
  1090. {
  1091. const float value(pData->param.ranges[i].getFixedValue(getParameterValue(i)));
  1092. pData->param.ranges[i].def = value;
  1093. #ifndef BUILD_BRIDGE
  1094. if (reallySendOsc)
  1095. {
  1096. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), value);
  1097. pData->engine->oscSend_control_set_default_value(pData->id, i, value);
  1098. }
  1099. #endif
  1100. if (sendCallback)
  1101. {
  1102. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1103. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1104. }
  1105. }
  1106. }
  1107. #ifdef BUILD_BRIDGE
  1108. return;
  1109. // unused
  1110. (void)sendGui;
  1111. (void)sendOsc;
  1112. #endif
  1113. }
  1114. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1115. {
  1116. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1117. {
  1118. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1119. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1120. }
  1121. }
  1122. // -------------------------------------------------------------------
  1123. // Set ui stuff
  1124. void CarlaPlugin::idle()
  1125. {
  1126. if (! pData->enabled)
  1127. return;
  1128. if (pData->hints & PLUGIN_NEEDS_SINGLE_THREAD)
  1129. {
  1130. // Process postponed events
  1131. postRtEventsRun();
  1132. // Update parameter outputs
  1133. for (uint32_t i=0; i < pData->param.count; ++i)
  1134. {
  1135. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1136. uiParameterChange(i, getParameterValue(i));
  1137. }
  1138. }
  1139. if (pData->transientTryCounter == 0)
  1140. return;
  1141. if (++pData->transientTryCounter % 10 != 0)
  1142. return;
  1143. if (pData->transientTryCounter >= 200)
  1144. return;
  1145. carla_stdout("Trying to get window...");
  1146. QString uiTitle(QString("%1 (GUI)").arg(pData->name));
  1147. if (CarlaPluginUi::tryTransientWinIdMatch(pData->osc.data.target != nullptr ? pData->osc.thread.getPid() : 0, uiTitle.toUtf8().constData(), pData->engine->getOptions().frontendWinId))
  1148. pData->transientTryCounter = 0;
  1149. }
  1150. void CarlaPlugin::showCustomUI(const bool yesNo)
  1151. {
  1152. CARLA_SAFE_ASSERT(false);
  1153. return;
  1154. // unused
  1155. (void)yesNo;
  1156. }
  1157. // -------------------------------------------------------------------
  1158. // Plugin state
  1159. void CarlaPlugin::reloadPrograms(const bool)
  1160. {
  1161. }
  1162. // -------------------------------------------------------------------
  1163. // Plugin processing
  1164. void CarlaPlugin::activate() noexcept
  1165. {
  1166. CARLA_SAFE_ASSERT(! pData->active);
  1167. }
  1168. void CarlaPlugin::deactivate() noexcept
  1169. {
  1170. CARLA_SAFE_ASSERT(pData->active);
  1171. }
  1172. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1173. {
  1174. }
  1175. void CarlaPlugin::sampleRateChanged(const double)
  1176. {
  1177. }
  1178. void CarlaPlugin::offlineModeChanged(const bool)
  1179. {
  1180. }
  1181. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1182. {
  1183. if (forcedOffline)
  1184. {
  1185. pData->masterMutex.lock();
  1186. return true;
  1187. }
  1188. return pData->masterMutex.tryLock();
  1189. }
  1190. void CarlaPlugin::unlock() noexcept
  1191. {
  1192. pData->masterMutex.unlock();
  1193. }
  1194. // -------------------------------------------------------------------
  1195. // Plugin buffers
  1196. void CarlaPlugin::initBuffers()
  1197. {
  1198. pData->audioIn.initBuffers();
  1199. pData->audioOut.initBuffers();
  1200. pData->event.initBuffers();
  1201. }
  1202. void CarlaPlugin::clearBuffers()
  1203. {
  1204. pData->clearBuffers();
  1205. }
  1206. // -------------------------------------------------------------------
  1207. // OSC stuff
  1208. void CarlaPlugin::registerToOscClient() noexcept
  1209. {
  1210. #ifdef BUILD_BRIDGE
  1211. if (! pData->engine->isOscBridgeRegistered())
  1212. #else
  1213. if (! pData->engine->isOscControlRegistered())
  1214. #endif
  1215. return;
  1216. #ifndef BUILD_BRIDGE
  1217. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1218. #endif
  1219. // Base data
  1220. {
  1221. // TODO - clear buf
  1222. char bufName[STR_MAX+1] = { '\0' };
  1223. char bufLabel[STR_MAX+1] = { '\0' };
  1224. char bufMaker[STR_MAX+1] = { '\0' };
  1225. char bufCopyright[STR_MAX+1] = { '\0' };
  1226. getRealName(bufName);
  1227. getLabel(bufLabel);
  1228. getMaker(bufMaker);
  1229. getCopyright(bufCopyright);
  1230. #ifdef BUILD_BRIDGE
  1231. pData->engine->oscSend_bridge_plugin_info1(getCategory(), pData->hints, getUniqueId());
  1232. pData->engine->oscSend_bridge_plugin_info2(bufName, bufLabel, bufMaker, bufCopyright);
  1233. #else
  1234. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1235. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1236. #endif
  1237. }
  1238. // Base count
  1239. {
  1240. uint32_t paramIns, paramOuts;
  1241. getParameterCountInfo(paramIns, paramOuts);
  1242. #ifdef BUILD_BRIDGE
  1243. pData->engine->oscSend_bridge_audio_count(getAudioInCount(), getAudioOutCount());
  1244. pData->engine->oscSend_bridge_midi_count(getMidiInCount(), getMidiOutCount());
  1245. pData->engine->oscSend_bridge_parameter_count(paramIns, paramOuts);
  1246. #else
  1247. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1248. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1249. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1250. #endif
  1251. }
  1252. // Plugin Parameters
  1253. if (pData->param.count > 0 && pData->param.count < pData->engine->getOptions().maxParameters)
  1254. {
  1255. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1256. for (uint32_t i=0; i < pData->param.count; ++i)
  1257. {
  1258. carla_zeroChar(bufName, STR_MAX);
  1259. carla_zeroChar(bufUnit, STR_MAX);
  1260. getParameterName(i, bufName);
  1261. getParameterUnit(i, bufUnit);
  1262. const ParameterData& paramData(pData->param.data[i]);
  1263. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1264. #ifdef BUILD_BRIDGE
  1265. pData->engine->oscSend_bridge_parameter_data(i, paramData.rindex, paramData.type, paramData.hints, bufName, bufUnit);
  1266. pData->engine->oscSend_bridge_parameter_ranges1(i, paramRanges.def, paramRanges.min, paramRanges.max);
  1267. pData->engine->oscSend_bridge_parameter_ranges2(i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1268. pData->engine->oscSend_bridge_parameter_value(i, getParameterValue(i));
  1269. pData->engine->oscSend_bridge_parameter_midi_cc(i, paramData.midiCC);
  1270. pData->engine->oscSend_bridge_parameter_midi_channel(i, paramData.midiChannel);
  1271. #else
  1272. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1273. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1274. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1275. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1276. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1277. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1278. #endif
  1279. }
  1280. }
  1281. // Programs
  1282. if (pData->prog.count > 0)
  1283. {
  1284. #ifdef BUILD_BRIDGE
  1285. pData->engine->oscSend_bridge_program_count(pData->prog.count);
  1286. for (uint32_t i=0; i < pData->prog.count; ++i)
  1287. pData->engine->oscSend_bridge_program_name(i, pData->prog.names[i]);
  1288. pData->engine->oscSend_bridge_current_program(pData->prog.current);
  1289. #else
  1290. pData->engine->oscSend_control_set_program_count(pData->id, pData->prog.count);
  1291. for (uint32_t i=0; i < pData->prog.count; ++i)
  1292. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1293. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1294. #endif
  1295. }
  1296. // MIDI Programs
  1297. if (pData->midiprog.count > 0)
  1298. {
  1299. #ifdef BUILD_BRIDGE
  1300. pData->engine->oscSend_bridge_midi_program_count(pData->midiprog.count);
  1301. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1302. {
  1303. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1304. pData->engine->oscSend_bridge_midi_program_data(i, mpData.bank, mpData.program, mpData.name);
  1305. }
  1306. pData->engine->oscSend_bridge_current_midi_program(pData->midiprog.current);
  1307. #else
  1308. pData->engine->oscSend_control_set_midi_program_count(pData->id, pData->midiprog.count);
  1309. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1310. {
  1311. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1312. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1313. }
  1314. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1315. #endif
  1316. }
  1317. #ifndef BUILD_BRIDGE
  1318. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1319. // Internal Parameters
  1320. {
  1321. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1322. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1323. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1324. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1325. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1326. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1327. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1328. }
  1329. #endif
  1330. }
  1331. void CarlaPlugin::updateOscData(const lo_address& source, const char* const url)
  1332. {
  1333. // FIXME - remove debug prints later
  1334. carla_stdout("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  1335. pData->osc.data.free();
  1336. const int proto = lo_address_get_protocol(source);
  1337. {
  1338. const char* host = lo_address_get_hostname(source);
  1339. const char* port = lo_address_get_port(source);
  1340. pData->osc.data.source = lo_address_new_with_proto(proto, host, port);
  1341. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1342. }
  1343. {
  1344. char* host = lo_url_get_hostname(url);
  1345. char* port = lo_url_get_port(url);
  1346. pData->osc.data.path = carla_strdup_free(lo_url_get_path(url));
  1347. pData->osc.data.target = lo_address_new_with_proto(proto, host, port);
  1348. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, pData->osc.data.path);
  1349. std::free(host);
  1350. std::free(port);
  1351. }
  1352. #ifndef BUILD_BRIDGE
  1353. if (pData->hints & PLUGIN_IS_BRIDGE)
  1354. {
  1355. carla_stdout("CarlaPlugin::updateOscData() - done");
  1356. return;
  1357. }
  1358. #endif
  1359. osc_send_sample_rate(pData->osc.data, static_cast<float>(pData->engine->getSampleRate()));
  1360. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  1361. {
  1362. const CustomData& cData(it.getValue());
  1363. CARLA_SAFE_ASSERT_CONTINUE(cData.type != nullptr && cData.type[0] != '\0');
  1364. CARLA_SAFE_ASSERT_CONTINUE(cData.key != nullptr && cData.key[0] != '\0');
  1365. CARLA_SAFE_ASSERT_CONTINUE(cData.value != nullptr);
  1366. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0)
  1367. osc_send_configure(pData->osc.data, cData.key, cData.value);
  1368. }
  1369. if (pData->prog.current >= 0)
  1370. osc_send_program(pData->osc.data, static_cast<uint32_t>(pData->prog.current));
  1371. if (pData->midiprog.current >= 0)
  1372. {
  1373. const MidiProgramData& curMidiProg(pData->midiprog.getCurrent());
  1374. if (getType() == PLUGIN_DSSI)
  1375. osc_send_program(pData->osc.data, curMidiProg.bank, curMidiProg.program);
  1376. else
  1377. osc_send_midi_program(pData->osc.data, curMidiProg.bank, curMidiProg.program);
  1378. }
  1379. for (uint32_t i=0; i < pData->param.count; ++i)
  1380. osc_send_control(pData->osc.data, pData->param.data[i].rindex, getParameterValue(i));
  1381. if ((pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0 && pData->engine->getOptions().frontendWinId != 0)
  1382. pData->transientTryCounter = 1;
  1383. carla_stdout("CarlaPlugin::updateOscData() - done");
  1384. }
  1385. // void CarlaPlugin::freeOscData()
  1386. // {
  1387. // pData->osc.data.free();
  1388. // }
  1389. bool CarlaPlugin::waitForOscGuiShow()
  1390. {
  1391. carla_stdout("CarlaPlugin::waitForOscGuiShow()");
  1392. uint i=0, oscUiTimeout = pData->engine->getOptions().uiBridgesTimeout;
  1393. // wait for UI 'update' call
  1394. for (; i < oscUiTimeout/100; ++i)
  1395. {
  1396. if (pData->osc.data.target != nullptr)
  1397. {
  1398. carla_stdout("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  1399. osc_send_show(pData->osc.data);
  1400. return true;
  1401. }
  1402. if (pData->osc.thread.isThreadRunning())
  1403. carla_msleep(100);
  1404. else
  1405. return false;
  1406. }
  1407. carla_stdout("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  1408. return false;
  1409. }
  1410. // -------------------------------------------------------------------
  1411. // MIDI events
  1412. #ifndef BUILD_BRIDGE
  1413. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1414. {
  1415. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1416. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1417. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1418. if (! pData->active)
  1419. return;
  1420. ExternalMidiNote extNote;
  1421. extNote.channel = static_cast<int8_t>(channel);
  1422. extNote.note = note;
  1423. extNote.velo = velo;
  1424. pData->extNotes.append(extNote);
  1425. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1426. {
  1427. if (velo > 0)
  1428. uiNoteOn(channel, note, velo);
  1429. else
  1430. uiNoteOff(channel, note);
  1431. }
  1432. if (sendOsc && pData->engine->isOscControlRegistered())
  1433. {
  1434. if (velo > 0)
  1435. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1436. else
  1437. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1438. }
  1439. if (sendCallback)
  1440. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1441. }
  1442. #endif
  1443. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1444. {
  1445. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1446. return;
  1447. PluginPostRtEvent postEvent;
  1448. postEvent.type = kPluginPostRtEventNoteOff;
  1449. postEvent.value1 = pData->ctrlChannel;
  1450. postEvent.value2 = 0;
  1451. postEvent.value3 = 0.0f;
  1452. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1453. {
  1454. postEvent.value2 = i;
  1455. pData->postRtEvents.appendRT(postEvent);
  1456. }
  1457. }
  1458. // -------------------------------------------------------------------
  1459. // Post-poned events
  1460. void CarlaPlugin::postRtEventsRun()
  1461. {
  1462. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1463. #ifndef BUILD_BRIDGE
  1464. const bool sendOsc(pData->engine->isOscControlRegistered());
  1465. #endif
  1466. while (! pData->postRtEvents.data.isEmpty())
  1467. {
  1468. const PluginPostRtEvent& event(pData->postRtEvents.data.getFirst(true));
  1469. switch (event.type)
  1470. {
  1471. case kPluginPostRtEventNull:
  1472. break;
  1473. case kPluginPostRtEventDebug:
  1474. #ifndef BUILD_BRIDGE
  1475. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1476. #endif
  1477. break;
  1478. case kPluginPostRtEventParameterChange:
  1479. // Update UI
  1480. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1481. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1482. #ifndef BUILD_BRIDGE
  1483. if (event.value2 != 1)
  1484. {
  1485. // Update OSC control client
  1486. if (sendOsc)
  1487. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1488. // Update Host
  1489. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1490. }
  1491. #endif
  1492. break;
  1493. case kPluginPostRtEventProgramChange:
  1494. // Update UI
  1495. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1496. uiProgramChange(static_cast<uint32_t>(event.value1));
  1497. #ifndef BUILD_BRIDGE
  1498. // Update OSC control client
  1499. if (sendOsc)
  1500. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1501. // Update Host
  1502. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1503. // Update param values
  1504. for (uint32_t j=0; j < pData->param.count; ++j)
  1505. {
  1506. const float paramValue(getParameterValue(j));
  1507. if (sendOsc)
  1508. {
  1509. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1510. pData->engine->oscSend_control_set_default_value(pData->id, j, pData->param.ranges[j].def);
  1511. }
  1512. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1513. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, pData->param.ranges[j].def, nullptr);
  1514. }
  1515. #endif
  1516. break;
  1517. case kPluginPostRtEventMidiProgramChange:
  1518. // Update UI
  1519. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1520. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1521. #ifndef BUILD_BRIDGE
  1522. // Update OSC control client
  1523. if (sendOsc)
  1524. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1525. // Update Host
  1526. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1527. // Update param values
  1528. for (uint32_t j=0; j < pData->param.count; ++j)
  1529. {
  1530. const float paramValue(getParameterValue(j));
  1531. if (sendOsc)
  1532. {
  1533. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1534. pData->engine->oscSend_control_set_default_value(pData->id, j, pData->param.ranges[j].def);
  1535. }
  1536. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1537. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, pData->param.ranges[j].def, nullptr);
  1538. }
  1539. #endif
  1540. break;
  1541. case kPluginPostRtEventNoteOn:
  1542. {
  1543. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1544. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1545. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1546. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1547. const uint8_t note = static_cast<uint8_t>(event.value2);
  1548. const uint8_t velocity = uint8_t(event.value3);
  1549. // Update UI
  1550. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1551. uiNoteOn(channel, note, velocity);
  1552. #ifndef BUILD_BRIDGE
  1553. // Update OSC control client
  1554. if (sendOsc)
  1555. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1556. // Update Host
  1557. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1558. #endif
  1559. break;
  1560. }
  1561. case kPluginPostRtEventNoteOff:
  1562. {
  1563. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1564. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1565. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1566. const uint8_t note = static_cast<uint8_t>(event.value2);
  1567. // Update UI
  1568. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1569. uiNoteOff(channel, note);
  1570. #ifndef BUILD_BRIDGE
  1571. // Update OSC control client
  1572. if (sendOsc)
  1573. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1574. // Update Host
  1575. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1576. #endif
  1577. break;
  1578. }
  1579. }
  1580. }
  1581. }
  1582. // -------------------------------------------------------------------
  1583. // Post-poned UI Stuff
  1584. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1585. {
  1586. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1587. return;
  1588. // unused
  1589. (void)value;
  1590. }
  1591. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1592. {
  1593. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1594. }
  1595. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1596. {
  1597. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1598. }
  1599. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1600. {
  1601. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1602. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1603. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1604. }
  1605. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1606. {
  1607. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1608. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1609. }
  1610. bool CarlaPlugin::canRunInRack() const noexcept
  1611. {
  1612. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1613. }
  1614. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1615. {
  1616. return pData->engine;
  1617. }
  1618. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1619. {
  1620. return pData->client;
  1621. }
  1622. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1623. {
  1624. return pData->audioIn.ports[index].port;
  1625. }
  1626. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1627. {
  1628. return pData->audioOut.ports[index].port;
  1629. }
  1630. // -------------------------------------------------------------------
  1631. // Scoped Disabler
  1632. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1633. : fPlugin(plugin)
  1634. {
  1635. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1636. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1637. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1638. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1639. plugin->pData->masterMutex.lock();
  1640. if (plugin->pData->enabled)
  1641. plugin->pData->enabled = false;
  1642. if (plugin->pData->client->isActive())
  1643. plugin->pData->client->deactivate();
  1644. }
  1645. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1646. {
  1647. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1648. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1649. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1650. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1651. fPlugin->pData->enabled = true;
  1652. fPlugin->pData->client->activate();
  1653. fPlugin->pData->masterMutex.unlock();
  1654. }
  1655. // -------------------------------------------------------------------
  1656. // Scoped Process Locker
  1657. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1658. : fPlugin(plugin),
  1659. fBlock(block)
  1660. {
  1661. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1662. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1663. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1664. if (! fBlock)
  1665. return;
  1666. plugin->pData->singleMutex.lock();
  1667. }
  1668. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1669. {
  1670. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1671. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1672. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1673. if (! fBlock)
  1674. return;
  1675. #ifndef BUILD_BRIDGE
  1676. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1677. fPlugin->pData->needsReset = true;
  1678. #endif
  1679. fPlugin->pData->singleMutex.unlock();
  1680. }
  1681. // -------------------------------------------------------------------
  1682. CARLA_BACKEND_END_NAMESPACE