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.

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