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.

2102 lines
66KB

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