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.

2121 lines
67KB

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