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.

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