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.

2097 lines
66KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2014 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 "CarlaMathUtils.hpp"
  21. #include "CarlaPluginUi.hpp"
  22. #include <ctime>
  23. #include <QtCore/QFile>
  24. #include <QtCore/QTextStream>
  25. #include <QtXml/QDomNode>
  26. CARLA_BACKEND_START_NAMESPACE
  27. // -------------------------------------------------------------------
  28. // Fallback data
  29. static const ParameterData kParameterDataNull = { PARAMETER_UNKNOWN, 0x0, PARAMETER_NULL, -1, -1, 0 };
  30. static const ParameterRanges kParameterRangesNull = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
  31. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  32. static const CustomData kCustomDataNull = { nullptr, nullptr, nullptr };
  33. static bool gIsLoadingProject = false;
  34. // -------------------------------------------------------------------
  35. // ParamSymbol struct, needed for CarlaPlugin::loadStateSave()
  36. struct ParamSymbol {
  37. int32_t index;
  38. const char* symbol;
  39. ParamSymbol(uint32_t i, const char* s)
  40. : index(static_cast<int32_t>(i)),
  41. symbol(carla_strdup(s)) {}
  42. ~ParamSymbol()
  43. {
  44. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
  45. delete[] symbol;
  46. symbol = nullptr;
  47. }
  48. #ifdef CARLA_PROPER_CPP11_SUPPORT
  49. ParamSymbol() = delete;
  50. CARLA_DECLARE_NON_COPY_STRUCT(ParamSymbol)
  51. #endif
  52. };
  53. // -----------------------------------------------------------------------
  54. CarlaPlugin* CarlaPlugin::newFileGIG(const Initializer& init, const bool use16Outs)
  55. {
  56. carla_debug("CarlaPlugin::newFileGIG({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  57. #ifdef WANT_LINUXSAMPLER
  58. return newLinuxSampler(init, "GIG", use16Outs);
  59. #else
  60. init.engine->setLastError("GIG support not available");
  61. return nullptr;
  62. // unused
  63. (void)use16Outs;
  64. #endif
  65. }
  66. CarlaPlugin* CarlaPlugin::newFileSF2(const Initializer& init, const bool use16Outs)
  67. {
  68. carla_debug("CarlaPlugin::newFileSF2({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  69. #if defined(WANT_FLUIDSYNTH)
  70. return newFluidSynth(init, use16Outs);
  71. #elif defined(WANT_LINUXSAMPLER)
  72. return newLinuxSampler(init, "SF2", use16Outs);
  73. #else
  74. init.engine->setLastError("SF2 support not available");
  75. return nullptr;
  76. // unused
  77. (void)use16Outs;
  78. #endif
  79. }
  80. CarlaPlugin* CarlaPlugin::newFileSFZ(const Initializer& init)
  81. {
  82. carla_debug("CarlaPlugin::newFileSFZ({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  83. #ifdef WANT_LINUXSAMPLER
  84. return newLinuxSampler(init, "SFZ", false);
  85. #else
  86. init.engine->setLastError("SFZ support not available");
  87. return nullptr;
  88. #endif
  89. }
  90. // -------------------------------------------------------------------
  91. // Constructor and destructor
  92. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const uint id)
  93. : pData(new ProtectedData(engine, id, this))
  94. {
  95. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  96. CARLA_SAFE_ASSERT(id < engine->getMaxPluginNumber());
  97. carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  98. switch (engine->getProccessMode())
  99. {
  100. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  101. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  102. CARLA_SAFE_ASSERT(id < MAX_DEFAULT_PLUGINS);
  103. break;
  104. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  105. CARLA_SAFE_ASSERT(id < MAX_RACK_PLUGINS);
  106. break;
  107. case ENGINE_PROCESS_MODE_PATCHBAY:
  108. CARLA_SAFE_ASSERT(id < MAX_PATCHBAY_PLUGINS);
  109. break;
  110. case ENGINE_PROCESS_MODE_BRIDGE:
  111. CARLA_SAFE_ASSERT(id == 0);
  112. break;
  113. }
  114. }
  115. CarlaPlugin::~CarlaPlugin()
  116. {
  117. carla_debug("CarlaPlugin::~CarlaPlugin()");
  118. delete pData;
  119. }
  120. // -------------------------------------------------------------------
  121. // Information (base)
  122. uint CarlaPlugin::getId() const noexcept
  123. {
  124. return pData->id;
  125. }
  126. uint CarlaPlugin::getHints() const noexcept
  127. {
  128. return pData->hints;
  129. }
  130. uint CarlaPlugin::getOptionsEnabled() const noexcept
  131. {
  132. return pData->options;
  133. }
  134. bool CarlaPlugin::isEnabled() const noexcept
  135. {
  136. return pData->enabled;
  137. }
  138. const char* CarlaPlugin::getName() const noexcept
  139. {
  140. return pData->name;
  141. }
  142. const char* CarlaPlugin::getFilename() const noexcept
  143. {
  144. return pData->filename;
  145. }
  146. const char* CarlaPlugin::getIconName() const noexcept
  147. {
  148. return pData->iconName;
  149. }
  150. PluginCategory CarlaPlugin::getCategory() const noexcept
  151. {
  152. PluginCategory category = PLUGIN_CATEGORY_NONE;
  153. try {
  154. category = getPluginCategoryFromName(pData->name);
  155. } catch(...) {}
  156. return category;
  157. }
  158. int64_t CarlaPlugin::getUniqueId() const noexcept
  159. {
  160. return 0;
  161. }
  162. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  163. {
  164. return pData->latency;
  165. }
  166. // -------------------------------------------------------------------
  167. // Information (count)
  168. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  169. {
  170. return pData->audioIn.count;
  171. }
  172. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  173. {
  174. return pData->audioOut.count;
  175. }
  176. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  177. {
  178. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  179. }
  180. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  181. {
  182. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  183. }
  184. uint32_t CarlaPlugin::getParameterCount() const noexcept
  185. {
  186. return pData->param.count;
  187. }
  188. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
  189. {
  190. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  191. return 0;
  192. }
  193. uint32_t CarlaPlugin::getProgramCount() const noexcept
  194. {
  195. return pData->prog.count;
  196. }
  197. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  198. {
  199. return pData->midiprog.count;
  200. }
  201. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  202. {
  203. return static_cast<uint32_t>(pData->custom.count());
  204. }
  205. // -------------------------------------------------------------------
  206. // Information (current data)
  207. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  208. {
  209. return pData->prog.current;
  210. }
  211. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  212. {
  213. return pData->midiprog.current;
  214. }
  215. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
  216. {
  217. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  218. return pData->param.data[parameterId];
  219. }
  220. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
  221. {
  222. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  223. return pData->param.ranges[parameterId];
  224. }
  225. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
  226. {
  227. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  228. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  229. }
  230. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
  231. {
  232. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  233. return pData->midiprog.data[index];
  234. }
  235. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
  236. {
  237. CARLA_SAFE_ASSERT_RETURN(index < pData->custom.count(), kCustomDataNull);
  238. return pData->custom.getAt(index, kCustomDataNull);
  239. }
  240. int32_t CarlaPlugin::getChunkData(void** const dataPtr) const noexcept
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  243. CARLA_SAFE_ASSERT(false); // this should never happen
  244. return 0;
  245. }
  246. // -------------------------------------------------------------------
  247. // Information (per-plugin data)
  248. uint CarlaPlugin::getOptionsAvailable() const noexcept
  249. {
  250. CARLA_SAFE_ASSERT(false); // this should never happen
  251. return 0x0;
  252. }
  253. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
  254. {
  255. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  256. CARLA_SAFE_ASSERT(false); // this should never happen
  257. return 0.0f;
  258. }
  259. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  262. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  263. CARLA_SAFE_ASSERT(false); // this should never happen
  264. return 0.0f;
  265. }
  266. void CarlaPlugin::getLabel(char* const strBuf) const noexcept
  267. {
  268. strBuf[0] = '\0';
  269. }
  270. void CarlaPlugin::getMaker(char* const strBuf) const noexcept
  271. {
  272. strBuf[0] = '\0';
  273. }
  274. void CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  275. {
  276. strBuf[0] = '\0';
  277. }
  278. void CarlaPlugin::getRealName(char* const strBuf) const noexcept
  279. {
  280. strBuf[0] = '\0';
  281. }
  282. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  283. {
  284. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  285. CARLA_SAFE_ASSERT(false); // this should never happen
  286. strBuf[0] = '\0';
  287. }
  288. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  289. {
  290. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  291. strBuf[0] = '\0';
  292. }
  293. void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) const noexcept
  294. {
  295. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  296. CARLA_SAFE_ASSERT(false); // this should never happen
  297. strBuf[0] = '\0';
  298. }
  299. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  300. {
  301. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  302. strBuf[0] = '\0';
  303. }
  304. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  305. {
  306. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  307. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  308. CARLA_SAFE_ASSERT(false); // this should never happen
  309. strBuf[0] = '\0';
  310. }
  311. float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
  312. {
  313. CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
  314. switch (parameterId)
  315. {
  316. case PARAMETER_ACTIVE:
  317. return pData->active;
  318. case PARAMETER_CTRL_CHANNEL:
  319. return pData->ctrlChannel;
  320. #ifndef BUILD_BRIDGE
  321. case PARAMETER_DRYWET:
  322. return pData->postProc.dryWet;
  323. case PARAMETER_VOLUME:
  324. return pData->postProc.volume;
  325. case PARAMETER_BALANCE_LEFT:
  326. return pData->postProc.balanceLeft;
  327. case PARAMETER_BALANCE_RIGHT:
  328. return pData->postProc.balanceRight;
  329. case PARAMETER_PANNING:
  330. return pData->postProc.panning;
  331. #endif
  332. };
  333. CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
  334. return getParameterValue(static_cast<uint32_t>(parameterId));
  335. }
  336. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  337. {
  338. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  339. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr,);
  340. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  341. }
  342. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  343. {
  344. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  345. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr,);
  346. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  347. }
  348. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  349. {
  350. ins = 0;
  351. outs = 0;
  352. for (uint32_t i=0; i < pData->param.count; ++i)
  353. {
  354. if (pData->param.data[i].type == PARAMETER_INPUT)
  355. ++ins;
  356. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  357. ++outs;
  358. }
  359. }
  360. // -------------------------------------------------------------------
  361. // Set data (state)
  362. void CarlaPlugin::prepareForSave()
  363. {
  364. }
  365. void CarlaPlugin::resetParameters() noexcept
  366. {
  367. for (uint i=0; i < pData->param.count; ++i)
  368. {
  369. const ParameterData& paramData(pData->param.data[i]);
  370. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  371. if (paramData.type != PARAMETER_INPUT)
  372. continue;
  373. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  374. continue;
  375. setParameterValue(i, paramRanges.def, true, true, true);
  376. }
  377. }
  378. void CarlaPlugin::randomizeParameters() noexcept
  379. {
  380. float value, random;
  381. char strBuf[STR_MAX+1];
  382. strBuf[STR_MAX] = '\0';
  383. std::srand(static_cast<uint>(std::time(nullptr)));
  384. for (uint i=0; i < pData->param.count; ++i)
  385. {
  386. const ParameterData& paramData(pData->param.data[i]);
  387. if (paramData.type != PARAMETER_INPUT)
  388. continue;
  389. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  390. continue;
  391. getParameterName(i, strBuf);
  392. if (std::strstr(strBuf, "olume") != nullptr)
  393. continue;
  394. if (std::strstr(strBuf, "Master") != nullptr)
  395. continue;
  396. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  397. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  398. {
  399. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  400. value = random > 0.5 ? paramRanges.max : paramRanges.min;
  401. }
  402. else
  403. {
  404. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  405. value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
  406. if (paramData.hints & PARAMETER_IS_INTEGER)
  407. value = std::rint(value);
  408. }
  409. setParameterValue(i, value, true, true, true);
  410. }
  411. }
  412. const StateSave& CarlaPlugin::getStateSave()
  413. {
  414. pData->stateSave.clear();
  415. prepareForSave();
  416. char strBuf[STR_MAX+1];
  417. // ---------------------------------------------------------------
  418. // Basic info
  419. getLabel(strBuf);
  420. pData->stateSave.type = carla_strdup(getPluginTypeAsString(getType()));
  421. pData->stateSave.name = carla_strdup(pData->name);
  422. pData->stateSave.label = carla_strdup(strBuf);
  423. pData->stateSave.uniqueId = getUniqueId();
  424. if (pData->filename != nullptr)
  425. pData->stateSave.binary = carla_strdup(pData->filename);
  426. // ---------------------------------------------------------------
  427. // Internals
  428. pData->stateSave.active = pData->active;
  429. #ifndef BUILD_BRIDGE
  430. pData->stateSave.dryWet = pData->postProc.dryWet;
  431. pData->stateSave.volume = pData->postProc.volume;
  432. pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
  433. pData->stateSave.balanceRight = pData->postProc.balanceRight;
  434. pData->stateSave.panning = pData->postProc.panning;
  435. pData->stateSave.ctrlChannel = pData->ctrlChannel;
  436. #endif
  437. // ---------------------------------------------------------------
  438. // Chunk
  439. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  440. {
  441. void* data = nullptr;
  442. const int32_t dataSize(getChunkData(&data));
  443. if (data != nullptr && dataSize > 0)
  444. {
  445. pData->stateSave.chunk = carla_strdup(QByteArray((char*)data, dataSize).toBase64().constData());
  446. // Don't save anything else if using chunks
  447. return pData->stateSave;
  448. }
  449. }
  450. // ---------------------------------------------------------------
  451. // Current Program
  452. if (pData->prog.current >= 0 && getType() != PLUGIN_LV2)
  453. {
  454. pData->stateSave.currentProgramIndex = pData->prog.current;
  455. pData->stateSave.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  456. }
  457. // ---------------------------------------------------------------
  458. // Current MIDI Program
  459. if (pData->midiprog.current >= 0 && getType() != PLUGIN_LV2)
  460. {
  461. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  462. pData->stateSave.currentMidiBank = static_cast<int32_t>(mpData.bank);
  463. pData->stateSave.currentMidiProgram = static_cast<int32_t>(mpData.program);
  464. }
  465. // ---------------------------------------------------------------
  466. // Parameters
  467. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  468. for (uint32_t i=0; i < pData->param.count; ++i)
  469. {
  470. const ParameterData& paramData(pData->param.data[i]);
  471. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  472. continue;
  473. StateParameter* const stateParameter(new StateParameter());
  474. stateParameter->isInput = (paramData.type == PARAMETER_INPUT);
  475. stateParameter->index = paramData.index;
  476. stateParameter->midiCC = paramData.midiCC;
  477. stateParameter->midiChannel = paramData.midiChannel;
  478. getParameterName(i, strBuf);
  479. stateParameter->name = carla_strdup(strBuf);
  480. getParameterSymbol(i, strBuf);
  481. stateParameter->symbol = carla_strdup(strBuf);;
  482. stateParameter->value = getParameterValue(i);
  483. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  484. stateParameter->value /= sampleRate;
  485. pData->stateSave.parameters.append(stateParameter);
  486. }
  487. // ---------------------------------------------------------------
  488. // Custom Data
  489. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  490. {
  491. const CustomData& cData(it.getValue());
  492. StateCustomData* stateCustomData(new StateCustomData());
  493. stateCustomData->type = carla_strdup(cData.type);
  494. stateCustomData->key = carla_strdup(cData.key);
  495. stateCustomData->value = carla_strdup(cData.value);
  496. pData->stateSave.customData.append(stateCustomData);
  497. }
  498. return pData->stateSave;
  499. }
  500. void CarlaPlugin::loadStateSave(const StateSave& stateSave)
  501. {
  502. char strBuf[STR_MAX+1];
  503. const bool usesMultiProgs(pData->extraHints & PLUGIN_EXTRA_HINT_USES_MULTI_PROGS);
  504. gIsLoadingProject = true;
  505. ScopedValueSetter<bool>(gIsLoadingProject, false);
  506. // ---------------------------------------------------------------
  507. // Part 1 - PRE-set custom data (only that which reload programs)
  508. for (LinkedList<StateCustomData*>::Itenerator it = stateSave.customData.begin(); it.valid(); it.next())
  509. {
  510. const StateCustomData* const stateCustomData(it.getValue());
  511. const char* const key(stateCustomData->key);
  512. bool wantData = false;
  513. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  514. wantData = true;
  515. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  516. wantData = true;
  517. if (wantData)
  518. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  519. }
  520. // ---------------------------------------------------------------
  521. // Part 2 - set program
  522. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  523. {
  524. int32_t programId = -1;
  525. // index < count
  526. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  527. {
  528. programId = stateSave.currentProgramIndex;
  529. }
  530. // index not valid, try to find by name
  531. else
  532. {
  533. for (uint32_t i=0; i < pData->prog.count; ++i)
  534. {
  535. strBuf[0] = '\0';
  536. getProgramName(i, strBuf);
  537. if (strBuf[0] != '\0' && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  538. {
  539. programId = static_cast<int32_t>(i);
  540. break;
  541. }
  542. }
  543. }
  544. // set program now, if valid
  545. if (programId >= 0)
  546. setProgram(programId, true, true, true);
  547. }
  548. // ---------------------------------------------------------------
  549. // Part 3 - set midi program
  550. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  551. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  552. // ---------------------------------------------------------------
  553. // Part 4a - get plugin parameter symbols
  554. LinkedList<ParamSymbol*> paramSymbols;
  555. if (getType() == PLUGIN_LADSPA || getType() == PLUGIN_LV2)
  556. {
  557. for (uint32_t i=0; i < pData->param.count; ++i)
  558. {
  559. strBuf[0] = '\0';
  560. getParameterSymbol(i, strBuf);
  561. if (strBuf[0] != '\0')
  562. {
  563. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  564. paramSymbols.append(paramSymbol);
  565. }
  566. }
  567. }
  568. // ---------------------------------------------------------------
  569. // Part 4b - set parameter values (carefully)
  570. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  571. for (LinkedList<StateParameter*>::Itenerator it = stateSave.parameters.begin(); it.valid(); it.next())
  572. {
  573. StateParameter* const stateParameter(it.getValue());
  574. int32_t index = -1;
  575. if (getType() == PLUGIN_LADSPA)
  576. {
  577. // Try to set by symbol, otherwise use index
  578. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  579. {
  580. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  581. {
  582. ParamSymbol* const paramSymbol(it2.getValue());
  583. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  584. {
  585. index = paramSymbol->index;
  586. break;
  587. }
  588. }
  589. if (index == -1)
  590. index = stateParameter->index;
  591. }
  592. else
  593. index = stateParameter->index;
  594. }
  595. else if (getType() == PLUGIN_LV2)
  596. {
  597. // Symbol only
  598. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  599. {
  600. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  601. {
  602. ParamSymbol* const paramSymbol(it2.getValue());
  603. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  604. {
  605. index = paramSymbol->index;
  606. break;
  607. }
  608. }
  609. if (index == -1)
  610. carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
  611. }
  612. else
  613. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  614. }
  615. else
  616. {
  617. // Index only
  618. index = stateParameter->index;
  619. }
  620. // Now set parameter
  621. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  622. {
  623. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  624. if (stateParameter->isInput)
  625. {
  626. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  627. stateParameter->value *= sampleRate;
  628. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  629. }
  630. #ifndef BUILD_BRIDGE
  631. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  632. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  633. #endif
  634. }
  635. else
  636. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  637. }
  638. // ---------------------------------------------------------------
  639. // Part 4c - clear
  640. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin(); it.valid(); it.next())
  641. {
  642. ParamSymbol* const paramSymbol(it.getValue());
  643. delete paramSymbol;
  644. }
  645. paramSymbols.clear();
  646. // ---------------------------------------------------------------
  647. // Part 5 - set custom data
  648. for (LinkedList<StateCustomData*>::Itenerator it = stateSave.customData.begin(); it.valid(); it.next())
  649. {
  650. const StateCustomData* const stateCustomData(it.getValue());
  651. const char* const key(stateCustomData->key);
  652. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  653. continue;
  654. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  655. continue;
  656. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  657. }
  658. // ---------------------------------------------------------------
  659. // Part 5x - set lv2 state
  660. if (getType() == PLUGIN_LV2 && pData->custom.count() > 0)
  661. setCustomData(CUSTOM_DATA_TYPE_STRING, "CarlaLoadLv2StateNow", "true", true);
  662. // ---------------------------------------------------------------
  663. // Part 6 - set chunk
  664. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  665. setChunkData(stateSave.chunk);
  666. // ---------------------------------------------------------------
  667. // Part 6 - set internal stuff
  668. #ifndef BUILD_BRIDGE
  669. setDryWet(stateSave.dryWet, true, true);
  670. setVolume(stateSave.volume, true, true);
  671. setBalanceLeft(stateSave.balanceLeft, true, true);
  672. setBalanceRight(stateSave.balanceRight, true, true);
  673. setPanning(stateSave.panning, true, true);
  674. setCtrlChannel(stateSave.ctrlChannel, true, true);
  675. #endif
  676. setActive(stateSave.active, true, true);
  677. }
  678. bool CarlaPlugin::saveStateToFile(const char* const filename)
  679. {
  680. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  681. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  682. QFile file(filename);
  683. if (! file.open(QIODevice::WriteOnly | QIODevice::Text))
  684. return false;
  685. QTextStream out(&file);
  686. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  687. out << "<!DOCTYPE CARLA-PRESET>\n";
  688. out << "<CARLA-PRESET VERSION='2.0'>\n";
  689. out << getStateSave().toString();
  690. out << "</CARLA-PRESET>\n";
  691. file.close();
  692. return true;
  693. }
  694. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  695. {
  696. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  697. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  698. QFile file(filename);
  699. if (! file.open(QIODevice::ReadOnly | QIODevice::Text))
  700. return false;
  701. QDomDocument xml;
  702. xml.setContent(file.readAll());
  703. file.close();
  704. QDomNode xmlNode(xml.documentElement());
  705. if (xmlNode.toElement().tagName().compare("carla-preset", Qt::CaseInsensitive) == 0)
  706. {
  707. pData->engine->setLastError("Not a valid Carla preset file");
  708. return false;
  709. }
  710. pData->stateSave.fillFromXmlNode(xmlNode);
  711. loadStateSave(pData->stateSave);
  712. return true;
  713. }
  714. // -------------------------------------------------------------------
  715. // Set data (internal stuff)
  716. void CarlaPlugin::setId(const uint newId) noexcept
  717. {
  718. pData->id = newId;
  719. }
  720. void CarlaPlugin::setName(const char* const newName)
  721. {
  722. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  723. if (pData->name != nullptr)
  724. delete[] pData->name;
  725. pData->name = carla_strdup(newName);
  726. }
  727. void CarlaPlugin::setOption(const uint option, const bool yesNo)
  728. {
  729. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  730. if (yesNo)
  731. pData->options |= option;
  732. else
  733. pData->options &= ~option;
  734. #ifndef BUILD_BRIDGE
  735. pData->saveSetting(option, yesNo);
  736. #endif
  737. }
  738. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  739. {
  740. if (pData->enabled == yesNo)
  741. return;
  742. pData->enabled = yesNo;
  743. pData->masterMutex.lock();
  744. pData->masterMutex.unlock();
  745. }
  746. // -------------------------------------------------------------------
  747. // Set data (internal stuff)
  748. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  749. {
  750. #ifndef BUILD_BRIDGE
  751. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  752. #endif
  753. if (pData->active == active)
  754. return;
  755. {
  756. const ScopedSingleProcessLocker spl(this, true);
  757. if (active)
  758. activate();
  759. else
  760. deactivate();
  761. }
  762. pData->active = active;
  763. #ifndef BUILD_BRIDGE
  764. const float value(active ? 1.0f : 0.0f);
  765. if (sendOsc && pData->engine->isOscControlRegistered())
  766. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  767. if (sendCallback)
  768. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  769. #else
  770. return;
  771. // unused
  772. (void)sendOsc;
  773. (void)sendCallback;
  774. #endif
  775. }
  776. #ifndef BUILD_BRIDGE
  777. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  778. {
  779. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  780. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  781. if (pData->postProc.dryWet == fixedValue)
  782. return;
  783. pData->postProc.dryWet = fixedValue;
  784. if (sendOsc && pData->engine->isOscControlRegistered())
  785. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  786. if (sendCallback)
  787. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  788. }
  789. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  790. {
  791. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  792. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  793. if (pData->postProc.volume == fixedValue)
  794. return;
  795. pData->postProc.volume = fixedValue;
  796. if (sendOsc && pData->engine->isOscControlRegistered())
  797. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  798. if (sendCallback)
  799. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  800. }
  801. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  802. {
  803. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  804. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  805. if (pData->postProc.balanceLeft == fixedValue)
  806. return;
  807. pData->postProc.balanceLeft = fixedValue;
  808. if (sendOsc && pData->engine->isOscControlRegistered())
  809. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  810. if (sendCallback)
  811. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  812. }
  813. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  814. {
  815. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  816. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  817. if (pData->postProc.balanceRight == fixedValue)
  818. return;
  819. pData->postProc.balanceRight = fixedValue;
  820. if (sendOsc && pData->engine->isOscControlRegistered())
  821. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  822. if (sendCallback)
  823. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  824. }
  825. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  826. {
  827. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  828. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  829. if (pData->postProc.panning == fixedValue)
  830. return;
  831. pData->postProc.panning = fixedValue;
  832. if (sendOsc && pData->engine->isOscControlRegistered())
  833. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  834. if (sendCallback)
  835. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  836. }
  837. #endif
  838. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  839. {
  840. #ifndef BUILD_BRIDGE
  841. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  842. #endif
  843. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  844. if (pData->ctrlChannel == channel)
  845. return;
  846. pData->ctrlChannel = channel;
  847. #ifndef BUILD_BRIDGE
  848. const float ctrlf(channel);
  849. if (sendOsc && pData->engine->isOscControlRegistered())
  850. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, ctrlf);
  851. if (sendCallback)
  852. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, ctrlf, nullptr);
  853. if (pData->hints & PLUGIN_IS_BRIDGE)
  854. osc_send_control(pData->osc.data, PARAMETER_CTRL_CHANNEL, ctrlf);
  855. #else
  856. return;
  857. // unused
  858. (void)sendOsc;
  859. (void)sendCallback;
  860. #endif
  861. }
  862. // -------------------------------------------------------------------
  863. // Set data (plugin-specific stuff)
  864. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  865. {
  866. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  867. #ifdef BUILD_BRIDGE
  868. if (! gIsLoadingProject)
  869. {
  870. //CARLA_ASSERT(! sendGui); // this should never happen
  871. }
  872. #endif
  873. #ifdef BUILD_BRIDGE
  874. if (sendGui == sendOsc && sendOsc == sendCallback && ! sendCallback) {
  875. //pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(parameterId), 1, value);
  876. }
  877. #else
  878. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  879. uiParameterChange(parameterId, value);
  880. if (sendOsc && pData->engine->isOscControlRegistered())
  881. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  882. #endif
  883. if (sendCallback)
  884. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  885. }
  886. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  887. {
  888. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  889. switch (rindex)
  890. {
  891. case PARAMETER_ACTIVE:
  892. return setActive((value > 0.0f), sendOsc, sendCallback);
  893. case PARAMETER_CTRL_CHANNEL:
  894. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  895. #ifndef BUILD_BRIDGE
  896. case PARAMETER_DRYWET:
  897. return setDryWet(value, sendOsc, sendCallback);
  898. case PARAMETER_VOLUME:
  899. return setVolume(value, sendOsc, sendCallback);
  900. case PARAMETER_BALANCE_LEFT:
  901. return setBalanceLeft(value, sendOsc, sendCallback);
  902. case PARAMETER_BALANCE_RIGHT:
  903. return setBalanceRight(value, sendOsc, sendCallback);
  904. case PARAMETER_PANNING:
  905. return setPanning(value, sendOsc, sendCallback);
  906. #endif
  907. }
  908. for (uint32_t i=0; i < pData->param.count; ++i)
  909. {
  910. if (pData->param.data[i].rindex == rindex)
  911. {
  912. if (getParameterValue(i) != value)
  913. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  914. break;
  915. }
  916. }
  917. }
  918. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  919. {
  920. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  921. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  922. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  923. pData->param.data[parameterId].midiChannel = channel;
  924. #ifndef BUILD_BRIDGE
  925. if (sendOsc && pData->engine->isOscControlRegistered())
  926. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  927. if (sendCallback)
  928. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  929. if (pData->hints & PLUGIN_IS_BRIDGE)
  930. {} // TODO
  931. #else
  932. return;
  933. // unused
  934. (void)sendOsc;
  935. (void)sendCallback;
  936. #endif
  937. }
  938. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  939. {
  940. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  941. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  942. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F,);
  943. pData->param.data[parameterId].midiCC = cc;
  944. #ifndef BUILD_BRIDGE
  945. if (sendOsc && pData->engine->isOscControlRegistered())
  946. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  947. if (sendCallback)
  948. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  949. if (pData->hints & PLUGIN_IS_BRIDGE)
  950. {} // TODO
  951. #else
  952. return;
  953. // unused
  954. (void)sendOsc;
  955. (void)sendCallback;
  956. #endif
  957. }
  958. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  959. {
  960. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  961. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  962. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  963. #ifdef BUILD_BRIDGE
  964. if (! gIsLoadingProject) {
  965. CARLA_SAFE_ASSERT_RETURN(! sendGui,); // this should never happen
  966. }
  967. #else
  968. // unused
  969. (void)sendGui;
  970. #endif
  971. bool saveData = true;
  972. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  973. {
  974. // Ignore some keys
  975. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  976. saveData = false;
  977. //else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVE_NOW) == 0 || std::strcmp(key, CARLA_BRIDGE_MSG_SET_CHUNK) == 0 || std::strcmp(key, CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  978. // saveData = false;
  979. }
  980. if (! saveData)
  981. return;
  982. // Check if we already have this key
  983. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  984. {
  985. CustomData& cData(it.getValue());
  986. CARLA_SAFE_ASSERT_CONTINUE(cData.type != nullptr && cData.type[0] != '\0');
  987. CARLA_SAFE_ASSERT_CONTINUE(cData.key != nullptr && cData.key[0] != '\0');
  988. CARLA_SAFE_ASSERT_CONTINUE(cData.value != nullptr);
  989. if (std::strcmp(cData.key, key) == 0)
  990. {
  991. if (cData.value != nullptr)
  992. delete[] cData.value;
  993. cData.value = carla_strdup(value);
  994. return;
  995. }
  996. }
  997. // Otherwise store it
  998. CustomData newData;
  999. newData.type = carla_strdup(type);
  1000. newData.key = carla_strdup(key);
  1001. newData.value = carla_strdup(value);
  1002. pData->custom.append(newData);
  1003. }
  1004. void CarlaPlugin::setChunkData(const char* const stringData)
  1005. {
  1006. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr && stringData[0] != '\0',);
  1007. CARLA_SAFE_ASSERT(false); // this should never happen
  1008. }
  1009. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1010. {
  1011. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1012. #ifdef BUILD_BRIDGE
  1013. if (! gIsLoadingProject) {
  1014. CARLA_ASSERT(! sendGui); // this should never happen
  1015. }
  1016. #endif
  1017. pData->prog.current = index;
  1018. #ifndef BUILD_BRIDGE
  1019. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1020. if (reallySendOsc)
  1021. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1022. #endif
  1023. if (sendCallback)
  1024. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1025. // Change default parameter values
  1026. if (index >= 0)
  1027. {
  1028. #ifndef BUILD_BRIDGE
  1029. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1030. uiProgramChange(static_cast<uint32_t>(index));
  1031. #endif
  1032. if (getType() == PLUGIN_FILE_CSD || getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || getType() == PLUGIN_FILE_SFZ)
  1033. return;
  1034. pData->updateParameterValues(this, sendOsc, sendCallback, true);
  1035. }
  1036. #ifdef BUILD_BRIDGE
  1037. return;
  1038. // unused
  1039. (void)sendGui;
  1040. (void)sendOsc;
  1041. #endif
  1042. }
  1043. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1044. {
  1045. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1046. #ifdef BUILD_BRIDGE
  1047. if (! gIsLoadingProject) {
  1048. CARLA_ASSERT(! sendGui); // this should never happen
  1049. }
  1050. #endif
  1051. pData->midiprog.current = index;
  1052. #ifndef BUILD_BRIDGE
  1053. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1054. if (reallySendOsc)
  1055. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1056. #endif
  1057. if (sendCallback)
  1058. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1059. if (index >= 0)
  1060. {
  1061. #ifndef BUILD_BRIDGE
  1062. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1063. uiMidiProgramChange(static_cast<uint32_t>(index));
  1064. #endif
  1065. if (getType() == PLUGIN_FILE_CSD || getType() == PLUGIN_FILE_GIG || getType() == PLUGIN_FILE_SF2 || getType() == PLUGIN_FILE_SFZ)
  1066. return;
  1067. pData->updateParameterValues(this, sendOsc, sendCallback, true);
  1068. }
  1069. #ifdef BUILD_BRIDGE
  1070. return;
  1071. // unused
  1072. (void)sendGui;
  1073. (void)sendOsc;
  1074. #endif
  1075. }
  1076. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1077. {
  1078. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1079. {
  1080. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1081. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1082. }
  1083. }
  1084. // -------------------------------------------------------------------
  1085. // Set ui stuff
  1086. void CarlaPlugin::idle()
  1087. {
  1088. if (! pData->enabled)
  1089. return;
  1090. if (pData->hints & PLUGIN_NEEDS_SINGLE_THREAD)
  1091. {
  1092. // Process postponed events
  1093. postRtEventsRun();
  1094. // Update parameter outputs
  1095. for (uint32_t i=0; i < pData->param.count; ++i)
  1096. {
  1097. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1098. uiParameterChange(i, getParameterValue(i));
  1099. }
  1100. }
  1101. if (pData->transientTryCounter == 0)
  1102. return;
  1103. if (++pData->transientTryCounter % 10 != 0)
  1104. return;
  1105. if (pData->transientTryCounter >= 200)
  1106. return;
  1107. carla_stdout("Trying to get window...");
  1108. QString uiTitle(QString("%1 (GUI)").arg(pData->name));
  1109. if (CarlaPluginUi::tryTransientWinIdMatch(pData->osc.data.target != nullptr ? pData->osc.thread.getPid() : 0, uiTitle.toUtf8().constData(), pData->engine->getOptions().frontendWinId))
  1110. pData->transientTryCounter = 0;
  1111. }
  1112. void CarlaPlugin::showCustomUI(const bool yesNo)
  1113. {
  1114. CARLA_SAFE_ASSERT(false);
  1115. return;
  1116. // unused
  1117. (void)yesNo;
  1118. }
  1119. // -------------------------------------------------------------------
  1120. // Plugin state
  1121. void CarlaPlugin::reloadPrograms(const bool)
  1122. {
  1123. }
  1124. // -------------------------------------------------------------------
  1125. // Plugin processing
  1126. void CarlaPlugin::activate() noexcept
  1127. {
  1128. CARLA_SAFE_ASSERT(! pData->active);
  1129. }
  1130. void CarlaPlugin::deactivate() noexcept
  1131. {
  1132. CARLA_SAFE_ASSERT(pData->active);
  1133. }
  1134. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1135. {
  1136. }
  1137. void CarlaPlugin::sampleRateChanged(const double)
  1138. {
  1139. }
  1140. void CarlaPlugin::offlineModeChanged(const bool)
  1141. {
  1142. }
  1143. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1144. {
  1145. if (forcedOffline)
  1146. {
  1147. pData->masterMutex.lock();
  1148. return true;
  1149. }
  1150. return pData->masterMutex.tryLock();
  1151. }
  1152. void CarlaPlugin::unlock() noexcept
  1153. {
  1154. pData->masterMutex.unlock();
  1155. }
  1156. // -------------------------------------------------------------------
  1157. // Plugin buffers
  1158. void CarlaPlugin::initBuffers() const noexcept
  1159. {
  1160. pData->audioIn.initBuffers();
  1161. pData->audioOut.initBuffers();
  1162. pData->event.initBuffers();
  1163. }
  1164. void CarlaPlugin::clearBuffers() noexcept
  1165. {
  1166. pData->clearBuffers();
  1167. }
  1168. // -------------------------------------------------------------------
  1169. // OSC stuff
  1170. void CarlaPlugin::registerToOscClient() noexcept
  1171. {
  1172. #ifdef BUILD_BRIDGE
  1173. if (! pData->engine->isOscBridgeRegistered())
  1174. #else
  1175. if (! pData->engine->isOscControlRegistered())
  1176. #endif
  1177. return;
  1178. #ifndef BUILD_BRIDGE
  1179. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1180. #endif
  1181. // Base data
  1182. {
  1183. // TODO - clear buf
  1184. char bufName[STR_MAX+1] = { '\0' };
  1185. char bufLabel[STR_MAX+1] = { '\0' };
  1186. char bufMaker[STR_MAX+1] = { '\0' };
  1187. char bufCopyright[STR_MAX+1] = { '\0' };
  1188. getRealName(bufName);
  1189. getLabel(bufLabel);
  1190. getMaker(bufMaker);
  1191. getCopyright(bufCopyright);
  1192. #ifdef BUILD_BRIDGE
  1193. pData->engine->oscSend_bridge_plugin_info1(getCategory(), pData->hints, getUniqueId());
  1194. pData->engine->oscSend_bridge_plugin_info2(bufName, bufLabel, bufMaker, bufCopyright);
  1195. #else
  1196. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1197. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1198. #endif
  1199. }
  1200. // Base count
  1201. {
  1202. uint32_t paramIns, paramOuts;
  1203. getParameterCountInfo(paramIns, paramOuts);
  1204. #ifdef BUILD_BRIDGE
  1205. pData->engine->oscSend_bridge_audio_count(getAudioInCount(), getAudioOutCount());
  1206. pData->engine->oscSend_bridge_midi_count(getMidiInCount(), getMidiOutCount());
  1207. pData->engine->oscSend_bridge_parameter_count(paramIns, paramOuts);
  1208. #else
  1209. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1210. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1211. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1212. #endif
  1213. }
  1214. // Plugin Parameters
  1215. if (pData->param.count > 0 && pData->param.count < pData->engine->getOptions().maxParameters)
  1216. {
  1217. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1218. for (uint32_t i=0; i < pData->param.count; ++i)
  1219. {
  1220. carla_zeroChar(bufName, STR_MAX);
  1221. carla_zeroChar(bufUnit, STR_MAX);
  1222. getParameterName(i, bufName);
  1223. getParameterUnit(i, bufUnit);
  1224. const ParameterData& paramData(pData->param.data[i]);
  1225. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1226. #ifdef BUILD_BRIDGE
  1227. pData->engine->oscSend_bridge_parameter_data(i, paramData.rindex, paramData.type, paramData.hints, bufName, bufUnit);
  1228. pData->engine->oscSend_bridge_parameter_ranges1(i, paramRanges.def, paramRanges.min, paramRanges.max);
  1229. pData->engine->oscSend_bridge_parameter_ranges2(i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1230. pData->engine->oscSend_bridge_parameter_value(i, getParameterValue(i));
  1231. pData->engine->oscSend_bridge_parameter_midi_cc(i, paramData.midiCC);
  1232. pData->engine->oscSend_bridge_parameter_midi_channel(i, paramData.midiChannel);
  1233. #else
  1234. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1235. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1236. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1237. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1238. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1239. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1240. #endif
  1241. }
  1242. }
  1243. // Programs
  1244. if (pData->prog.count > 0)
  1245. {
  1246. #ifdef BUILD_BRIDGE
  1247. pData->engine->oscSend_bridge_program_count(pData->prog.count);
  1248. for (uint32_t i=0; i < pData->prog.count; ++i)
  1249. pData->engine->oscSend_bridge_program_name(i, pData->prog.names[i]);
  1250. pData->engine->oscSend_bridge_current_program(pData->prog.current);
  1251. #else
  1252. pData->engine->oscSend_control_set_program_count(pData->id, pData->prog.count);
  1253. for (uint32_t i=0; i < pData->prog.count; ++i)
  1254. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1255. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1256. #endif
  1257. }
  1258. // MIDI Programs
  1259. if (pData->midiprog.count > 0)
  1260. {
  1261. #ifdef BUILD_BRIDGE
  1262. pData->engine->oscSend_bridge_midi_program_count(pData->midiprog.count);
  1263. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1264. {
  1265. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1266. pData->engine->oscSend_bridge_midi_program_data(i, mpData.bank, mpData.program, mpData.name);
  1267. }
  1268. pData->engine->oscSend_bridge_current_midi_program(pData->midiprog.current);
  1269. #else
  1270. pData->engine->oscSend_control_set_midi_program_count(pData->id, pData->midiprog.count);
  1271. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1272. {
  1273. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1274. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1275. }
  1276. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1277. #endif
  1278. }
  1279. #ifndef BUILD_BRIDGE
  1280. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1281. // Internal Parameters
  1282. {
  1283. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1284. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1285. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1286. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1287. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1288. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1289. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1290. }
  1291. #endif
  1292. }
  1293. void CarlaPlugin::updateOscData(const lo_address& source, const char* const url)
  1294. {
  1295. // FIXME - remove debug prints later
  1296. carla_stdout("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  1297. pData->osc.data.free();
  1298. const int proto = lo_address_get_protocol(source);
  1299. {
  1300. const char* host = lo_address_get_hostname(source);
  1301. const char* port = lo_address_get_port(source);
  1302. pData->osc.data.source = lo_address_new_with_proto(proto, host, port);
  1303. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1304. }
  1305. {
  1306. char* host = lo_url_get_hostname(url);
  1307. char* port = lo_url_get_port(url);
  1308. pData->osc.data.path = carla_strdup_free(lo_url_get_path(url));
  1309. pData->osc.data.target = lo_address_new_with_proto(proto, host, port);
  1310. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, pData->osc.data.path);
  1311. std::free(host);
  1312. std::free(port);
  1313. }
  1314. #ifndef BUILD_BRIDGE
  1315. if (pData->hints & PLUGIN_IS_BRIDGE)
  1316. {
  1317. carla_stdout("CarlaPlugin::updateOscData() - done");
  1318. return;
  1319. }
  1320. #endif
  1321. // send possible extra data first
  1322. if (updateOscDataExtra())
  1323. pData->engine->idleOsc();
  1324. osc_send_sample_rate(pData->osc.data, static_cast<float>(pData->engine->getSampleRate()));
  1325. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  1326. {
  1327. const CustomData& cData(it.getValue());
  1328. CARLA_SAFE_ASSERT_CONTINUE(cData.type != nullptr && cData.type[0] != '\0');
  1329. CARLA_SAFE_ASSERT_CONTINUE(cData.key != nullptr && cData.key[0] != '\0');
  1330. CARLA_SAFE_ASSERT_CONTINUE(cData.value != nullptr);
  1331. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0)
  1332. osc_send_configure(pData->osc.data, cData.key, cData.value);
  1333. }
  1334. if (pData->prog.current >= 0)
  1335. osc_send_program(pData->osc.data, static_cast<uint32_t>(pData->prog.current));
  1336. if (pData->midiprog.current >= 0)
  1337. {
  1338. const MidiProgramData& curMidiProg(pData->midiprog.getCurrent());
  1339. if (getType() == PLUGIN_DSSI)
  1340. osc_send_program(pData->osc.data, curMidiProg.bank, curMidiProg.program);
  1341. else
  1342. osc_send_midi_program(pData->osc.data, curMidiProg.bank, curMidiProg.program);
  1343. }
  1344. for (uint32_t i=0; i < pData->param.count; ++i)
  1345. osc_send_control(pData->osc.data, pData->param.data[i].rindex, getParameterValue(i));
  1346. if ((pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0 && pData->engine->getOptions().frontendWinId != 0)
  1347. pData->transientTryCounter = 1;
  1348. carla_stdout("CarlaPlugin::updateOscData() - done");
  1349. }
  1350. bool CarlaPlugin::updateOscDataExtra()
  1351. {
  1352. return false;
  1353. }
  1354. // void CarlaPlugin::freeOscData()
  1355. // {
  1356. // pData->osc.data.free();
  1357. // }
  1358. bool CarlaPlugin::waitForOscGuiShow()
  1359. {
  1360. carla_stdout("CarlaPlugin::waitForOscGuiShow()");
  1361. uint i=0, oscUiTimeout = pData->engine->getOptions().uiBridgesTimeout;
  1362. // wait for UI 'update' call
  1363. for (; i < oscUiTimeout/100; ++i)
  1364. {
  1365. if (pData->osc.data.target != nullptr)
  1366. {
  1367. carla_stdout("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  1368. osc_send_show(pData->osc.data);
  1369. return true;
  1370. }
  1371. if (pData->osc.thread.isThreadRunning())
  1372. carla_msleep(100);
  1373. else
  1374. return false;
  1375. }
  1376. carla_stdout("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  1377. return false;
  1378. }
  1379. // -------------------------------------------------------------------
  1380. // MIDI events
  1381. #ifndef BUILD_BRIDGE
  1382. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1383. {
  1384. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1385. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1386. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1387. if (! pData->active)
  1388. return;
  1389. ExternalMidiNote extNote;
  1390. extNote.channel = static_cast<int8_t>(channel);
  1391. extNote.note = note;
  1392. extNote.velo = velo;
  1393. pData->extNotes.appendNonRT(extNote);
  1394. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1395. {
  1396. if (velo > 0)
  1397. uiNoteOn(channel, note, velo);
  1398. else
  1399. uiNoteOff(channel, note);
  1400. }
  1401. if (sendOsc && pData->engine->isOscControlRegistered())
  1402. {
  1403. if (velo > 0)
  1404. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1405. else
  1406. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1407. }
  1408. if (sendCallback)
  1409. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1410. }
  1411. #endif
  1412. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1413. {
  1414. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1415. return;
  1416. PluginPostRtEvent postEvent;
  1417. postEvent.type = kPluginPostRtEventNoteOff;
  1418. postEvent.value1 = pData->ctrlChannel;
  1419. postEvent.value2 = 0;
  1420. postEvent.value3 = 0.0f;
  1421. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1422. {
  1423. postEvent.value2 = i;
  1424. pData->postRtEvents.appendRT(postEvent);
  1425. }
  1426. }
  1427. // -------------------------------------------------------------------
  1428. // Post-poned events
  1429. void CarlaPlugin::postRtEventsRun()
  1430. {
  1431. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1432. #ifndef BUILD_BRIDGE
  1433. const bool sendOsc(pData->engine->isOscControlRegistered());
  1434. #endif
  1435. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin(); it.valid(); it.next())
  1436. {
  1437. const PluginPostRtEvent& event(it.getValue());
  1438. switch (event.type)
  1439. {
  1440. case kPluginPostRtEventNull:
  1441. break;
  1442. case kPluginPostRtEventDebug:
  1443. #ifndef BUILD_BRIDGE
  1444. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1445. #endif
  1446. break;
  1447. case kPluginPostRtEventParameterChange:
  1448. // Update UI
  1449. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1450. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1451. #ifndef BUILD_BRIDGE
  1452. if (event.value2 != 1)
  1453. {
  1454. // Update OSC control client
  1455. if (sendOsc)
  1456. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1457. // Update Host
  1458. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1459. }
  1460. #endif
  1461. break;
  1462. case kPluginPostRtEventProgramChange:
  1463. // Update UI
  1464. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1465. uiProgramChange(static_cast<uint32_t>(event.value1));
  1466. #ifndef BUILD_BRIDGE
  1467. // Update OSC control client
  1468. if (sendOsc)
  1469. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1470. // Update Host
  1471. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1472. // Update param values
  1473. for (uint32_t j=0; j < pData->param.count; ++j)
  1474. {
  1475. const float paramValue(getParameterValue(j));
  1476. if (sendOsc)
  1477. {
  1478. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1479. pData->engine->oscSend_control_set_default_value(pData->id, j, pData->param.ranges[j].def);
  1480. }
  1481. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1482. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, pData->param.ranges[j].def, nullptr);
  1483. }
  1484. #endif
  1485. break;
  1486. case kPluginPostRtEventMidiProgramChange:
  1487. // Update UI
  1488. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1489. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1490. #ifndef BUILD_BRIDGE
  1491. // Update OSC control client
  1492. if (sendOsc)
  1493. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1494. // Update Host
  1495. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1496. // Update param values
  1497. for (uint32_t j=0; j < pData->param.count; ++j)
  1498. {
  1499. const float paramValue(getParameterValue(j));
  1500. if (sendOsc)
  1501. {
  1502. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1503. pData->engine->oscSend_control_set_default_value(pData->id, j, pData->param.ranges[j].def);
  1504. }
  1505. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1506. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, pData->param.ranges[j].def, nullptr);
  1507. }
  1508. #endif
  1509. break;
  1510. case kPluginPostRtEventNoteOn:
  1511. {
  1512. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1513. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1514. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1515. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1516. const uint8_t note = static_cast<uint8_t>(event.value2);
  1517. const uint8_t velocity = uint8_t(event.value3);
  1518. // Update UI
  1519. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1520. uiNoteOn(channel, note, velocity);
  1521. #ifndef BUILD_BRIDGE
  1522. // Update OSC control client
  1523. if (sendOsc)
  1524. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1525. // Update Host
  1526. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1527. #endif
  1528. break;
  1529. }
  1530. case kPluginPostRtEventNoteOff:
  1531. {
  1532. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1533. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1534. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1535. const uint8_t note = static_cast<uint8_t>(event.value2);
  1536. // Update UI
  1537. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1538. uiNoteOff(channel, note);
  1539. #ifndef BUILD_BRIDGE
  1540. // Update OSC control client
  1541. if (sendOsc)
  1542. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1543. // Update Host
  1544. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1545. #endif
  1546. break;
  1547. }
  1548. }
  1549. }
  1550. pData->postRtEvents.data.clear();
  1551. }
  1552. // -------------------------------------------------------------------
  1553. // Post-poned UI Stuff
  1554. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1555. {
  1556. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1557. return;
  1558. // unused
  1559. (void)value;
  1560. }
  1561. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1562. {
  1563. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1564. }
  1565. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1566. {
  1567. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1568. }
  1569. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1570. {
  1571. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1572. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1573. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1574. }
  1575. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1576. {
  1577. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1578. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1579. }
  1580. bool CarlaPlugin::canRunInRack() const noexcept
  1581. {
  1582. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1583. }
  1584. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1585. {
  1586. return pData->engine;
  1587. }
  1588. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1589. {
  1590. return pData->client;
  1591. }
  1592. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1593. {
  1594. return pData->audioIn.ports[index].port;
  1595. }
  1596. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1597. {
  1598. return pData->audioOut.ports[index].port;
  1599. }
  1600. // -------------------------------------------------------------------
  1601. // Scoped Disabler
  1602. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1603. : fPlugin(plugin)
  1604. {
  1605. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1606. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1607. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1608. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1609. plugin->pData->masterMutex.lock();
  1610. if (plugin->pData->enabled)
  1611. plugin->pData->enabled = false;
  1612. if (plugin->pData->client->isActive())
  1613. plugin->pData->client->deactivate();
  1614. }
  1615. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1616. {
  1617. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1618. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1619. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1620. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1621. fPlugin->pData->enabled = true;
  1622. fPlugin->pData->client->activate();
  1623. fPlugin->pData->masterMutex.unlock();
  1624. }
  1625. // -------------------------------------------------------------------
  1626. // Scoped Process Locker
  1627. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1628. : fPlugin(plugin),
  1629. fBlock(block)
  1630. {
  1631. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1632. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1633. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1634. if (! fBlock)
  1635. return;
  1636. plugin->pData->singleMutex.lock();
  1637. }
  1638. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1639. {
  1640. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1641. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1642. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1643. if (! fBlock)
  1644. return;
  1645. #ifndef BUILD_BRIDGE
  1646. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1647. fPlugin->pData->needsReset = true;
  1648. #endif
  1649. fPlugin->pData->singleMutex.unlock();
  1650. }
  1651. // -------------------------------------------------------------------
  1652. CARLA_BACKEND_END_NAMESPACE