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.

2396 lines
78KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2017 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaBase64Utils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "CarlaPluginUI.hpp"
  23. #include "CarlaStringList.hpp"
  24. #include <ctime>
  25. #include "water/files/File.h"
  26. #include "water/streams/MemoryOutputStream.h"
  27. #include "water/xml/XmlDocument.h"
  28. #include "water/xml/XmlElement.h"
  29. using water::CharPointer_UTF8;
  30. using water::File;
  31. using water::MemoryOutputStream;
  32. using water::Result;
  33. using water::String;
  34. using water::XmlDocument;
  35. using water::XmlElement;
  36. CARLA_BACKEND_START_NAMESPACE
  37. // -------------------------------------------------------------------
  38. // Fallback data
  39. static const ParameterData kParameterDataNull = { PARAMETER_UNKNOWN, 0x0, PARAMETER_NULL, -1, -1, 0 };
  40. static const ParameterRanges kParameterRangesNull = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
  41. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  42. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  43. static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
  44. static const PluginPostRtEvent kPluginPostRtEventFallback = { kPluginPostRtEventNull, 0, 0, 0.0f };
  45. // -------------------------------------------------------------------
  46. // ParamSymbol struct, needed for CarlaPlugin::loadStateSave()
  47. struct ParamSymbol {
  48. int32_t index;
  49. const char* symbol;
  50. ParamSymbol(const uint32_t i, const char* const s)
  51. : index(static_cast<int32_t>(i)),
  52. symbol(carla_strdup(s)) {}
  53. ~ParamSymbol() noexcept
  54. {
  55. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
  56. delete[] symbol;
  57. symbol = nullptr;
  58. }
  59. #ifdef CARLA_PROPER_CPP11_SUPPORT
  60. ParamSymbol() = delete;
  61. CARLA_DECLARE_NON_COPY_STRUCT(ParamSymbol)
  62. #endif
  63. };
  64. // -------------------------------------------------------------------
  65. // Constructor and destructor
  66. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const uint id)
  67. : pData(new ProtectedData(engine, id))
  68. {
  69. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  70. CARLA_SAFE_ASSERT(id < engine->getMaxPluginNumber());
  71. carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  72. switch (engine->getProccessMode())
  73. {
  74. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  75. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  76. CARLA_SAFE_ASSERT(id < MAX_DEFAULT_PLUGINS);
  77. break;
  78. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  79. CARLA_SAFE_ASSERT(id < MAX_RACK_PLUGINS);
  80. break;
  81. case ENGINE_PROCESS_MODE_PATCHBAY:
  82. CARLA_SAFE_ASSERT(id < MAX_PATCHBAY_PLUGINS);
  83. break;
  84. case ENGINE_PROCESS_MODE_BRIDGE:
  85. CARLA_SAFE_ASSERT(id == 0);
  86. break;
  87. }
  88. }
  89. CarlaPlugin::~CarlaPlugin()
  90. {
  91. carla_debug("CarlaPlugin::~CarlaPlugin()");
  92. delete pData;
  93. }
  94. // -------------------------------------------------------------------
  95. // Information (base)
  96. uint CarlaPlugin::getId() const noexcept
  97. {
  98. return pData->id;
  99. }
  100. uint CarlaPlugin::getHints() const noexcept
  101. {
  102. return pData->hints;
  103. }
  104. uint CarlaPlugin::getOptionsEnabled() const noexcept
  105. {
  106. return pData->options;
  107. }
  108. bool CarlaPlugin::isEnabled() const noexcept
  109. {
  110. return pData->enabled;
  111. }
  112. const char* CarlaPlugin::getName() const noexcept
  113. {
  114. return pData->name;
  115. }
  116. const char* CarlaPlugin::getFilename() const noexcept
  117. {
  118. return pData->filename;
  119. }
  120. const char* CarlaPlugin::getIconName() const noexcept
  121. {
  122. return pData->iconName;
  123. }
  124. PluginCategory CarlaPlugin::getCategory() const noexcept
  125. {
  126. return getPluginCategoryFromName(pData->name);
  127. }
  128. int64_t CarlaPlugin::getUniqueId() const noexcept
  129. {
  130. return 0;
  131. }
  132. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  133. {
  134. return 0;
  135. }
  136. // -------------------------------------------------------------------
  137. // Information (count)
  138. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  139. {
  140. return pData->audioIn.count;
  141. }
  142. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  143. {
  144. return pData->audioOut.count;
  145. }
  146. uint32_t CarlaPlugin::getCVInCount() const noexcept
  147. {
  148. return pData->cvIn.count;
  149. }
  150. uint32_t CarlaPlugin::getCVOutCount() const noexcept
  151. {
  152. return pData->cvOut.count;
  153. }
  154. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  155. {
  156. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  157. }
  158. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  159. {
  160. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  161. }
  162. uint32_t CarlaPlugin::getParameterCount() const noexcept
  163. {
  164. return pData->param.count;
  165. }
  166. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
  167. {
  168. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  169. return 0;
  170. }
  171. uint32_t CarlaPlugin::getProgramCount() const noexcept
  172. {
  173. return pData->prog.count;
  174. }
  175. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  176. {
  177. return pData->midiprog.count;
  178. }
  179. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  180. {
  181. return static_cast<uint32_t>(pData->custom.count());
  182. }
  183. // -------------------------------------------------------------------
  184. // Information (current data)
  185. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  186. {
  187. return pData->prog.current;
  188. }
  189. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  190. {
  191. return pData->midiprog.current;
  192. }
  193. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
  194. {
  195. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  196. return pData->param.data[parameterId];
  197. }
  198. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  201. return pData->param.ranges[parameterId];
  202. }
  203. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
  204. {
  205. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  206. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  207. }
  208. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  211. return pData->midiprog.data[index];
  212. }
  213. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
  214. {
  215. return pData->custom.getAt(index, kCustomDataFallback);
  216. }
  217. std::size_t CarlaPlugin::getChunkData(void** const dataPtr) noexcept
  218. {
  219. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  220. CARLA_SAFE_ASSERT(false); // this should never happen
  221. return 0;
  222. }
  223. // -------------------------------------------------------------------
  224. // Information (per-plugin data)
  225. uint CarlaPlugin::getOptionsAvailable() const noexcept
  226. {
  227. CARLA_SAFE_ASSERT(false); // this should never happen
  228. return 0x0;
  229. }
  230. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
  231. {
  232. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  233. CARLA_SAFE_ASSERT(false); // this should never happen
  234. return 0.0f;
  235. }
  236. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
  237. {
  238. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  239. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  240. CARLA_SAFE_ASSERT(false); // this should never happen
  241. return 0.0f;
  242. }
  243. void CarlaPlugin::getLabel(char* const strBuf) const noexcept
  244. {
  245. strBuf[0] = '\0';
  246. }
  247. void CarlaPlugin::getMaker(char* const strBuf) const noexcept
  248. {
  249. strBuf[0] = '\0';
  250. }
  251. void CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  252. {
  253. strBuf[0] = '\0';
  254. }
  255. void CarlaPlugin::getRealName(char* const strBuf) const noexcept
  256. {
  257. strBuf[0] = '\0';
  258. }
  259. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  262. CARLA_SAFE_ASSERT(false); // this should never happen
  263. strBuf[0] = '\0';
  264. }
  265. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  268. strBuf[0] = '\0';
  269. }
  270. void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) noexcept
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  273. CARLA_SAFE_ASSERT(false); // this should never happen
  274. strBuf[0] = '\0';
  275. }
  276. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  277. {
  278. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  279. strBuf[0] = '\0';
  280. }
  281. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  282. {
  283. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  284. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  285. CARLA_SAFE_ASSERT(false); // this should never happen
  286. strBuf[0] = '\0';
  287. }
  288. float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
  289. {
  290. #ifndef BUILD_BRIDGE
  291. CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
  292. switch (parameterId)
  293. {
  294. case PARAMETER_ACTIVE:
  295. return pData->active;
  296. case PARAMETER_CTRL_CHANNEL:
  297. return pData->ctrlChannel;
  298. case PARAMETER_DRYWET:
  299. return pData->postProc.dryWet;
  300. case PARAMETER_VOLUME:
  301. return pData->postProc.volume;
  302. case PARAMETER_BALANCE_LEFT:
  303. return pData->postProc.balanceLeft;
  304. case PARAMETER_BALANCE_RIGHT:
  305. return pData->postProc.balanceRight;
  306. case PARAMETER_PANNING:
  307. return pData->postProc.panning;
  308. };
  309. #endif
  310. CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
  311. return getParameterValue(static_cast<uint32_t>(parameterId));
  312. }
  313. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  314. {
  315. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  316. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr,);
  317. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  318. }
  319. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  320. {
  321. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  322. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr,);
  323. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  324. }
  325. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  326. {
  327. ins = 0;
  328. outs = 0;
  329. for (uint32_t i=0; i < pData->param.count; ++i)
  330. {
  331. if (pData->param.data[i].type == PARAMETER_INPUT)
  332. ++ins;
  333. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  334. ++outs;
  335. }
  336. }
  337. // -------------------------------------------------------------------
  338. // Set data (state)
  339. void CarlaPlugin::prepareForSave()
  340. {
  341. }
  342. void CarlaPlugin::resetParameters() noexcept
  343. {
  344. for (uint i=0; i < pData->param.count; ++i)
  345. {
  346. const ParameterData& paramData(pData->param.data[i]);
  347. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  348. if (paramData.type != PARAMETER_INPUT)
  349. continue;
  350. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  351. continue;
  352. setParameterValue(i, paramRanges.def, true, true, true);
  353. }
  354. }
  355. void CarlaPlugin::randomizeParameters() noexcept
  356. {
  357. float value, random;
  358. char strBuf[STR_MAX+1];
  359. strBuf[STR_MAX] = '\0';
  360. std::srand(static_cast<uint>(std::time(nullptr)));
  361. for (uint i=0; i < pData->param.count; ++i)
  362. {
  363. const ParameterData& paramData(pData->param.data[i]);
  364. if (paramData.type != PARAMETER_INPUT)
  365. continue;
  366. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  367. continue;
  368. getParameterName(i, strBuf);
  369. if (std::strstr(strBuf, "olume") != nullptr)
  370. continue;
  371. if (std::strstr(strBuf, "Master") != nullptr)
  372. continue;
  373. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  374. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  375. {
  376. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  377. value = random > 0.5 ? paramRanges.max : paramRanges.min;
  378. }
  379. else
  380. {
  381. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  382. value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
  383. if (paramData.hints & PARAMETER_IS_INTEGER)
  384. value = std::rint(value);
  385. }
  386. setParameterValue(i, value, true, true, true);
  387. }
  388. }
  389. const CarlaStateSave& CarlaPlugin::getStateSave(const bool callPrepareForSave)
  390. {
  391. if (callPrepareForSave)
  392. prepareForSave();
  393. pData->stateSave.clear();
  394. const PluginType pluginType(getType());
  395. char strBuf[STR_MAX+1];
  396. // ---------------------------------------------------------------
  397. // Basic info
  398. getLabel(strBuf);
  399. pData->stateSave.type = carla_strdup(getPluginTypeAsString(getType()));
  400. pData->stateSave.name = carla_strdup(pData->name);
  401. pData->stateSave.label = carla_strdup(strBuf);
  402. pData->stateSave.uniqueId = getUniqueId();
  403. #ifndef BUILD_BRIDGE
  404. pData->stateSave.options = pData->options;
  405. #endif
  406. if (pData->filename != nullptr)
  407. pData->stateSave.binary = carla_strdup(pData->filename);
  408. #ifndef BUILD_BRIDGE
  409. // ---------------------------------------------------------------
  410. // Internals
  411. pData->stateSave.active = pData->active;
  412. pData->stateSave.dryWet = pData->postProc.dryWet;
  413. pData->stateSave.volume = pData->postProc.volume;
  414. pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
  415. pData->stateSave.balanceRight = pData->postProc.balanceRight;
  416. pData->stateSave.panning = pData->postProc.panning;
  417. pData->stateSave.ctrlChannel = pData->ctrlChannel;
  418. #endif
  419. bool usingChunk = false;
  420. // ---------------------------------------------------------------
  421. // Chunk
  422. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  423. {
  424. void* data = nullptr;
  425. const std::size_t dataSize(getChunkData(&data));
  426. if (data != nullptr && dataSize > 0)
  427. {
  428. pData->stateSave.chunk = CarlaString::asBase64(data, dataSize).dup();
  429. if (pluginType != PLUGIN_INTERNAL)
  430. usingChunk = true;
  431. }
  432. }
  433. // ---------------------------------------------------------------
  434. // Current Program
  435. if (pData->prog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_GIG)
  436. {
  437. pData->stateSave.currentProgramIndex = pData->prog.current;
  438. pData->stateSave.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  439. }
  440. // ---------------------------------------------------------------
  441. // Current MIDI Program
  442. if (pData->midiprog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_SF2)
  443. {
  444. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  445. pData->stateSave.currentMidiBank = static_cast<int32_t>(mpData.bank);
  446. pData->stateSave.currentMidiProgram = static_cast<int32_t>(mpData.program);
  447. }
  448. // ---------------------------------------------------------------
  449. // Parameters
  450. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  451. for (uint32_t i=0; i < pData->param.count; ++i)
  452. {
  453. const ParameterData& paramData(pData->param.data[i]);
  454. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  455. continue;
  456. const bool dummy = paramData.type != PARAMETER_INPUT || usingChunk;
  457. if (dummy && paramData.midiCC <= -1)
  458. continue;
  459. CarlaStateSave::Parameter* const stateParameter(new CarlaStateSave::Parameter());
  460. stateParameter->dummy = dummy;
  461. stateParameter->index = paramData.index;
  462. #ifndef BUILD_BRIDGE
  463. stateParameter->midiCC = paramData.midiCC;
  464. stateParameter->midiChannel = paramData.midiChannel;
  465. #endif
  466. getParameterName(i, strBuf);
  467. stateParameter->name = carla_strdup(strBuf);
  468. getParameterSymbol(i, strBuf);
  469. stateParameter->symbol = carla_strdup(strBuf);;
  470. if (! dummy)
  471. {
  472. stateParameter->value = getParameterValue(i);
  473. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  474. stateParameter->value /= sampleRate;
  475. }
  476. pData->stateSave.parameters.append(stateParameter);
  477. }
  478. // ---------------------------------------------------------------
  479. // Custom Data
  480. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  481. {
  482. const CustomData& cData(it.getValue(kCustomDataFallback));
  483. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  484. CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
  485. stateCustomData->type = carla_strdup(cData.type);
  486. stateCustomData->key = carla_strdup(cData.key);
  487. stateCustomData->value = carla_strdup(cData.value);
  488. pData->stateSave.customData.append(stateCustomData);
  489. }
  490. return pData->stateSave;
  491. }
  492. void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
  493. {
  494. char strBuf[STR_MAX+1];
  495. const bool usesMultiProgs(pData->hints & PLUGIN_USES_MULTI_PROGS);
  496. const PluginType pluginType(getType());
  497. // ---------------------------------------------------------------
  498. // Part 1 - PRE-set custom data (only those which reload programs)
  499. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  500. {
  501. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  502. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  503. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  504. const char* const key(stateCustomData->key);
  505. /**/ if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  506. std::strcmp (key, "load" ) == 0 ||
  507. std::strncmp(key, "patches", 7) == 0 ))
  508. pass();
  509. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  510. pass();
  511. else
  512. continue;
  513. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  514. }
  515. // ---------------------------------------------------------------
  516. // Part 2 - set program
  517. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  518. {
  519. int32_t programId = -1;
  520. // index < count
  521. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  522. {
  523. programId = stateSave.currentProgramIndex;
  524. }
  525. // index not valid, try to find by name
  526. else
  527. {
  528. for (uint32_t i=0; i < pData->prog.count; ++i)
  529. {
  530. strBuf[0] = '\0';
  531. getProgramName(i, strBuf);
  532. if (strBuf[0] != '\0' && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  533. {
  534. programId = static_cast<int32_t>(i);
  535. break;
  536. }
  537. }
  538. }
  539. // set program now, if valid
  540. if (programId >= 0)
  541. setProgram(programId, true, true, true);
  542. }
  543. // ---------------------------------------------------------------
  544. // Part 3 - set midi program
  545. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  546. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  547. // ---------------------------------------------------------------
  548. // Part 4a - get plugin parameter symbols
  549. LinkedList<ParamSymbol*> paramSymbols;
  550. if (pluginType == PLUGIN_LADSPA || pluginType == PLUGIN_LV2)
  551. {
  552. for (uint32_t i=0; i < pData->param.count; ++i)
  553. {
  554. strBuf[0] = '\0';
  555. getParameterSymbol(i, strBuf);
  556. if (strBuf[0] != '\0')
  557. {
  558. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  559. paramSymbols.append(paramSymbol);
  560. }
  561. }
  562. }
  563. // ---------------------------------------------------------------
  564. // Part 4b - set parameter values (carefully)
  565. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  566. for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin2(); it.valid(); it.next())
  567. {
  568. CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
  569. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  570. int32_t index = -1;
  571. if (pluginType == PLUGIN_LADSPA)
  572. {
  573. // Try to set by symbol, otherwise use index
  574. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  575. {
  576. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  577. {
  578. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  579. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  580. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  581. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  582. {
  583. index = paramSymbol->index;
  584. break;
  585. }
  586. }
  587. if (index == -1)
  588. index = stateParameter->index;
  589. }
  590. else
  591. index = stateParameter->index;
  592. }
  593. else if (pluginType == PLUGIN_LV2)
  594. {
  595. // Symbol only
  596. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  597. {
  598. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  599. {
  600. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  601. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  602. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  603. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  604. {
  605. index = paramSymbol->index;
  606. break;
  607. }
  608. }
  609. if (index == -1)
  610. carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
  611. }
  612. else
  613. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  614. }
  615. else
  616. {
  617. // Index only
  618. index = stateParameter->index;
  619. }
  620. // Now set parameter
  621. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  622. {
  623. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  624. if (! stateParameter->dummy)
  625. {
  626. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  627. stateParameter->value *= sampleRate;
  628. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  629. }
  630. #ifndef BUILD_BRIDGE
  631. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  632. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  633. #endif
  634. }
  635. else
  636. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  637. }
  638. // ---------------------------------------------------------------
  639. // Part 4c - clear
  640. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
  641. {
  642. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  643. delete paramSymbol;
  644. }
  645. paramSymbols.clear();
  646. // ---------------------------------------------------------------
  647. // Part 5 - set custom data
  648. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  649. {
  650. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  651. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  652. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  653. const char* const key(stateCustomData->key);
  654. if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  655. std::strcmp (key, "load" ) == 0 ||
  656. std::strncmp(key, "patches", 7) == 0 ))
  657. continue;
  658. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  659. continue;
  660. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  661. }
  662. // ---------------------------------------------------------------
  663. // Part 5x - set lv2 state
  664. if (pluginType == PLUGIN_LV2 && pData->custom.count() > 0)
  665. setCustomData(CUSTOM_DATA_TYPE_STRING, "CarlaLoadLv2StateNow", "true", true);
  666. // ---------------------------------------------------------------
  667. // Part 6 - set chunk
  668. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  669. {
  670. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  671. #ifdef CARLA_PROPER_CPP11_SUPPORT
  672. setChunkData(chunk.data(), chunk.size());
  673. #else
  674. setChunkData(&chunk.front(), chunk.size());
  675. #endif
  676. }
  677. #ifndef BUILD_BRIDGE
  678. // ---------------------------------------------------------------
  679. // Part 6 - set internal stuff
  680. const uint availOptions(getOptionsAvailable());
  681. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  682. {
  683. const uint option(1u << i);
  684. if (availOptions & option)
  685. setOption(option, (stateSave.options & option) != 0, true);
  686. }
  687. setDryWet(stateSave.dryWet, true, true);
  688. setVolume(stateSave.volume, true, true);
  689. setBalanceLeft(stateSave.balanceLeft, true, true);
  690. setBalanceRight(stateSave.balanceRight, true, true);
  691. setPanning(stateSave.panning, true, true);
  692. setCtrlChannel(stateSave.ctrlChannel, true, true);
  693. setActive(stateSave.active, true, true);
  694. #endif
  695. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  696. }
  697. bool CarlaPlugin::saveStateToFile(const char* const filename)
  698. {
  699. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  700. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  701. MemoryOutputStream out, streamState;
  702. getStateSave().dumpToMemoryStream(streamState);
  703. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  704. out << "<!DOCTYPE CARLA-PRESET>\n";
  705. out << "<CARLA-PRESET VERSION='2.0'>\n";
  706. out << streamState;
  707. out << "</CARLA-PRESET>\n";
  708. const String jfilename = String(CharPointer_UTF8(filename));
  709. File file(jfilename);
  710. if (file.replaceWithData(out.getData(), out.getDataSize()))
  711. return true;
  712. pData->engine->setLastError("Failed to write file");
  713. return false;
  714. }
  715. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  716. {
  717. // TODO set errors
  718. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  719. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  720. const String jfilename = String(CharPointer_UTF8(filename));
  721. File file(jfilename);
  722. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  723. XmlDocument xml(file);
  724. ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  725. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  726. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  727. // completely load file
  728. xmlElement = xml.getDocumentElement(false);
  729. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  730. if (pData->stateSave.fillFromXmlElement(xmlElement))
  731. {
  732. loadStateSave(pData->stateSave);
  733. return true;
  734. }
  735. return false;
  736. }
  737. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  738. {
  739. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  740. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  741. CarlaString bundlepath(lv2path);
  742. if (! bundlepath.endsWith(".lv2"))
  743. bundlepath += ".lv2";
  744. const File bundlefolder(bundlepath.buffer());
  745. if (bundlefolder.existsAsFile())
  746. {
  747. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  748. return false;
  749. }
  750. if (! bundlefolder.exists())
  751. {
  752. const Result res(bundlefolder.createDirectory());
  753. if (res.failed())
  754. {
  755. pData->engine->setLastError(res.getErrorMessage().toRawUTF8());
  756. return false;
  757. }
  758. }
  759. CarlaString symbol(pData->name);
  760. symbol.toBasic();
  761. {
  762. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  763. if (! saveStateToFile(pluginFilename))
  764. return false;
  765. }
  766. {
  767. MemoryOutputStream manifestStream;
  768. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  769. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  770. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  771. manifestStream << "\n";
  772. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  773. manifestStream << " a lv2:Plugin ;\n";
  774. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  775. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  776. manifestStream << "\n";
  777. manifestStream << "<ext-ui>\n";
  778. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  779. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  780. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  781. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  782. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  783. manifestStream << "\n";
  784. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  785. const File manifestFile(manifestFilename.buffer());
  786. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  787. {
  788. pData->engine->setLastError("Failed to write manifest.ttl file");
  789. return false;
  790. }
  791. }
  792. {
  793. MemoryOutputStream mainStream;
  794. mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
  795. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  796. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  797. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  798. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  799. mainStream << "\n";
  800. mainStream << "<>\n";
  801. mainStream << " a lv2:Plugin ;\n";
  802. mainStream << "\n";
  803. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  804. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  805. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  806. mainStream << "\n";
  807. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  808. {
  809. mainStream << " ui:ui <ext-ui> ;\n";
  810. mainStream << "\n";
  811. }
  812. const uint32_t midiIns = getMidiInCount();
  813. const uint32_t midiOuts = getMidiOutCount();
  814. int portIndex = 0;
  815. if (midiIns > 0)
  816. {
  817. mainStream << " lv2:port [\n";
  818. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  819. mainStream << " lv2:index 0 ;\n";
  820. mainStream << " lv2:symbol \"clv2_events_in\" ;\n";
  821. mainStream << " lv2:name \"Events Input\" ;\n";
  822. mainStream << " atom:bufferType atom:Sequence ;\n";
  823. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ,\n";
  824. mainStream << " <http://lv2plug.in/ns/ext/time#Position> ;\n";
  825. mainStream << " ] ;\n";
  826. ++portIndex;
  827. for (uint32_t i=1; i<midiIns; ++i)
  828. {
  829. const String portIndexNum(portIndex++);
  830. const String portIndexLabel(portIndex);
  831. mainStream << " lv2:port [\n";
  832. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  833. mainStream << " lv2:index " << portIndexNum << " ;\n";
  834. mainStream << " lv2:symbol \"clv2_midi_in_" << portIndexLabel << "\" ;\n";
  835. mainStream << " lv2:name \"MIDI Input " << portIndexLabel << "\" ;\n";
  836. mainStream << " ] ;\n";
  837. }
  838. }
  839. else
  840. {
  841. mainStream << " lv2:port [\n";
  842. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  843. mainStream << " lv2:index 0 ;\n";
  844. mainStream << " lv2:symbol \"clv2_time_info\" ;\n";
  845. mainStream << " lv2:name \"Time Info\" ;\n";
  846. mainStream << " atom:bufferType atom:Sequence ;\n";
  847. mainStream << " atom:supports <http://lv2plug.in/ns/ext/time#Position> ;\n";
  848. mainStream << " ] ;\n";
  849. ++portIndex;
  850. }
  851. for (uint32_t i=0; i<midiOuts; ++i)
  852. {
  853. const String portIndexNum(portIndex++);
  854. const String portIndexLabel(portIndex);
  855. mainStream << " lv2:port [\n";
  856. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  857. mainStream << " lv2:index " << portIndexNum << " ;\n";
  858. mainStream << " lv2:symbol \"clv2_midi_out_" << portIndexLabel << "\" ;\n";
  859. mainStream << " lv2:name \"MIDI Output " << portIndexLabel << "\" ;\n";
  860. mainStream << " atom:bufferType atom:Sequence ;\n";
  861. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n";
  862. mainStream << " ] ;\n";
  863. }
  864. mainStream << " lv2:port [\n";
  865. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  866. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  867. mainStream << " lv2:name \"freewheel\" ;\n";
  868. mainStream << " lv2:symbol \"clv2_freewheel\" ;\n";
  869. mainStream << " lv2:default 0 ;\n";
  870. mainStream << " lv2:minimum 0 ;\n";
  871. mainStream << " lv2:maximum 1 ;\n";
  872. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  873. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  874. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  875. mainStream << " ] ;\n";
  876. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  877. {
  878. const String portIndexNum(portIndex++);
  879. const String portIndexLabel(i+1);
  880. mainStream << " lv2:port [\n";
  881. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  882. mainStream << " lv2:index " << portIndexNum << " ;\n";
  883. mainStream << " lv2:symbol \"clv2_audio_in_" << portIndexLabel << "\" ;\n";
  884. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  885. mainStream << " ] ;\n";
  886. }
  887. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  888. {
  889. const String portIndexNum(portIndex++);
  890. const String portIndexLabel(i+1);
  891. mainStream << " lv2:port [\n";
  892. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  893. mainStream << " lv2:index " << portIndexNum << " ;\n";
  894. mainStream << " lv2:symbol \"clv2_audio_out_" << portIndexLabel << "\" ;\n";
  895. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  896. mainStream << " ] ;\n";
  897. }
  898. CarlaStringList uniqueSymbolNames;
  899. char strBufName[STR_MAX+1];
  900. char strBufSymbol[STR_MAX+1];
  901. strBufName[STR_MAX] = strBufSymbol[STR_MAX] = '\0';
  902. for (uint32_t i=0; i<pData->param.count; ++i)
  903. {
  904. const ParameterData& paramData(pData->param.data[i]);
  905. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  906. const String portIndexNum(portIndex++);
  907. mainStream << " lv2:port [\n";
  908. if (paramData.type == PARAMETER_INPUT)
  909. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  910. else
  911. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  912. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  913. mainStream << " lv2:portProperty lv2:toggled ;\n";
  914. if (paramData.hints & PARAMETER_IS_INTEGER)
  915. mainStream << " lv2:portProperty lv2:integer ;\n";
  916. // TODO logarithmic, enabled (not on gui), automable, samplerate, scalepoints
  917. strBufName[0] = strBufSymbol[0] = '\0';
  918. getParameterName(i, strBufName);
  919. getParameterSymbol(i, strBufSymbol);
  920. if (strBufSymbol[0] == '\0')
  921. {
  922. CarlaString s(strBufName);
  923. s.toBasic();
  924. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  925. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  926. {
  927. const size_t len(std::strlen(strBufSymbol));
  928. std::memmove(strBufSymbol+1, strBufSymbol, len);
  929. strBufSymbol[0] = '_';
  930. strBufSymbol[len+1] = '\0';
  931. }
  932. }
  933. if (uniqueSymbolNames.contains(strBufSymbol))
  934. {
  935. std::snprintf(strBufSymbol, STR_MAX, "clv2_param_%d", i+1);
  936. strBufSymbol[STR_MAX] = '\0';
  937. }
  938. mainStream << " lv2:index " << portIndexNum << " ;\n";
  939. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  940. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  941. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  942. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  943. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  944. // TODO midiCC, midiChannel
  945. mainStream << " ] ;\n";
  946. }
  947. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  948. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" .\n";
  949. mainStream << "\n";
  950. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  951. const File mainFile(mainFilename.buffer());
  952. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  953. {
  954. pData->engine->setLastError("Failed to write main plugin ttl file");
  955. return false;
  956. }
  957. }
  958. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  959. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  960. const File binaryFileTarget(binaryFilename.buffer());
  961. if (! binaryFileSource.createSymbolicLink(binaryFileTarget, true))
  962. {
  963. pData->engine->setLastError("Failed to create symbolik link of plugin binary");
  964. return false;
  965. }
  966. const EngineOptions& opts(pData->engine->getOptions());
  967. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  968. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  969. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  970. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  971. return true;
  972. }
  973. // -------------------------------------------------------------------
  974. // Set data (internal stuff)
  975. void CarlaPlugin::setId(const uint newId) noexcept
  976. {
  977. pData->id = newId;
  978. }
  979. void CarlaPlugin::setName(const char* const newName)
  980. {
  981. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  982. if (pData->name != nullptr)
  983. delete[] pData->name;
  984. pData->name = carla_strdup(newName);
  985. }
  986. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  987. {
  988. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  989. if (yesNo)
  990. pData->options |= option;
  991. else
  992. pData->options &= ~option;
  993. #ifndef BUILD_BRIDGE
  994. if (sendCallback)
  995. pData->engine->callback(ENGINE_CALLBACK_OPTION_CHANGED, pData->id, static_cast<int>(option), yesNo ? 1 : 0, 0.0f, nullptr);
  996. #else
  997. // unused
  998. return; (void)sendCallback;
  999. #endif
  1000. }
  1001. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  1002. {
  1003. if (pData->enabled == yesNo)
  1004. return;
  1005. pData->masterMutex.lock();
  1006. pData->enabled = yesNo;
  1007. if (yesNo && ! pData->client->isActive())
  1008. pData->client->activate();
  1009. pData->masterMutex.unlock();
  1010. }
  1011. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  1012. {
  1013. #ifndef BUILD_BRIDGE
  1014. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1015. #endif
  1016. if (pData->active == active)
  1017. return;
  1018. {
  1019. const ScopedSingleProcessLocker spl(this, true);
  1020. if (active)
  1021. activate();
  1022. else
  1023. deactivate();
  1024. }
  1025. pData->active = active;
  1026. #ifndef BUILD_BRIDGE
  1027. const float value(active ? 1.0f : 0.0f);
  1028. # ifdef HAVE_LIBLO
  1029. if (sendOsc && pData->engine->isOscControlRegistered())
  1030. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  1031. # endif
  1032. if (sendCallback)
  1033. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  1034. #endif
  1035. // may be unused
  1036. return; (void)sendOsc; (void)sendCallback;
  1037. }
  1038. #ifndef BUILD_BRIDGE
  1039. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1040. {
  1041. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1042. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1043. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1044. return;
  1045. pData->postProc.dryWet = fixedValue;
  1046. #ifdef HAVE_LIBLO
  1047. if (sendOsc && pData->engine->isOscControlRegistered())
  1048. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  1049. #endif
  1050. if (sendCallback)
  1051. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  1052. // may be unused
  1053. return; (void)sendOsc;
  1054. }
  1055. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1056. {
  1057. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1058. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1059. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1060. return;
  1061. pData->postProc.volume = fixedValue;
  1062. #ifdef HAVE_LIBLO
  1063. if (sendOsc && pData->engine->isOscControlRegistered())
  1064. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  1065. #endif
  1066. if (sendCallback)
  1067. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  1068. // may be unused
  1069. return; (void)sendOsc;
  1070. }
  1071. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1072. {
  1073. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1074. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1075. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1076. return;
  1077. pData->postProc.balanceLeft = fixedValue;
  1078. #ifdef HAVE_LIBLO
  1079. if (sendOsc && pData->engine->isOscControlRegistered())
  1080. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  1081. #endif
  1082. if (sendCallback)
  1083. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  1084. // may be unused
  1085. return; (void)sendOsc;
  1086. }
  1087. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1088. {
  1089. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1090. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1091. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1092. return;
  1093. pData->postProc.balanceRight = fixedValue;
  1094. #ifdef HAVE_LIBLO
  1095. if (sendOsc && pData->engine->isOscControlRegistered())
  1096. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  1097. #endif
  1098. if (sendCallback)
  1099. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  1100. // may be unused
  1101. return; (void)sendOsc;
  1102. }
  1103. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1104. {
  1105. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1106. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1107. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1108. return;
  1109. pData->postProc.panning = fixedValue;
  1110. #ifdef HAVE_LIBLO
  1111. if (sendOsc && pData->engine->isOscControlRegistered())
  1112. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  1113. #endif
  1114. if (sendCallback)
  1115. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  1116. // may be unused
  1117. return; (void)sendOsc;
  1118. }
  1119. #endif // ! BUILD_BRIDGE
  1120. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1121. {
  1122. #ifndef BUILD_BRIDGE
  1123. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1124. #endif
  1125. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1126. if (pData->ctrlChannel == channel)
  1127. return;
  1128. pData->ctrlChannel = channel;
  1129. #ifndef BUILD_BRIDGE
  1130. const float channelf(channel);
  1131. # ifdef HAVE_LIBLO
  1132. if (sendOsc && pData->engine->isOscControlRegistered())
  1133. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  1134. # endif
  1135. if (sendCallback)
  1136. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  1137. #endif
  1138. // may be unused
  1139. return; (void)sendOsc; (void)sendCallback;
  1140. }
  1141. // -------------------------------------------------------------------
  1142. // Set data (plugin-specific stuff)
  1143. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1144. {
  1145. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1146. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1147. uiParameterChange(parameterId, value);
  1148. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1149. if (sendOsc && pData->engine->isOscControlRegistered())
  1150. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  1151. #endif
  1152. if (sendCallback)
  1153. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  1154. // may be unused
  1155. return; (void)sendOsc;
  1156. }
  1157. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1158. {
  1159. #ifndef BUILD_BRIDGE
  1160. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1161. switch (rindex)
  1162. {
  1163. case PARAMETER_ACTIVE:
  1164. return setActive((value > 0.0f), sendOsc, sendCallback);
  1165. case PARAMETER_CTRL_CHANNEL:
  1166. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1167. case PARAMETER_DRYWET:
  1168. return setDryWet(value, sendOsc, sendCallback);
  1169. case PARAMETER_VOLUME:
  1170. return setVolume(value, sendOsc, sendCallback);
  1171. case PARAMETER_BALANCE_LEFT:
  1172. return setBalanceLeft(value, sendOsc, sendCallback);
  1173. case PARAMETER_BALANCE_RIGHT:
  1174. return setBalanceRight(value, sendOsc, sendCallback);
  1175. case PARAMETER_PANNING:
  1176. return setPanning(value, sendOsc, sendCallback);
  1177. }
  1178. #endif
  1179. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1180. for (uint32_t i=0; i < pData->param.count; ++i)
  1181. {
  1182. if (pData->param.data[i].rindex == rindex)
  1183. {
  1184. //if (carla_isNotEqual(getParameterValue(i), value))
  1185. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1186. break;
  1187. }
  1188. }
  1189. }
  1190. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1191. {
  1192. #ifndef BUILD_BRIDGE
  1193. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1194. #endif
  1195. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1196. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1197. pData->param.data[parameterId].midiChannel = channel;
  1198. #ifndef BUILD_BRIDGE
  1199. # ifdef HAVE_LIBLO
  1200. if (sendOsc && pData->engine->isOscControlRegistered())
  1201. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  1202. # endif
  1203. if (sendCallback)
  1204. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  1205. #endif
  1206. // may be unused
  1207. return; (void)sendOsc; (void)sendCallback;
  1208. }
  1209. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  1210. {
  1211. #ifndef BUILD_BRIDGE
  1212. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1213. #endif
  1214. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1215. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  1216. pData->param.data[parameterId].midiCC = cc;
  1217. #ifndef BUILD_BRIDGE
  1218. # ifdef HAVE_LIBLO
  1219. if (sendOsc && pData->engine->isOscControlRegistered())
  1220. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  1221. # endif
  1222. if (sendCallback)
  1223. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  1224. #endif
  1225. // may be unused
  1226. return; (void)sendOsc; (void)sendCallback;
  1227. }
  1228. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1229. {
  1230. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1231. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1232. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1233. // Ignore some keys
  1234. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1235. {
  1236. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  1237. return;
  1238. }
  1239. // Check if we already have this key
  1240. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1241. {
  1242. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1243. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1244. if (std::strcmp(customData.key, key) == 0)
  1245. {
  1246. if (customData.value != nullptr)
  1247. delete[] customData.value;
  1248. customData.value = carla_strdup(value);
  1249. return;
  1250. }
  1251. }
  1252. // Otherwise store it
  1253. CustomData customData;
  1254. customData.type = carla_strdup(type);
  1255. customData.key = carla_strdup(key);
  1256. customData.value = carla_strdup(value);
  1257. pData->custom.append(customData);
  1258. }
  1259. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1260. {
  1261. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1262. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1263. CARLA_SAFE_ASSERT(false); // this should never happen
  1264. }
  1265. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1266. {
  1267. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1268. pData->prog.current = index;
  1269. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1270. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1271. if (reallySendOsc && index < 50)
  1272. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1273. #else
  1274. const bool reallySendOsc(false);
  1275. #endif
  1276. if (sendCallback)
  1277. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1278. // Change default parameter values
  1279. if (index >= 0)
  1280. {
  1281. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1282. uiProgramChange(static_cast<uint32_t>(index));
  1283. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1284. return;
  1285. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1286. }
  1287. // may be unused
  1288. return; (void)sendOsc;
  1289. }
  1290. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1291. {
  1292. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1293. pData->midiprog.current = index;
  1294. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1295. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1296. if (reallySendOsc && index < 50)
  1297. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1298. #else
  1299. const bool reallySendOsc(false);
  1300. #endif
  1301. if (sendCallback)
  1302. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1303. if (index >= 0)
  1304. {
  1305. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1306. uiMidiProgramChange(static_cast<uint32_t>(index));
  1307. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1308. return;
  1309. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1310. }
  1311. // may be unused
  1312. return; (void)sendOsc;
  1313. }
  1314. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1315. {
  1316. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1317. {
  1318. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1319. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1320. }
  1321. }
  1322. // -------------------------------------------------------------------
  1323. // Plugin state
  1324. void CarlaPlugin::reloadPrograms(const bool)
  1325. {
  1326. }
  1327. // -------------------------------------------------------------------
  1328. // Plugin processing
  1329. void CarlaPlugin::activate() noexcept
  1330. {
  1331. CARLA_SAFE_ASSERT(! pData->active);
  1332. }
  1333. void CarlaPlugin::deactivate() noexcept
  1334. {
  1335. CARLA_SAFE_ASSERT(pData->active);
  1336. }
  1337. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1338. {
  1339. }
  1340. void CarlaPlugin::sampleRateChanged(const double)
  1341. {
  1342. }
  1343. void CarlaPlugin::offlineModeChanged(const bool)
  1344. {
  1345. }
  1346. // -------------------------------------------------------------------
  1347. // Misc
  1348. void CarlaPlugin::idle()
  1349. {
  1350. if (! pData->enabled)
  1351. return;
  1352. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1353. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1354. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1355. const bool sendOsc(pData->engine->isOscControlRegistered());
  1356. #endif
  1357. const uint32_t latency(getLatencyInFrames());
  1358. if (pData->latency.frames != latency)
  1359. {
  1360. carla_stdout("latency changed to %i samples", latency);
  1361. const ScopedSingleProcessLocker sspl(this, true);
  1362. pData->client->setLatency(latency);
  1363. #ifndef BUILD_BRIDGE
  1364. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1365. #else
  1366. pData->latency.frames = latency;
  1367. #endif
  1368. }
  1369. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1370. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin2(); it.valid(); it.next())
  1371. {
  1372. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1373. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1374. switch (event.type)
  1375. {
  1376. case kPluginPostRtEventNull: {
  1377. } break;
  1378. case kPluginPostRtEventDebug: {
  1379. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1380. } break;
  1381. case kPluginPostRtEventParameterChange: {
  1382. // Update UI
  1383. if (event.value1 >= 0 && hasUI)
  1384. {
  1385. if (needsUiMainThread)
  1386. pData->postUiEvents.append(event);
  1387. else
  1388. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1389. }
  1390. if (event.value2 != 1)
  1391. {
  1392. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1393. // Update OSC control client
  1394. if (sendOsc)
  1395. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1396. #endif
  1397. // Update Host
  1398. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1399. }
  1400. } break;
  1401. case kPluginPostRtEventProgramChange: {
  1402. // Update UI
  1403. if (event.value1 >= 0 && hasUI)
  1404. {
  1405. if (needsUiMainThread)
  1406. pData->postUiEvents.append(event);
  1407. else
  1408. uiProgramChange(static_cast<uint32_t>(event.value1));
  1409. }
  1410. // Update param values
  1411. for (uint32_t j=0; j < pData->param.count; ++j)
  1412. {
  1413. const float paramDefault(pData->param.ranges[j].def);
  1414. const float paramValue(getParameterValue(j));
  1415. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1416. if (sendOsc && j < 50)
  1417. {
  1418. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1419. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1420. }
  1421. #endif
  1422. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1423. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1424. }
  1425. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1426. // Update OSC control client
  1427. if (sendOsc)
  1428. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1429. #endif
  1430. // Update Host
  1431. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1432. } break;
  1433. case kPluginPostRtEventMidiProgramChange: {
  1434. // Update UI
  1435. if (event.value1 >= 0 && hasUI)
  1436. {
  1437. if (needsUiMainThread)
  1438. pData->postUiEvents.append(event);
  1439. else
  1440. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1441. }
  1442. // Update param values
  1443. for (uint32_t j=0; j < pData->param.count; ++j)
  1444. {
  1445. const float paramDefault(pData->param.ranges[j].def);
  1446. const float paramValue(getParameterValue(j));
  1447. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1448. if (sendOsc && j < 50)
  1449. {
  1450. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1451. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1452. }
  1453. #endif
  1454. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1455. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1456. }
  1457. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1458. // Update OSC control client
  1459. if (sendOsc)
  1460. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1461. #endif
  1462. // Update Host
  1463. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1464. } break;
  1465. case kPluginPostRtEventNoteOn: {
  1466. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1467. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1468. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1469. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1470. const uint8_t note = static_cast<uint8_t>(event.value2);
  1471. const uint8_t velocity = uint8_t(event.value3);
  1472. // Update UI
  1473. if (hasUI)
  1474. {
  1475. if (needsUiMainThread)
  1476. pData->postUiEvents.append(event);
  1477. else
  1478. uiNoteOn(channel, note, velocity);
  1479. }
  1480. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1481. // Update OSC control client
  1482. if (sendOsc)
  1483. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1484. #endif
  1485. // Update Host
  1486. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1487. } break;
  1488. case kPluginPostRtEventNoteOff: {
  1489. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1490. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1491. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1492. const uint8_t note = static_cast<uint8_t>(event.value2);
  1493. // Update UI
  1494. if (hasUI)
  1495. {
  1496. if (needsUiMainThread)
  1497. pData->postUiEvents.append(event);
  1498. else
  1499. uiNoteOff(channel, note);
  1500. }
  1501. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1502. // Update OSC control client
  1503. if (sendOsc)
  1504. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1505. #endif
  1506. // Update Host
  1507. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1508. } break;
  1509. }
  1510. }
  1511. pData->postRtEvents.data.clear();
  1512. }
  1513. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1514. {
  1515. if (forcedOffline)
  1516. {
  1517. pData->masterMutex.lock();
  1518. return true;
  1519. }
  1520. return pData->masterMutex.tryLock();
  1521. }
  1522. void CarlaPlugin::unlock() noexcept
  1523. {
  1524. pData->masterMutex.unlock();
  1525. }
  1526. // -------------------------------------------------------------------
  1527. // Plugin buffers
  1528. void CarlaPlugin::initBuffers() const noexcept
  1529. {
  1530. pData->audioIn.initBuffers();
  1531. pData->audioOut.initBuffers();
  1532. pData->cvIn.initBuffers();
  1533. pData->cvOut.initBuffers();
  1534. pData->event.initBuffers();
  1535. }
  1536. void CarlaPlugin::clearBuffers() noexcept
  1537. {
  1538. pData->clearBuffers();
  1539. }
  1540. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1541. // -------------------------------------------------------------------
  1542. // OSC stuff
  1543. void CarlaPlugin::registerToOscClient() noexcept
  1544. {
  1545. if (! pData->engine->isOscControlRegistered())
  1546. return;
  1547. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1548. // Base data
  1549. {
  1550. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1551. carla_zeroChars(bufName, STR_MAX);
  1552. carla_zeroChars(bufLabel, STR_MAX);
  1553. carla_zeroChars(bufMaker, STR_MAX);
  1554. carla_zeroChars(bufCopyright, STR_MAX);
  1555. getRealName(bufName);
  1556. getLabel(bufLabel);
  1557. getMaker(bufMaker);
  1558. getCopyright(bufCopyright);
  1559. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1560. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1561. }
  1562. // Base count
  1563. uint32_t paramIns, paramOuts;
  1564. {
  1565. getParameterCountInfo(paramIns, paramOuts);
  1566. if (paramIns > 49)
  1567. paramIns = 49;
  1568. if (paramOuts > 49)
  1569. paramOuts = 49;
  1570. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1571. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1572. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1573. }
  1574. // Plugin Parameters
  1575. if (const uint32_t count = std::min<uint32_t>(pData->param.count, 98U))
  1576. {
  1577. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1578. for (uint32_t i=0; i<count; ++i)
  1579. {
  1580. const ParameterData& paramData(pData->param.data[i]);
  1581. if (paramData.type == PARAMETER_INPUT)
  1582. {
  1583. if (--paramIns == 0)
  1584. break;
  1585. }
  1586. else if (paramData.type == PARAMETER_INPUT)
  1587. {
  1588. if (--paramOuts == 0)
  1589. break;
  1590. }
  1591. else
  1592. {
  1593. continue;
  1594. }
  1595. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1596. carla_zeroChars(bufName, STR_MAX);
  1597. carla_zeroChars(bufUnit, STR_MAX);
  1598. getParameterName(i, bufName);
  1599. getParameterUnit(i, bufUnit);
  1600. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1601. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1602. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1603. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1604. if (paramData.midiCC >= 0)
  1605. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1606. if (paramData.midiChannel != 0)
  1607. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1608. }
  1609. }
  1610. // Programs
  1611. if (const uint32_t count = std::min<uint32_t>(pData->prog.count, 50U))
  1612. {
  1613. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1614. for (uint32_t i=0; i < count; ++i)
  1615. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1616. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1617. }
  1618. // MIDI Programs
  1619. if (const uint32_t count = std::min<uint32_t>(pData->midiprog.count, 50U))
  1620. {
  1621. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1622. for (uint32_t i=0; i < count; ++i)
  1623. {
  1624. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1625. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1626. }
  1627. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1628. }
  1629. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1630. // Internal Parameters
  1631. {
  1632. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1633. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1634. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1635. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1636. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1637. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1638. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1639. }
  1640. }
  1641. #endif
  1642. // FIXME
  1643. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1644. {
  1645. // do nothing
  1646. }
  1647. //#endif // HAVE_LIBLO && ! BUILD_BRIDGE
  1648. // -------------------------------------------------------------------
  1649. // MIDI events
  1650. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1651. {
  1652. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1653. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1654. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1655. if (! pData->active)
  1656. return;
  1657. ExternalMidiNote extNote;
  1658. extNote.channel = static_cast<int8_t>(channel);
  1659. extNote.note = note;
  1660. extNote.velo = velo;
  1661. pData->extNotes.appendNonRT(extNote);
  1662. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1663. {
  1664. if (velo > 0)
  1665. uiNoteOn(channel, note, velo);
  1666. else
  1667. uiNoteOff(channel, note);
  1668. }
  1669. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1670. if (sendOsc && pData->engine->isOscControlRegistered())
  1671. {
  1672. if (velo > 0)
  1673. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1674. else
  1675. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1676. }
  1677. #endif
  1678. if (sendCallback)
  1679. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1680. // may be unused
  1681. return; (void)sendOsc;
  1682. }
  1683. #ifndef BUILD_BRIDGE
  1684. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1685. {
  1686. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1687. return;
  1688. PluginPostRtEvent postEvent;
  1689. postEvent.type = kPluginPostRtEventNoteOff;
  1690. postEvent.value1 = pData->ctrlChannel;
  1691. postEvent.value2 = 0;
  1692. postEvent.value3 = 0.0f;
  1693. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1694. {
  1695. postEvent.value2 = i;
  1696. pData->postRtEvents.appendRT(postEvent);
  1697. }
  1698. }
  1699. #endif
  1700. // -------------------------------------------------------------------
  1701. // UI Stuff
  1702. void CarlaPlugin::showCustomUI(const bool)
  1703. {
  1704. CARLA_SAFE_ASSERT(false);
  1705. }
  1706. void CarlaPlugin::uiIdle()
  1707. {
  1708. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1709. {
  1710. // Update parameter outputs
  1711. for (uint32_t i=0; i < pData->param.count; ++i)
  1712. {
  1713. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1714. uiParameterChange(i, getParameterValue(i));
  1715. }
  1716. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1717. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1718. {
  1719. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1720. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1721. switch (event.type)
  1722. {
  1723. case kPluginPostRtEventNull:
  1724. case kPluginPostRtEventDebug:
  1725. break;
  1726. case kPluginPostRtEventParameterChange:
  1727. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1728. break;
  1729. case kPluginPostRtEventProgramChange:
  1730. uiProgramChange(static_cast<uint32_t>(event.value1));
  1731. break;
  1732. case kPluginPostRtEventMidiProgramChange:
  1733. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1734. break;
  1735. case kPluginPostRtEventNoteOn:
  1736. uiNoteOn(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2), uint8_t(event.value3));
  1737. break;
  1738. case kPluginPostRtEventNoteOff:
  1739. uiNoteOff(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2));
  1740. break;
  1741. }
  1742. }
  1743. pData->postUiEvents.data.clear();
  1744. }
  1745. if (pData->transientTryCounter == 0)
  1746. return;
  1747. if (++pData->transientTryCounter % 10 != 0)
  1748. return;
  1749. if (pData->transientTryCounter >= 200)
  1750. return;
  1751. carla_stdout("Trying to get window...");
  1752. CarlaString uiTitle(pData->name);
  1753. uiTitle += " (GUI)";
  1754. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle, pData->engine->getOptions().frontendWinId, true))
  1755. pData->transientTryCounter = 0;
  1756. }
  1757. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1758. {
  1759. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1760. return;
  1761. // unused
  1762. (void)value;
  1763. }
  1764. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1765. {
  1766. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1767. }
  1768. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1769. {
  1770. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1771. }
  1772. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1773. {
  1774. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1775. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1776. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1777. }
  1778. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1779. {
  1780. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1781. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1782. }
  1783. bool CarlaPlugin::canRunInRack() const noexcept
  1784. {
  1785. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1786. }
  1787. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1788. {
  1789. return pData->engine;
  1790. }
  1791. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1792. {
  1793. return pData->client;
  1794. }
  1795. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1796. {
  1797. return pData->audioIn.ports[index].port;
  1798. }
  1799. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1800. {
  1801. return pData->audioOut.ports[index].port;
  1802. }
  1803. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1804. {
  1805. return pData->cvIn.ports[index].port;
  1806. }
  1807. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1808. {
  1809. return pData->cvOut.ports[index].port;
  1810. }
  1811. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1812. {
  1813. return pData->event.portIn;
  1814. }
  1815. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1816. {
  1817. return pData->event.portOut;
  1818. }
  1819. void* CarlaPlugin::getNativeHandle() const noexcept
  1820. {
  1821. return nullptr;
  1822. }
  1823. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1824. {
  1825. return nullptr;
  1826. }
  1827. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1828. {
  1829. return 0;
  1830. }
  1831. // -------------------------------------------------------------------
  1832. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1833. {
  1834. return pData->nodeId;
  1835. }
  1836. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1837. {
  1838. pData->nodeId = nodeId;
  1839. }
  1840. // -------------------------------------------------------------------
  1841. // Scoped Disabler
  1842. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1843. : fPlugin(plugin),
  1844. fWasEnabled(false)
  1845. {
  1846. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1847. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1848. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1849. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1850. plugin->pData->masterMutex.lock();
  1851. if (plugin->pData->enabled)
  1852. {
  1853. fWasEnabled = true;
  1854. plugin->pData->enabled = false;
  1855. if (plugin->pData->client->isActive())
  1856. plugin->pData->client->deactivate();
  1857. }
  1858. }
  1859. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1860. {
  1861. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1862. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1863. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1864. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1865. if (fWasEnabled)
  1866. {
  1867. fPlugin->pData->enabled = true;
  1868. fPlugin->pData->client->activate();
  1869. }
  1870. fPlugin->pData->masterMutex.unlock();
  1871. }
  1872. // -------------------------------------------------------------------
  1873. // Scoped Process Locker
  1874. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1875. : fPlugin(plugin),
  1876. fBlock(block)
  1877. {
  1878. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1879. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1880. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1881. if (! fBlock)
  1882. return;
  1883. plugin->pData->singleMutex.lock();
  1884. }
  1885. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1886. {
  1887. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1888. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1889. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1890. if (! fBlock)
  1891. return;
  1892. #ifndef BUILD_BRIDGE
  1893. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1894. fPlugin->pData->needsReset = true;
  1895. #endif
  1896. fPlugin->pData->singleMutex.unlock();
  1897. }
  1898. // -------------------------------------------------------------------
  1899. CARLA_BACKEND_END_NAMESPACE