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.

2139 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. #ifndef BUILD_BRIDGE
  738. pData->saveSetting(option, yesNo);
  739. #endif
  740. }
  741. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  742. {
  743. if (pData->enabled == yesNo)
  744. return;
  745. pData->enabled = yesNo;
  746. pData->masterMutex.lock();
  747. pData->masterMutex.unlock();
  748. }
  749. // -------------------------------------------------------------------
  750. // Set data (internal stuff)
  751. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  752. {
  753. #ifndef BUILD_BRIDGE
  754. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  755. #endif
  756. if (pData->active == active)
  757. return;
  758. {
  759. const ScopedSingleProcessLocker spl(this, true);
  760. if (active)
  761. activate();
  762. else
  763. deactivate();
  764. }
  765. pData->active = active;
  766. #ifndef BUILD_BRIDGE
  767. const float value(active ? 1.0f : 0.0f);
  768. if (sendOsc && pData->engine->isOscControlRegistered())
  769. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  770. if (sendCallback)
  771. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  772. #else
  773. return;
  774. // unused
  775. (void)sendOsc;
  776. (void)sendCallback;
  777. #endif
  778. }
  779. #ifndef BUILD_BRIDGE
  780. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  781. {
  782. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  783. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  784. if (pData->postProc.dryWet == fixedValue)
  785. return;
  786. pData->postProc.dryWet = fixedValue;
  787. if (sendOsc && pData->engine->isOscControlRegistered())
  788. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  789. if (sendCallback)
  790. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  791. }
  792. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  793. {
  794. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  795. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  796. if (pData->postProc.volume == fixedValue)
  797. return;
  798. pData->postProc.volume = fixedValue;
  799. if (sendOsc && pData->engine->isOscControlRegistered())
  800. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  801. if (sendCallback)
  802. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  803. }
  804. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  805. {
  806. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  807. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  808. if (pData->postProc.balanceLeft == fixedValue)
  809. return;
  810. pData->postProc.balanceLeft = fixedValue;
  811. if (sendOsc && pData->engine->isOscControlRegistered())
  812. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  813. if (sendCallback)
  814. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  815. }
  816. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  817. {
  818. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  819. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  820. if (pData->postProc.balanceRight == fixedValue)
  821. return;
  822. pData->postProc.balanceRight = fixedValue;
  823. if (sendOsc && pData->engine->isOscControlRegistered())
  824. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  825. if (sendCallback)
  826. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  827. }
  828. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  829. {
  830. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  831. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  832. if (pData->postProc.panning == fixedValue)
  833. return;
  834. pData->postProc.panning = fixedValue;
  835. if (sendOsc && pData->engine->isOscControlRegistered())
  836. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  837. if (sendCallback)
  838. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  839. }
  840. #endif
  841. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  842. {
  843. #ifndef BUILD_BRIDGE
  844. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  845. #endif
  846. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  847. if (pData->ctrlChannel == channel)
  848. return;
  849. pData->ctrlChannel = channel;
  850. #ifndef BUILD_BRIDGE
  851. const float ctrlf(channel);
  852. if (sendOsc && pData->engine->isOscControlRegistered())
  853. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, ctrlf);
  854. if (sendCallback)
  855. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, ctrlf, nullptr);
  856. if (pData->hints & PLUGIN_IS_BRIDGE)
  857. osc_send_control(pData->osc.data, PARAMETER_CTRL_CHANNEL, ctrlf);
  858. #else
  859. return;
  860. // unused
  861. (void)sendOsc;
  862. (void)sendCallback;
  863. #endif
  864. }
  865. // -------------------------------------------------------------------
  866. // Set data (plugin-specific stuff)
  867. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  868. {
  869. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  870. #ifdef BUILD_BRIDGE
  871. if (! gIsLoadingProject)
  872. {
  873. //CARLA_ASSERT(! sendGui); // this should never happen
  874. }
  875. #endif
  876. #ifdef BUILD_BRIDGE
  877. if (sendGui == sendOsc && sendOsc == sendCallback && ! sendCallback) {
  878. //pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(parameterId), 1, value);
  879. }
  880. #else
  881. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  882. uiParameterChange(parameterId, value);
  883. if (sendOsc && pData->engine->isOscControlRegistered())
  884. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  885. #endif
  886. if (sendCallback)
  887. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  888. }
  889. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  890. {
  891. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  892. switch (rindex)
  893. {
  894. case PARAMETER_ACTIVE:
  895. return setActive((value > 0.0f), sendOsc, sendCallback);
  896. case PARAMETER_CTRL_CHANNEL:
  897. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  898. #ifndef BUILD_BRIDGE
  899. case PARAMETER_DRYWET:
  900. return setDryWet(value, sendOsc, sendCallback);
  901. case PARAMETER_VOLUME:
  902. return setVolume(value, sendOsc, sendCallback);
  903. case PARAMETER_BALANCE_LEFT:
  904. return setBalanceLeft(value, sendOsc, sendCallback);
  905. case PARAMETER_BALANCE_RIGHT:
  906. return setBalanceRight(value, sendOsc, sendCallback);
  907. case PARAMETER_PANNING:
  908. return setPanning(value, sendOsc, sendCallback);
  909. #endif
  910. }
  911. for (uint32_t i=0; i < pData->param.count; ++i)
  912. {
  913. if (pData->param.data[i].rindex == rindex)
  914. {
  915. if (getParameterValue(i) != value)
  916. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  917. break;
  918. }
  919. }
  920. }
  921. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  922. {
  923. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  924. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  925. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  926. pData->param.data[parameterId].midiChannel = channel;
  927. #ifndef BUILD_BRIDGE
  928. if (sendOsc && pData->engine->isOscControlRegistered())
  929. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  930. if (sendCallback)
  931. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  932. if (pData->hints & PLUGIN_IS_BRIDGE)
  933. {} // TODO
  934. #else
  935. return;
  936. // unused
  937. (void)sendOsc;
  938. (void)sendCallback;
  939. #endif
  940. }
  941. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  942. {
  943. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  944. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  945. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F,);
  946. pData->param.data[parameterId].midiCC = cc;
  947. #ifndef BUILD_BRIDGE
  948. if (sendOsc && pData->engine->isOscControlRegistered())
  949. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  950. if (sendCallback)
  951. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  952. if (pData->hints & PLUGIN_IS_BRIDGE)
  953. {} // TODO
  954. #else
  955. return;
  956. // unused
  957. (void)sendOsc;
  958. (void)sendCallback;
  959. #endif
  960. }
  961. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  962. {
  963. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  964. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  965. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  966. #ifdef BUILD_BRIDGE
  967. if (! gIsLoadingProject) {
  968. CARLA_SAFE_ASSERT_RETURN(! sendGui,); // this should never happen
  969. }
  970. #else
  971. // unused
  972. (void)sendGui;
  973. #endif
  974. bool saveData = true;
  975. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  976. {
  977. // Ignore some keys
  978. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  979. saveData = false;
  980. //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)
  981. // saveData = false;
  982. }
  983. if (! saveData)
  984. return;
  985. // Check if we already have this key
  986. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  987. {
  988. CustomData& cData(it.getValue());
  989. CARLA_SAFE_ASSERT_CONTINUE(cData.type != nullptr && cData.type[0] != '\0');
  990. CARLA_SAFE_ASSERT_CONTINUE(cData.key != nullptr && cData.key[0] != '\0');
  991. CARLA_SAFE_ASSERT_CONTINUE(cData.value != nullptr);
  992. if (std::strcmp(cData.key, key) == 0)
  993. {
  994. if (cData.value != nullptr)
  995. delete[] cData.value;
  996. cData.value = carla_strdup(value);
  997. return;
  998. }
  999. }
  1000. // Otherwise store it
  1001. CustomData newData;
  1002. newData.type = carla_strdup(type);
  1003. newData.key = carla_strdup(key);
  1004. newData.value = carla_strdup(value);
  1005. pData->custom.append(newData);
  1006. }
  1007. void CarlaPlugin::setChunkData(const char* const stringData)
  1008. {
  1009. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr && stringData[0] != '\0',);
  1010. CARLA_SAFE_ASSERT(false); // this should never happen
  1011. }
  1012. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1013. {
  1014. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1015. #ifdef BUILD_BRIDGE
  1016. if (! gIsLoadingProject) {
  1017. CARLA_ASSERT(! sendGui); // this should never happen
  1018. }
  1019. #endif
  1020. pData->prog.current = index;
  1021. #ifndef BUILD_BRIDGE
  1022. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1023. if (reallySendOsc)
  1024. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1025. #endif
  1026. if (sendCallback)
  1027. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1028. // Change default parameter values
  1029. if (index >= 0)
  1030. {
  1031. #ifndef BUILD_BRIDGE
  1032. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1033. uiProgramChange(static_cast<uint32_t>(index));
  1034. #endif
  1035. if (getType() == PLUGIN_FILE_CSD || getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || getType() == PLUGIN_FILE_SFZ)
  1036. return;
  1037. for (uint32_t i=0; i < pData->param.count; ++i)
  1038. {
  1039. const float value(pData->param.ranges[i].getFixedValue(getParameterValue(i)));
  1040. pData->param.ranges[i].def = value;
  1041. #ifndef BUILD_BRIDGE
  1042. if (reallySendOsc)
  1043. {
  1044. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), value);
  1045. pData->engine->oscSend_control_set_default_value(pData->id, i, value);
  1046. }
  1047. #endif
  1048. if (sendCallback)
  1049. {
  1050. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1051. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1052. }
  1053. }
  1054. }
  1055. #ifdef BUILD_BRIDGE
  1056. return;
  1057. // unused
  1058. (void)sendGui;
  1059. (void)sendOsc;
  1060. #endif
  1061. }
  1062. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1063. {
  1064. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1065. #ifdef BUILD_BRIDGE
  1066. if (! gIsLoadingProject) {
  1067. CARLA_ASSERT(! sendGui); // this should never happen
  1068. }
  1069. #endif
  1070. pData->midiprog.current = index;
  1071. #ifndef BUILD_BRIDGE
  1072. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1073. if (reallySendOsc)
  1074. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1075. #endif
  1076. if (sendCallback)
  1077. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1078. if (index >= 0)
  1079. {
  1080. #ifndef BUILD_BRIDGE
  1081. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1082. uiMidiProgramChange(static_cast<uint32_t>(index));
  1083. #endif
  1084. if (getType() == PLUGIN_FILE_CSD || getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || getType() == PLUGIN_FILE_SFZ)
  1085. return;
  1086. for (uint32_t i=0; i < pData->param.count; ++i)
  1087. {
  1088. const float value(pData->param.ranges[i].getFixedValue(getParameterValue(i)));
  1089. pData->param.ranges[i].def = value;
  1090. #ifndef BUILD_BRIDGE
  1091. if (reallySendOsc)
  1092. {
  1093. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), value);
  1094. pData->engine->oscSend_control_set_default_value(pData->id, i, value);
  1095. }
  1096. #endif
  1097. if (sendCallback)
  1098. {
  1099. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1100. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1101. }
  1102. }
  1103. }
  1104. #ifdef BUILD_BRIDGE
  1105. return;
  1106. // unused
  1107. (void)sendGui;
  1108. (void)sendOsc;
  1109. #endif
  1110. }
  1111. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1112. {
  1113. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1114. {
  1115. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1116. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1117. }
  1118. }
  1119. // -------------------------------------------------------------------
  1120. // Set ui stuff
  1121. void CarlaPlugin::idle()
  1122. {
  1123. if (! pData->enabled)
  1124. return;
  1125. if (pData->hints & PLUGIN_NEEDS_SINGLE_THREAD)
  1126. {
  1127. // Process postponed events
  1128. postRtEventsRun();
  1129. // Update parameter outputs
  1130. for (uint32_t i=0; i < pData->param.count; ++i)
  1131. {
  1132. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1133. uiParameterChange(i, getParameterValue(i));
  1134. }
  1135. }
  1136. if (pData->transientTryCounter == 0)
  1137. return;
  1138. if (++pData->transientTryCounter % 10 != 0)
  1139. return;
  1140. if (pData->transientTryCounter >= 200)
  1141. return;
  1142. carla_stdout("Trying to get window...");
  1143. QString uiTitle(QString("%1 (GUI)").arg(pData->name));
  1144. if (CarlaPluginUi::tryTransientWinIdMatch(pData->osc.data.target != nullptr ? pData->osc.thread.getPid() : 0, uiTitle.toUtf8().constData(), pData->engine->getOptions().frontendWinId))
  1145. pData->transientTryCounter = 0;
  1146. }
  1147. void CarlaPlugin::showCustomUI(const bool yesNo)
  1148. {
  1149. CARLA_SAFE_ASSERT(false);
  1150. return;
  1151. // unused
  1152. (void)yesNo;
  1153. }
  1154. // -------------------------------------------------------------------
  1155. // Plugin state
  1156. void CarlaPlugin::reloadPrograms(const bool)
  1157. {
  1158. }
  1159. // -------------------------------------------------------------------
  1160. // Plugin processing
  1161. void CarlaPlugin::activate() noexcept
  1162. {
  1163. CARLA_SAFE_ASSERT(! pData->active);
  1164. }
  1165. void CarlaPlugin::deactivate() noexcept
  1166. {
  1167. CARLA_SAFE_ASSERT(pData->active);
  1168. }
  1169. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1170. {
  1171. }
  1172. void CarlaPlugin::sampleRateChanged(const double)
  1173. {
  1174. }
  1175. void CarlaPlugin::offlineModeChanged(const bool)
  1176. {
  1177. }
  1178. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1179. {
  1180. if (forcedOffline)
  1181. {
  1182. pData->masterMutex.lock();
  1183. return true;
  1184. }
  1185. return pData->masterMutex.tryLock();
  1186. }
  1187. void CarlaPlugin::unlock() noexcept
  1188. {
  1189. pData->masterMutex.unlock();
  1190. }
  1191. // -------------------------------------------------------------------
  1192. // Plugin buffers
  1193. void CarlaPlugin::initBuffers() const noexcept
  1194. {
  1195. pData->audioIn.initBuffers();
  1196. pData->audioOut.initBuffers();
  1197. pData->event.initBuffers();
  1198. }
  1199. void CarlaPlugin::clearBuffers() noexcept
  1200. {
  1201. pData->clearBuffers();
  1202. }
  1203. // -------------------------------------------------------------------
  1204. // OSC stuff
  1205. void CarlaPlugin::registerToOscClient() noexcept
  1206. {
  1207. #ifdef BUILD_BRIDGE
  1208. if (! pData->engine->isOscBridgeRegistered())
  1209. #else
  1210. if (! pData->engine->isOscControlRegistered())
  1211. #endif
  1212. return;
  1213. #ifndef BUILD_BRIDGE
  1214. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1215. #endif
  1216. // Base data
  1217. {
  1218. // TODO - clear buf
  1219. char bufName[STR_MAX+1] = { '\0' };
  1220. char bufLabel[STR_MAX+1] = { '\0' };
  1221. char bufMaker[STR_MAX+1] = { '\0' };
  1222. char bufCopyright[STR_MAX+1] = { '\0' };
  1223. getRealName(bufName);
  1224. getLabel(bufLabel);
  1225. getMaker(bufMaker);
  1226. getCopyright(bufCopyright);
  1227. #ifdef BUILD_BRIDGE
  1228. pData->engine->oscSend_bridge_plugin_info1(getCategory(), pData->hints, getUniqueId());
  1229. pData->engine->oscSend_bridge_plugin_info2(bufName, bufLabel, bufMaker, bufCopyright);
  1230. #else
  1231. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1232. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1233. #endif
  1234. }
  1235. // Base count
  1236. {
  1237. uint32_t paramIns, paramOuts;
  1238. getParameterCountInfo(paramIns, paramOuts);
  1239. #ifdef BUILD_BRIDGE
  1240. pData->engine->oscSend_bridge_audio_count(getAudioInCount(), getAudioOutCount());
  1241. pData->engine->oscSend_bridge_midi_count(getMidiInCount(), getMidiOutCount());
  1242. pData->engine->oscSend_bridge_parameter_count(paramIns, paramOuts);
  1243. #else
  1244. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1245. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1246. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1247. #endif
  1248. }
  1249. // Plugin Parameters
  1250. if (pData->param.count > 0 && pData->param.count < pData->engine->getOptions().maxParameters)
  1251. {
  1252. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1253. for (uint32_t i=0; i < pData->param.count; ++i)
  1254. {
  1255. carla_zeroChar(bufName, STR_MAX);
  1256. carla_zeroChar(bufUnit, STR_MAX);
  1257. getParameterName(i, bufName);
  1258. getParameterUnit(i, bufUnit);
  1259. const ParameterData& paramData(pData->param.data[i]);
  1260. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1261. #ifdef BUILD_BRIDGE
  1262. pData->engine->oscSend_bridge_parameter_data(i, paramData.rindex, paramData.type, paramData.hints, bufName, bufUnit);
  1263. pData->engine->oscSend_bridge_parameter_ranges1(i, paramRanges.def, paramRanges.min, paramRanges.max);
  1264. pData->engine->oscSend_bridge_parameter_ranges2(i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1265. pData->engine->oscSend_bridge_parameter_value(i, getParameterValue(i));
  1266. pData->engine->oscSend_bridge_parameter_midi_cc(i, paramData.midiCC);
  1267. pData->engine->oscSend_bridge_parameter_midi_channel(i, paramData.midiChannel);
  1268. #else
  1269. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1270. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1271. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1272. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1273. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1274. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1275. #endif
  1276. }
  1277. }
  1278. // Programs
  1279. if (pData->prog.count > 0)
  1280. {
  1281. #ifdef BUILD_BRIDGE
  1282. pData->engine->oscSend_bridge_program_count(pData->prog.count);
  1283. for (uint32_t i=0; i < pData->prog.count; ++i)
  1284. pData->engine->oscSend_bridge_program_name(i, pData->prog.names[i]);
  1285. pData->engine->oscSend_bridge_current_program(pData->prog.current);
  1286. #else
  1287. pData->engine->oscSend_control_set_program_count(pData->id, pData->prog.count);
  1288. for (uint32_t i=0; i < pData->prog.count; ++i)
  1289. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1290. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1291. #endif
  1292. }
  1293. // MIDI Programs
  1294. if (pData->midiprog.count > 0)
  1295. {
  1296. #ifdef BUILD_BRIDGE
  1297. pData->engine->oscSend_bridge_midi_program_count(pData->midiprog.count);
  1298. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1299. {
  1300. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1301. pData->engine->oscSend_bridge_midi_program_data(i, mpData.bank, mpData.program, mpData.name);
  1302. }
  1303. pData->engine->oscSend_bridge_current_midi_program(pData->midiprog.current);
  1304. #else
  1305. pData->engine->oscSend_control_set_midi_program_count(pData->id, pData->midiprog.count);
  1306. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1307. {
  1308. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1309. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1310. }
  1311. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1312. #endif
  1313. }
  1314. #ifndef BUILD_BRIDGE
  1315. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1316. // Internal Parameters
  1317. {
  1318. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1319. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1320. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1321. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1322. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1323. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1324. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1325. }
  1326. #endif
  1327. }
  1328. void CarlaPlugin::updateOscData(const lo_address& source, const char* const url)
  1329. {
  1330. // FIXME - remove debug prints later
  1331. carla_stdout("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  1332. pData->osc.data.free();
  1333. const int proto = lo_address_get_protocol(source);
  1334. {
  1335. const char* host = lo_address_get_hostname(source);
  1336. const char* port = lo_address_get_port(source);
  1337. pData->osc.data.source = lo_address_new_with_proto(proto, host, port);
  1338. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1339. }
  1340. {
  1341. char* host = lo_url_get_hostname(url);
  1342. char* port = lo_url_get_port(url);
  1343. pData->osc.data.path = carla_strdup_free(lo_url_get_path(url));
  1344. pData->osc.data.target = lo_address_new_with_proto(proto, host, port);
  1345. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, pData->osc.data.path);
  1346. std::free(host);
  1347. std::free(port);
  1348. }
  1349. #ifndef BUILD_BRIDGE
  1350. if (pData->hints & PLUGIN_IS_BRIDGE)
  1351. {
  1352. carla_stdout("CarlaPlugin::updateOscData() - done");
  1353. return;
  1354. }
  1355. #endif
  1356. // send possible extra data first
  1357. if (updateOscDataExtra())
  1358. pData->engine->idleOsc();
  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. bool CarlaPlugin::updateOscDataExtra()
  1386. {
  1387. return false;
  1388. }
  1389. // void CarlaPlugin::freeOscData()
  1390. // {
  1391. // pData->osc.data.free();
  1392. // }
  1393. bool CarlaPlugin::waitForOscGuiShow()
  1394. {
  1395. carla_stdout("CarlaPlugin::waitForOscGuiShow()");
  1396. uint i=0, oscUiTimeout = pData->engine->getOptions().uiBridgesTimeout;
  1397. // wait for UI 'update' call
  1398. for (; i < oscUiTimeout/100; ++i)
  1399. {
  1400. if (pData->osc.data.target != nullptr)
  1401. {
  1402. carla_stdout("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  1403. osc_send_show(pData->osc.data);
  1404. return true;
  1405. }
  1406. if (pData->osc.thread.isThreadRunning())
  1407. carla_msleep(100);
  1408. else
  1409. return false;
  1410. }
  1411. carla_stdout("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  1412. return false;
  1413. }
  1414. // -------------------------------------------------------------------
  1415. // MIDI events
  1416. #ifndef BUILD_BRIDGE
  1417. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1418. {
  1419. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1420. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1421. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1422. if (! pData->active)
  1423. return;
  1424. ExternalMidiNote extNote;
  1425. extNote.channel = static_cast<int8_t>(channel);
  1426. extNote.note = note;
  1427. extNote.velo = velo;
  1428. pData->extNotes.appendNonRT(extNote);
  1429. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1430. {
  1431. if (velo > 0)
  1432. uiNoteOn(channel, note, velo);
  1433. else
  1434. uiNoteOff(channel, note);
  1435. }
  1436. if (sendOsc && pData->engine->isOscControlRegistered())
  1437. {
  1438. if (velo > 0)
  1439. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1440. else
  1441. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1442. }
  1443. if (sendCallback)
  1444. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1445. }
  1446. #endif
  1447. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1448. {
  1449. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1450. return;
  1451. PluginPostRtEvent postEvent;
  1452. postEvent.type = kPluginPostRtEventNoteOff;
  1453. postEvent.value1 = pData->ctrlChannel;
  1454. postEvent.value2 = 0;
  1455. postEvent.value3 = 0.0f;
  1456. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1457. {
  1458. postEvent.value2 = i;
  1459. pData->postRtEvents.appendRT(postEvent);
  1460. }
  1461. }
  1462. // -------------------------------------------------------------------
  1463. // Post-poned events
  1464. void CarlaPlugin::postRtEventsRun()
  1465. {
  1466. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1467. #ifndef BUILD_BRIDGE
  1468. const bool sendOsc(pData->engine->isOscControlRegistered());
  1469. #endif
  1470. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin(); it.valid(); it.next())
  1471. {
  1472. const PluginPostRtEvent& event(it.getValue());
  1473. switch (event.type)
  1474. {
  1475. case kPluginPostRtEventNull:
  1476. break;
  1477. case kPluginPostRtEventDebug:
  1478. #ifndef BUILD_BRIDGE
  1479. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1480. #endif
  1481. break;
  1482. case kPluginPostRtEventParameterChange:
  1483. // Update UI
  1484. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1485. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1486. #ifndef BUILD_BRIDGE
  1487. if (event.value2 != 1)
  1488. {
  1489. // Update OSC control client
  1490. if (sendOsc)
  1491. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1492. // Update Host
  1493. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1494. }
  1495. #endif
  1496. break;
  1497. case kPluginPostRtEventProgramChange:
  1498. // Update UI
  1499. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1500. uiProgramChange(static_cast<uint32_t>(event.value1));
  1501. #ifndef BUILD_BRIDGE
  1502. // Update OSC control client
  1503. if (sendOsc)
  1504. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1505. // Update Host
  1506. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1507. // Update param values
  1508. for (uint32_t j=0; j < pData->param.count; ++j)
  1509. {
  1510. const float paramValue(getParameterValue(j));
  1511. if (sendOsc)
  1512. {
  1513. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1514. pData->engine->oscSend_control_set_default_value(pData->id, j, pData->param.ranges[j].def);
  1515. }
  1516. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1517. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, pData->param.ranges[j].def, nullptr);
  1518. }
  1519. #endif
  1520. break;
  1521. case kPluginPostRtEventMidiProgramChange:
  1522. // Update UI
  1523. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1524. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1525. #ifndef BUILD_BRIDGE
  1526. // Update OSC control client
  1527. if (sendOsc)
  1528. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1529. // Update Host
  1530. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1531. // Update param values
  1532. for (uint32_t j=0; j < pData->param.count; ++j)
  1533. {
  1534. const float paramValue(getParameterValue(j));
  1535. if (sendOsc)
  1536. {
  1537. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1538. pData->engine->oscSend_control_set_default_value(pData->id, j, pData->param.ranges[j].def);
  1539. }
  1540. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1541. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, pData->param.ranges[j].def, nullptr);
  1542. }
  1543. #endif
  1544. break;
  1545. case kPluginPostRtEventNoteOn:
  1546. {
  1547. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1548. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1549. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1550. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1551. const uint8_t note = static_cast<uint8_t>(event.value2);
  1552. const uint8_t velocity = uint8_t(event.value3);
  1553. // Update UI
  1554. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1555. uiNoteOn(channel, note, velocity);
  1556. #ifndef BUILD_BRIDGE
  1557. // Update OSC control client
  1558. if (sendOsc)
  1559. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1560. // Update Host
  1561. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1562. #endif
  1563. break;
  1564. }
  1565. case kPluginPostRtEventNoteOff:
  1566. {
  1567. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1568. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1569. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1570. const uint8_t note = static_cast<uint8_t>(event.value2);
  1571. // Update UI
  1572. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1573. uiNoteOff(channel, note);
  1574. #ifndef BUILD_BRIDGE
  1575. // Update OSC control client
  1576. if (sendOsc)
  1577. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1578. // Update Host
  1579. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1580. #endif
  1581. break;
  1582. }
  1583. }
  1584. }
  1585. pData->postRtEvents.data.clear();
  1586. }
  1587. // -------------------------------------------------------------------
  1588. // Post-poned UI Stuff
  1589. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1590. {
  1591. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1592. return;
  1593. // unused
  1594. (void)value;
  1595. }
  1596. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1597. {
  1598. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1599. }
  1600. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1601. {
  1602. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1603. }
  1604. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1605. {
  1606. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1607. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1608. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1609. }
  1610. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1611. {
  1612. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1613. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1614. }
  1615. bool CarlaPlugin::canRunInRack() const noexcept
  1616. {
  1617. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1618. }
  1619. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1620. {
  1621. return pData->engine;
  1622. }
  1623. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1624. {
  1625. return pData->client;
  1626. }
  1627. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1628. {
  1629. return pData->audioIn.ports[index].port;
  1630. }
  1631. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1632. {
  1633. return pData->audioOut.ports[index].port;
  1634. }
  1635. // -------------------------------------------------------------------
  1636. // Scoped Disabler
  1637. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1638. : fPlugin(plugin)
  1639. {
  1640. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1641. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1642. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1643. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1644. plugin->pData->masterMutex.lock();
  1645. if (plugin->pData->enabled)
  1646. plugin->pData->enabled = false;
  1647. if (plugin->pData->client->isActive())
  1648. plugin->pData->client->deactivate();
  1649. }
  1650. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1651. {
  1652. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1653. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1654. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1655. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1656. fPlugin->pData->enabled = true;
  1657. fPlugin->pData->client->activate();
  1658. fPlugin->pData->masterMutex.unlock();
  1659. }
  1660. // -------------------------------------------------------------------
  1661. // Scoped Process Locker
  1662. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1663. : fPlugin(plugin),
  1664. fBlock(block)
  1665. {
  1666. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1667. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1668. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1669. if (! fBlock)
  1670. return;
  1671. plugin->pData->singleMutex.lock();
  1672. }
  1673. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1674. {
  1675. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1676. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1677. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1678. if (! fBlock)
  1679. return;
  1680. #ifndef BUILD_BRIDGE
  1681. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1682. fPlugin->pData->needsReset = true;
  1683. #endif
  1684. fPlugin->pData->singleMutex.unlock();
  1685. }
  1686. // -------------------------------------------------------------------
  1687. CARLA_BACKEND_END_NAMESPACE