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.

2141 lines
61KB

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