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.

2118 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 "juce_data_structures.h"
  20. using namespace juce;
  21. CARLA_BACKEND_START_NAMESPACE
  22. // -------------------------------------------------------------------
  23. // Fallback data
  24. static const ParameterData kParameterDataNull;
  25. static const ParameterRanges kParameterRangesNull;
  26. static const MidiProgramData kMidiProgramDataNull;
  27. static const CustomData kCustomDataNull;
  28. static bool gIsLoadingProject = false;
  29. // -------------------------------------------------------------------
  30. // ParamSymbol struct, needed for CarlaPlugin::loadSaveState()
  31. struct ParamSymbol {
  32. uint32_t index;
  33. const char* symbol;
  34. ParamSymbol(uint32_t i, const char* s)
  35. : index(i),
  36. symbol(carla_strdup(s)) {}
  37. ~ParamSymbol()
  38. {
  39. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
  40. delete[] symbol;
  41. symbol = nullptr;
  42. }
  43. #ifdef CARLA_PROPER_CPP11_SUPPORT
  44. ParamSymbol() = delete;
  45. CARLA_DECLARE_NON_COPY_STRUCT(ParamSymbol)
  46. #endif
  47. };
  48. // -------------------------------------------------------------------
  49. // Library functions, defined in CarlaPluginInternal.hpp
  50. static LibCounter sLibCounter;
  51. bool CarlaPluginProtectedData::libOpen(const char* const filename)
  52. {
  53. lib = sLibCounter.open(filename);
  54. return (lib != nullptr);
  55. }
  56. bool CarlaPluginProtectedData::libClose()
  57. {
  58. const bool ret = sLibCounter.close(lib);
  59. lib = nullptr;
  60. return ret;
  61. }
  62. void* CarlaPluginProtectedData::libSymbol(const char* const symbol)
  63. {
  64. return lib_symbol(lib, symbol);
  65. }
  66. bool CarlaPluginProtectedData::uiLibOpen(const char* const filename)
  67. {
  68. uiLib = sLibCounter.open(filename);
  69. return (uiLib != nullptr);
  70. }
  71. bool CarlaPluginProtectedData::uiLibClose()
  72. {
  73. const bool ret = sLibCounter.close(uiLib);
  74. uiLib = nullptr;
  75. return ret;
  76. }
  77. void* CarlaPluginProtectedData::uiLibSymbol(const char* const symbol)
  78. {
  79. return lib_symbol(uiLib, symbol);
  80. }
  81. // -------------------------------------------------------------------
  82. // Settings functions, defined in CarlaPluginInternal.hpp
  83. // FIXME - this doesn't work!!
  84. void CarlaPluginProtectedData::saveSetting(const unsigned int option, const bool yesNo)
  85. {
  86. PropertiesFile::Options opts;
  87. opts.applicationName = "common"; // TODO, (const char*)idStr
  88. opts.filenameSuffix = ".cfg";
  89. opts.osxLibrarySubFolder = "Application Support";
  90. #ifdef CARLA_OS_LINUX
  91. opts.folderName = "config/falkTX/Carla/PluginSettings/";
  92. #else
  93. opts.folderName = "falkTX\\Carla\\PluginSettings\\";
  94. #endif
  95. ApplicationProperties appProps;
  96. appProps.setStorageParameters(opts);
  97. PropertiesFile* const props(appProps.getUserSettings());
  98. CARLA_SAFE_ASSERT_RETURN(props != nullptr,);
  99. switch (option)
  100. {
  101. case PLUGIN_OPTION_FIXED_BUFFERS:
  102. props->setValue("FixedBuffers", yesNo);
  103. break;
  104. case PLUGIN_OPTION_FORCE_STEREO:
  105. props->setValue("ForceStereo", yesNo);
  106. break;
  107. case PLUGIN_OPTION_MAP_PROGRAM_CHANGES:
  108. props->setValue("MapProgramChanges", yesNo);
  109. break;
  110. case PLUGIN_OPTION_USE_CHUNKS:
  111. props->setValue("UseChunks", yesNo);
  112. break;
  113. case PLUGIN_OPTION_SEND_CONTROL_CHANGES:
  114. props->setValue("SendControlChanges", yesNo);
  115. break;
  116. case PLUGIN_OPTION_SEND_CHANNEL_PRESSURE:
  117. props->setValue("SendChannelPressure", yesNo);
  118. break;
  119. case PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH:
  120. props->setValue("SendNoteAftertouch", yesNo);
  121. break;
  122. case PLUGIN_OPTION_SEND_PITCHBEND:
  123. props->setValue("SendPitchbend", yesNo);
  124. break;
  125. case PLUGIN_OPTION_SEND_ALL_SOUND_OFF:
  126. props->setValue("SendAllSoundOff", yesNo);
  127. break;
  128. default:
  129. break;
  130. }
  131. appProps.saveIfNeeded();
  132. appProps.closeFiles();
  133. }
  134. unsigned int CarlaPluginProtectedData::loadSettings(const unsigned int options, const unsigned int availOptions)
  135. {
  136. PropertiesFile::Options opts;
  137. opts.applicationName = "common"; // TODO, (const char*)idStr
  138. opts.filenameSuffix = ".cfg";
  139. opts.osxLibrarySubFolder = "Application Support";
  140. #ifdef CARLA_OS_LINUX
  141. opts.folderName = "config/falkTX/Carla/PluginSettings/";
  142. #else
  143. opts.folderName = "falkTX\\Carla\\PluginSettings\\";
  144. #endif
  145. ApplicationProperties appProps;
  146. appProps.setStorageParameters(opts);
  147. PropertiesFile* const props(appProps.getUserSettings());
  148. CARLA_SAFE_ASSERT_RETURN(props != nullptr, options);
  149. unsigned int newOptions = 0x0;
  150. #define CHECK_AND_SET_OPTION(KEY, BIT) \
  151. if ((availOptions & BIT) != 0 || BIT == PLUGIN_OPTION_FORCE_STEREO) \
  152. { \
  153. if (props->containsKey(KEY)) \
  154. { \
  155. if (props->getBoolValue(KEY, bool(options & BIT))) \
  156. newOptions |= BIT; \
  157. } \
  158. else if (options & BIT) \
  159. newOptions |= BIT; \
  160. }
  161. CHECK_AND_SET_OPTION("FixedBuffers", PLUGIN_OPTION_FIXED_BUFFERS);
  162. CHECK_AND_SET_OPTION("ForceStereo", PLUGIN_OPTION_FORCE_STEREO);
  163. CHECK_AND_SET_OPTION("MapProgramChanges", PLUGIN_OPTION_MAP_PROGRAM_CHANGES);
  164. CHECK_AND_SET_OPTION("UseChunks", PLUGIN_OPTION_USE_CHUNKS);
  165. CHECK_AND_SET_OPTION("SendControlChanges", PLUGIN_OPTION_SEND_CONTROL_CHANGES);
  166. CHECK_AND_SET_OPTION("SendChannelPressure", PLUGIN_OPTION_SEND_CHANNEL_PRESSURE);
  167. CHECK_AND_SET_OPTION("SendNoteAftertouch", PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH);
  168. CHECK_AND_SET_OPTION("SendPitchbend", PLUGIN_OPTION_SEND_PITCHBEND);
  169. CHECK_AND_SET_OPTION("SendAllSoundOff", PLUGIN_OPTION_SEND_ALL_SOUND_OFF);
  170. #undef CHECK_AND_SET_OPTION
  171. return newOptions;
  172. }
  173. // -------------------------------------------------------------------
  174. // Constructor and destructor
  175. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const unsigned int id)
  176. : fId(id),
  177. fHints(0x0),
  178. fOptions(0x0),
  179. fEnabled(false),
  180. fIconName("plugin"),
  181. pData(new CarlaPluginProtectedData(engine, this))
  182. {
  183. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  184. CARLA_ASSERT(id < engine->getMaxPluginNumber());
  185. CARLA_ASSERT(id == engine->getCurrentPluginCount());
  186. carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  187. switch (engine->getProccessMode())
  188. {
  189. case PROCESS_MODE_SINGLE_CLIENT:
  190. case PROCESS_MODE_MULTIPLE_CLIENTS:
  191. CARLA_ASSERT(id < MAX_DEFAULT_PLUGINS);
  192. break;
  193. case PROCESS_MODE_CONTINUOUS_RACK:
  194. CARLA_ASSERT(id < MAX_RACK_PLUGINS);
  195. break;
  196. case PROCESS_MODE_PATCHBAY:
  197. CARLA_ASSERT(id < MAX_PATCHBAY_PLUGINS);
  198. break;
  199. case PROCESS_MODE_BRIDGE:
  200. CARLA_ASSERT(id == 0);
  201. break;
  202. }
  203. }
  204. CarlaPlugin::~CarlaPlugin()
  205. {
  206. carla_debug("CarlaPlugin::~CarlaPlugin()");
  207. pData->cleanup();
  208. delete pData;
  209. }
  210. // -------------------------------------------------------------------
  211. // Information (base)
  212. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  213. {
  214. return pData->latency;
  215. }
  216. // -------------------------------------------------------------------
  217. // Information (count)
  218. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  219. {
  220. return pData->audioIn.count;
  221. }
  222. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  223. {
  224. return pData->audioOut.count;
  225. }
  226. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  227. {
  228. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  229. }
  230. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  231. {
  232. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  233. }
  234. uint32_t CarlaPlugin::getParameterCount() const noexcept
  235. {
  236. return pData->param.count;
  237. }
  238. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const
  239. {
  240. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  241. return 0;
  242. }
  243. uint32_t CarlaPlugin::getProgramCount() const noexcept
  244. {
  245. return pData->prog.count;
  246. }
  247. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  248. {
  249. return pData->midiprog.count;
  250. }
  251. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  252. {
  253. return pData->custom.count();
  254. }
  255. // -------------------------------------------------------------------
  256. // Information (current data)
  257. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  258. {
  259. return pData->prog.current;
  260. }
  261. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  262. {
  263. return pData->midiprog.current;
  264. }
  265. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  268. return pData->param.data[parameterId];
  269. }
  270. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  273. return pData->param.ranges[parameterId];
  274. }
  275. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const
  276. {
  277. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  278. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  279. }
  280. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const
  281. {
  282. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  283. return pData->midiprog.data[index];
  284. }
  285. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const
  286. {
  287. CARLA_SAFE_ASSERT_RETURN(index < pData->custom.count(), kCustomDataNull);
  288. return pData->custom.getAt(index);
  289. }
  290. int32_t CarlaPlugin::getChunkData(void** const dataPtr) const
  291. {
  292. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  293. CARLA_ASSERT(false); // this should never happen
  294. return 0;
  295. }
  296. // -------------------------------------------------------------------
  297. // Information (per-plugin data)
  298. unsigned int CarlaPlugin::getAvailableOptions() const
  299. {
  300. CARLA_ASSERT(false); // this should never happen
  301. return 0x0;
  302. }
  303. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const
  304. {
  305. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  306. CARLA_ASSERT(false); // this should never happen
  307. return 0.0f;
  308. }
  309. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  312. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  313. CARLA_ASSERT(false); // this should never happen
  314. return 0.0f;
  315. }
  316. void CarlaPlugin::getLabel(char* const strBuf) const
  317. {
  318. strBuf[0] = '\0';
  319. }
  320. void CarlaPlugin::getMaker(char* const strBuf) const
  321. {
  322. strBuf[0] = '\0';
  323. }
  324. void CarlaPlugin::getCopyright(char* const strBuf) const
  325. {
  326. strBuf[0] = '\0';
  327. }
  328. void CarlaPlugin::getRealName(char* const strBuf) const
  329. {
  330. strBuf[0] = '\0';
  331. }
  332. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const
  333. {
  334. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  335. CARLA_ASSERT(false); // this should never happen
  336. strBuf[0] = '\0';
  337. }
  338. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const
  339. {
  340. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  341. strBuf[0] = '\0';
  342. }
  343. void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) const
  344. {
  345. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  346. CARLA_ASSERT(false); // this should never happen
  347. strBuf[0] = '\0';
  348. }
  349. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const
  350. {
  351. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  352. strBuf[0] = '\0';
  353. }
  354. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const
  355. {
  356. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  357. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  358. CARLA_ASSERT(false); // this should never happen
  359. strBuf[0] = '\0';
  360. }
  361. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const
  362. {
  363. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  364. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr,);
  365. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  366. }
  367. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const
  368. {
  369. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  370. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr,);
  371. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  372. }
  373. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs, uint32_t& total) const
  374. {
  375. ins = 0;
  376. outs = 0;
  377. total = pData->param.count;
  378. for (uint32_t i=0; i < pData->param.count; ++i)
  379. {
  380. if (pData->param.data[i].type == PARAMETER_INPUT)
  381. ++ins;
  382. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  383. ++outs;
  384. }
  385. }
  386. // -------------------------------------------------------------------
  387. // Set data (state)
  388. void CarlaPlugin::prepareForSave()
  389. {
  390. }
  391. const SaveState& CarlaPlugin::getSaveState()
  392. {
  393. pData->saveState.reset();
  394. prepareForSave();
  395. char strBuf[STR_MAX+1];
  396. // ---------------------------------------------------------------
  397. // Basic info
  398. getLabel(strBuf);
  399. pData->saveState.type = carla_strdup(getPluginTypeAsString(getType()));
  400. pData->saveState.name = carla_strdup(fName);
  401. pData->saveState.label = carla_strdup(strBuf);
  402. pData->saveState.binary = carla_strdup(fFilename);
  403. pData->saveState.uniqueID = getUniqueId();
  404. // ---------------------------------------------------------------
  405. // Internals
  406. pData->saveState.active = pData->active;
  407. #ifndef BUILD_BRIDGE
  408. pData->saveState.dryWet = pData->postProc.dryWet;
  409. pData->saveState.volume = pData->postProc.volume;
  410. pData->saveState.balanceLeft = pData->postProc.balanceLeft;
  411. pData->saveState.balanceRight = pData->postProc.balanceRight;
  412. pData->saveState.panning = pData->postProc.panning;
  413. pData->saveState.ctrlChannel = pData->ctrlChannel;
  414. #endif
  415. // ---------------------------------------------------------------
  416. // Chunk
  417. if (fOptions & PLUGIN_OPTION_USE_CHUNKS)
  418. {
  419. void* data = nullptr;
  420. const int32_t dataSize(getChunkData(&data));
  421. if (data != nullptr && dataSize > 0)
  422. {
  423. MemoryBlock memBlock(data, dataSize);
  424. pData->saveState.chunk = carla_strdup(memBlock.toBase64Encoding().toRawUTF8());
  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(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 (NonRtList<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 (NonRtList<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. NonRtList<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(pData->engine->getSampleRate());
  549. for (NonRtList<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 (NonRtList<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 (NonRtList<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 (NonRtList<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 (NonRtList<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 && (fOptions & 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. File file(filename);
  653. String content;
  654. fillXmlStringFromSaveState(content, getSaveState());
  655. MemoryOutputStream out;
  656. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  657. out << "<!DOCTYPE CARLA-PRESET>\n";
  658. out << "<CARLA-PRESET VERSION='2.0'>\n";
  659. out << content;
  660. out << "</CARLA-PRESET>\n";
  661. return file.replaceWithData(out.getData(), out.getDataSize());
  662. }
  663. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  664. {
  665. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  666. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  667. File file(filename);
  668. XmlDocument xml(file);
  669. if (XmlElement* const xmlCheck = xml.getDocumentElement(true))
  670. {
  671. if (xmlCheck->getTagName().equalsIgnoreCase("carla-preset"))
  672. {
  673. if (XmlElement* const xmlElem = xml.getDocumentElement(false))
  674. {
  675. pData->saveState.reset();
  676. fillSaveStateFromXmlElement(pData->saveState, xmlElem);
  677. loadSaveState(pData->saveState);
  678. delete xmlElem;
  679. delete xmlCheck;
  680. return true;
  681. }
  682. else
  683. pData->engine->setLastError("Failed to parse file");
  684. }
  685. else
  686. pData->engine->setLastError("Invalid Carla preset file");
  687. delete xmlCheck;
  688. return false;
  689. }
  690. pData->engine->setLastError("Not a valid file");
  691. return false;
  692. }
  693. // -------------------------------------------------------------------
  694. // Set data (internal stuff)
  695. void CarlaPlugin::setId(const unsigned int newId) noexcept
  696. {
  697. fId = newId;
  698. }
  699. void CarlaPlugin::setName(const char* const newName)
  700. {
  701. CARLA_ASSERT(newName != nullptr && newName[0] != '\0');
  702. fName = newName;
  703. }
  704. void CarlaPlugin::setOption(const unsigned int option, const bool yesNo)
  705. {
  706. CARLA_ASSERT(getAvailableOptions() & option);
  707. if (yesNo)
  708. fOptions |= option;
  709. else
  710. fOptions &= ~option;
  711. pData->saveSetting(option, yesNo);
  712. }
  713. void CarlaPlugin::setEnabled(const bool yesNo)
  714. {
  715. if (fEnabled == yesNo)
  716. return;
  717. fEnabled = yesNo;
  718. pData->masterMutex.lock();
  719. pData->masterMutex.unlock();
  720. }
  721. // -------------------------------------------------------------------
  722. // Set data (internal stuff)
  723. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback)
  724. {
  725. #ifndef BUILD_BRIDGE
  726. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  727. #endif
  728. if (pData->active == active)
  729. return;
  730. {
  731. const ScopedSingleProcessLocker spl(this, true);
  732. if (active)
  733. activate();
  734. else
  735. deactivate();
  736. }
  737. pData->active = active;
  738. #ifndef BUILD_BRIDGE
  739. const float value(active ? 1.0f : 0.0f);
  740. if (sendOsc)
  741. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_ACTIVE, value);
  742. if (sendCallback)
  743. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_ACTIVE, 0, value, nullptr);
  744. #else
  745. return;
  746. // unused
  747. (void)sendOsc;
  748. (void)sendCallback;
  749. #endif
  750. }
  751. #ifndef BUILD_BRIDGE
  752. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback)
  753. {
  754. CARLA_ASSERT(value >= 0.0f && value <= 1.0f);
  755. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  756. if (pData->postProc.dryWet == fixedValue)
  757. return;
  758. pData->postProc.dryWet = fixedValue;
  759. if (sendOsc)
  760. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_DRYWET, fixedValue);
  761. if (sendCallback)
  762. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  763. }
  764. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback)
  765. {
  766. CARLA_ASSERT(value >= 0.0f && value <= 1.27f);
  767. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  768. if (pData->postProc.volume == fixedValue)
  769. return;
  770. pData->postProc.volume = fixedValue;
  771. if (sendOsc)
  772. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_VOLUME, fixedValue);
  773. if (sendCallback)
  774. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  775. }
  776. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback)
  777. {
  778. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  779. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  780. if (pData->postProc.balanceLeft == fixedValue)
  781. return;
  782. pData->postProc.balanceLeft = fixedValue;
  783. if (sendOsc)
  784. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_BALANCE_LEFT, fixedValue);
  785. if (sendCallback)
  786. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  787. }
  788. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback)
  789. {
  790. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  791. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  792. if (pData->postProc.balanceRight == fixedValue)
  793. return;
  794. pData->postProc.balanceRight = fixedValue;
  795. if (sendOsc)
  796. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_BALANCE_RIGHT, fixedValue);
  797. if (sendCallback)
  798. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  799. }
  800. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback)
  801. {
  802. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  803. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  804. if (pData->postProc.panning == fixedValue)
  805. return;
  806. pData->postProc.panning = fixedValue;
  807. if (sendOsc)
  808. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_PANNING, fixedValue);
  809. if (sendCallback)
  810. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_PANNING, 0, fixedValue, nullptr);
  811. }
  812. #endif
  813. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback)
  814. {
  815. #ifndef BUILD_BRIDGE
  816. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  817. #endif
  818. CARLA_ASSERT_INT(channel >= -1 && channel < MAX_MIDI_CHANNELS, channel);
  819. if (pData->ctrlChannel == channel)
  820. return;
  821. pData->ctrlChannel = channel;
  822. #ifndef BUILD_BRIDGE
  823. const float ctrlf(channel);
  824. if (sendOsc)
  825. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_CTRL_CHANNEL, ctrlf);
  826. if (sendCallback)
  827. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_CTRL_CHANNEL, 0, ctrlf, nullptr);
  828. if (fHints & PLUGIN_IS_BRIDGE)
  829. osc_send_control(pData->osc.data, PARAMETER_CTRL_CHANNEL, ctrlf);
  830. #else
  831. return;
  832. // unused
  833. (void)sendOsc;
  834. (void)sendCallback;
  835. #endif
  836. }
  837. // -------------------------------------------------------------------
  838. // Set data (plugin-specific stuff)
  839. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  840. {
  841. CARLA_ASSERT(parameterId < pData->param.count);
  842. #ifdef BUILD_BRIDGE
  843. if (! gIsLoadingProject)
  844. {
  845. CARLA_ASSERT(! sendGui); // this should never happen
  846. }
  847. #endif
  848. #ifndef BUILD_BRIDGE
  849. if (sendGui)
  850. uiParameterChange(parameterId, value);
  851. if (sendOsc)
  852. pData->engine->oscSend_control_set_parameter_value(fId, parameterId, value);
  853. #endif
  854. if (sendCallback)
  855. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, parameterId, 0, value, nullptr);
  856. #ifdef BUILD_BRIDGE
  857. return;
  858. // unused
  859. (void)sendGui;
  860. (void)sendOsc;
  861. #endif
  862. }
  863. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  864. {
  865. CARLA_ASSERT(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL);
  866. if (rindex <= PARAMETER_MAX)
  867. return;
  868. if (rindex == PARAMETER_NULL)
  869. return;
  870. if (rindex == PARAMETER_ACTIVE)
  871. return setActive((value > 0.0f), sendOsc, sendCallback);
  872. if (rindex == PARAMETER_CTRL_CHANNEL)
  873. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  874. #ifndef BUILD_BRIDGE
  875. if (rindex == PARAMETER_DRYWET)
  876. return setDryWet(value, sendOsc, sendCallback);
  877. if (rindex == PARAMETER_VOLUME)
  878. return setVolume(value, sendOsc, sendCallback);
  879. if (rindex == PARAMETER_BALANCE_LEFT)
  880. return setBalanceLeft(value, sendOsc, sendCallback);
  881. if (rindex == PARAMETER_BALANCE_RIGHT)
  882. return setBalanceRight(value, sendOsc, sendCallback);
  883. if (rindex == PARAMETER_PANNING)
  884. return setPanning(value, sendOsc, sendCallback);
  885. #endif
  886. for (uint32_t i=0; i < pData->param.count; ++i)
  887. {
  888. if (pData->param.data[i].rindex == rindex)
  889. {
  890. if (getParameterValue(i) != value)
  891. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  892. break;
  893. }
  894. }
  895. }
  896. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback)
  897. {
  898. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  899. CARLA_ASSERT(parameterId < pData->param.count);
  900. CARLA_ASSERT_INT(channel < MAX_MIDI_CHANNELS, channel);
  901. if (channel >= MAX_MIDI_CHANNELS)
  902. channel = MAX_MIDI_CHANNELS;
  903. pData->param.data[parameterId].midiChannel = channel;
  904. #ifndef BUILD_BRIDGE
  905. if (sendOsc)
  906. pData->engine->oscSend_control_set_parameter_midi_channel(fId, parameterId, channel);
  907. if (sendCallback)
  908. pData->engine->callback(CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, fId, parameterId, channel, 0.0f, nullptr);
  909. if (fHints & PLUGIN_IS_BRIDGE)
  910. {} // TODO
  911. #else
  912. return;
  913. // unused
  914. (void)sendOsc;
  915. (void)sendCallback;
  916. #endif
  917. }
  918. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback)
  919. {
  920. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  921. CARLA_ASSERT(parameterId < pData->param.count);
  922. CARLA_ASSERT_INT(cc >= -1 && cc <= 0x5F, cc);
  923. if (cc < -1 || cc > 0x5F)
  924. cc = -1;
  925. pData->param.data[parameterId].midiCC = cc;
  926. #ifndef BUILD_BRIDGE
  927. if (sendOsc)
  928. pData->engine->oscSend_control_set_parameter_midi_cc(fId, parameterId, cc);
  929. if (sendCallback)
  930. pData->engine->callback(CALLBACK_PARAMETER_MIDI_CC_CHANGED, fId, parameterId, cc, 0.0f, nullptr);
  931. if (fHints & PLUGIN_IS_BRIDGE)
  932. {} // TODO
  933. #else
  934. return;
  935. // unused
  936. (void)sendOsc;
  937. (void)sendCallback;
  938. #endif
  939. }
  940. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  941. {
  942. CARLA_ASSERT(type != nullptr);
  943. CARLA_ASSERT(key != nullptr);
  944. CARLA_ASSERT(value != nullptr);
  945. #ifdef BUILD_BRIDGE
  946. if (! gIsLoadingProject)
  947. {
  948. CARLA_ASSERT(! sendGui); // this should never happen
  949. }
  950. #endif
  951. if (type == nullptr)
  952. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is null", type, key, value, bool2str(sendGui));
  953. if (key == nullptr)
  954. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  955. if (value == nullptr)
  956. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  957. bool saveData = true;
  958. if (std::strcmp(type, CUSTOM_DATA_STRING) == 0)
  959. {
  960. // Ignore some keys
  961. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  962. saveData = false;
  963. //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)
  964. // saveData = false;
  965. }
  966. if (! saveData)
  967. return;
  968. // Check if we already have this key
  969. for (NonRtList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  970. {
  971. CustomData& cData(*it);
  972. CARLA_ASSERT(cData.type != nullptr);
  973. CARLA_ASSERT(cData.key != nullptr);
  974. CARLA_ASSERT(cData.value != nullptr);
  975. if (cData.type == nullptr)
  976. return;
  977. if (cData.key == nullptr)
  978. return;
  979. if (std::strcmp(cData.key, key) == 0)
  980. {
  981. if (cData.value != nullptr)
  982. delete[] cData.value;
  983. cData.value = carla_strdup(value);
  984. return;
  985. }
  986. }
  987. // Otherwise store it
  988. CustomData newData;
  989. newData.type = carla_strdup(type);
  990. newData.key = carla_strdup(key);
  991. newData.value = carla_strdup(value);
  992. pData->custom.append(newData);
  993. }
  994. void CarlaPlugin::setChunkData(const char* const stringData)
  995. {
  996. CARLA_ASSERT(stringData != nullptr);
  997. CARLA_ASSERT(false); // this should never happen
  998. return;
  999. // unused
  1000. (void)stringData;
  1001. }
  1002. void CarlaPlugin::setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1003. {
  1004. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->prog.count));
  1005. #ifdef BUILD_BRIDGE
  1006. if (! gIsLoadingProject)
  1007. {
  1008. CARLA_ASSERT(! sendGui); // this should never happen
  1009. }
  1010. #endif
  1011. if (index > static_cast<int32_t>(pData->prog.count))
  1012. return;
  1013. const int32_t fixedIndex(carla_fixValue<int32_t>(-1, pData->prog.count, index));
  1014. pData->prog.current = fixedIndex;
  1015. #ifndef BUILD_BRIDGE
  1016. if (sendOsc)
  1017. pData->engine->oscSend_control_set_program(fId, fixedIndex);
  1018. #endif
  1019. if (sendCallback)
  1020. pData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, fixedIndex, 0, 0.0f, nullptr);
  1021. // Change default parameter values
  1022. if (fixedIndex >= 0)
  1023. {
  1024. #ifndef BUILD_BRIDGE
  1025. if (sendGui)
  1026. uiProgramChange(fixedIndex);
  1027. #endif
  1028. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1029. return;
  1030. for (uint32_t i=0; i < pData->param.count; ++i)
  1031. {
  1032. const float value(pData->param.ranges[i].getFixedValue(getParameterValue(i)));
  1033. pData->param.ranges[i].def = value;
  1034. if (sendOsc || sendCallback)
  1035. {
  1036. #ifndef BUILD_BRIDGE
  1037. pData->engine->oscSend_control_set_default_value(fId, i, value);
  1038. pData->engine->oscSend_control_set_parameter_value(fId, i, value);
  1039. #endif
  1040. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, i, 0, value, nullptr);
  1041. pData->engine->callback(CALLBACK_PARAMETER_DEFAULT_CHANGED, fId, i, 0, value, nullptr);
  1042. }
  1043. }
  1044. }
  1045. #ifdef BUILD_BRIDGE
  1046. return;
  1047. // unused
  1048. (void)sendGui;
  1049. #endif
  1050. }
  1051. void CarlaPlugin::setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1052. {
  1053. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count));
  1054. #ifdef BUILD_BRIDGE
  1055. if (! gIsLoadingProject)
  1056. {
  1057. CARLA_ASSERT(! sendGui); // this should never happen
  1058. }
  1059. #endif
  1060. if (index > static_cast<int32_t>(pData->midiprog.count))
  1061. return;
  1062. const int32_t fixedIndex(carla_fixValue<int32_t>(-1, pData->midiprog.count, index));
  1063. pData->midiprog.current = fixedIndex;
  1064. #ifndef BUILD_BRIDGE
  1065. if (sendOsc)
  1066. pData->engine->oscSend_control_set_midi_program(fId, fixedIndex);
  1067. #endif
  1068. if (sendCallback)
  1069. pData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, fixedIndex, 0, 0.0f, nullptr);
  1070. if (fixedIndex >= 0)
  1071. {
  1072. #ifndef BUILD_BRIDGE
  1073. if (sendGui)
  1074. uiMidiProgramChange(fixedIndex);
  1075. #endif
  1076. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1077. return;
  1078. for (uint32_t i=0; i < pData->param.count; ++i)
  1079. {
  1080. const float value(pData->param.ranges[i].getFixedValue(getParameterValue(i)));
  1081. pData->param.ranges[i].def = value;
  1082. if (sendOsc || sendCallback)
  1083. {
  1084. #ifndef BUILD_BRIDGE
  1085. pData->engine->oscSend_control_set_default_value(fId, i, value);
  1086. pData->engine->oscSend_control_set_parameter_value(fId, i, value);
  1087. #endif
  1088. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, i, 0, value, nullptr);
  1089. pData->engine->callback(CALLBACK_PARAMETER_DEFAULT_CHANGED, fId, i, 0, value, nullptr);
  1090. }
  1091. }
  1092. }
  1093. #ifdef BUILD_BRIDGE
  1094. return;
  1095. // unused
  1096. (void)sendGui;
  1097. #endif
  1098. }
  1099. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1100. {
  1101. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1102. {
  1103. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1104. return setMidiProgram(i, sendGui, sendOsc, sendCallback);
  1105. }
  1106. }
  1107. // -------------------------------------------------------------------
  1108. // Set gui stuff
  1109. void CarlaPlugin::showGui(const bool yesNo)
  1110. {
  1111. CARLA_ASSERT(false);
  1112. return;
  1113. // unused
  1114. (void)yesNo;
  1115. }
  1116. void CarlaPlugin::idleGui()
  1117. {
  1118. if (! fEnabled)
  1119. return;
  1120. if (fHints & PLUGIN_NEEDS_SINGLE_THREAD)
  1121. {
  1122. // Process postponed events
  1123. postRtEventsRun();
  1124. // Update parameter outputs
  1125. for (uint32_t i=0; i < pData->param.count; ++i)
  1126. {
  1127. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1128. uiParameterChange(i, getParameterValue(i));
  1129. }
  1130. }
  1131. }
  1132. // -------------------------------------------------------------------
  1133. // Plugin state
  1134. void CarlaPlugin::reloadPrograms(const bool)
  1135. {
  1136. }
  1137. // -------------------------------------------------------------------
  1138. // Plugin processing
  1139. void CarlaPlugin::activate()
  1140. {
  1141. CARLA_ASSERT(! pData->active);
  1142. }
  1143. void CarlaPlugin::deactivate()
  1144. {
  1145. CARLA_ASSERT(pData->active);
  1146. }
  1147. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1148. {
  1149. }
  1150. void CarlaPlugin::sampleRateChanged(const double)
  1151. {
  1152. }
  1153. void CarlaPlugin::offlineModeChanged(const bool)
  1154. {
  1155. }
  1156. bool CarlaPlugin::tryLock()
  1157. {
  1158. return pData->masterMutex.tryLock();
  1159. }
  1160. void CarlaPlugin::unlock()
  1161. {
  1162. pData->masterMutex.unlock();
  1163. }
  1164. // -------------------------------------------------------------------
  1165. // Plugin buffers
  1166. void CarlaPlugin::initBuffers()
  1167. {
  1168. pData->audioIn.initBuffers();
  1169. pData->audioOut.initBuffers();
  1170. pData->event.initBuffers();
  1171. }
  1172. void CarlaPlugin::clearBuffers()
  1173. {
  1174. pData->clearBuffers();
  1175. }
  1176. // -------------------------------------------------------------------
  1177. // OSC stuff
  1178. void CarlaPlugin::registerToOscClient()
  1179. {
  1180. #ifdef BUILD_BRIDGE
  1181. if (! pData->engine->isOscBridgeRegistered())
  1182. return;
  1183. #else
  1184. if (! pData->engine->isOscControlRegistered())
  1185. return;
  1186. #endif
  1187. #ifndef BUILD_BRIDGE
  1188. pData->engine->oscSend_control_add_plugin_start(fId, fName);
  1189. #endif
  1190. // Base data
  1191. {
  1192. char bufName[STR_MAX+1] = { '\0' };
  1193. char bufLabel[STR_MAX+1] = { '\0' };
  1194. char bufMaker[STR_MAX+1] = { '\0' };
  1195. char bufCopyright[STR_MAX+1] = { '\0' };
  1196. getRealName(bufName);
  1197. getLabel(bufLabel);
  1198. getMaker(bufMaker);
  1199. getCopyright(bufCopyright);
  1200. #ifdef BUILD_BRIDGE
  1201. pData->engine->oscSend_bridge_plugin_info(getCategory(), fHints, bufName, bufLabel, bufMaker, bufCopyright, getUniqueId());
  1202. #else
  1203. pData->engine->oscSend_control_set_plugin_data(fId, getType(), getCategory(), fHints, bufName, bufLabel, bufMaker, bufCopyright, getUniqueId());
  1204. #endif
  1205. }
  1206. // Base count
  1207. {
  1208. uint32_t cIns, cOuts, cTotals;
  1209. getParameterCountInfo(cIns, cOuts, cTotals);
  1210. #ifdef BUILD_BRIDGE
  1211. pData->engine->oscSend_bridge_audio_count(getAudioInCount(), getAudioOutCount(), getAudioInCount() + getAudioOutCount());
  1212. pData->engine->oscSend_bridge_midi_count(getMidiInCount(), getMidiOutCount(), getMidiInCount() + getMidiOutCount());
  1213. pData->engine->oscSend_bridge_parameter_count(cIns, cOuts, cTotals);
  1214. #else
  1215. pData->engine->oscSend_control_set_plugin_ports(fId, getAudioInCount(), getAudioOutCount(), getMidiInCount(), getMidiOutCount(), cIns, cOuts, cTotals);
  1216. #endif
  1217. }
  1218. // Plugin Parameters
  1219. if (pData->param.count > 0 && pData->param.count < pData->engine->getOptions().maxParameters)
  1220. {
  1221. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1222. for (uint32_t i=0; i < pData->param.count; ++i)
  1223. {
  1224. carla_fill<char>(bufName, STR_MAX, '\0');
  1225. carla_fill<char>(bufUnit, STR_MAX, '\0');
  1226. getParameterName(i, bufName);
  1227. getParameterUnit(i, bufUnit);
  1228. const ParameterData& paramData(pData->param.data[i]);
  1229. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1230. #ifdef BUILD_BRIDGE
  1231. pData->engine->oscSend_bridge_parameter_info(i, bufName, bufUnit);
  1232. pData->engine->oscSend_bridge_parameter_data(i, paramData.type, paramData.rindex, paramData.hints, paramData.midiChannel, paramData.midiCC);
  1233. pData->engine->oscSend_bridge_parameter_ranges(i, paramRanges.def, paramRanges.min, paramRanges.max, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1234. pData->engine->oscSend_bridge_set_parameter_value(i, getParameterValue(i));
  1235. #else
  1236. pData->engine->oscSend_control_set_parameter_data(fId, i, paramData.type, paramData.hints, bufName, bufUnit, getParameterValue(i));
  1237. pData->engine->oscSend_control_set_parameter_ranges(fId, i, paramRanges.min, paramRanges.max, paramRanges.def, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1238. pData->engine->oscSend_control_set_parameter_midi_cc(fId, i, paramData.midiCC);
  1239. pData->engine->oscSend_control_set_parameter_midi_channel(fId, i, paramData.midiChannel);
  1240. #endif
  1241. }
  1242. }
  1243. // Programs
  1244. if (pData->prog.count > 0)
  1245. {
  1246. #ifdef BUILD_BRIDGE
  1247. pData->engine->oscSend_bridge_program_count(pData->prog.count);
  1248. for (uint32_t i=0; i < pData->prog.count; ++i)
  1249. pData->engine->oscSend_bridge_program_info(i, pData->prog.names[i]);
  1250. pData->engine->oscSend_bridge_set_program(pData->prog.current);
  1251. #else
  1252. pData->engine->oscSend_control_set_program_count(fId, pData->prog.count);
  1253. for (uint32_t i=0; i < pData->prog.count; ++i)
  1254. pData->engine->oscSend_control_set_program_name(fId, i, pData->prog.names[i]);
  1255. pData->engine->oscSend_control_set_program(fId, pData->prog.current);
  1256. #endif
  1257. }
  1258. // MIDI Programs
  1259. if (pData->midiprog.count > 0)
  1260. {
  1261. #ifdef BUILD_BRIDGE
  1262. pData->engine->oscSend_bridge_midi_program_count(pData->midiprog.count);
  1263. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1264. {
  1265. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1266. pData->engine->oscSend_bridge_midi_program_info(i, mpData.bank, mpData.program, mpData.name);
  1267. }
  1268. pData->engine->oscSend_bridge_set_midi_program(pData->midiprog.current);
  1269. #else
  1270. pData->engine->oscSend_control_set_midi_program_count(fId, pData->midiprog.count);
  1271. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1272. {
  1273. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1274. pData->engine->oscSend_control_set_midi_program_data(fId, i, mpData.bank, mpData.program, mpData.name);
  1275. }
  1276. pData->engine->oscSend_control_set_midi_program(fId, pData->midiprog.current);
  1277. #endif
  1278. }
  1279. #ifndef BUILD_BRIDGE
  1280. pData->engine->oscSend_control_add_plugin_end(fId);
  1281. // Internal Parameters
  1282. {
  1283. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_DRYWET, pData->postProc.dryWet);
  1284. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_VOLUME, pData->postProc.volume);
  1285. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1286. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1287. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_PANNING, pData->postProc.panning);
  1288. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1289. pData->engine->oscSend_control_set_parameter_value(fId, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1290. }
  1291. #endif
  1292. }
  1293. void CarlaPlugin::updateOscData(const lo_address& source, const char* const url)
  1294. {
  1295. // FIXME - remove debug prints later
  1296. carla_stdout("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  1297. pData->osc.data.free();
  1298. const int proto = lo_address_get_protocol(source);
  1299. {
  1300. const char* host = lo_address_get_hostname(source);
  1301. const char* port = lo_address_get_port(source);
  1302. pData->osc.data.source = lo_address_new_with_proto(proto, host, port);
  1303. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1304. }
  1305. {
  1306. char* host = lo_url_get_hostname(url);
  1307. char* port = lo_url_get_port(url);
  1308. pData->osc.data.path = carla_strdup_free(lo_url_get_path(url));
  1309. pData->osc.data.target = lo_address_new_with_proto(proto, host, port);
  1310. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, pData->osc.data.path);
  1311. std::free(host);
  1312. std::free(port);
  1313. }
  1314. #ifndef BUILD_BRIDGE
  1315. if (fHints & PLUGIN_IS_BRIDGE)
  1316. return;
  1317. #endif
  1318. osc_send_sample_rate(pData->osc.data, pData->engine->getSampleRate());
  1319. for (NonRtList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  1320. {
  1321. const CustomData& cData(*it);
  1322. CARLA_ASSERT(cData.type != nullptr);
  1323. CARLA_ASSERT(cData.key != nullptr);
  1324. CARLA_ASSERT(cData.value != nullptr);
  1325. if (std::strcmp(cData.type, CUSTOM_DATA_STRING) == 0)
  1326. osc_send_configure(pData->osc.data, cData.key, cData.value);
  1327. }
  1328. if (pData->prog.current >= 0)
  1329. osc_send_program(pData->osc.data, pData->prog.current);
  1330. if (pData->midiprog.current >= 0)
  1331. {
  1332. const MidiProgramData& curMidiProg(pData->midiprog.getCurrent());
  1333. if (getType() == PLUGIN_DSSI)
  1334. osc_send_program(pData->osc.data, curMidiProg.bank, curMidiProg.program);
  1335. else
  1336. osc_send_midi_program(pData->osc.data, curMidiProg.bank, curMidiProg.program);
  1337. }
  1338. for (uint32_t i=0; i < pData->param.count; ++i)
  1339. osc_send_control(pData->osc.data, pData->param.data[i].rindex, getParameterValue(i));
  1340. carla_stdout("CarlaPlugin::updateOscData() - done");
  1341. }
  1342. // void CarlaPlugin::freeOscData()
  1343. // {
  1344. // pData->osc.data.free();
  1345. // }
  1346. bool CarlaPlugin::waitForOscGuiShow()
  1347. {
  1348. carla_stdout("CarlaPlugin::waitForOscGuiShow()");
  1349. uint i=0, oscUiTimeout = pData->engine->getOptions().uiBridgesTimeout;
  1350. // wait for UI 'update' call
  1351. for (; i < oscUiTimeout/100; ++i)
  1352. {
  1353. if (pData->osc.data.target != nullptr)
  1354. {
  1355. carla_stdout("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  1356. osc_send_show(pData->osc.data);
  1357. return true;
  1358. }
  1359. else
  1360. carla_msleep(100);
  1361. }
  1362. carla_stdout("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  1363. return false;
  1364. }
  1365. // -------------------------------------------------------------------
  1366. // MIDI events
  1367. #ifndef BUILD_BRIDGE
  1368. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1369. {
  1370. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1371. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1372. CARLA_ASSERT(velo < MAX_MIDI_VALUE);
  1373. if (! pData->active)
  1374. return;
  1375. ExternalMidiNote extNote;
  1376. extNote.channel = channel;
  1377. extNote.note = note;
  1378. extNote.velo = velo;
  1379. pData->extNotes.append(extNote);
  1380. if (sendGui)
  1381. {
  1382. if (velo > 0)
  1383. uiNoteOn(channel, note, velo);
  1384. else
  1385. uiNoteOff(channel, note);
  1386. }
  1387. if (sendOsc)
  1388. {
  1389. if (velo > 0)
  1390. pData->engine->oscSend_control_note_on(fId, channel, note, velo);
  1391. else
  1392. pData->engine->oscSend_control_note_off(fId, channel, note);
  1393. }
  1394. if (sendCallback)
  1395. pData->engine->callback((velo > 0) ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, fId, channel, note, velo, nullptr);
  1396. }
  1397. #endif
  1398. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1399. {
  1400. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1401. return;
  1402. PluginPostRtEvent postEvent;
  1403. postEvent.type = kPluginPostRtEventNoteOff;
  1404. postEvent.value1 = pData->ctrlChannel;
  1405. postEvent.value2 = 0;
  1406. postEvent.value3 = 0.0f;
  1407. for (unsigned short i=0; i < MAX_MIDI_NOTE; ++i)
  1408. {
  1409. postEvent.value2 = i;
  1410. pData->postRtEvents.appendRT(postEvent);
  1411. }
  1412. }
  1413. // -------------------------------------------------------------------
  1414. // Post-poned events
  1415. void CarlaPlugin::postRtEventsRun()
  1416. {
  1417. const CarlaMutex::ScopedLocker sl(pData->postRtEvents.mutex);
  1418. while (! pData->postRtEvents.data.isEmpty())
  1419. {
  1420. const PluginPostRtEvent& event(pData->postRtEvents.data.getFirst(true));
  1421. switch (event.type)
  1422. {
  1423. case kPluginPostRtEventNull:
  1424. break;
  1425. case kPluginPostRtEventDebug:
  1426. #ifndef BUILD_BRIDGE
  1427. pData->engine->callback(CALLBACK_DEBUG, fId, event.value1, event.value2, event.value3, nullptr);
  1428. #endif
  1429. break;
  1430. case kPluginPostRtEventParameterChange:
  1431. // Update UI
  1432. if (event.value1 >= 0)
  1433. uiParameterChange(event.value1, event.value3);
  1434. #ifndef BUILD_BRIDGE
  1435. if (event.value2 != 1)
  1436. {
  1437. // Update OSC control client
  1438. if (pData->engine->isOscControlRegistered())
  1439. pData->engine->oscSend_control_set_parameter_value(fId, event.value1, event.value3);
  1440. // Update Host
  1441. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, event.value1, 0, event.value3, nullptr);
  1442. }
  1443. #endif
  1444. break;
  1445. case kPluginPostRtEventProgramChange:
  1446. // Update UI
  1447. if (event.value1 >= 0)
  1448. uiProgramChange(event.value1);
  1449. #ifndef BUILD_BRIDGE
  1450. // Update OSC control client
  1451. if (pData->engine->isOscControlRegistered())
  1452. pData->engine->oscSend_control_set_program(fId, event.value1);
  1453. // Update Host
  1454. pData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, event.value1, 0, 0.0f, nullptr);
  1455. // Update param values
  1456. {
  1457. const bool sendOsc(pData->engine->isOscControlRegistered());
  1458. for (uint32_t j=0; j < pData->param.count; ++j)
  1459. {
  1460. const float value(getParameterValue(j));
  1461. if (sendOsc)
  1462. {
  1463. pData->engine->oscSend_control_set_parameter_value(fId, j, value);
  1464. pData->engine->oscSend_control_set_default_value(fId, j, pData->param.ranges[j].def);
  1465. }
  1466. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, j, 0, value, nullptr);
  1467. pData->engine->callback(CALLBACK_PARAMETER_DEFAULT_CHANGED, fId, j, 0, pData->param.ranges[j].def, nullptr);
  1468. }
  1469. }
  1470. #endif
  1471. break;
  1472. case kPluginPostRtEventMidiProgramChange:
  1473. // Update UI
  1474. if (event.value1 >= 0)
  1475. uiMidiProgramChange(event.value1);
  1476. #ifndef BUILD_BRIDGE
  1477. // Update OSC control client
  1478. if (pData->engine->isOscControlRegistered())
  1479. pData->engine->oscSend_control_set_midi_program(fId, event.value1);
  1480. // Update Host
  1481. pData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, event.value1, 0, 0.0f, nullptr);
  1482. // Update param values
  1483. {
  1484. const bool sendOsc(pData->engine->isOscControlRegistered());
  1485. for (uint32_t j=0; j < pData->param.count; ++j)
  1486. {
  1487. const float value(getParameterValue(j));
  1488. if (sendOsc)
  1489. {
  1490. pData->engine->oscSend_control_set_parameter_value(fId, j, value);
  1491. pData->engine->oscSend_control_set_default_value(fId, j, pData->param.ranges[j].def);
  1492. }
  1493. pData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, j, 0, value, nullptr);
  1494. pData->engine->callback(CALLBACK_PARAMETER_DEFAULT_CHANGED, fId, j, 0, pData->param.ranges[j].def, nullptr);
  1495. }
  1496. }
  1497. #endif
  1498. break;
  1499. case kPluginPostRtEventNoteOn:
  1500. // Update UI
  1501. uiNoteOn(event.value1, event.value2, int(event.value3));
  1502. #ifndef BUILD_BRIDGE
  1503. // Update OSC control client
  1504. if (pData->engine->isOscControlRegistered())
  1505. pData->engine->oscSend_control_note_on(fId, event.value1, event.value2, int(event.value3));
  1506. // Update Host
  1507. pData->engine->callback(CALLBACK_NOTE_ON, fId, event.value1, event.value2, int(event.value3), nullptr);
  1508. #endif
  1509. break;
  1510. case kPluginPostRtEventNoteOff:
  1511. // Update UI
  1512. uiNoteOff(event.value1, event.value2);
  1513. #ifndef BUILD_BRIDGE
  1514. // Update OSC control client
  1515. if (pData->engine->isOscControlRegistered())
  1516. pData->engine->oscSend_control_note_off(fId, event.value1, event.value2);
  1517. // Update Host
  1518. pData->engine->callback(CALLBACK_NOTE_OFF, fId, event.value1, event.value2, 0.0f, nullptr);
  1519. #endif
  1520. break;
  1521. }
  1522. }
  1523. }
  1524. // -------------------------------------------------------------------
  1525. // Post-poned UI Stuff
  1526. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value)
  1527. {
  1528. CARLA_ASSERT(index < getParameterCount());
  1529. return;
  1530. // unused
  1531. (void)index;
  1532. (void)value;
  1533. }
  1534. void CarlaPlugin::uiProgramChange(const uint32_t index)
  1535. {
  1536. CARLA_ASSERT(index < getProgramCount());
  1537. return;
  1538. // unused
  1539. (void)index;
  1540. }
  1541. void CarlaPlugin::uiMidiProgramChange(const uint32_t index)
  1542. {
  1543. CARLA_ASSERT(index < getMidiProgramCount());
  1544. return;
  1545. // unused
  1546. (void)index;
  1547. }
  1548. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1549. {
  1550. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1551. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1552. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1553. return;
  1554. // unused
  1555. (void)channel;
  1556. (void)note;
  1557. (void)velo;
  1558. }
  1559. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note)
  1560. {
  1561. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1562. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1563. return;
  1564. // unused
  1565. (void)channel;
  1566. (void)note;
  1567. }
  1568. bool CarlaPlugin::canRunInRack() const noexcept
  1569. {
  1570. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1571. }
  1572. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1573. {
  1574. return pData->engine;
  1575. }
  1576. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1577. {
  1578. return pData->client;
  1579. }
  1580. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1581. {
  1582. return pData->audioIn.ports[index].port;
  1583. }
  1584. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1585. {
  1586. return pData->audioOut.ports[index].port;
  1587. }
  1588. // -------------------------------------------------------------------
  1589. // Scoped Disabler
  1590. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin)
  1591. : fPlugin(plugin)
  1592. {
  1593. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1594. CARLA_ASSERT(plugin != nullptr);
  1595. CARLA_ASSERT(plugin->pData != nullptr);
  1596. CARLA_ASSERT(plugin->pData->client != nullptr);
  1597. if (plugin == nullptr)
  1598. return;
  1599. if (plugin->pData == nullptr)
  1600. return;
  1601. if (plugin->pData->client == nullptr)
  1602. return;
  1603. plugin->pData->masterMutex.lock();
  1604. if (plugin->fEnabled)
  1605. plugin->fEnabled = false;
  1606. if (plugin->pData->client->isActive())
  1607. plugin->pData->client->deactivate();
  1608. }
  1609. CarlaPlugin::ScopedDisabler::~ScopedDisabler()
  1610. {
  1611. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1612. CARLA_ASSERT(fPlugin != nullptr);
  1613. CARLA_ASSERT(fPlugin->pData != nullptr);
  1614. CARLA_ASSERT(fPlugin->pData->client != nullptr);
  1615. if (fPlugin == nullptr)
  1616. return;
  1617. if (fPlugin->pData == nullptr)
  1618. return;
  1619. if (fPlugin->pData->client == nullptr)
  1620. return;
  1621. fPlugin->fEnabled = true;
  1622. fPlugin->pData->client->activate();
  1623. fPlugin->pData->masterMutex.unlock();
  1624. }
  1625. // -------------------------------------------------------------------
  1626. // Scoped Process Locker
  1627. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block)
  1628. : fPlugin(plugin),
  1629. fBlock(block)
  1630. {
  1631. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1632. CARLA_ASSERT(fPlugin != nullptr && fPlugin->pData != nullptr);
  1633. if (fPlugin == nullptr)
  1634. return;
  1635. if (fPlugin->pData == nullptr)
  1636. return;
  1637. if (fBlock)
  1638. plugin->pData->singleMutex.lock();
  1639. }
  1640. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker()
  1641. {
  1642. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1643. CARLA_ASSERT(fPlugin != nullptr && fPlugin->pData != nullptr);
  1644. if (fPlugin == nullptr)
  1645. return;
  1646. if (fPlugin->pData == nullptr)
  1647. return;
  1648. if (fBlock)
  1649. {
  1650. #ifndef BUILD_BRIDGE
  1651. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1652. fPlugin->pData->needsReset = true;
  1653. #endif
  1654. fPlugin->pData->singleMutex.unlock();
  1655. }
  1656. }
  1657. // #ifdef BUILD_BRIDGE
  1658. // CarlaPlugin* newFailAsBridge(const CarlaPlugin::Initializer& init)
  1659. // {
  1660. // init.engine->setLastError("Can't use this in plugin bridges");
  1661. // return nullptr;
  1662. // }
  1663. //
  1664. // CarlaPlugin* CarlaPlugin::newNative(const Initializer& init) { return newFailAsBridge(init); }
  1665. // CarlaPlugin* CarlaPlugin::newGIG(const Initializer& init, const bool) { return newFailAsBridge(init); }
  1666. // CarlaPlugin* CarlaPlugin::newSF2(const Initializer& init, const bool) { return newFailAsBridge(init); }
  1667. // CarlaPlugin* CarlaPlugin::newSFZ(const Initializer& init, const bool) { return newFailAsBridge(init); }
  1668. //
  1669. // # ifdef WANT_NATIVE
  1670. // size_t CarlaPlugin::getNativePluginCount() { return 0; }
  1671. // const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t) { return nullptr; }
  1672. // # endif
  1673. // #endif
  1674. // -------------------------------------------------------------------
  1675. CARLA_BACKEND_END_NAMESPACE