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.

2327 lines
75KB

  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. setChunkData(chunk.data(), chunk.size());
  671. }
  672. #ifndef BUILD_BRIDGE
  673. // ---------------------------------------------------------------
  674. // Part 6 - set internal stuff
  675. const uint availOptions(getOptionsAvailable());
  676. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  677. {
  678. const uint option(1u << i);
  679. if (availOptions & option)
  680. setOption(option, (stateSave.options & option) != 0, true);
  681. }
  682. setDryWet(stateSave.dryWet, true, true);
  683. setVolume(stateSave.volume, true, true);
  684. setBalanceLeft(stateSave.balanceLeft, true, true);
  685. setBalanceRight(stateSave.balanceRight, true, true);
  686. setPanning(stateSave.panning, true, true);
  687. setCtrlChannel(stateSave.ctrlChannel, true, true);
  688. setActive(stateSave.active, true, true);
  689. #endif
  690. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  691. }
  692. bool CarlaPlugin::saveStateToFile(const char* const filename)
  693. {
  694. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  695. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  696. MemoryOutputStream out, streamState;
  697. getStateSave().dumpToMemoryStream(streamState);
  698. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  699. out << "<!DOCTYPE CARLA-PRESET>\n";
  700. out << "<CARLA-PRESET VERSION='2.0'>\n";
  701. out << streamState;
  702. out << "</CARLA-PRESET>\n";
  703. const String jfilename = String(CharPointer_UTF8(filename));
  704. File file(jfilename);
  705. if (file.replaceWithData(out.getData(), out.getDataSize()))
  706. return true;
  707. pData->engine->setLastError("Failed to write file");
  708. return false;
  709. }
  710. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  711. {
  712. // TODO set errors
  713. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  714. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  715. const String jfilename = String(CharPointer_UTF8(filename));
  716. File file(jfilename);
  717. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  718. XmlDocument xml(file);
  719. ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  720. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  721. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  722. // completely load file
  723. xmlElement = xml.getDocumentElement(false);
  724. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  725. if (pData->stateSave.fillFromXmlElement(xmlElement))
  726. {
  727. loadStateSave(pData->stateSave);
  728. return true;
  729. }
  730. return false;
  731. }
  732. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  733. {
  734. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  735. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  736. CarlaString bundlepath(lv2path);
  737. if (! bundlepath.endsWith(".lv2"))
  738. bundlepath += ".lv2";
  739. const File bundlefolder(bundlepath.buffer());
  740. if (bundlefolder.existsAsFile())
  741. {
  742. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  743. return false;
  744. }
  745. if (! bundlefolder.exists())
  746. {
  747. const Result res(bundlefolder.createDirectory());
  748. if (res.failed())
  749. {
  750. pData->engine->setLastError(res.getErrorMessage().toRawUTF8());
  751. return false;
  752. }
  753. }
  754. CarlaString symbol(pData->name);
  755. symbol.toBasic();
  756. char strBufName[STR_MAX+1];
  757. char strBufSymbol[STR_MAX+1];
  758. strBufName[STR_MAX] = strBufSymbol[STR_MAX] = '\0';
  759. {
  760. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  761. if (! saveStateToFile(pluginFilename))
  762. return false;
  763. }
  764. {
  765. MemoryOutputStream manifestStream;
  766. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  767. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  768. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  769. manifestStream << "\n";
  770. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  771. manifestStream << " a lv2:Plugin ;\n";
  772. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  773. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  774. manifestStream << "\n";
  775. manifestStream << "<ext-ui>\n";
  776. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  777. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  778. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  779. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  780. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  781. manifestStream << "\n";
  782. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  783. const File manifestFile(manifestFilename.buffer());
  784. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  785. {
  786. pData->engine->setLastError("Failed to write manifest.ttl file");
  787. return false;
  788. }
  789. }
  790. {
  791. MemoryOutputStream mainStream;
  792. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  793. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  794. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  795. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  796. mainStream << "\n";
  797. mainStream << "<>\n";
  798. mainStream << " a lv2:Plugin ;\n";
  799. mainStream << "\n";
  800. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  801. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  802. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  803. mainStream << "\n";
  804. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  805. {
  806. mainStream << " ui:ui <ext-ui> ;\n";
  807. mainStream << "\n";
  808. }
  809. int portIndex = 0;
  810. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  811. {
  812. const String portIndexNum(portIndex++);
  813. const String portIndexLabel(portIndex);
  814. mainStream << " lv2:port [\n";
  815. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  816. mainStream << " lv2:index " << portIndexNum << " ;\n";
  817. mainStream << " lv2:symbol \"lv2_audio_in_" << portIndexLabel << "\" ;\n";
  818. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  819. mainStream << " ] ;\n";
  820. }
  821. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  822. {
  823. const String portIndexNum(portIndex++);
  824. const String portIndexLabel(portIndex);
  825. mainStream << " lv2:port [\n";
  826. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  827. mainStream << " lv2:index " << portIndexNum << " ;\n";
  828. mainStream << " lv2:symbol \"lv2_audio_out_" << portIndexLabel << "\" ;\n";
  829. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  830. mainStream << " ] ;\n";
  831. }
  832. mainStream << " lv2:port [\n";
  833. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  834. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  835. mainStream << " lv2:name \"freewheel\" ;\n";
  836. mainStream << " lv2:symbol \"freewheel\" ;\n";
  837. mainStream << " lv2:default 0 ;\n";
  838. mainStream << " lv2:minimum 0 ;\n";
  839. mainStream << " lv2:maximum 1 ;\n";
  840. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  841. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  842. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  843. mainStream << " ] ;\n";
  844. for (uint32_t i=0; i<pData->param.count; ++i)
  845. {
  846. const ParameterData& paramData(pData->param.data[i]);
  847. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  848. const String portIndexNum(portIndex++);
  849. const String portIndexLabel(portIndex);
  850. mainStream << " lv2:port [\n";
  851. if (paramData.type == PARAMETER_INPUT)
  852. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  853. else
  854. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  855. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  856. mainStream << " lv2:portProperty lv2:toggled ;\n";
  857. if (paramData.hints & PARAMETER_IS_INTEGER)
  858. mainStream << " lv2:portProperty lv2:integer ;\n";
  859. // TODO logarithmic, enabled (not on gui), automable, samplerate, scalepoints
  860. strBufName[0] = strBufSymbol[0] = '\0';
  861. getParameterName(i, strBufName);
  862. getParameterSymbol(i, strBufSymbol);
  863. if (strBufSymbol[0] == '\0')
  864. {
  865. CarlaString s(strBufName);
  866. s.toBasic();
  867. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  868. // FIXME - must be unique
  869. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  870. {
  871. const size_t len(std::strlen(strBufSymbol));
  872. std::memmove(strBufSymbol+1, strBufSymbol, len);
  873. strBufSymbol[0] = '_';
  874. strBufSymbol[len+1] = '\0';
  875. }
  876. }
  877. mainStream << " lv2:index " << portIndexNum << " ;\n";
  878. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  879. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  880. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  881. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  882. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  883. // TODO midiCC, midiChannel
  884. mainStream << " ] ;\n";
  885. }
  886. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  887. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" .\n";
  888. mainStream << "\n";
  889. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  890. const File mainFile(mainFilename.buffer());
  891. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  892. {
  893. pData->engine->setLastError("Failed to write main plugin ttl file");
  894. return false;
  895. }
  896. }
  897. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  898. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  899. const File binaryFileTarget(binaryFilename.buffer());
  900. if (! binaryFileSource.createSymbolicLink(binaryFileTarget, true))
  901. {
  902. pData->engine->setLastError("Failed to create symbolik link of plugin binary");
  903. return false;
  904. }
  905. const EngineOptions& opts(pData->engine->getOptions());
  906. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  907. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  908. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  909. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  910. return true;
  911. }
  912. // -------------------------------------------------------------------
  913. // Set data (internal stuff)
  914. void CarlaPlugin::setId(const uint newId) noexcept
  915. {
  916. pData->id = newId;
  917. }
  918. void CarlaPlugin::setName(const char* const newName)
  919. {
  920. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  921. if (pData->name != nullptr)
  922. delete[] pData->name;
  923. pData->name = carla_strdup(newName);
  924. }
  925. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  926. {
  927. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  928. if (yesNo)
  929. pData->options |= option;
  930. else
  931. pData->options &= ~option;
  932. #ifndef BUILD_BRIDGE
  933. if (sendCallback)
  934. pData->engine->callback(ENGINE_CALLBACK_OPTION_CHANGED, pData->id, static_cast<int>(option), yesNo ? 1 : 0, 0.0f, nullptr);
  935. #else
  936. // unused
  937. return; (void)sendCallback;
  938. #endif
  939. }
  940. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  941. {
  942. if (pData->enabled == yesNo)
  943. return;
  944. pData->masterMutex.lock();
  945. pData->enabled = yesNo;
  946. if (yesNo && ! pData->client->isActive())
  947. pData->client->activate();
  948. pData->masterMutex.unlock();
  949. }
  950. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  951. {
  952. #ifndef BUILD_BRIDGE
  953. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  954. #endif
  955. if (pData->active == active)
  956. return;
  957. {
  958. const ScopedSingleProcessLocker spl(this, true);
  959. if (active)
  960. activate();
  961. else
  962. deactivate();
  963. }
  964. pData->active = active;
  965. #ifndef BUILD_BRIDGE
  966. const float value(active ? 1.0f : 0.0f);
  967. # ifdef HAVE_LIBLO
  968. if (sendOsc && pData->engine->isOscControlRegistered())
  969. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  970. # endif
  971. if (sendCallback)
  972. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  973. #endif
  974. // may be unused
  975. return; (void)sendOsc; (void)sendCallback;
  976. }
  977. #ifndef BUILD_BRIDGE
  978. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  979. {
  980. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  981. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  982. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  983. return;
  984. pData->postProc.dryWet = fixedValue;
  985. #ifdef HAVE_LIBLO
  986. if (sendOsc && pData->engine->isOscControlRegistered())
  987. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  988. #endif
  989. if (sendCallback)
  990. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  991. // may be unused
  992. return; (void)sendOsc;
  993. }
  994. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  995. {
  996. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  997. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  998. if (carla_isEqual(pData->postProc.volume, fixedValue))
  999. return;
  1000. pData->postProc.volume = fixedValue;
  1001. #ifdef HAVE_LIBLO
  1002. if (sendOsc && pData->engine->isOscControlRegistered())
  1003. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  1004. #endif
  1005. if (sendCallback)
  1006. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  1007. // may be unused
  1008. return; (void)sendOsc;
  1009. }
  1010. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1011. {
  1012. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1013. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1014. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1015. return;
  1016. pData->postProc.balanceLeft = fixedValue;
  1017. #ifdef HAVE_LIBLO
  1018. if (sendOsc && pData->engine->isOscControlRegistered())
  1019. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  1020. #endif
  1021. if (sendCallback)
  1022. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  1023. // may be unused
  1024. return; (void)sendOsc;
  1025. }
  1026. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1027. {
  1028. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1029. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1030. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1031. return;
  1032. pData->postProc.balanceRight = fixedValue;
  1033. #ifdef HAVE_LIBLO
  1034. if (sendOsc && pData->engine->isOscControlRegistered())
  1035. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  1036. #endif
  1037. if (sendCallback)
  1038. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  1039. // may be unused
  1040. return; (void)sendOsc;
  1041. }
  1042. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1043. {
  1044. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1045. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1046. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1047. return;
  1048. pData->postProc.panning = fixedValue;
  1049. #ifdef HAVE_LIBLO
  1050. if (sendOsc && pData->engine->isOscControlRegistered())
  1051. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  1052. #endif
  1053. if (sendCallback)
  1054. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  1055. // may be unused
  1056. return; (void)sendOsc;
  1057. }
  1058. #endif // ! BUILD_BRIDGE
  1059. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1060. {
  1061. #ifndef BUILD_BRIDGE
  1062. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1063. #endif
  1064. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1065. if (pData->ctrlChannel == channel)
  1066. return;
  1067. pData->ctrlChannel = channel;
  1068. #ifndef BUILD_BRIDGE
  1069. const float channelf(channel);
  1070. # ifdef HAVE_LIBLO
  1071. if (sendOsc && pData->engine->isOscControlRegistered())
  1072. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  1073. # endif
  1074. if (sendCallback)
  1075. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  1076. #endif
  1077. // may be unused
  1078. return; (void)sendOsc; (void)sendCallback;
  1079. }
  1080. // -------------------------------------------------------------------
  1081. // Set data (plugin-specific stuff)
  1082. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1083. {
  1084. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1085. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1086. uiParameterChange(parameterId, value);
  1087. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1088. if (sendOsc && pData->engine->isOscControlRegistered())
  1089. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  1090. #endif
  1091. if (sendCallback)
  1092. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  1093. // may be unused
  1094. return; (void)sendOsc;
  1095. }
  1096. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1097. {
  1098. #ifndef BUILD_BRIDGE
  1099. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1100. switch (rindex)
  1101. {
  1102. case PARAMETER_ACTIVE:
  1103. return setActive((value > 0.0f), sendOsc, sendCallback);
  1104. case PARAMETER_CTRL_CHANNEL:
  1105. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1106. case PARAMETER_DRYWET:
  1107. return setDryWet(value, sendOsc, sendCallback);
  1108. case PARAMETER_VOLUME:
  1109. return setVolume(value, sendOsc, sendCallback);
  1110. case PARAMETER_BALANCE_LEFT:
  1111. return setBalanceLeft(value, sendOsc, sendCallback);
  1112. case PARAMETER_BALANCE_RIGHT:
  1113. return setBalanceRight(value, sendOsc, sendCallback);
  1114. case PARAMETER_PANNING:
  1115. return setPanning(value, sendOsc, sendCallback);
  1116. }
  1117. #endif
  1118. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1119. for (uint32_t i=0; i < pData->param.count; ++i)
  1120. {
  1121. if (pData->param.data[i].rindex == rindex)
  1122. {
  1123. //if (carla_isNotEqual(getParameterValue(i), value))
  1124. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1125. break;
  1126. }
  1127. }
  1128. }
  1129. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1130. {
  1131. #ifndef BUILD_BRIDGE
  1132. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1133. #endif
  1134. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1135. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1136. pData->param.data[parameterId].midiChannel = channel;
  1137. #ifndef BUILD_BRIDGE
  1138. # ifdef HAVE_LIBLO
  1139. if (sendOsc && pData->engine->isOscControlRegistered())
  1140. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  1141. # endif
  1142. if (sendCallback)
  1143. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  1144. #endif
  1145. // may be unused
  1146. return; (void)sendOsc; (void)sendCallback;
  1147. }
  1148. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  1149. {
  1150. #ifndef BUILD_BRIDGE
  1151. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1152. #endif
  1153. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1154. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  1155. pData->param.data[parameterId].midiCC = cc;
  1156. #ifndef BUILD_BRIDGE
  1157. # ifdef HAVE_LIBLO
  1158. if (sendOsc && pData->engine->isOscControlRegistered())
  1159. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  1160. # endif
  1161. if (sendCallback)
  1162. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  1163. #endif
  1164. // may be unused
  1165. return; (void)sendOsc; (void)sendCallback;
  1166. }
  1167. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1168. {
  1169. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1170. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1171. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1172. // Ignore some keys
  1173. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1174. {
  1175. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  1176. return;
  1177. }
  1178. // Check if we already have this key
  1179. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1180. {
  1181. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1182. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1183. if (std::strcmp(customData.key, key) == 0)
  1184. {
  1185. if (customData.value != nullptr)
  1186. delete[] customData.value;
  1187. customData.value = carla_strdup(value);
  1188. return;
  1189. }
  1190. }
  1191. // Otherwise store it
  1192. CustomData customData;
  1193. customData.type = carla_strdup(type);
  1194. customData.key = carla_strdup(key);
  1195. customData.value = carla_strdup(value);
  1196. pData->custom.append(customData);
  1197. }
  1198. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1199. {
  1200. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1201. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1202. CARLA_SAFE_ASSERT(false); // this should never happen
  1203. }
  1204. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1205. {
  1206. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1207. pData->prog.current = index;
  1208. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1209. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1210. if (reallySendOsc && index < 50)
  1211. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1212. #else
  1213. const bool reallySendOsc(false);
  1214. #endif
  1215. if (sendCallback)
  1216. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1217. // Change default parameter values
  1218. if (index >= 0)
  1219. {
  1220. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1221. uiProgramChange(static_cast<uint32_t>(index));
  1222. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1223. return;
  1224. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1225. }
  1226. // may be unused
  1227. return; (void)sendOsc;
  1228. }
  1229. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1230. {
  1231. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1232. pData->midiprog.current = index;
  1233. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1234. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1235. if (reallySendOsc && index < 50)
  1236. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1237. #else
  1238. const bool reallySendOsc(false);
  1239. #endif
  1240. if (sendCallback)
  1241. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1242. if (index >= 0)
  1243. {
  1244. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1245. uiMidiProgramChange(static_cast<uint32_t>(index));
  1246. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1247. return;
  1248. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1249. }
  1250. // may be unused
  1251. return; (void)sendOsc;
  1252. }
  1253. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1254. {
  1255. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1256. {
  1257. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1258. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1259. }
  1260. }
  1261. // -------------------------------------------------------------------
  1262. // Plugin state
  1263. void CarlaPlugin::reloadPrograms(const bool)
  1264. {
  1265. }
  1266. // -------------------------------------------------------------------
  1267. // Plugin processing
  1268. void CarlaPlugin::activate() noexcept
  1269. {
  1270. CARLA_SAFE_ASSERT(! pData->active);
  1271. }
  1272. void CarlaPlugin::deactivate() noexcept
  1273. {
  1274. CARLA_SAFE_ASSERT(pData->active);
  1275. }
  1276. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1277. {
  1278. }
  1279. void CarlaPlugin::sampleRateChanged(const double)
  1280. {
  1281. }
  1282. void CarlaPlugin::offlineModeChanged(const bool)
  1283. {
  1284. }
  1285. // -------------------------------------------------------------------
  1286. // Misc
  1287. void CarlaPlugin::idle()
  1288. {
  1289. if (! pData->enabled)
  1290. return;
  1291. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1292. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1293. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1294. const bool sendOsc(pData->engine->isOscControlRegistered());
  1295. #endif
  1296. const uint32_t latency(getLatencyInFrames());
  1297. if (pData->latency.frames != latency)
  1298. {
  1299. carla_stdout("latency changed to %i samples", latency);
  1300. const ScopedSingleProcessLocker sspl(this, true);
  1301. pData->client->setLatency(latency);
  1302. #ifndef BUILD_BRIDGE
  1303. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1304. #else
  1305. pData->latency.frames = latency;
  1306. #endif
  1307. }
  1308. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1309. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin2(); it.valid(); it.next())
  1310. {
  1311. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1312. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1313. switch (event.type)
  1314. {
  1315. case kPluginPostRtEventNull: {
  1316. } break;
  1317. case kPluginPostRtEventDebug: {
  1318. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1319. } break;
  1320. case kPluginPostRtEventParameterChange: {
  1321. // Update UI
  1322. if (event.value1 >= 0 && hasUI)
  1323. {
  1324. if (needsUiMainThread)
  1325. pData->postUiEvents.append(event);
  1326. else
  1327. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1328. }
  1329. if (event.value2 != 1)
  1330. {
  1331. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1332. // Update OSC control client
  1333. if (sendOsc)
  1334. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1335. #endif
  1336. // Update Host
  1337. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1338. }
  1339. } break;
  1340. case kPluginPostRtEventProgramChange: {
  1341. // Update UI
  1342. if (event.value1 >= 0 && hasUI)
  1343. {
  1344. if (needsUiMainThread)
  1345. pData->postUiEvents.append(event);
  1346. else
  1347. uiProgramChange(static_cast<uint32_t>(event.value1));
  1348. }
  1349. // Update param values
  1350. for (uint32_t j=0; j < pData->param.count; ++j)
  1351. {
  1352. const float paramDefault(pData->param.ranges[j].def);
  1353. const float paramValue(getParameterValue(j));
  1354. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1355. if (sendOsc && j < 50)
  1356. {
  1357. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1358. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1359. }
  1360. #endif
  1361. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1362. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1363. }
  1364. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1365. // Update OSC control client
  1366. if (sendOsc)
  1367. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1368. #endif
  1369. // Update Host
  1370. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1371. } break;
  1372. case kPluginPostRtEventMidiProgramChange: {
  1373. // Update UI
  1374. if (event.value1 >= 0 && hasUI)
  1375. {
  1376. if (needsUiMainThread)
  1377. pData->postUiEvents.append(event);
  1378. else
  1379. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1380. }
  1381. // Update param values
  1382. for (uint32_t j=0; j < pData->param.count; ++j)
  1383. {
  1384. const float paramDefault(pData->param.ranges[j].def);
  1385. const float paramValue(getParameterValue(j));
  1386. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1387. if (sendOsc && j < 50)
  1388. {
  1389. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1390. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1391. }
  1392. #endif
  1393. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1394. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1395. }
  1396. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1397. // Update OSC control client
  1398. if (sendOsc)
  1399. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1400. #endif
  1401. // Update Host
  1402. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1403. } break;
  1404. case kPluginPostRtEventNoteOn: {
  1405. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1406. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1407. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1408. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1409. const uint8_t note = static_cast<uint8_t>(event.value2);
  1410. const uint8_t velocity = uint8_t(event.value3);
  1411. // Update UI
  1412. if (hasUI)
  1413. {
  1414. if (needsUiMainThread)
  1415. pData->postUiEvents.append(event);
  1416. else
  1417. uiNoteOn(channel, note, velocity);
  1418. }
  1419. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1420. // Update OSC control client
  1421. if (sendOsc)
  1422. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1423. #endif
  1424. // Update Host
  1425. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1426. } break;
  1427. case kPluginPostRtEventNoteOff: {
  1428. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1429. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1430. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1431. const uint8_t note = static_cast<uint8_t>(event.value2);
  1432. // Update UI
  1433. if (hasUI)
  1434. {
  1435. if (needsUiMainThread)
  1436. pData->postUiEvents.append(event);
  1437. else
  1438. uiNoteOff(channel, note);
  1439. }
  1440. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1441. // Update OSC control client
  1442. if (sendOsc)
  1443. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1444. #endif
  1445. // Update Host
  1446. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1447. } break;
  1448. }
  1449. }
  1450. pData->postRtEvents.data.clear();
  1451. }
  1452. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1453. {
  1454. if (forcedOffline)
  1455. {
  1456. pData->masterMutex.lock();
  1457. return true;
  1458. }
  1459. return pData->masterMutex.tryLock();
  1460. }
  1461. void CarlaPlugin::unlock() noexcept
  1462. {
  1463. pData->masterMutex.unlock();
  1464. }
  1465. // -------------------------------------------------------------------
  1466. // Plugin buffers
  1467. void CarlaPlugin::initBuffers() const noexcept
  1468. {
  1469. pData->audioIn.initBuffers();
  1470. pData->audioOut.initBuffers();
  1471. pData->cvIn.initBuffers();
  1472. pData->cvOut.initBuffers();
  1473. pData->event.initBuffers();
  1474. }
  1475. void CarlaPlugin::clearBuffers() noexcept
  1476. {
  1477. pData->clearBuffers();
  1478. }
  1479. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1480. // -------------------------------------------------------------------
  1481. // OSC stuff
  1482. void CarlaPlugin::registerToOscClient() noexcept
  1483. {
  1484. if (! pData->engine->isOscControlRegistered())
  1485. return;
  1486. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1487. // Base data
  1488. {
  1489. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1490. carla_zeroChars(bufName, STR_MAX);
  1491. carla_zeroChars(bufLabel, STR_MAX);
  1492. carla_zeroChars(bufMaker, STR_MAX);
  1493. carla_zeroChars(bufCopyright, STR_MAX);
  1494. getRealName(bufName);
  1495. getLabel(bufLabel);
  1496. getMaker(bufMaker);
  1497. getCopyright(bufCopyright);
  1498. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1499. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1500. }
  1501. // Base count
  1502. uint32_t paramIns, paramOuts;
  1503. {
  1504. getParameterCountInfo(paramIns, paramOuts);
  1505. if (paramIns > 49)
  1506. paramIns = 49;
  1507. if (paramOuts > 49)
  1508. paramOuts = 49;
  1509. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1510. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1511. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1512. }
  1513. // Plugin Parameters
  1514. if (const uint32_t count = std::min<uint32_t>(pData->param.count, 98U))
  1515. {
  1516. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1517. for (uint32_t i=0; i<count; ++i)
  1518. {
  1519. const ParameterData& paramData(pData->param.data[i]);
  1520. if (paramData.type == PARAMETER_INPUT)
  1521. {
  1522. if (--paramIns == 0)
  1523. break;
  1524. }
  1525. else if (paramData.type == PARAMETER_INPUT)
  1526. {
  1527. if (--paramOuts == 0)
  1528. break;
  1529. }
  1530. else
  1531. {
  1532. continue;
  1533. }
  1534. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1535. carla_zeroChars(bufName, STR_MAX);
  1536. carla_zeroChars(bufUnit, STR_MAX);
  1537. getParameterName(i, bufName);
  1538. getParameterUnit(i, bufUnit);
  1539. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1540. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1541. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1542. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1543. if (paramData.midiCC >= 0)
  1544. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1545. if (paramData.midiChannel != 0)
  1546. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1547. }
  1548. }
  1549. // Programs
  1550. if (const uint32_t count = std::min<uint32_t>(pData->prog.count, 50U))
  1551. {
  1552. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1553. for (uint32_t i=0; i < count; ++i)
  1554. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1555. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1556. }
  1557. // MIDI Programs
  1558. if (const uint32_t count = std::min<uint32_t>(pData->midiprog.count, 50U))
  1559. {
  1560. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1561. for (uint32_t i=0; i < count; ++i)
  1562. {
  1563. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1564. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1565. }
  1566. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1567. }
  1568. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1569. // Internal Parameters
  1570. {
  1571. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1572. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1573. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1574. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1575. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1576. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1577. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1578. }
  1579. }
  1580. #endif
  1581. // FIXME
  1582. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1583. {
  1584. // do nothing
  1585. }
  1586. //#endif // HAVE_LIBLO && ! BUILD_BRIDGE
  1587. // -------------------------------------------------------------------
  1588. // MIDI events
  1589. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1590. {
  1591. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1592. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1593. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1594. if (! pData->active)
  1595. return;
  1596. ExternalMidiNote extNote;
  1597. extNote.channel = static_cast<int8_t>(channel);
  1598. extNote.note = note;
  1599. extNote.velo = velo;
  1600. pData->extNotes.appendNonRT(extNote);
  1601. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1602. {
  1603. if (velo > 0)
  1604. uiNoteOn(channel, note, velo);
  1605. else
  1606. uiNoteOff(channel, note);
  1607. }
  1608. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1609. if (sendOsc && pData->engine->isOscControlRegistered())
  1610. {
  1611. if (velo > 0)
  1612. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1613. else
  1614. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1615. }
  1616. #endif
  1617. if (sendCallback)
  1618. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1619. // may be unused
  1620. return; (void)sendOsc;
  1621. }
  1622. #ifndef BUILD_BRIDGE
  1623. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1624. {
  1625. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1626. return;
  1627. PluginPostRtEvent postEvent;
  1628. postEvent.type = kPluginPostRtEventNoteOff;
  1629. postEvent.value1 = pData->ctrlChannel;
  1630. postEvent.value2 = 0;
  1631. postEvent.value3 = 0.0f;
  1632. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1633. {
  1634. postEvent.value2 = i;
  1635. pData->postRtEvents.appendRT(postEvent);
  1636. }
  1637. }
  1638. #endif
  1639. // -------------------------------------------------------------------
  1640. // UI Stuff
  1641. void CarlaPlugin::showCustomUI(const bool)
  1642. {
  1643. CARLA_SAFE_ASSERT(false);
  1644. }
  1645. void CarlaPlugin::uiIdle()
  1646. {
  1647. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1648. {
  1649. // Update parameter outputs
  1650. for (uint32_t i=0; i < pData->param.count; ++i)
  1651. {
  1652. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1653. uiParameterChange(i, getParameterValue(i));
  1654. }
  1655. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1656. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1657. {
  1658. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1659. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1660. switch (event.type)
  1661. {
  1662. case kPluginPostRtEventNull:
  1663. case kPluginPostRtEventDebug:
  1664. break;
  1665. case kPluginPostRtEventParameterChange:
  1666. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1667. break;
  1668. case kPluginPostRtEventProgramChange:
  1669. uiProgramChange(static_cast<uint32_t>(event.value1));
  1670. break;
  1671. case kPluginPostRtEventMidiProgramChange:
  1672. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1673. break;
  1674. case kPluginPostRtEventNoteOn:
  1675. uiNoteOn(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2), uint8_t(event.value3));
  1676. break;
  1677. case kPluginPostRtEventNoteOff:
  1678. uiNoteOff(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2));
  1679. break;
  1680. }
  1681. }
  1682. pData->postUiEvents.data.clear();
  1683. }
  1684. if (pData->transientTryCounter == 0)
  1685. return;
  1686. if (++pData->transientTryCounter % 10 != 0)
  1687. return;
  1688. if (pData->transientTryCounter >= 200)
  1689. return;
  1690. carla_stdout("Trying to get window...");
  1691. CarlaString uiTitle(pData->name);
  1692. uiTitle += " (GUI)";
  1693. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle, pData->engine->getOptions().frontendWinId, true))
  1694. pData->transientTryCounter = 0;
  1695. }
  1696. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1697. {
  1698. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1699. return;
  1700. // unused
  1701. (void)value;
  1702. }
  1703. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1704. {
  1705. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1706. }
  1707. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1708. {
  1709. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1710. }
  1711. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1712. {
  1713. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1714. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1715. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1716. }
  1717. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1718. {
  1719. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1720. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1721. }
  1722. bool CarlaPlugin::canRunInRack() const noexcept
  1723. {
  1724. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1725. }
  1726. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1727. {
  1728. return pData->engine;
  1729. }
  1730. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1731. {
  1732. return pData->client;
  1733. }
  1734. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1735. {
  1736. return pData->audioIn.ports[index].port;
  1737. }
  1738. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1739. {
  1740. return pData->audioOut.ports[index].port;
  1741. }
  1742. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1743. {
  1744. return pData->cvIn.ports[index].port;
  1745. }
  1746. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1747. {
  1748. return pData->cvOut.ports[index].port;
  1749. }
  1750. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1751. {
  1752. return pData->event.portIn;
  1753. }
  1754. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1755. {
  1756. return pData->event.portOut;
  1757. }
  1758. void* CarlaPlugin::getNativeHandle() const noexcept
  1759. {
  1760. return nullptr;
  1761. }
  1762. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1763. {
  1764. return nullptr;
  1765. }
  1766. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1767. {
  1768. return 0;
  1769. }
  1770. // -------------------------------------------------------------------
  1771. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1772. {
  1773. return pData->nodeId;
  1774. }
  1775. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1776. {
  1777. pData->nodeId = nodeId;
  1778. }
  1779. // -------------------------------------------------------------------
  1780. // Scoped Disabler
  1781. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1782. : fPlugin(plugin),
  1783. fWasEnabled(false)
  1784. {
  1785. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1786. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1787. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1788. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1789. plugin->pData->masterMutex.lock();
  1790. if (plugin->pData->enabled)
  1791. {
  1792. fWasEnabled = true;
  1793. plugin->pData->enabled = false;
  1794. if (plugin->pData->client->isActive())
  1795. plugin->pData->client->deactivate();
  1796. }
  1797. }
  1798. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1799. {
  1800. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1801. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1802. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1803. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1804. if (fWasEnabled)
  1805. {
  1806. fPlugin->pData->enabled = true;
  1807. fPlugin->pData->client->activate();
  1808. }
  1809. fPlugin->pData->masterMutex.unlock();
  1810. }
  1811. // -------------------------------------------------------------------
  1812. // Scoped Process Locker
  1813. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1814. : fPlugin(plugin),
  1815. fBlock(block)
  1816. {
  1817. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1818. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1819. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1820. if (! fBlock)
  1821. return;
  1822. plugin->pData->singleMutex.lock();
  1823. }
  1824. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1825. {
  1826. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1827. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1828. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1829. if (! fBlock)
  1830. return;
  1831. #ifndef BUILD_BRIDGE
  1832. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1833. fPlugin->pData->needsReset = true;
  1834. #endif
  1835. fPlugin->pData->singleMutex.unlock();
  1836. }
  1837. // -------------------------------------------------------------------
  1838. CARLA_BACKEND_END_NAMESPACE