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.

2044 lines
63KB

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