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.

2081 lines
66KB

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