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.

2006 lines
64KB

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