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.

2070 lines
65KB

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