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.

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