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.

2115 lines
65KB

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