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.

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