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.

2013 lines
64KB

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