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.

2489 lines
80KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2019 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, 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. bool CarlaPlugin::getLabel(char* const strBuf) const noexcept
  244. {
  245. strBuf[0] = '\0';
  246. return false;
  247. }
  248. bool CarlaPlugin::getMaker(char* const strBuf) const noexcept
  249. {
  250. strBuf[0] = '\0';
  251. return false;
  252. }
  253. bool CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  254. {
  255. strBuf[0] = '\0';
  256. return false;
  257. }
  258. bool CarlaPlugin::getRealName(char* const strBuf) const noexcept
  259. {
  260. strBuf[0] = '\0';
  261. return false;
  262. }
  263. bool CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  264. {
  265. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  266. CARLA_SAFE_ASSERT(false); // this should never happen
  267. strBuf[0] = '\0';
  268. return false;
  269. }
  270. bool CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  273. strBuf[0] = '\0';
  274. return false;
  275. }
  276. bool CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) noexcept
  277. {
  278. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  279. CARLA_SAFE_ASSERT(false); // this should never happen
  280. strBuf[0] = '\0';
  281. return false;
  282. }
  283. bool CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  284. {
  285. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  286. strBuf[0] = '\0';
  287. return false;
  288. }
  289. bool CarlaPlugin::getParameterComment(const uint32_t parameterId, char* const strBuf) const noexcept
  290. {
  291. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  292. strBuf[0] = '\0';
  293. return false;
  294. }
  295. bool CarlaPlugin::getParameterGroupName(const uint32_t parameterId, char* const strBuf) const noexcept
  296. {
  297. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  298. strBuf[0] = '\0';
  299. return false;
  300. }
  301. bool CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  302. {
  303. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  304. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), false);
  305. CARLA_SAFE_ASSERT(false); // this should never happen
  306. strBuf[0] = '\0';
  307. return false;
  308. }
  309. float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
  310. {
  311. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  312. CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
  313. switch (parameterId)
  314. {
  315. case PARAMETER_ACTIVE:
  316. return pData->active;
  317. case PARAMETER_CTRL_CHANNEL:
  318. return pData->ctrlChannel;
  319. case PARAMETER_DRYWET:
  320. return pData->postProc.dryWet;
  321. case PARAMETER_VOLUME:
  322. return pData->postProc.volume;
  323. case PARAMETER_BALANCE_LEFT:
  324. return pData->postProc.balanceLeft;
  325. case PARAMETER_BALANCE_RIGHT:
  326. return pData->postProc.balanceRight;
  327. case PARAMETER_PANNING:
  328. return pData->postProc.panning;
  329. };
  330. #endif
  331. CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
  332. return getParameterValue(static_cast<uint32_t>(parameterId));
  333. }
  334. bool CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  335. {
  336. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count, false);
  337. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr, false);
  338. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  339. return false;
  340. }
  341. bool CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  342. {
  343. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, false);
  344. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr, false);
  345. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  346. return false;
  347. }
  348. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  349. {
  350. ins = 0;
  351. outs = 0;
  352. for (uint32_t i=0; i < pData->param.count; ++i)
  353. {
  354. if (pData->param.data[i].type == PARAMETER_INPUT)
  355. ++ins;
  356. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  357. ++outs;
  358. }
  359. }
  360. // -------------------------------------------------------------------
  361. // Set data (state)
  362. void CarlaPlugin::prepareForSave()
  363. {
  364. }
  365. void CarlaPlugin::resetParameters() noexcept
  366. {
  367. for (uint i=0; i < pData->param.count; ++i)
  368. {
  369. const ParameterData& paramData(pData->param.data[i]);
  370. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  371. if (paramData.type != PARAMETER_INPUT)
  372. continue;
  373. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  374. continue;
  375. setParameterValue(i, paramRanges.def, true, true, true);
  376. }
  377. }
  378. void CarlaPlugin::randomizeParameters() noexcept
  379. {
  380. float value, random;
  381. char strBuf[STR_MAX+1];
  382. strBuf[STR_MAX] = '\0';
  383. std::srand(static_cast<uint>(std::time(nullptr)));
  384. for (uint i=0; i < pData->param.count; ++i)
  385. {
  386. const ParameterData& paramData(pData->param.data[i]);
  387. if (paramData.type != PARAMETER_INPUT)
  388. continue;
  389. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  390. continue;
  391. getParameterName(i, strBuf);
  392. if (std::strstr(strBuf, "olume") != nullptr)
  393. continue;
  394. if (std::strstr(strBuf, "Master") != nullptr)
  395. continue;
  396. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  397. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  398. {
  399. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  400. value = random > 0.5f ? paramRanges.max : paramRanges.min;
  401. }
  402. else
  403. {
  404. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  405. value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
  406. if (paramData.hints & PARAMETER_IS_INTEGER)
  407. value = std::rint(value);
  408. }
  409. setParameterValue(i, value, true, true, true);
  410. }
  411. }
  412. const CarlaStateSave& CarlaPlugin::getStateSave(const bool callPrepareForSave)
  413. {
  414. if (callPrepareForSave)
  415. prepareForSave();
  416. pData->stateSave.clear();
  417. const PluginType pluginType(getType());
  418. char strBuf[STR_MAX+1];
  419. // ---------------------------------------------------------------
  420. // Basic info
  421. getLabel(strBuf);
  422. pData->stateSave.type = carla_strdup(getPluginTypeAsString(pluginType));
  423. pData->stateSave.name = carla_strdup(pData->name);
  424. pData->stateSave.label = carla_strdup(strBuf);
  425. pData->stateSave.uniqueId = getUniqueId();
  426. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  427. pData->stateSave.options = pData->options;
  428. #endif
  429. if (pData->filename != nullptr)
  430. pData->stateSave.binary = carla_strdup(pData->filename);
  431. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  432. // ---------------------------------------------------------------
  433. // Internals
  434. pData->stateSave.active = pData->active;
  435. pData->stateSave.dryWet = pData->postProc.dryWet;
  436. pData->stateSave.volume = pData->postProc.volume;
  437. pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
  438. pData->stateSave.balanceRight = pData->postProc.balanceRight;
  439. pData->stateSave.panning = pData->postProc.panning;
  440. pData->stateSave.ctrlChannel = pData->ctrlChannel;
  441. #endif
  442. bool usingChunk = false;
  443. // ---------------------------------------------------------------
  444. // Chunk
  445. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  446. {
  447. void* data = nullptr;
  448. const std::size_t dataSize(getChunkData(&data));
  449. if (data != nullptr && dataSize > 0)
  450. {
  451. pData->stateSave.chunk = CarlaString::asBase64(data, dataSize).dup();
  452. if (pluginType != PLUGIN_INTERNAL)
  453. usingChunk = true;
  454. }
  455. }
  456. // ---------------------------------------------------------------
  457. // Current Program
  458. if (pData->prog.current >= 0 && pluginType != 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 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_SF2)
  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. const bool dummy = paramData.type != PARAMETER_INPUT || usingChunk;
  480. if (dummy && paramData.midiCC <= -1)
  481. continue;
  482. CarlaStateSave::Parameter* const stateParameter(new CarlaStateSave::Parameter());
  483. stateParameter->dummy = dummy;
  484. stateParameter->index = paramData.index;
  485. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  486. stateParameter->midiCC = paramData.midiCC;
  487. stateParameter->midiChannel = paramData.midiChannel;
  488. #endif
  489. getParameterName(i, strBuf);
  490. stateParameter->name = carla_strdup(strBuf);
  491. getParameterSymbol(i, strBuf);
  492. stateParameter->symbol = carla_strdup(strBuf);;
  493. if (! dummy)
  494. {
  495. stateParameter->value = getParameterValue(i);
  496. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  497. stateParameter->value /= sampleRate;
  498. }
  499. pData->stateSave.parameters.append(stateParameter);
  500. }
  501. // ---------------------------------------------------------------
  502. // Custom Data
  503. if (pData->hints & PLUGIN_IS_BRIDGE)
  504. waitForBridgeSaveSignal();
  505. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  506. {
  507. const CustomData& cData(it.getValue(kCustomDataFallback));
  508. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  509. CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
  510. stateCustomData->type = carla_strdup(cData.type);
  511. stateCustomData->key = carla_strdup(cData.key);
  512. stateCustomData->value = carla_strdup(cData.value);
  513. pData->stateSave.customData.append(stateCustomData);
  514. }
  515. return pData->stateSave;
  516. }
  517. void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
  518. {
  519. char strBuf[STR_MAX+1];
  520. const bool usesMultiProgs(pData->hints & PLUGIN_USES_MULTI_PROGS);
  521. const PluginType pluginType(getType());
  522. // ---------------------------------------------------------------
  523. // Part 1 - PRE-set custom data (only those which reload programs)
  524. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  525. {
  526. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  527. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  528. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  529. const char* const key(stateCustomData->key);
  530. /**/ if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  531. std::strcmp (key, "load" ) == 0 ||
  532. std::strncmp(key, "patches", 7) == 0 ))
  533. pass();
  534. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  535. pass();
  536. else
  537. continue;
  538. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  539. }
  540. // ---------------------------------------------------------------
  541. // Part 2 - set program
  542. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  543. {
  544. int32_t programId = -1;
  545. // index < count
  546. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  547. {
  548. programId = stateSave.currentProgramIndex;
  549. }
  550. // index not valid, try to find by name
  551. else
  552. {
  553. for (uint32_t i=0; i < pData->prog.count; ++i)
  554. {
  555. strBuf[0] = '\0';
  556. getProgramName(i, strBuf);
  557. if (strBuf[0] != '\0' && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  558. {
  559. programId = static_cast<int32_t>(i);
  560. break;
  561. }
  562. }
  563. }
  564. // set program now, if valid
  565. if (programId >= 0)
  566. setProgram(programId, true, true, true);
  567. }
  568. // ---------------------------------------------------------------
  569. // Part 3 - set midi program
  570. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  571. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  572. // ---------------------------------------------------------------
  573. // Part 4a - get plugin parameter symbols
  574. LinkedList<ParamSymbol*> paramSymbols;
  575. if (pluginType == PLUGIN_LADSPA || pluginType == PLUGIN_LV2)
  576. {
  577. for (uint32_t i=0; i < pData->param.count; ++i)
  578. {
  579. strBuf[0] = '\0';
  580. getParameterSymbol(i, strBuf);
  581. if (strBuf[0] != '\0')
  582. {
  583. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  584. paramSymbols.append(paramSymbol);
  585. }
  586. }
  587. }
  588. // ---------------------------------------------------------------
  589. // Part 4b - set parameter values (carefully)
  590. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  591. for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin2(); it.valid(); it.next())
  592. {
  593. CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
  594. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  595. int32_t index = -1;
  596. if (pluginType == PLUGIN_LADSPA)
  597. {
  598. // Try to set by symbol, otherwise use index
  599. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  600. {
  601. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  602. {
  603. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  604. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  605. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  606. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  607. {
  608. index = paramSymbol->index;
  609. break;
  610. }
  611. }
  612. if (index == -1)
  613. index = stateParameter->index;
  614. }
  615. else
  616. {
  617. index = stateParameter->index;
  618. }
  619. }
  620. else if (pluginType == PLUGIN_LV2)
  621. {
  622. // Symbol only
  623. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  624. {
  625. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  626. {
  627. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  628. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  629. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  630. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  631. {
  632. index = paramSymbol->index;
  633. break;
  634. }
  635. }
  636. if (index == -1)
  637. carla_stderr("Failed to find LV2 parameter symbol '%s' for '%s'",
  638. stateParameter->symbol, pData->name);
  639. }
  640. else
  641. {
  642. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  643. }
  644. }
  645. else
  646. {
  647. // Index only
  648. index = stateParameter->index;
  649. }
  650. // Now set parameter
  651. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  652. {
  653. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  654. if (! stateParameter->dummy)
  655. {
  656. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  657. stateParameter->value *= sampleRate;
  658. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  659. }
  660. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  661. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  662. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  663. #endif
  664. }
  665. else
  666. carla_stderr("Could not set parameter '%s' value for '%s'",
  667. stateParameter->name, pData->name);
  668. }
  669. // ---------------------------------------------------------------
  670. // Part 4c - clear
  671. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
  672. {
  673. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  674. delete paramSymbol;
  675. }
  676. paramSymbols.clear();
  677. // ---------------------------------------------------------------
  678. // Part 5 - set custom data
  679. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  680. {
  681. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  682. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  683. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  684. const char* const key(stateCustomData->key);
  685. if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  686. std::strcmp (key, "load" ) == 0 ||
  687. std::strncmp(key, "patches", 7) == 0 ))
  688. continue;
  689. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  690. continue;
  691. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  692. }
  693. // ---------------------------------------------------------------
  694. // Part 5x - set lv2 state
  695. if (pluginType == PLUGIN_LV2)
  696. {
  697. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  698. {
  699. const CustomData& customData(it.getValue(kCustomDataFallback));
  700. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  701. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  702. continue;
  703. restoreLV2State();
  704. break;
  705. }
  706. }
  707. // ---------------------------------------------------------------
  708. // Part 6 - set chunk
  709. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  710. {
  711. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  712. #ifdef CARLA_PROPER_CPP11_SUPPORT
  713. setChunkData(chunk.data(), chunk.size());
  714. #else
  715. setChunkData(&chunk.front(), chunk.size());
  716. #endif
  717. }
  718. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  719. // ---------------------------------------------------------------
  720. // Part 6 - set internal stuff
  721. const uint availOptions(getOptionsAvailable());
  722. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  723. {
  724. const uint option(1u << i);
  725. if (availOptions & option)
  726. setOption(option, (stateSave.options & option) != 0, true);
  727. }
  728. setDryWet(stateSave.dryWet, true, true);
  729. setVolume(stateSave.volume, true, true);
  730. setBalanceLeft(stateSave.balanceLeft, true, true);
  731. setBalanceRight(stateSave.balanceRight, true, true);
  732. setPanning(stateSave.panning, true, true);
  733. setCtrlChannel(stateSave.ctrlChannel, true, true);
  734. setActive(stateSave.active, true, true);
  735. if (! pData->engine->isLoadingProject())
  736. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0f, nullptr);
  737. #endif
  738. }
  739. bool CarlaPlugin::saveStateToFile(const char* const filename)
  740. {
  741. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  742. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  743. MemoryOutputStream out, streamState;
  744. getStateSave().dumpToMemoryStream(streamState);
  745. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  746. out << "<!DOCTYPE CARLA-PRESET>\n";
  747. out << "<CARLA-PRESET VERSION='2.0'>\n";
  748. out << streamState;
  749. out << "</CARLA-PRESET>\n";
  750. const String jfilename = String(CharPointer_UTF8(filename));
  751. File file(jfilename);
  752. if (file.replaceWithData(out.getData(), out.getDataSize()))
  753. return true;
  754. pData->engine->setLastError("Failed to write file");
  755. return false;
  756. }
  757. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  758. {
  759. // TODO set errors
  760. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  761. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  762. const String jfilename = String(CharPointer_UTF8(filename));
  763. File file(jfilename);
  764. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  765. XmlDocument xml(file);
  766. ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  767. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  768. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  769. // completely load file
  770. xmlElement = xml.getDocumentElement(false);
  771. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  772. if (pData->stateSave.fillFromXmlElement(xmlElement))
  773. {
  774. loadStateSave(pData->stateSave);
  775. return true;
  776. }
  777. return false;
  778. }
  779. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  780. {
  781. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  782. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  783. CarlaString bundlepath(lv2path);
  784. if (! bundlepath.endsWith(".lv2"))
  785. bundlepath += ".lv2";
  786. const File bundlefolder(bundlepath.buffer());
  787. if (bundlefolder.existsAsFile())
  788. {
  789. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  790. return false;
  791. }
  792. if (! bundlefolder.exists())
  793. {
  794. const Result res(bundlefolder.createDirectory());
  795. if (res.failed())
  796. {
  797. pData->engine->setLastError(res.getErrorMessage().toRawUTF8());
  798. return false;
  799. }
  800. }
  801. CarlaString symbol(pData->name);
  802. symbol.toBasic();
  803. {
  804. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  805. if (! saveStateToFile(pluginFilename))
  806. return false;
  807. }
  808. {
  809. MemoryOutputStream manifestStream;
  810. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  811. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  812. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  813. manifestStream << "\n";
  814. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  815. manifestStream << " a lv2:Plugin ;\n";
  816. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  817. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  818. manifestStream << "\n";
  819. manifestStream << "<ext-ui>\n";
  820. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  821. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  822. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  823. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  824. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  825. manifestStream << "\n";
  826. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  827. const File manifestFile(manifestFilename.buffer());
  828. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  829. {
  830. pData->engine->setLastError("Failed to write manifest.ttl file");
  831. return false;
  832. }
  833. }
  834. {
  835. MemoryOutputStream mainStream;
  836. mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
  837. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  838. mainStream << "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  839. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  840. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  841. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  842. mainStream << "\n";
  843. mainStream << "<>\n";
  844. mainStream << " a lv2:Plugin ;\n";
  845. mainStream << "\n";
  846. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  847. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  848. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  849. mainStream << "\n";
  850. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  851. {
  852. mainStream << " ui:ui <ext-ui> ;\n";
  853. mainStream << "\n";
  854. }
  855. const uint32_t midiIns = getMidiInCount();
  856. const uint32_t midiOuts = getMidiOutCount();
  857. int portIndex = 0;
  858. if (midiIns > 0)
  859. {
  860. mainStream << " lv2:port [\n";
  861. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  862. mainStream << " lv2:index 0 ;\n";
  863. mainStream << " lv2:symbol \"clv2_events_in\" ;\n";
  864. mainStream << " lv2:name \"Events Input\" ;\n";
  865. mainStream << " atom:bufferType atom:Sequence ;\n";
  866. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ,\n";
  867. mainStream << " <http://lv2plug.in/ns/ext/time#Position> ;\n";
  868. mainStream << " ] ;\n";
  869. ++portIndex;
  870. for (uint32_t i=1; i<midiIns; ++i)
  871. {
  872. const String portIndexNum(portIndex++);
  873. const String portIndexLabel(portIndex);
  874. mainStream << " lv2:port [\n";
  875. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  876. mainStream << " lv2:index " << portIndexNum << " ;\n";
  877. mainStream << " lv2:symbol \"clv2_midi_in_" << portIndexLabel << "\" ;\n";
  878. mainStream << " lv2:name \"MIDI Input " << portIndexLabel << "\" ;\n";
  879. mainStream << " ] ;\n";
  880. }
  881. }
  882. else
  883. {
  884. mainStream << " lv2:port [\n";
  885. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  886. mainStream << " lv2:index 0 ;\n";
  887. mainStream << " lv2:symbol \"clv2_time_info\" ;\n";
  888. mainStream << " lv2:name \"Time Info\" ;\n";
  889. mainStream << " atom:bufferType atom:Sequence ;\n";
  890. mainStream << " atom:supports <http://lv2plug.in/ns/ext/time#Position> ;\n";
  891. mainStream << " ] ;\n";
  892. ++portIndex;
  893. }
  894. for (uint32_t i=0; i<midiOuts; ++i)
  895. {
  896. const String portIndexNum(portIndex++);
  897. const String portIndexLabel(portIndex);
  898. mainStream << " lv2:port [\n";
  899. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  900. mainStream << " lv2:index " << portIndexNum << " ;\n";
  901. mainStream << " lv2:symbol \"clv2_midi_out_" << portIndexLabel << "\" ;\n";
  902. mainStream << " lv2:name \"MIDI Output " << portIndexLabel << "\" ;\n";
  903. mainStream << " atom:bufferType atom:Sequence ;\n";
  904. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n";
  905. mainStream << " ] ;\n";
  906. }
  907. mainStream << " lv2:port [\n";
  908. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  909. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  910. mainStream << " lv2:name \"freewheel\" ;\n";
  911. mainStream << " lv2:symbol \"clv2_freewheel\" ;\n";
  912. mainStream << " lv2:default 0 ;\n";
  913. mainStream << " lv2:minimum 0 ;\n";
  914. mainStream << " lv2:maximum 1 ;\n";
  915. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  916. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  917. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  918. mainStream << " ] ;\n";
  919. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  920. {
  921. const String portIndexNum(portIndex++);
  922. const String portIndexLabel(i+1);
  923. mainStream << " lv2:port [\n";
  924. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  925. mainStream << " lv2:index " << portIndexNum << " ;\n";
  926. mainStream << " lv2:symbol \"clv2_audio_in_" << portIndexLabel << "\" ;\n";
  927. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  928. mainStream << " ] ;\n";
  929. }
  930. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  931. {
  932. const String portIndexNum(portIndex++);
  933. const String portIndexLabel(i+1);
  934. mainStream << " lv2:port [\n";
  935. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  936. mainStream << " lv2:index " << portIndexNum << " ;\n";
  937. mainStream << " lv2:symbol \"clv2_audio_out_" << portIndexLabel << "\" ;\n";
  938. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  939. mainStream << " ] ;\n";
  940. }
  941. CarlaStringList uniqueSymbolNames;
  942. char strBufName[STR_MAX+1];
  943. char strBufSymbol[STR_MAX+1];
  944. strBufName[STR_MAX] = strBufSymbol[STR_MAX] = '\0';
  945. for (uint32_t i=0; i<pData->param.count; ++i)
  946. {
  947. const ParameterData& paramData(pData->param.data[i]);
  948. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  949. const String portIndexNum(portIndex++);
  950. mainStream << " lv2:port [\n";
  951. if (paramData.type == PARAMETER_INPUT)
  952. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  953. else
  954. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  955. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  956. mainStream << " lv2:portProperty lv2:toggled ;\n";
  957. if (paramData.hints & PARAMETER_IS_INTEGER)
  958. mainStream << " lv2:portProperty lv2:integer ;\n";
  959. // TODO logarithmic, enabled (not on gui), automable, samplerate, scalepoints
  960. strBufName[0] = strBufSymbol[0] = '\0';
  961. getParameterName(i, strBufName);
  962. getParameterSymbol(i, strBufSymbol);
  963. if (strBufSymbol[0] == '\0')
  964. {
  965. CarlaString s(strBufName);
  966. s.toBasic();
  967. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  968. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  969. {
  970. const size_t len(std::strlen(strBufSymbol));
  971. std::memmove(strBufSymbol+1, strBufSymbol, len);
  972. strBufSymbol[0] = '_';
  973. strBufSymbol[len+1] = '\0';
  974. }
  975. }
  976. if (uniqueSymbolNames.contains(strBufSymbol))
  977. {
  978. std::snprintf(strBufSymbol, STR_MAX, "clv2_param_%d", i+1);
  979. strBufSymbol[STR_MAX] = '\0';
  980. }
  981. mainStream << " lv2:index " << portIndexNum << " ;\n";
  982. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  983. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  984. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  985. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  986. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  987. // TODO midiCC, midiChannel
  988. mainStream << " ] ;\n";
  989. }
  990. char strBuf[STR_MAX];
  991. getMaker(strBuf);
  992. strBuf[STR_MAX-1] = '\0';
  993. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  994. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" ;\n";
  995. mainStream << " doap:maintainer [ foaf:name \"\"\"" << strBuf << "\"\"\" ] .\n";
  996. mainStream << "\n";
  997. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  998. const File mainFile(mainFilename.buffer());
  999. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  1000. {
  1001. pData->engine->setLastError("Failed to write main plugin ttl file");
  1002. return false;
  1003. }
  1004. }
  1005. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  1006. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  1007. const File binaryFileTarget(binaryFilename.buffer());
  1008. const EngineOptions& opts(pData->engine->getOptions());
  1009. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  1010. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  1011. if (! binaryFileSource.copyFileTo(binaryFileTarget))
  1012. {
  1013. pData->engine->setLastError("Failed to copy plugin binary");
  1014. return false;
  1015. }
  1016. #ifdef CARLA_OS_WIN
  1017. File(opts.resourceDir).copyDirectoryTo(File(resFolderTarget.buffer()));
  1018. // Copying all the binaries is pointless, just go through the expected needed bits
  1019. const File binFolder1(opts.binaryDir);
  1020. const File binFolder2(binFolderTarget.buffer());
  1021. binFolder2.createDirectory();
  1022. static const char* files[] = {
  1023. "carla-bridge-native.exe",
  1024. "carla-bridge-win32.exe",
  1025. "carla-discovery-win32.exe",
  1026. "carla-discovery-win64.exe",
  1027. "libcarla_utils.dll"
  1028. };
  1029. for (int i=0; i<5; ++i)
  1030. binFolder1.getChildFile(files[i]).copyFileTo(binFolder2.getChildFile(files[i]));;
  1031. #else
  1032. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  1033. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  1034. #endif
  1035. return true;
  1036. }
  1037. // -------------------------------------------------------------------
  1038. // Set data (internal stuff)
  1039. void CarlaPlugin::setId(const uint newId) noexcept
  1040. {
  1041. pData->id = newId;
  1042. }
  1043. void CarlaPlugin::setName(const char* const newName)
  1044. {
  1045. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  1046. if (pData->name != nullptr)
  1047. delete[] pData->name;
  1048. pData->name = carla_strdup(newName);
  1049. }
  1050. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  1051. {
  1052. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  1053. if (yesNo)
  1054. pData->options |= option;
  1055. else
  1056. pData->options &= ~option;
  1057. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1058. if (sendCallback)
  1059. pData->engine->callback(true, true,
  1060. ENGINE_CALLBACK_OPTION_CHANGED,
  1061. pData->id,
  1062. static_cast<int>(option),
  1063. yesNo ? 1 : 0,
  1064. 0, 0.0f, nullptr);
  1065. #else
  1066. // unused
  1067. return; (void)sendCallback;
  1068. #endif
  1069. }
  1070. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  1071. {
  1072. if (pData->enabled == yesNo)
  1073. return;
  1074. pData->masterMutex.lock();
  1075. pData->enabled = yesNo;
  1076. if (yesNo && ! pData->client->isActive())
  1077. pData->client->activate();
  1078. pData->masterMutex.unlock();
  1079. }
  1080. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  1081. {
  1082. if (pData->engineBridged) {
  1083. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1084. } else if (pData->enginePlugin) {
  1085. // nothing here
  1086. } else {
  1087. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1088. }
  1089. if (pData->active == active)
  1090. return;
  1091. {
  1092. const ScopedSingleProcessLocker spl(this, true);
  1093. if (active)
  1094. activate();
  1095. else
  1096. deactivate();
  1097. }
  1098. pData->active = active;
  1099. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1100. const float value = active ? 1.0f : 0.0f;
  1101. pData->engine->callback(sendCallback, sendOsc,
  1102. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1103. pData->id,
  1104. PARAMETER_ACTIVE,
  1105. 0, 0,
  1106. value,
  1107. nullptr);
  1108. #endif
  1109. }
  1110. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1111. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1112. {
  1113. if (pData->engineBridged) {
  1114. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1115. } else {
  1116. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1117. }
  1118. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1119. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1120. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1121. return;
  1122. pData->postProc.dryWet = fixedValue;
  1123. pData->engine->callback(sendCallback, sendOsc,
  1124. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1125. pData->id,
  1126. PARAMETER_DRYWET,
  1127. 0, 0,
  1128. fixedValue,
  1129. nullptr);
  1130. }
  1131. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1132. {
  1133. if (pData->engineBridged) {
  1134. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1135. } else {
  1136. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1137. }
  1138. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1139. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1140. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1141. return;
  1142. pData->postProc.volume = fixedValue;
  1143. pData->engine->callback(sendCallback, sendOsc,
  1144. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1145. pData->id,
  1146. PARAMETER_VOLUME,
  1147. 0, 0,
  1148. fixedValue,
  1149. nullptr);
  1150. }
  1151. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1152. {
  1153. if (pData->engineBridged) {
  1154. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1155. } else {
  1156. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1157. }
  1158. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1159. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1160. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1161. return;
  1162. pData->postProc.balanceLeft = fixedValue;
  1163. pData->engine->callback(sendCallback, sendOsc,
  1164. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1165. pData->id,
  1166. PARAMETER_BALANCE_LEFT,
  1167. 0, 0,
  1168. fixedValue,
  1169. nullptr);
  1170. }
  1171. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1172. {
  1173. if (pData->engineBridged) {
  1174. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1175. } else {
  1176. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1177. }
  1178. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1179. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1180. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1181. return;
  1182. pData->postProc.balanceRight = fixedValue;
  1183. pData->engine->callback(sendCallback, sendOsc,
  1184. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1185. pData->id,
  1186. PARAMETER_BALANCE_RIGHT,
  1187. 0, 0,
  1188. fixedValue,
  1189. nullptr);
  1190. }
  1191. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1192. {
  1193. if (pData->engineBridged) {
  1194. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1195. } else {
  1196. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1197. }
  1198. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1199. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1200. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1201. return;
  1202. pData->postProc.panning = fixedValue;
  1203. pData->engine->callback(sendCallback, sendOsc,
  1204. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1205. pData->id,
  1206. PARAMETER_PANNING,
  1207. 0, 0,
  1208. fixedValue,
  1209. nullptr);
  1210. }
  1211. void CarlaPlugin::setDryWetRT(const float value) noexcept
  1212. {
  1213. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1214. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1215. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1216. return;
  1217. pData->postProc.dryWet = fixedValue;
  1218. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 1, 0, fixedValue);
  1219. }
  1220. void CarlaPlugin::setVolumeRT(const float value) noexcept
  1221. {
  1222. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1223. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1224. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1225. return;
  1226. pData->postProc.volume = fixedValue;
  1227. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 1, 0, fixedValue);
  1228. }
  1229. void CarlaPlugin::setBalanceLeftRT(const float value) noexcept
  1230. {
  1231. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1232. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1233. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1234. return;
  1235. pData->postProc.balanceLeft = fixedValue;
  1236. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 1, 0, fixedValue);
  1237. }
  1238. void CarlaPlugin::setBalanceRightRT(const float value) noexcept
  1239. {
  1240. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1241. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1242. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1243. return;
  1244. pData->postProc.balanceRight = fixedValue;
  1245. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 1, 0, fixedValue);
  1246. }
  1247. void CarlaPlugin::setPanningRT(const float value) noexcept
  1248. {
  1249. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1250. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1251. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1252. return;
  1253. pData->postProc.panning = fixedValue;
  1254. }
  1255. #endif // ! BUILD_BRIDGE_ALTERNATIVE_ARCH
  1256. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1257. {
  1258. if (pData->engineBridged) {
  1259. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1260. } else {
  1261. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1262. }
  1263. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1264. if (pData->ctrlChannel == channel)
  1265. return;
  1266. pData->ctrlChannel = channel;
  1267. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1268. const float channelf = static_cast<float>(channel);
  1269. pData->engine->callback(sendCallback, sendOsc,
  1270. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1271. pData->id,
  1272. PARAMETER_CTRL_CHANNEL,
  1273. 0, 0,
  1274. channelf, nullptr);
  1275. #endif
  1276. }
  1277. // -------------------------------------------------------------------
  1278. // Set data (plugin-specific stuff)
  1279. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1280. {
  1281. if (pData->engineBridged) {
  1282. // NOTE: some LV2 plugins feedback messages to UI on purpose
  1283. CARLA_SAFE_ASSERT_RETURN(getType() == PLUGIN_LV2 || !sendGui,);
  1284. } else if (pData->enginePlugin) {
  1285. // nothing here
  1286. } else {
  1287. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  1288. }
  1289. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1290. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1291. uiParameterChange(parameterId, value);
  1292. pData->engine->callback(sendCallback, sendOsc,
  1293. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1294. pData->id,
  1295. static_cast<int>(parameterId),
  1296. 0, 0,
  1297. value,
  1298. nullptr);
  1299. }
  1300. void CarlaPlugin::setParameterValueRT(const uint32_t parameterId, const float value) noexcept
  1301. {
  1302. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(parameterId), 1, 0, value);
  1303. }
  1304. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1305. {
  1306. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1307. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1308. switch (rindex)
  1309. {
  1310. case PARAMETER_ACTIVE:
  1311. return setActive((value > 0.0f), sendOsc, sendCallback);
  1312. case PARAMETER_CTRL_CHANNEL:
  1313. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1314. case PARAMETER_DRYWET:
  1315. return setDryWet(value, sendOsc, sendCallback);
  1316. case PARAMETER_VOLUME:
  1317. return setVolume(value, sendOsc, sendCallback);
  1318. case PARAMETER_BALANCE_LEFT:
  1319. return setBalanceLeft(value, sendOsc, sendCallback);
  1320. case PARAMETER_BALANCE_RIGHT:
  1321. return setBalanceRight(value, sendOsc, sendCallback);
  1322. case PARAMETER_PANNING:
  1323. return setPanning(value, sendOsc, sendCallback);
  1324. }
  1325. #endif
  1326. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1327. for (uint32_t i=0; i < pData->param.count; ++i)
  1328. {
  1329. if (pData->param.data[i].rindex == rindex)
  1330. {
  1331. //if (carla_isNotEqual(getParameterValue(i), value))
  1332. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1338. {
  1339. if (pData->engineBridged) {
  1340. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1341. } else {
  1342. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1343. }
  1344. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1345. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1346. pData->param.data[parameterId].midiChannel = channel;
  1347. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1348. pData->engine->callback(sendCallback, sendOsc,
  1349. ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED,
  1350. pData->id,
  1351. static_cast<int>(parameterId),
  1352. channel,
  1353. 0, 0.0f, nullptr);
  1354. #endif
  1355. }
  1356. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  1357. {
  1358. if (pData->engineBridged) {
  1359. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1360. } else {
  1361. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1362. }
  1363. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1364. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  1365. pData->param.data[parameterId].midiCC = cc;
  1366. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1367. pData->engine->callback(sendCallback, sendOsc,
  1368. ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED,
  1369. pData->id,
  1370. static_cast<int>(parameterId),
  1371. cc,
  1372. 0, 0.0f, nullptr);
  1373. #endif
  1374. }
  1375. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1376. {
  1377. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1378. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1379. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1380. // Ignore some keys
  1381. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1382. {
  1383. const PluginType ptype = getType();
  1384. if ((ptype == PLUGIN_INTERNAL && std::strncmp(key, "CarlaAlternateFile", 18) == 0) ||
  1385. (ptype == PLUGIN_DSSI && std::strcmp (key, "guiVisible") == 0) ||
  1386. (ptype == PLUGIN_LV2 && std::strncmp(key, "OSC:", 4) == 0))
  1387. return;
  1388. }
  1389. // Check if we already have this key
  1390. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1391. {
  1392. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1393. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1394. if (std::strcmp(customData.key, key) == 0)
  1395. {
  1396. if (customData.value != nullptr)
  1397. delete[] customData.value;
  1398. customData.value = carla_strdup(value);
  1399. return;
  1400. }
  1401. }
  1402. // Otherwise store it
  1403. CustomData customData;
  1404. customData.type = carla_strdup(type);
  1405. customData.key = carla_strdup(key);
  1406. customData.value = carla_strdup(value);
  1407. pData->custom.append(customData);
  1408. }
  1409. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1410. {
  1411. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1412. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1413. CARLA_SAFE_ASSERT(false); // this should never happen
  1414. }
  1415. void CarlaPlugin::setProgram(const int32_t index,
  1416. const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1417. {
  1418. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1419. pData->prog.current = index;
  1420. pData->engine->callback(sendCallback, sendOsc,
  1421. ENGINE_CALLBACK_PROGRAM_CHANGED,
  1422. pData->id,
  1423. index,
  1424. 0, 0, 0.0f, nullptr);
  1425. // Change default parameter values
  1426. if (index >= 0)
  1427. {
  1428. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1429. uiProgramChange(static_cast<uint32_t>(index));
  1430. switch (getType())
  1431. {
  1432. case PLUGIN_SF2:
  1433. case PLUGIN_SFZ:
  1434. break;
  1435. default:
  1436. pData->updateParameterValues(this, sendCallback, sendOsc, true);
  1437. break;
  1438. }
  1439. }
  1440. }
  1441. void CarlaPlugin::setMidiProgram(const int32_t index,
  1442. const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1443. {
  1444. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1445. pData->midiprog.current = index;
  1446. pData->engine->callback(sendCallback, sendOsc,
  1447. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  1448. pData->id,
  1449. index,
  1450. 0, 0, 0.0f, nullptr);
  1451. // Change default parameter values
  1452. if (index >= 0)
  1453. {
  1454. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1455. uiMidiProgramChange(static_cast<uint32_t>(index));
  1456. switch (getType())
  1457. {
  1458. case PLUGIN_SF2:
  1459. case PLUGIN_SFZ:
  1460. break;
  1461. default:
  1462. pData->updateParameterValues(this, sendCallback, sendOsc, true);
  1463. break;
  1464. }
  1465. }
  1466. }
  1467. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1468. {
  1469. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1470. {
  1471. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1472. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1473. }
  1474. }
  1475. void CarlaPlugin::setProgramRT(const uint32_t uindex) noexcept
  1476. {
  1477. CARLA_SAFE_ASSERT_RETURN(uindex < pData->prog.count,);
  1478. const int32_t index = static_cast<int32_t>(uindex);
  1479. pData->prog.current = index;
  1480. // Change default parameter values
  1481. switch (getType())
  1482. {
  1483. case PLUGIN_SF2:
  1484. case PLUGIN_SFZ:
  1485. break;
  1486. default:
  1487. pData->updateDefaultParameterValues(this);
  1488. break;
  1489. }
  1490. pData->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0, 0.0f);
  1491. }
  1492. void CarlaPlugin::setMidiProgramRT(const uint32_t uindex) noexcept
  1493. {
  1494. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  1495. const int32_t index = static_cast<int32_t>(uindex);
  1496. pData->midiprog.current = index;
  1497. // Change default parameter values
  1498. switch (getType())
  1499. {
  1500. case PLUGIN_SF2:
  1501. case PLUGIN_SFZ:
  1502. break;
  1503. default:
  1504. pData->updateDefaultParameterValues(this);
  1505. break;
  1506. }
  1507. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0, 0.0f);
  1508. }
  1509. // -------------------------------------------------------------------
  1510. // Plugin state
  1511. void CarlaPlugin::reloadPrograms(const bool)
  1512. {
  1513. }
  1514. // -------------------------------------------------------------------
  1515. // Plugin processing
  1516. void CarlaPlugin::activate() noexcept
  1517. {
  1518. CARLA_SAFE_ASSERT(! pData->active);
  1519. }
  1520. void CarlaPlugin::deactivate() noexcept
  1521. {
  1522. CARLA_SAFE_ASSERT(pData->active);
  1523. }
  1524. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1525. {
  1526. }
  1527. void CarlaPlugin::sampleRateChanged(const double)
  1528. {
  1529. }
  1530. void CarlaPlugin::offlineModeChanged(const bool)
  1531. {
  1532. }
  1533. // -------------------------------------------------------------------
  1534. // Misc
  1535. void CarlaPlugin::idle()
  1536. {
  1537. if (! pData->enabled)
  1538. return;
  1539. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1540. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1541. const uint32_t latency(getLatencyInFrames());
  1542. if (pData->latency.frames != latency)
  1543. {
  1544. carla_stdout("latency changed to %i samples", latency);
  1545. const ScopedSingleProcessLocker sspl(this, true);
  1546. pData->client->setLatency(latency);
  1547. #ifndef BUILD_BRIDGE
  1548. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1549. #else
  1550. pData->latency.frames = latency;
  1551. #endif
  1552. }
  1553. const CarlaMutexLocker sl(pData->postRtEvents.getDataMutex());
  1554. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.getDataIterator(); it.valid(); it.next())
  1555. {
  1556. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1557. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1558. switch (event.type)
  1559. {
  1560. case kPluginPostRtEventNull: {
  1561. } break;
  1562. case kPluginPostRtEventDebug: {
  1563. pData->engine->callback(true, true,
  1564. ENGINE_CALLBACK_DEBUG, pData->id,
  1565. event.value1, event.value2, event.value3, event.valuef, nullptr);
  1566. } break;
  1567. case kPluginPostRtEventParameterChange: {
  1568. // Update UI
  1569. if (event.value1 >= 0 && hasUI)
  1570. {
  1571. if (needsUiMainThread)
  1572. pData->postUiEvents.append(event);
  1573. else
  1574. uiParameterChange(static_cast<uint32_t>(event.value1), event.valuef);
  1575. }
  1576. if (event.value2 != 0)
  1577. {
  1578. // Update Host
  1579. pData->engine->callback(true, true,
  1580. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1581. pData->id,
  1582. event.value1,
  1583. 0, 0,
  1584. event.valuef,
  1585. nullptr);
  1586. }
  1587. } break;
  1588. case kPluginPostRtEventProgramChange: {
  1589. // Update UI
  1590. if (event.value1 >= 0 && hasUI)
  1591. {
  1592. if (needsUiMainThread)
  1593. pData->postUiEvents.append(event);
  1594. else
  1595. uiProgramChange(static_cast<uint32_t>(event.value1));
  1596. }
  1597. // Update param values
  1598. for (uint32_t j=0; j < pData->param.count; ++j)
  1599. {
  1600. const float paramDefault(pData->param.ranges[j].def);
  1601. const float paramValue(getParameterValue(j));
  1602. pData->engine->callback(true, true,
  1603. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1604. pData->id,
  1605. static_cast<int>(j),
  1606. 0, 0,
  1607. paramValue,
  1608. nullptr);
  1609. pData->engine->callback(true, true,
  1610. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  1611. pData->id,
  1612. static_cast<int>(j),
  1613. 0, 0,
  1614. paramDefault,
  1615. nullptr);
  1616. }
  1617. // Update Host
  1618. pData->engine->callback(true, true,
  1619. ENGINE_CALLBACK_PROGRAM_CHANGED,
  1620. pData->id,
  1621. event.value1,
  1622. 0, 0, 0.0f, nullptr);
  1623. } break;
  1624. case kPluginPostRtEventMidiProgramChange: {
  1625. // Update UI
  1626. if (event.value1 >= 0 && hasUI)
  1627. {
  1628. if (needsUiMainThread)
  1629. pData->postUiEvents.append(event);
  1630. else
  1631. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1632. }
  1633. // Update param values
  1634. for (uint32_t j=0; j < pData->param.count; ++j)
  1635. {
  1636. const float paramDefault(pData->param.ranges[j].def);
  1637. const float paramValue(getParameterValue(j));
  1638. pData->engine->callback(true, true,
  1639. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1640. pData->id,
  1641. static_cast<int>(j),
  1642. 0, 0,
  1643. paramValue,
  1644. nullptr);
  1645. pData->engine->callback(true, true,
  1646. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  1647. pData->id,
  1648. static_cast<int>(j),
  1649. 0, 0,
  1650. paramDefault,
  1651. nullptr);
  1652. }
  1653. // Update Host
  1654. pData->engine->callback(true, true,
  1655. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  1656. pData->id,
  1657. event.value1,
  1658. 0, 0, 0.0f, nullptr);
  1659. } break;
  1660. case kPluginPostRtEventNoteOn: {
  1661. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1662. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1663. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1664. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1665. const uint8_t note = static_cast<uint8_t>(event.value2);
  1666. const uint8_t velocity = static_cast<uint8_t>(event.value3);
  1667. // Update UI
  1668. if (hasUI)
  1669. {
  1670. if (needsUiMainThread)
  1671. pData->postUiEvents.append(event);
  1672. else
  1673. uiNoteOn(channel, note, velocity);
  1674. }
  1675. // Update Host
  1676. pData->engine->callback(true, true,
  1677. ENGINE_CALLBACK_NOTE_ON,
  1678. pData->id,
  1679. event.value1,
  1680. event.value2,
  1681. event.value3,
  1682. 0.0f, nullptr);
  1683. } break;
  1684. case kPluginPostRtEventNoteOff: {
  1685. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1686. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1687. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1688. const uint8_t note = static_cast<uint8_t>(event.value2);
  1689. // Update UI
  1690. if (hasUI)
  1691. {
  1692. if (needsUiMainThread)
  1693. pData->postUiEvents.append(event);
  1694. else
  1695. uiNoteOff(channel, note);
  1696. }
  1697. // Update Host
  1698. pData->engine->callback(true, true,
  1699. ENGINE_CALLBACK_NOTE_OFF,
  1700. pData->id,
  1701. event.value1,
  1702. event.value2,
  1703. 0, 0.0f, nullptr);
  1704. } break;
  1705. }
  1706. }
  1707. pData->postRtEvents.clearData();
  1708. }
  1709. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1710. {
  1711. if (forcedOffline)
  1712. {
  1713. #ifndef STOAT_TEST_BUILD
  1714. pData->masterMutex.lock();
  1715. return true;
  1716. #endif
  1717. }
  1718. return pData->masterMutex.tryLock();
  1719. }
  1720. void CarlaPlugin::unlock() noexcept
  1721. {
  1722. pData->masterMutex.unlock();
  1723. }
  1724. // -------------------------------------------------------------------
  1725. // Plugin buffers
  1726. void CarlaPlugin::initBuffers() const noexcept
  1727. {
  1728. pData->audioIn.initBuffers();
  1729. pData->audioOut.initBuffers();
  1730. pData->cvIn.initBuffers();
  1731. pData->cvOut.initBuffers();
  1732. pData->event.initBuffers();
  1733. }
  1734. void CarlaPlugin::clearBuffers() noexcept
  1735. {
  1736. pData->clearBuffers();
  1737. }
  1738. // -------------------------------------------------------------------
  1739. // OSC stuff
  1740. // FIXME
  1741. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1742. {
  1743. // do nothing
  1744. }
  1745. // -------------------------------------------------------------------
  1746. // MIDI events
  1747. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1748. {
  1749. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1750. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1751. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1752. if (! pData->active)
  1753. return;
  1754. ExternalMidiNote extNote;
  1755. extNote.channel = static_cast<int8_t>(channel);
  1756. extNote.note = note;
  1757. extNote.velo = velo;
  1758. pData->extNotes.appendNonRT(extNote);
  1759. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1760. {
  1761. if (velo > 0)
  1762. uiNoteOn(channel, note, velo);
  1763. else
  1764. uiNoteOff(channel, note);
  1765. }
  1766. pData->engine->callback(sendCallback, sendOsc,
  1767. (velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF,
  1768. pData->id,
  1769. channel,
  1770. note,
  1771. velo,
  1772. 0.0f, nullptr);
  1773. }
  1774. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1775. void CarlaPlugin::postponeRtAllNotesOff()
  1776. {
  1777. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1778. return;
  1779. PluginPostRtEvent postEvent;
  1780. postEvent.type = kPluginPostRtEventNoteOff;
  1781. postEvent.value1 = pData->ctrlChannel;
  1782. postEvent.value2 = 0;
  1783. postEvent.value3 = 0;
  1784. postEvent.valuef = 0.0f;
  1785. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1786. {
  1787. postEvent.value2 = i;
  1788. pData->postRtEvents.appendRT(postEvent);
  1789. }
  1790. }
  1791. #endif
  1792. // -------------------------------------------------------------------
  1793. // UI Stuff
  1794. void CarlaPlugin::showCustomUI(const bool yesNo)
  1795. {
  1796. if (yesNo) {
  1797. CARLA_SAFE_ASSERT(false);
  1798. }
  1799. }
  1800. void CarlaPlugin::uiIdle()
  1801. {
  1802. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1803. {
  1804. // Update parameter outputs
  1805. for (uint32_t i=0; i < pData->param.count; ++i)
  1806. {
  1807. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1808. uiParameterChange(i, getParameterValue(i));
  1809. }
  1810. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1811. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1812. {
  1813. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1814. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1815. switch (event.type)
  1816. {
  1817. case kPluginPostRtEventNull:
  1818. case kPluginPostRtEventDebug:
  1819. break;
  1820. case kPluginPostRtEventParameterChange:
  1821. uiParameterChange(static_cast<uint32_t>(event.value1), event.valuef);
  1822. break;
  1823. case kPluginPostRtEventProgramChange:
  1824. uiProgramChange(static_cast<uint32_t>(event.value1));
  1825. break;
  1826. case kPluginPostRtEventMidiProgramChange:
  1827. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1828. break;
  1829. case kPluginPostRtEventNoteOn:
  1830. uiNoteOn(static_cast<uint8_t>(event.value1),
  1831. static_cast<uint8_t>(event.value2),
  1832. static_cast<uint8_t>(event.value3));
  1833. break;
  1834. case kPluginPostRtEventNoteOff:
  1835. uiNoteOff(static_cast<uint8_t>(event.value1),
  1836. static_cast<uint8_t>(event.value2));
  1837. break;
  1838. }
  1839. }
  1840. pData->postUiEvents.data.clear();
  1841. }
  1842. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1843. if (pData->transientTryCounter == 0)
  1844. return;
  1845. if (++pData->transientTryCounter % 10 != 0)
  1846. return;
  1847. if (pData->transientTryCounter >= 200)
  1848. return;
  1849. carla_stdout("Trying to get window...");
  1850. CarlaString uiTitle(pData->name);
  1851. uiTitle += " (GUI)";
  1852. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle,
  1853. pData->engine->getOptions().frontendWinId, pData->transientFirstTry))
  1854. {
  1855. pData->transientTryCounter = 0;
  1856. pData->transientFirstTry = false;
  1857. }
  1858. #endif
  1859. }
  1860. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1861. {
  1862. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1863. return;
  1864. // unused
  1865. (void)value;
  1866. }
  1867. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1868. {
  1869. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1870. }
  1871. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1872. {
  1873. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1874. }
  1875. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1876. {
  1877. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1878. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1879. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1880. }
  1881. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1882. {
  1883. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1884. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1885. }
  1886. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1887. {
  1888. return pData->engine;
  1889. }
  1890. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1891. {
  1892. return pData->client;
  1893. }
  1894. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1895. {
  1896. return pData->audioIn.ports[index].port;
  1897. }
  1898. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1899. {
  1900. return pData->audioOut.ports[index].port;
  1901. }
  1902. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1903. {
  1904. return pData->cvIn.ports[index].port;
  1905. }
  1906. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1907. {
  1908. return pData->cvOut.ports[index].port;
  1909. }
  1910. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1911. {
  1912. return pData->event.portIn;
  1913. }
  1914. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1915. {
  1916. return pData->event.portOut;
  1917. }
  1918. void* CarlaPlugin::getNativeHandle() const noexcept
  1919. {
  1920. return nullptr;
  1921. }
  1922. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1923. {
  1924. return nullptr;
  1925. }
  1926. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1927. {
  1928. return 0;
  1929. }
  1930. // -------------------------------------------------------------------
  1931. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1932. {
  1933. return pData->nodeId;
  1934. }
  1935. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1936. {
  1937. pData->nodeId = nodeId;
  1938. }
  1939. // -------------------------------------------------------------------
  1940. void CarlaPlugin::restoreLV2State() noexcept
  1941. {
  1942. carla_stderr2("Warning: restoreLV2State() called for non-implemented type");
  1943. }
  1944. void CarlaPlugin::waitForBridgeSaveSignal() noexcept
  1945. {
  1946. }
  1947. // -------------------------------------------------------------------
  1948. // Scoped Disabler
  1949. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1950. : fPlugin(plugin),
  1951. fWasEnabled(false)
  1952. {
  1953. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1954. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1955. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1956. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1957. plugin->pData->masterMutex.lock();
  1958. if (plugin->pData->enabled)
  1959. {
  1960. fWasEnabled = true;
  1961. plugin->pData->enabled = false;
  1962. if (plugin->pData->client->isActive())
  1963. plugin->pData->client->deactivate();
  1964. }
  1965. }
  1966. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1967. {
  1968. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1969. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1970. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1971. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1972. if (fWasEnabled)
  1973. {
  1974. fPlugin->pData->enabled = true;
  1975. fPlugin->pData->client->activate();
  1976. }
  1977. fPlugin->pData->masterMutex.unlock();
  1978. }
  1979. // -------------------------------------------------------------------
  1980. // Scoped Process Locker
  1981. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1982. : fPlugin(plugin),
  1983. fBlock(block)
  1984. {
  1985. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1986. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1987. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1988. if (! fBlock)
  1989. return;
  1990. plugin->pData->singleMutex.lock();
  1991. }
  1992. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1993. {
  1994. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1995. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1996. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1997. if (! fBlock)
  1998. return;
  1999. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2000. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  2001. fPlugin->pData->needsReset = true;
  2002. #endif
  2003. fPlugin->pData->singleMutex.unlock();
  2004. }
  2005. // -------------------------------------------------------------------
  2006. CARLA_BACKEND_END_NAMESPACE