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.

2162 lines
65KB

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