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.

2143 lines
67KB

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