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.

2069 lines
64KB

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