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.

2075 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 (carla_standalone_get_transient_win_id() != 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. unsigned int CarlaPlugin::getPatchbayClientId() const noexcept
  141. {
  142. return pData->patchbayClientId;
  143. }
  144. bool CarlaPlugin::isEnabled() const noexcept
  145. {
  146. return pData->enabled;
  147. }
  148. const char* CarlaPlugin::getName() const noexcept
  149. {
  150. return pData->name;
  151. }
  152. const char* CarlaPlugin::getFilename() const noexcept
  153. {
  154. return pData->filename;
  155. }
  156. const char* CarlaPlugin::getIconName() const noexcept
  157. {
  158. return pData->iconName;
  159. }
  160. PluginCategory CarlaPlugin::getCategory() const noexcept
  161. {
  162. PluginCategory category = PLUGIN_CATEGORY_NONE;
  163. try {
  164. category = getPluginCategoryFromName(pData->name);
  165. } catch(...) {}
  166. return category;
  167. }
  168. long CarlaPlugin::getUniqueId() const noexcept
  169. {
  170. return 0;
  171. }
  172. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  173. {
  174. return pData->latency;
  175. }
  176. // -------------------------------------------------------------------
  177. // Information (count)
  178. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  179. {
  180. return pData->audioIn.count;
  181. }
  182. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  183. {
  184. return pData->audioOut.count;
  185. }
  186. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  187. {
  188. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  189. }
  190. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  191. {
  192. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  193. }
  194. uint32_t CarlaPlugin::getParameterCount() const noexcept
  195. {
  196. return pData->param.count;
  197. }
  198. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  201. return 0;
  202. }
  203. uint32_t CarlaPlugin::getProgramCount() const noexcept
  204. {
  205. return pData->prog.count;
  206. }
  207. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  208. {
  209. return pData->midiprog.count;
  210. }
  211. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  212. {
  213. return static_cast<uint32_t>(pData->custom.count());
  214. }
  215. // -------------------------------------------------------------------
  216. // Information (current data)
  217. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  218. {
  219. return pData->prog.current;
  220. }
  221. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  222. {
  223. return pData->midiprog.current;
  224. }
  225. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
  226. {
  227. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  228. return pData->param.data[parameterId];
  229. }
  230. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
  231. {
  232. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  233. return pData->param.ranges[parameterId];
  234. }
  235. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
  236. {
  237. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  238. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  239. }
  240. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  243. return pData->midiprog.data[index];
  244. }
  245. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
  246. {
  247. CARLA_SAFE_ASSERT_RETURN(index < pData->custom.count(), kCustomDataNull);
  248. return pData->custom.getAt(index);
  249. }
  250. int32_t CarlaPlugin::getChunkData(void** const dataPtr) const noexcept
  251. {
  252. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  253. CARLA_ASSERT(false); // this should never happen
  254. return 0;
  255. }
  256. // -------------------------------------------------------------------
  257. // Information (per-plugin data)
  258. unsigned int CarlaPlugin::getOptionsAvailable() const noexcept
  259. {
  260. CARLA_ASSERT(false); // this should never happen
  261. return 0x0;
  262. }
  263. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
  264. {
  265. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  266. CARLA_ASSERT(false); // this should never happen
  267. return 0.0f;
  268. }
  269. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
  270. {
  271. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  272. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  273. CARLA_ASSERT(false); // this should never happen
  274. return 0.0f;
  275. }
  276. void CarlaPlugin::getLabel(char* const strBuf) const noexcept
  277. {
  278. strBuf[0] = '\0';
  279. }
  280. void CarlaPlugin::getMaker(char* const strBuf) const noexcept
  281. {
  282. strBuf[0] = '\0';
  283. }
  284. void CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  285. {
  286. strBuf[0] = '\0';
  287. }
  288. void CarlaPlugin::getRealName(char* const strBuf) const noexcept
  289. {
  290. strBuf[0] = '\0';
  291. }
  292. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  293. {
  294. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  295. CARLA_ASSERT(false); // this should never happen
  296. strBuf[0] = '\0';
  297. }
  298. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  299. {
  300. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  301. strBuf[0] = '\0';
  302. }
  303. void CarlaPlugin::getParameterText(const uint32_t parameterId, const float, char* const strBuf) const noexcept
  304. {
  305. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  306. CARLA_ASSERT(false); // this should never happen
  307. strBuf[0] = '\0';
  308. }
  309. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  312. strBuf[0] = '\0';
  313. }
  314. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  315. {
  316. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  317. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  318. CARLA_ASSERT(false); // this should never happen
  319. strBuf[0] = '\0';
  320. }
  321. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  322. {
  323. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  324. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr,);
  325. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  326. }
  327. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  328. {
  329. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  330. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr,);
  331. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  332. }
  333. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  334. {
  335. ins = 0;
  336. outs = 0;
  337. for (uint32_t i=0; i < pData->param.count; ++i)
  338. {
  339. if (pData->param.data[i].type == PARAMETER_INPUT)
  340. ++ins;
  341. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  342. ++outs;
  343. }
  344. }
  345. // -------------------------------------------------------------------
  346. // Set data (state)
  347. void CarlaPlugin::prepareForSave()
  348. {
  349. }
  350. const SaveState& CarlaPlugin::getSaveState()
  351. {
  352. pData->saveState.reset();
  353. prepareForSave();
  354. char strBuf[STR_MAX+1];
  355. // ---------------------------------------------------------------
  356. // Basic info
  357. getLabel(strBuf);
  358. pData->saveState.type = carla_strdup(getPluginTypeAsString(getType()));
  359. pData->saveState.name = carla_strdup(pData->name);
  360. pData->saveState.label = carla_strdup(strBuf);
  361. pData->saveState.binary = carla_strdup(pData->filename);
  362. pData->saveState.uniqueID = getUniqueId();
  363. // ---------------------------------------------------------------
  364. // Internals
  365. pData->saveState.active = pData->active;
  366. #ifndef BUILD_BRIDGE
  367. pData->saveState.dryWet = pData->postProc.dryWet;
  368. pData->saveState.volume = pData->postProc.volume;
  369. pData->saveState.balanceLeft = pData->postProc.balanceLeft;
  370. pData->saveState.balanceRight = pData->postProc.balanceRight;
  371. pData->saveState.panning = pData->postProc.panning;
  372. pData->saveState.ctrlChannel = pData->ctrlChannel;
  373. #endif
  374. // ---------------------------------------------------------------
  375. // Chunk
  376. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  377. {
  378. void* data = nullptr;
  379. const int32_t dataSize(getChunkData(&data));
  380. if (data != nullptr && dataSize > 0)
  381. {
  382. pData->saveState.chunk = carla_strdup(QByteArray((char*)data, dataSize).toBase64().constData());
  383. // Don't save anything else if using chunks
  384. return pData->saveState;
  385. }
  386. }
  387. // ---------------------------------------------------------------
  388. // Current Program
  389. if (pData->prog.current >= 0 && getType() != PLUGIN_LV2)
  390. {
  391. pData->saveState.currentProgramIndex = pData->prog.current;
  392. pData->saveState.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  393. }
  394. // ---------------------------------------------------------------
  395. // Current MIDI Program
  396. if (pData->midiprog.current >= 0 && getType() != PLUGIN_LV2)
  397. {
  398. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  399. pData->saveState.currentMidiBank = static_cast<int32_t>(mpData.bank);
  400. pData->saveState.currentMidiProgram = static_cast<int32_t>(mpData.program);
  401. }
  402. // ---------------------------------------------------------------
  403. // Parameters
  404. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  405. for (uint32_t i=0; i < pData->param.count; ++i)
  406. {
  407. const ParameterData& paramData(pData->param.data[i]);
  408. if (paramData.type != PARAMETER_INPUT || (paramData.hints & PARAMETER_IS_ENABLED) == 0)
  409. continue;
  410. StateParameter* const stateParameter(new StateParameter());
  411. stateParameter->index = paramData.index;
  412. stateParameter->midiCC = paramData.midiCC;
  413. stateParameter->midiChannel = paramData.midiChannel;
  414. getParameterName(i, strBuf);
  415. stateParameter->name = carla_strdup(strBuf);
  416. getParameterSymbol(i, strBuf);
  417. stateParameter->symbol = carla_strdup(strBuf);;
  418. stateParameter->value = getParameterValue(i);
  419. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  420. stateParameter->value /= sampleRate;
  421. pData->saveState.parameters.append(stateParameter);
  422. }
  423. // ---------------------------------------------------------------
  424. // Custom Data
  425. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  426. {
  427. const CustomData& cData(it.getValue());
  428. StateCustomData* stateCustomData(new StateCustomData());
  429. stateCustomData->type = carla_strdup(cData.type);
  430. stateCustomData->key = carla_strdup(cData.key);
  431. stateCustomData->value = carla_strdup(cData.value);
  432. pData->saveState.customData.append(stateCustomData);
  433. }
  434. return pData->saveState;
  435. }
  436. void CarlaPlugin::loadSaveState(const SaveState& saveState)
  437. {
  438. char strBuf[STR_MAX+1];
  439. const bool usesMultiProgs(getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || (getType() == PLUGIN_INTERNAL && getCategory() == PLUGIN_CATEGORY_SYNTH));
  440. gIsLoadingProject = true;
  441. ScopedValueSetter<bool>(gIsLoadingProject, false);
  442. // ---------------------------------------------------------------
  443. // Part 1 - PRE-set custom data (only that which reload programs)
  444. for (LinkedList<StateCustomData*>::Itenerator it = saveState.customData.begin(); it.valid(); it.next())
  445. {
  446. const StateCustomData* const stateCustomData(it.getValue());
  447. const char* const key(stateCustomData->key);
  448. bool wantData = false;
  449. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  450. wantData = true;
  451. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  452. wantData = true;
  453. if (wantData)
  454. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  455. }
  456. // ---------------------------------------------------------------
  457. // Part 2 - set program
  458. if (saveState.currentProgramIndex >= 0 && saveState.currentProgramName != nullptr)
  459. {
  460. int32_t programId = -1;
  461. // index < count
  462. if (saveState.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  463. {
  464. programId = saveState.currentProgramIndex;
  465. }
  466. // index not valid, try to find by name
  467. else
  468. {
  469. for (uint32_t i=0; i < pData->prog.count; ++i)
  470. {
  471. strBuf[0] = '\0';
  472. getProgramName(i, strBuf);
  473. if (strBuf[0] != '\0' && std::strcmp(saveState.currentProgramName, strBuf) == 0)
  474. {
  475. programId = static_cast<int32_t>(i);
  476. break;
  477. }
  478. }
  479. }
  480. // set program now, if valid
  481. if (programId >= 0)
  482. setProgram(programId, true, true, true);
  483. }
  484. // ---------------------------------------------------------------
  485. // Part 3 - set midi program
  486. if (saveState.currentMidiBank >= 0 && saveState.currentMidiProgram >= 0 && ! usesMultiProgs)
  487. setMidiProgramById(static_cast<uint32_t>(saveState.currentMidiBank), static_cast<uint32_t>(saveState.currentMidiProgram), true, true, true);
  488. // ---------------------------------------------------------------
  489. // Part 4a - get plugin parameter symbols
  490. LinkedList<ParamSymbol*> paramSymbols;
  491. if (getType() == PLUGIN_LADSPA || getType() == PLUGIN_LV2)
  492. {
  493. for (uint32_t i=0; i < pData->param.count; ++i)
  494. {
  495. strBuf[0] = '\0';
  496. getParameterSymbol(i, strBuf);
  497. if (strBuf[0] != '\0')
  498. {
  499. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  500. paramSymbols.append(paramSymbol);
  501. }
  502. }
  503. }
  504. // ---------------------------------------------------------------
  505. // Part 4b - set parameter values (carefully)
  506. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  507. for (LinkedList<StateParameter*>::Itenerator it = saveState.parameters.begin(); it.valid(); it.next())
  508. {
  509. StateParameter* const stateParameter(it.getValue());
  510. int32_t index = -1;
  511. if (getType() == PLUGIN_LADSPA)
  512. {
  513. // Try to set by symbol, otherwise use index
  514. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  515. {
  516. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  517. {
  518. ParamSymbol* const paramSymbol(it2.getValue());
  519. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  520. {
  521. index = paramSymbol->index;
  522. break;
  523. }
  524. }
  525. if (index == -1)
  526. index = stateParameter->index;
  527. }
  528. else
  529. index = stateParameter->index;
  530. }
  531. else if (getType() == PLUGIN_LV2)
  532. {
  533. // Symbol only
  534. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  535. {
  536. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  537. {
  538. ParamSymbol* const paramSymbol(it2.getValue());
  539. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  540. {
  541. index = paramSymbol->index;
  542. break;
  543. }
  544. }
  545. if (index == -1)
  546. carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
  547. }
  548. else
  549. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  550. }
  551. else
  552. {
  553. // Index only
  554. index = stateParameter->index;
  555. }
  556. // Now set parameter
  557. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  558. {
  559. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  560. stateParameter->value *= sampleRate;
  561. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  562. #ifndef BUILD_BRIDGE
  563. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  564. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  565. #endif
  566. }
  567. else
  568. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  569. }
  570. // ---------------------------------------------------------------
  571. // Part 4c - clear
  572. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin(); it.valid(); it.next())
  573. {
  574. ParamSymbol* const paramSymbol(it.getValue());
  575. delete paramSymbol;
  576. }
  577. paramSymbols.clear();
  578. // ---------------------------------------------------------------
  579. // Part 5 - set custom data
  580. for (LinkedList<StateCustomData*>::Itenerator it = saveState.customData.begin(); it.valid(); it.next())
  581. {
  582. const StateCustomData* const stateCustomData(it.getValue());
  583. const char* const key(stateCustomData->key);
  584. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  585. continue;
  586. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  587. continue;
  588. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  589. }
  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_ASSERT(type != nullptr);
  897. CARLA_ASSERT(key != nullptr);
  898. CARLA_ASSERT(value != nullptr);
  899. #ifdef BUILD_BRIDGE
  900. if (! gIsLoadingProject)
  901. {
  902. CARLA_ASSERT(! sendGui); // this should never happen
  903. }
  904. #endif
  905. if (type == nullptr)
  906. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is null", type, key, value, bool2str(sendGui));
  907. if (key == nullptr)
  908. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  909. if (value == nullptr)
  910. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  911. bool saveData = true;
  912. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  913. {
  914. // Ignore some keys
  915. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  916. saveData = false;
  917. //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)
  918. // saveData = false;
  919. }
  920. if (! saveData)
  921. return;
  922. // Check if we already have this key
  923. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  924. {
  925. CustomData& cData(it.getValue());
  926. CARLA_ASSERT(cData.type != nullptr);
  927. CARLA_ASSERT(cData.key != nullptr);
  928. CARLA_ASSERT(cData.value != nullptr);
  929. if (cData.type == nullptr)
  930. return;
  931. if (cData.key == nullptr)
  932. return;
  933. if (std::strcmp(cData.key, key) == 0)
  934. {
  935. if (cData.value != nullptr)
  936. delete[] cData.value;
  937. cData.value = carla_strdup(value);
  938. return;
  939. }
  940. }
  941. // Otherwise store it
  942. CustomData newData;
  943. newData.type = carla_strdup(type);
  944. newData.key = carla_strdup(key);
  945. newData.value = carla_strdup(value);
  946. pData->custom.append(newData);
  947. }
  948. void CarlaPlugin::setChunkData(const char* const stringData)
  949. {
  950. CARLA_ASSERT(stringData != nullptr);
  951. CARLA_ASSERT(false); // this should never happen
  952. return;
  953. // unused
  954. (void)stringData;
  955. }
  956. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  957. {
  958. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  959. #ifdef BUILD_BRIDGE
  960. if (! gIsLoadingProject) {
  961. CARLA_ASSERT(! sendGui); // this should never happen
  962. }
  963. #endif
  964. pData->prog.current = index;
  965. #ifndef BUILD_BRIDGE
  966. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  967. if (reallySendOsc)
  968. pData->engine->oscSend_control_set_current_program(pData->id, index);
  969. #endif
  970. if (sendCallback)
  971. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  972. // Change default parameter values
  973. if (index >= 0)
  974. {
  975. #ifndef BUILD_BRIDGE
  976. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  977. uiProgramChange(static_cast<uint32_t>(index));
  978. #endif
  979. if (getType() == PLUGIN_FILE_CSD || getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || getType() == PLUGIN_FILE_SFZ)
  980. return;
  981. for (uint32_t i=0; i < pData->param.count; ++i)
  982. {
  983. const float value(pData->param.ranges[i].getFixedValue(getParameterValue(i)));
  984. pData->param.ranges[i].def = value;
  985. #ifndef BUILD_BRIDGE
  986. if (reallySendOsc)
  987. {
  988. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), value);
  989. pData->engine->oscSend_control_set_default_value(pData->id, i, value);
  990. }
  991. #endif
  992. if (sendCallback)
  993. {
  994. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  995. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  996. }
  997. }
  998. }
  999. #ifdef BUILD_BRIDGE
  1000. return;
  1001. // unused
  1002. (void)sendGui;
  1003. (void)sendOsc;
  1004. #endif
  1005. }
  1006. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1007. {
  1008. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1009. #ifdef BUILD_BRIDGE
  1010. if (! gIsLoadingProject) {
  1011. CARLA_ASSERT(! sendGui); // this should never happen
  1012. }
  1013. #endif
  1014. pData->midiprog.current = index;
  1015. #ifndef BUILD_BRIDGE
  1016. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1017. if (reallySendOsc)
  1018. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1019. #endif
  1020. if (sendCallback)
  1021. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1022. if (index >= 0)
  1023. {
  1024. #ifndef BUILD_BRIDGE
  1025. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1026. uiMidiProgramChange(static_cast<uint32_t>(index));
  1027. #endif
  1028. if (getType() == PLUGIN_FILE_CSD || getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || getType() == PLUGIN_FILE_SFZ)
  1029. return;
  1030. for (uint32_t i=0; i < pData->param.count; ++i)
  1031. {
  1032. const float value(pData->param.ranges[i].getFixedValue(getParameterValue(i)));
  1033. pData->param.ranges[i].def = value;
  1034. #ifndef BUILD_BRIDGE
  1035. if (reallySendOsc)
  1036. {
  1037. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), value);
  1038. pData->engine->oscSend_control_set_default_value(pData->id, i, value);
  1039. }
  1040. #endif
  1041. if (sendCallback)
  1042. {
  1043. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1044. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(i), 0, value, nullptr);
  1045. }
  1046. }
  1047. }
  1048. #ifdef BUILD_BRIDGE
  1049. return;
  1050. // unused
  1051. (void)sendGui;
  1052. (void)sendOsc;
  1053. #endif
  1054. }
  1055. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1056. {
  1057. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1058. {
  1059. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1060. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1061. }
  1062. }
  1063. // -------------------------------------------------------------------
  1064. // Set ui stuff
  1065. void CarlaPlugin::idle()
  1066. {
  1067. if (! pData->enabled)
  1068. return;
  1069. if (pData->hints & PLUGIN_NEEDS_SINGLE_THREAD)
  1070. {
  1071. // Process postponed events
  1072. postRtEventsRun();
  1073. // Update parameter outputs
  1074. for (uint32_t i=0; i < pData->param.count; ++i)
  1075. {
  1076. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1077. uiParameterChange(i, getParameterValue(i));
  1078. }
  1079. }
  1080. if (pData->transientTryCounter == 0)
  1081. return;
  1082. if (++pData->transientTryCounter % 10 != 0)
  1083. return;
  1084. if (pData->transientTryCounter >= 200)
  1085. return;
  1086. carla_stdout("Trying to get window...");
  1087. QString uiTitle(QString("%1 (GUI)").arg(pData->name));
  1088. if (CarlaPluginUi::tryTransientWinIdMatch(pData->osc.data.target != nullptr ? pData->osc.thread.getPid() : 0, uiTitle.toUtf8().constData(), carla_standalone_get_transient_win_id()))
  1089. pData->transientTryCounter = 0;
  1090. }
  1091. void CarlaPlugin::showCustomUI(const bool yesNo)
  1092. {
  1093. CARLA_ASSERT(false);
  1094. return;
  1095. // unused
  1096. (void)yesNo;
  1097. }
  1098. // -------------------------------------------------------------------
  1099. // Plugin state
  1100. void CarlaPlugin::reloadPrograms(const bool)
  1101. {
  1102. }
  1103. // -------------------------------------------------------------------
  1104. // Plugin processing
  1105. void CarlaPlugin::activate() noexcept
  1106. {
  1107. CARLA_ASSERT(! pData->active);
  1108. }
  1109. void CarlaPlugin::deactivate() noexcept
  1110. {
  1111. CARLA_ASSERT(pData->active);
  1112. }
  1113. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1114. {
  1115. }
  1116. void CarlaPlugin::sampleRateChanged(const double)
  1117. {
  1118. }
  1119. void CarlaPlugin::offlineModeChanged(const bool)
  1120. {
  1121. }
  1122. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1123. {
  1124. if (forcedOffline)
  1125. {
  1126. pData->masterMutex.lock();
  1127. return true;
  1128. }
  1129. return pData->masterMutex.tryLock();
  1130. }
  1131. void CarlaPlugin::unlock() noexcept
  1132. {
  1133. pData->masterMutex.unlock();
  1134. }
  1135. // -------------------------------------------------------------------
  1136. // Plugin buffers
  1137. void CarlaPlugin::initBuffers()
  1138. {
  1139. pData->audioIn.initBuffers();
  1140. pData->audioOut.initBuffers();
  1141. pData->event.initBuffers();
  1142. }
  1143. void CarlaPlugin::clearBuffers()
  1144. {
  1145. pData->clearBuffers();
  1146. }
  1147. // -------------------------------------------------------------------
  1148. // OSC stuff
  1149. void CarlaPlugin::registerToOscClient() noexcept
  1150. {
  1151. #ifdef BUILD_BRIDGE
  1152. if (! pData->engine->isOscBridgeRegistered())
  1153. #else
  1154. if (! pData->engine->isOscControlRegistered())
  1155. #endif
  1156. return;
  1157. #ifndef BUILD_BRIDGE
  1158. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1159. #endif
  1160. // Base data
  1161. {
  1162. // TODO - clear buf
  1163. char bufName[STR_MAX+1] = { '\0' };
  1164. char bufLabel[STR_MAX+1] = { '\0' };
  1165. char bufMaker[STR_MAX+1] = { '\0' };
  1166. char bufCopyright[STR_MAX+1] = { '\0' };
  1167. getRealName(bufName);
  1168. getLabel(bufLabel);
  1169. getMaker(bufMaker);
  1170. getCopyright(bufCopyright);
  1171. #ifdef BUILD_BRIDGE
  1172. pData->engine->oscSend_bridge_plugin_info1(getCategory(), pData->hints, getUniqueId());
  1173. pData->engine->oscSend_bridge_plugin_info2(bufName, bufLabel, bufMaker, bufCopyright);
  1174. #else
  1175. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1176. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1177. #endif
  1178. }
  1179. // Base count
  1180. {
  1181. uint32_t paramIns, paramOuts;
  1182. getParameterCountInfo(paramIns, paramOuts);
  1183. #ifdef BUILD_BRIDGE
  1184. pData->engine->oscSend_bridge_audio_count(getAudioInCount(), getAudioOutCount());
  1185. pData->engine->oscSend_bridge_midi_count(getMidiInCount(), getMidiOutCount());
  1186. pData->engine->oscSend_bridge_parameter_count(paramIns, paramOuts);
  1187. #else
  1188. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1189. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1190. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1191. #endif
  1192. }
  1193. // Plugin Parameters
  1194. if (pData->param.count > 0 && pData->param.count < pData->engine->getOptions().maxParameters)
  1195. {
  1196. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1197. for (uint32_t i=0; i < pData->param.count; ++i)
  1198. {
  1199. carla_fill<char>(bufName, STR_MAX, '\0');
  1200. carla_fill<char>(bufUnit, STR_MAX, '\0');
  1201. getParameterName(i, bufName);
  1202. getParameterUnit(i, bufUnit);
  1203. const ParameterData& paramData(pData->param.data[i]);
  1204. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1205. #ifdef BUILD_BRIDGE
  1206. pData->engine->oscSend_bridge_parameter_data(i, paramData.rindex, paramData.type, paramData.hints, bufName, bufUnit);
  1207. pData->engine->oscSend_bridge_parameter_ranges1(i, paramRanges.def, paramRanges.min, paramRanges.max);
  1208. pData->engine->oscSend_bridge_parameter_ranges2(i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1209. pData->engine->oscSend_bridge_parameter_value(i, getParameterValue(i));
  1210. pData->engine->oscSend_bridge_parameter_midi_cc(i, paramData.midiCC);
  1211. pData->engine->oscSend_bridge_parameter_midi_channel(i, paramData.midiChannel);
  1212. #else
  1213. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1214. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1215. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1216. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1217. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1218. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1219. #endif
  1220. }
  1221. }
  1222. // Programs
  1223. if (pData->prog.count > 0)
  1224. {
  1225. #ifdef BUILD_BRIDGE
  1226. pData->engine->oscSend_bridge_program_count(pData->prog.count);
  1227. for (uint32_t i=0; i < pData->prog.count; ++i)
  1228. pData->engine->oscSend_bridge_program_name(i, pData->prog.names[i]);
  1229. pData->engine->oscSend_bridge_current_program(pData->prog.current);
  1230. #else
  1231. pData->engine->oscSend_control_set_program_count(pData->id, pData->prog.count);
  1232. for (uint32_t i=0; i < pData->prog.count; ++i)
  1233. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1234. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1235. #endif
  1236. }
  1237. // MIDI Programs
  1238. if (pData->midiprog.count > 0)
  1239. {
  1240. #ifdef BUILD_BRIDGE
  1241. pData->engine->oscSend_bridge_midi_program_count(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_bridge_midi_program_data(i, mpData.bank, mpData.program, mpData.name);
  1246. }
  1247. pData->engine->oscSend_bridge_current_midi_program(pData->midiprog.current);
  1248. #else
  1249. pData->engine->oscSend_control_set_midi_program_count(pData->id, pData->midiprog.count);
  1250. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1251. {
  1252. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1253. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1254. }
  1255. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1256. #endif
  1257. }
  1258. #ifndef BUILD_BRIDGE
  1259. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1260. // Internal Parameters
  1261. {
  1262. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1263. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1264. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1265. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1266. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1267. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1268. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1269. }
  1270. #endif
  1271. }
  1272. void CarlaPlugin::updateOscData(const lo_address& source, const char* const url)
  1273. {
  1274. // FIXME - remove debug prints later
  1275. carla_stdout("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  1276. pData->osc.data.free();
  1277. const int proto = lo_address_get_protocol(source);
  1278. {
  1279. const char* host = lo_address_get_hostname(source);
  1280. const char* port = lo_address_get_port(source);
  1281. pData->osc.data.source = lo_address_new_with_proto(proto, host, port);
  1282. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1283. }
  1284. {
  1285. char* host = lo_url_get_hostname(url);
  1286. char* port = lo_url_get_port(url);
  1287. pData->osc.data.path = carla_strdup_free(lo_url_get_path(url));
  1288. pData->osc.data.target = lo_address_new_with_proto(proto, host, port);
  1289. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, pData->osc.data.path);
  1290. std::free(host);
  1291. std::free(port);
  1292. }
  1293. #ifndef BUILD_BRIDGE
  1294. if (pData->hints & PLUGIN_IS_BRIDGE)
  1295. return;
  1296. #endif
  1297. osc_send_sample_rate(pData->osc.data, static_cast<float>(pData->engine->getSampleRate()));
  1298. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  1299. {
  1300. const CustomData& cData(it.getValue());
  1301. CARLA_ASSERT(cData.type != nullptr);
  1302. CARLA_ASSERT(cData.key != nullptr);
  1303. CARLA_ASSERT(cData.value != nullptr);
  1304. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0)
  1305. osc_send_configure(pData->osc.data, cData.key, cData.value);
  1306. }
  1307. if (pData->prog.current >= 0)
  1308. osc_send_program(pData->osc.data, static_cast<uint32_t>(pData->prog.current));
  1309. if (pData->midiprog.current >= 0)
  1310. {
  1311. const MidiProgramData& curMidiProg(pData->midiprog.getCurrent());
  1312. if (getType() == PLUGIN_DSSI)
  1313. osc_send_program(pData->osc.data, curMidiProg.bank, curMidiProg.program);
  1314. else
  1315. osc_send_midi_program(pData->osc.data, curMidiProg.bank, curMidiProg.program);
  1316. }
  1317. for (uint32_t i=0; i < pData->param.count; ++i)
  1318. osc_send_control(pData->osc.data, pData->param.data[i].rindex, getParameterValue(i));
  1319. if ((pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0 && carla_standalone_get_transient_win_id() != 0)
  1320. pData->transientTryCounter = 1;
  1321. carla_stdout("CarlaPlugin::updateOscData() - done");
  1322. }
  1323. // void CarlaPlugin::freeOscData()
  1324. // {
  1325. // pData->osc.data.free();
  1326. // }
  1327. bool CarlaPlugin::waitForOscGuiShow()
  1328. {
  1329. carla_stdout("CarlaPlugin::waitForOscGuiShow()");
  1330. uint i=0, oscUiTimeout = pData->engine->getOptions().uiBridgesTimeout;
  1331. // wait for UI 'update' call
  1332. for (; i < oscUiTimeout/100; ++i)
  1333. {
  1334. if (pData->osc.data.target != nullptr)
  1335. {
  1336. carla_stdout("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  1337. osc_send_show(pData->osc.data);
  1338. return true;
  1339. }
  1340. else
  1341. carla_msleep(100);
  1342. }
  1343. carla_stdout("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  1344. return false;
  1345. }
  1346. // -------------------------------------------------------------------
  1347. // MIDI events
  1348. #ifndef BUILD_BRIDGE
  1349. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1350. {
  1351. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1352. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1353. CARLA_ASSERT(velo < MAX_MIDI_VALUE);
  1354. if (! pData->active)
  1355. return;
  1356. ExternalMidiNote extNote;
  1357. extNote.channel = static_cast<int8_t>(channel);
  1358. extNote.note = note;
  1359. extNote.velo = velo;
  1360. pData->extNotes.append(extNote);
  1361. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1362. {
  1363. if (velo > 0)
  1364. uiNoteOn(channel, note, velo);
  1365. else
  1366. uiNoteOff(channel, note);
  1367. }
  1368. if (sendOsc && pData->engine->isOscControlRegistered())
  1369. {
  1370. if (velo > 0)
  1371. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1372. else
  1373. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1374. }
  1375. if (sendCallback)
  1376. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1377. }
  1378. #endif
  1379. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1380. {
  1381. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1382. return;
  1383. PluginPostRtEvent postEvent;
  1384. postEvent.type = kPluginPostRtEventNoteOff;
  1385. postEvent.value1 = pData->ctrlChannel;
  1386. postEvent.value2 = 0;
  1387. postEvent.value3 = 0.0f;
  1388. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1389. {
  1390. postEvent.value2 = i;
  1391. pData->postRtEvents.appendRT(postEvent);
  1392. }
  1393. }
  1394. // -------------------------------------------------------------------
  1395. // Post-poned events
  1396. void CarlaPlugin::postRtEventsRun()
  1397. {
  1398. const CarlaMutex::ScopedLocker sl(pData->postRtEvents.mutex);
  1399. #ifndef BUILD_BRIDGE
  1400. const bool sendOsc(pData->engine->isOscControlRegistered());
  1401. #endif
  1402. while (! pData->postRtEvents.data.isEmpty())
  1403. {
  1404. const PluginPostRtEvent& event(pData->postRtEvents.data.getFirst(true));
  1405. switch (event.type)
  1406. {
  1407. case kPluginPostRtEventNull:
  1408. break;
  1409. case kPluginPostRtEventDebug:
  1410. #ifndef BUILD_BRIDGE
  1411. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1412. #endif
  1413. break;
  1414. case kPluginPostRtEventParameterChange:
  1415. // Update UI
  1416. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1417. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1418. #ifndef BUILD_BRIDGE
  1419. if (event.value2 != 1)
  1420. {
  1421. // Update OSC control client
  1422. if (sendOsc)
  1423. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1424. // Update Host
  1425. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1426. }
  1427. #endif
  1428. break;
  1429. case kPluginPostRtEventProgramChange:
  1430. // Update UI
  1431. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1432. uiProgramChange(static_cast<uint32_t>(event.value1));
  1433. #ifndef BUILD_BRIDGE
  1434. // Update OSC control client
  1435. if (sendOsc)
  1436. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1437. // Update Host
  1438. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1439. // Update param values
  1440. for (uint32_t j=0; j < pData->param.count; ++j)
  1441. {
  1442. const float paramValue(getParameterValue(j));
  1443. if (sendOsc)
  1444. {
  1445. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1446. pData->engine->oscSend_control_set_default_value(pData->id, j, pData->param.ranges[j].def);
  1447. }
  1448. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1449. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, pData->param.ranges[j].def, nullptr);
  1450. }
  1451. #endif
  1452. break;
  1453. case kPluginPostRtEventMidiProgramChange:
  1454. // Update UI
  1455. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1456. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1457. #ifndef BUILD_BRIDGE
  1458. // Update OSC control client
  1459. if (sendOsc)
  1460. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1461. // Update Host
  1462. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1463. // Update param values
  1464. for (uint32_t j=0; j < pData->param.count; ++j)
  1465. {
  1466. const float paramValue(getParameterValue(j));
  1467. if (sendOsc)
  1468. {
  1469. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1470. pData->engine->oscSend_control_set_default_value(pData->id, j, pData->param.ranges[j].def);
  1471. }
  1472. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1473. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, pData->param.ranges[j].def, nullptr);
  1474. }
  1475. #endif
  1476. break;
  1477. case kPluginPostRtEventNoteOn:
  1478. {
  1479. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1480. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1481. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1482. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1483. const uint8_t note = static_cast<uint8_t>(event.value2);
  1484. const uint8_t velocity = uint8_t(event.value3);
  1485. // Update UI
  1486. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1487. uiNoteOn(channel, note, velocity);
  1488. #ifndef BUILD_BRIDGE
  1489. // Update OSC control client
  1490. if (sendOsc)
  1491. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1492. // Update Host
  1493. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1494. #endif
  1495. break;
  1496. }
  1497. case kPluginPostRtEventNoteOff:
  1498. {
  1499. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1500. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1501. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1502. const uint8_t note = static_cast<uint8_t>(event.value2);
  1503. // Update UI
  1504. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1505. uiNoteOff(channel, note);
  1506. #ifndef BUILD_BRIDGE
  1507. // Update OSC control client
  1508. if (sendOsc)
  1509. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1510. // Update Host
  1511. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1512. #endif
  1513. break;
  1514. }
  1515. }
  1516. }
  1517. }
  1518. // -------------------------------------------------------------------
  1519. // Post-poned UI Stuff
  1520. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1521. {
  1522. CARLA_ASSERT(index < getParameterCount());
  1523. return;
  1524. // unused
  1525. (void)index;
  1526. (void)value;
  1527. }
  1528. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1529. {
  1530. CARLA_ASSERT(index < getProgramCount());
  1531. return;
  1532. // unused
  1533. (void)index;
  1534. }
  1535. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1536. {
  1537. CARLA_ASSERT(index < getMidiProgramCount());
  1538. return;
  1539. // unused
  1540. (void)index;
  1541. }
  1542. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1543. {
  1544. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1545. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1546. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1547. return;
  1548. // unused
  1549. (void)channel;
  1550. (void)note;
  1551. (void)velo;
  1552. }
  1553. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1554. {
  1555. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1556. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1557. return;
  1558. // unused
  1559. (void)channel;
  1560. (void)note;
  1561. }
  1562. bool CarlaPlugin::canRunInRack() const noexcept
  1563. {
  1564. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1565. }
  1566. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1567. {
  1568. return pData->engine;
  1569. }
  1570. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1571. {
  1572. return pData->client;
  1573. }
  1574. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1575. {
  1576. return pData->audioIn.ports[index].port;
  1577. }
  1578. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1579. {
  1580. return pData->audioOut.ports[index].port;
  1581. }
  1582. // -------------------------------------------------------------------
  1583. // Scoped Disabler
  1584. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1585. : fPlugin(plugin)
  1586. {
  1587. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1588. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1589. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1590. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1591. plugin->pData->masterMutex.lock();
  1592. if (plugin->pData->enabled)
  1593. plugin->pData->enabled = false;
  1594. if (plugin->pData->client->isActive())
  1595. plugin->pData->client->deactivate();
  1596. }
  1597. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1598. {
  1599. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1600. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1601. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1602. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1603. fPlugin->pData->enabled = true;
  1604. fPlugin->pData->client->activate();
  1605. fPlugin->pData->masterMutex.unlock();
  1606. }
  1607. // -------------------------------------------------------------------
  1608. // Scoped Process Locker
  1609. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1610. : fPlugin(plugin),
  1611. fBlock(block)
  1612. {
  1613. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1614. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1615. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1616. if (! fBlock)
  1617. return;
  1618. plugin->pData->singleMutex.lock();
  1619. }
  1620. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1621. {
  1622. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1623. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1624. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1625. if (! fBlock)
  1626. return;
  1627. #ifndef BUILD_BRIDGE
  1628. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1629. fPlugin->pData->needsReset = true;
  1630. #endif
  1631. fPlugin->pData->singleMutex.unlock();
  1632. }
  1633. // -------------------------------------------------------------------
  1634. CARLA_BACKEND_END_NAMESPACE