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.

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