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.

2131 lines
64KB

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