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.

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