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.

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