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.

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