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.

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