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.

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