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.

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