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.

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