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.

2123 lines
60KB

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