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.

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