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.

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