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.

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