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.

2163 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. return true;
  794. }
  795. // -------------------------------------------------------------------
  796. // Set data (internal stuff)
  797. void CarlaPlugin::setId(const unsigned int newId)
  798. {
  799. fId = newId;
  800. }
  801. void CarlaPlugin::setName(const char* const newName)
  802. {
  803. fName = newName;
  804. }
  805. void CarlaPlugin::setOption(const unsigned int option, const bool yesNo)
  806. {
  807. CARLA_ASSERT(availableOptions() & option);
  808. if (yesNo)
  809. fOptions |= option;
  810. else
  811. fOptions &= ~option;
  812. kData->saveSetting(option, yesNo);
  813. }
  814. void CarlaPlugin::setEnabled(const bool yesNo)
  815. {
  816. fEnabled = yesNo;
  817. }
  818. // -------------------------------------------------------------------
  819. // Set data (internal stuff)
  820. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback)
  821. {
  822. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  823. if (kData->active == active)
  824. return;
  825. {
  826. const ScopedSingleProcessLocker spl(this, true);
  827. if (active)
  828. activate();
  829. else
  830. deactivate();
  831. }
  832. kData->active = active;
  833. const float value = active ? 1.0f : 0.0f;
  834. #ifndef BUILD_BRIDGE
  835. if (sendOsc)
  836. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_ACTIVE, value);
  837. #else
  838. // unused
  839. (void)sendOsc;
  840. #endif
  841. if (sendCallback)
  842. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_ACTIVE, 0, value, nullptr);
  843. #ifndef BUILD_BRIDGE
  844. else if (fHints & PLUGIN_IS_BRIDGE)
  845. osc_send_control(&kData->osc.data, PARAMETER_ACTIVE, value);
  846. #endif
  847. }
  848. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback)
  849. {
  850. CARLA_ASSERT(value >= 0.0f && value <= 1.0f);
  851. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  852. if (kData->postProc.dryWet == fixedValue)
  853. return;
  854. kData->postProc.dryWet = fixedValue;
  855. #ifndef BUILD_BRIDGE
  856. if (sendOsc)
  857. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_DRYWET, fixedValue);
  858. #else
  859. // unused
  860. (void)sendOsc;
  861. #endif
  862. if (sendCallback)
  863. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  864. #ifndef BUILD_BRIDGE
  865. else if (fHints & PLUGIN_IS_BRIDGE)
  866. osc_send_control(&kData->osc.data, PARAMETER_DRYWET, fixedValue);
  867. #endif
  868. }
  869. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback)
  870. {
  871. CARLA_ASSERT(value >= 0.0f && value <= 1.27f);
  872. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  873. if (kData->postProc.volume == fixedValue)
  874. return;
  875. kData->postProc.volume = fixedValue;
  876. #ifndef BUILD_BRIDGE
  877. if (sendOsc)
  878. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_VOLUME, fixedValue);
  879. #else
  880. // unused
  881. (void)sendOsc;
  882. #endif
  883. if (sendCallback)
  884. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  885. #ifndef BUILD_BRIDGE
  886. else if (fHints & PLUGIN_IS_BRIDGE)
  887. osc_send_control(&kData->osc.data, PARAMETER_VOLUME, fixedValue);
  888. #endif
  889. }
  890. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback)
  891. {
  892. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  893. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  894. if (kData->postProc.balanceLeft == fixedValue)
  895. return;
  896. kData->postProc.balanceLeft = fixedValue;
  897. #ifndef BUILD_BRIDGE
  898. if (sendOsc)
  899. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_LEFT, fixedValue);
  900. #else
  901. // unused
  902. (void)sendOsc;
  903. #endif
  904. if (sendCallback)
  905. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  906. #ifndef BUILD_BRIDGE
  907. else if (fHints & PLUGIN_IS_BRIDGE)
  908. osc_send_control(&kData->osc.data, PARAMETER_BALANCE_LEFT, fixedValue);
  909. #endif
  910. }
  911. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback)
  912. {
  913. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  914. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  915. if (kData->postProc.balanceRight == fixedValue)
  916. return;
  917. kData->postProc.balanceRight = fixedValue;
  918. #ifndef BUILD_BRIDGE
  919. if (sendOsc)
  920. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_RIGHT, fixedValue);
  921. #else
  922. // unused
  923. (void)sendOsc;
  924. #endif
  925. if (sendCallback)
  926. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  927. #ifndef BUILD_BRIDGE
  928. else if (fHints & PLUGIN_IS_BRIDGE)
  929. osc_send_control(&kData->osc.data, PARAMETER_BALANCE_RIGHT, fixedValue);
  930. #endif
  931. }
  932. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback)
  933. {
  934. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  935. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  936. if (kData->postProc.panning == fixedValue)
  937. return;
  938. kData->postProc.panning = fixedValue;
  939. #ifndef BUILD_BRIDGE
  940. if (sendOsc)
  941. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_PANNING, fixedValue);
  942. #else
  943. // unused
  944. (void)sendOsc;
  945. #endif
  946. if (sendCallback)
  947. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_PANNING, 0, fixedValue, nullptr);
  948. #ifndef BUILD_BRIDGE
  949. else if (fHints & PLUGIN_IS_BRIDGE)
  950. osc_send_control(&kData->osc.data, PARAMETER_PANNING, fixedValue);
  951. #endif
  952. }
  953. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback)
  954. {
  955. if (kData->ctrlChannel == channel)
  956. return;
  957. kData->ctrlChannel = channel;
  958. #ifndef BUILD_BRIDGE
  959. const float ctrlf = channel;
  960. if (sendOsc)
  961. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_CTRL_CHANNEL, ctrlf);
  962. #else
  963. // unused
  964. (void)sendOsc;
  965. #endif
  966. if (sendCallback)
  967. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_CTRL_CHANNEL, 0, channel, nullptr);
  968. #ifndef BUILD_BRIDGE
  969. else if (fHints & PLUGIN_IS_BRIDGE)
  970. osc_send_control(&kData->osc.data, PARAMETER_CTRL_CHANNEL, ctrlf);
  971. #endif
  972. }
  973. // -------------------------------------------------------------------
  974. // Set data (plugin-specific stuff)
  975. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  976. {
  977. CARLA_ASSERT(parameterId < kData->param.count);
  978. if (sendGui)
  979. uiParameterChange(parameterId, value);
  980. #ifndef BUILD_BRIDGE
  981. if (sendOsc)
  982. kData->engine->osc_send_control_set_parameter_value(fId, parameterId, value);
  983. #else
  984. // unused
  985. (void)sendOsc;
  986. #endif
  987. if (sendCallback)
  988. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, parameterId, 0, value, nullptr);
  989. #ifndef BUILD_BRIDGE
  990. else if (fHints & PLUGIN_IS_BRIDGE)
  991. osc_send_control(&kData->osc.data, parameterId, value);
  992. #endif
  993. }
  994. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  995. {
  996. CARLA_ASSERT(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL);
  997. if (rindex <= PARAMETER_MAX)
  998. return;
  999. if (rindex == PARAMETER_NULL)
  1000. return;
  1001. if (rindex == PARAMETER_ACTIVE)
  1002. return setActive((value > 0.0f), sendOsc, sendCallback);
  1003. if (rindex == PARAMETER_DRYWET)
  1004. return setDryWet(value, sendOsc, sendCallback);
  1005. if (rindex == PARAMETER_VOLUME)
  1006. return setVolume(value, sendOsc, sendCallback);
  1007. if (rindex == PARAMETER_BALANCE_LEFT)
  1008. return setBalanceLeft(value, sendOsc, sendCallback);
  1009. if (rindex == PARAMETER_BALANCE_RIGHT)
  1010. return setBalanceRight(value, sendOsc, sendCallback);
  1011. if (rindex == PARAMETER_PANNING)
  1012. return setPanning(value, sendOsc, sendCallback);
  1013. if (rindex == PARAMETER_CTRL_CHANNEL)
  1014. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1015. for (uint32_t i=0; i < kData->param.count; ++i)
  1016. {
  1017. if (kData->param.data[i].rindex == rindex)
  1018. {
  1019. if (getParameterValue(i) != value)
  1020. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback)
  1026. {
  1027. CARLA_ASSERT(parameterId < kData->param.count);
  1028. CARLA_ASSERT_INT(channel < MAX_MIDI_CHANNELS, channel);
  1029. if (channel >= MAX_MIDI_CHANNELS)
  1030. channel = MAX_MIDI_CHANNELS;
  1031. kData->param.data[parameterId].midiChannel = channel;
  1032. #ifndef BUILD_BRIDGE
  1033. if (sendOsc)
  1034. kData->engine->osc_send_control_set_parameter_midi_channel(fId, parameterId, channel);
  1035. #else
  1036. // unused
  1037. (void)sendOsc;
  1038. #endif
  1039. if (sendCallback)
  1040. kData->engine->callback(CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, fId, parameterId, channel, 0.0f, nullptr);
  1041. #ifndef BUILD_BRIDGE
  1042. else if (fHints & PLUGIN_IS_BRIDGE)
  1043. {} // TODO
  1044. #endif
  1045. }
  1046. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback)
  1047. {
  1048. CARLA_ASSERT(parameterId < kData->param.count);
  1049. CARLA_ASSERT_INT(cc >= -1, cc);
  1050. if (cc < -1 || cc > 0x5F)
  1051. cc = -1;
  1052. kData->param.data[parameterId].midiCC = cc;
  1053. #ifndef BUILD_BRIDGE
  1054. if (sendOsc)
  1055. kData->engine->osc_send_control_set_parameter_midi_cc(fId, parameterId, cc);
  1056. #else
  1057. // unused
  1058. (void)sendOsc;
  1059. #endif
  1060. if (sendCallback)
  1061. kData->engine->callback(CALLBACK_PARAMETER_MIDI_CC_CHANGED, fId, parameterId, cc, 0.0f, nullptr);
  1062. #ifndef BUILD_BRIDGE
  1063. else if (fHints & PLUGIN_IS_BRIDGE)
  1064. {} // TODO
  1065. #endif
  1066. }
  1067. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  1068. {
  1069. CARLA_ASSERT(type != nullptr);
  1070. CARLA_ASSERT(key != nullptr);
  1071. CARLA_ASSERT(value != nullptr);
  1072. if (type == nullptr)
  1073. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is null", type, key, value, bool2str(sendGui));
  1074. if (key == nullptr)
  1075. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  1076. if (value == nullptr)
  1077. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  1078. bool saveData = true;
  1079. if (std::strcmp(type, CUSTOM_DATA_STRING) == 0)
  1080. {
  1081. // Ignore some keys
  1082. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  1083. saveData = false;
  1084. //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)
  1085. // saveData = false;
  1086. }
  1087. if (saveData)
  1088. {
  1089. // Check if we already have this key
  1090. for (auto it = kData->custom.begin(); it.valid(); it.next())
  1091. {
  1092. CustomData& cData(*it);
  1093. CARLA_ASSERT(cData.type != nullptr);
  1094. CARLA_ASSERT(cData.key != nullptr);
  1095. CARLA_ASSERT(cData.value != nullptr);
  1096. if (cData.type == nullptr)
  1097. return;
  1098. if (cData.key == nullptr)
  1099. return;
  1100. if (std::strcmp(cData.key, key) == 0)
  1101. {
  1102. if (cData.value != nullptr)
  1103. delete[] cData.value;
  1104. cData.value = carla_strdup(value);
  1105. return;
  1106. }
  1107. }
  1108. // Otherwise store it
  1109. CustomData newData;
  1110. newData.type = carla_strdup(type);
  1111. newData.key = carla_strdup(key);
  1112. newData.value = carla_strdup(value);
  1113. kData->custom.append(newData);
  1114. }
  1115. }
  1116. void CarlaPlugin::setChunkData(const char* const stringData)
  1117. {
  1118. CARLA_ASSERT(stringData != nullptr);
  1119. return;
  1120. // unused
  1121. (void)stringData;
  1122. }
  1123. void CarlaPlugin::setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1124. {
  1125. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->prog.count));
  1126. if (index > static_cast<int32_t>(kData->prog.count))
  1127. return;
  1128. const int32_t fixedIndex = carla_fixValue<int32_t>(-1, kData->prog.count, index);
  1129. kData->prog.current = fixedIndex;
  1130. // Change default parameter values
  1131. if (fixedIndex >= 0)
  1132. {
  1133. if (sendGui)
  1134. uiProgramChange(fixedIndex);
  1135. for (uint32_t i=0; i < kData->param.count; ++i)
  1136. {
  1137. // FIXME?
  1138. kData->param.ranges[i].def = getParameterValue(i);
  1139. kData->param.ranges[i].fixDefault();
  1140. if (sendOsc)
  1141. {
  1142. #ifndef BUILD_BRIDGE
  1143. kData->engine->osc_send_control_set_default_value(fId, i, kData->param.ranges[i].def);
  1144. kData->engine->osc_send_control_set_parameter_value(fId, i, kData->param.ranges[i].def);
  1145. #endif
  1146. }
  1147. }
  1148. }
  1149. #ifndef BUILD_BRIDGE
  1150. if (sendOsc)
  1151. kData->engine->osc_send_control_set_program(fId, fixedIndex);
  1152. #endif
  1153. if (sendCallback)
  1154. kData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, fixedIndex, 0, 0.0f, nullptr);
  1155. }
  1156. void CarlaPlugin::setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1157. {
  1158. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  1159. if (index > static_cast<int32_t>(kData->midiprog.count))
  1160. return;
  1161. const int32_t fixedIndex = carla_fixValue<int32_t>(-1, kData->midiprog.count, index);
  1162. kData->midiprog.current = fixedIndex;
  1163. if (fixedIndex >= 0)
  1164. {
  1165. if (sendGui)
  1166. uiMidiProgramChange(fixedIndex);
  1167. // Change default parameter values (sound banks never change defaults)
  1168. #ifndef BUILD_BRIDGE // FIXME
  1169. if (type() != PLUGIN_GIG && type() != PLUGIN_SF2 && type() != PLUGIN_SFZ)
  1170. #endif
  1171. {
  1172. for (uint32_t i=0; i < kData->param.count; ++i)
  1173. {
  1174. // FIXME?
  1175. kData->param.ranges[i].def = getParameterValue(i);
  1176. kData->param.ranges[i].fixDefault();
  1177. if (sendOsc)
  1178. {
  1179. #ifndef BUILD_BRIDGE
  1180. kData->engine->osc_send_control_set_default_value(fId, i, kData->param.ranges[i].def);
  1181. kData->engine->osc_send_control_set_parameter_value(fId, i, kData->param.ranges[i].def);
  1182. #endif
  1183. }
  1184. }
  1185. }
  1186. }
  1187. #ifndef BUILD_BRIDGE
  1188. if (sendOsc)
  1189. kData->engine->osc_send_control_set_midi_program(fId, fixedIndex);
  1190. #endif
  1191. if (sendCallback)
  1192. kData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, fixedIndex, 0, 0.0f, nullptr);
  1193. }
  1194. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1195. {
  1196. for (uint32_t i=0; i < kData->midiprog.count; ++i)
  1197. {
  1198. if (kData->midiprog.data[i].bank == bank && kData->midiprog.data[i].program == program)
  1199. return setMidiProgram(i, sendGui, sendOsc, sendCallback);
  1200. }
  1201. }
  1202. // -------------------------------------------------------------------
  1203. // Set gui stuff
  1204. void CarlaPlugin::showGui(const bool yesNo)
  1205. {
  1206. return;
  1207. // unused
  1208. (void)yesNo;
  1209. }
  1210. void CarlaPlugin::idleGui()
  1211. {
  1212. if (! fEnabled)
  1213. return;
  1214. if (fHints & PLUGIN_HAS_SINGLE_THREAD)
  1215. {
  1216. // Process postponed events
  1217. postRtEventsRun();
  1218. // Update parameter outputs
  1219. for (uint32_t i=0; i < kData->param.count; ++i)
  1220. {
  1221. if (kData->param.data[i].type == PARAMETER_OUTPUT)
  1222. uiParameterChange(i, getParameterValue(i));
  1223. }
  1224. }
  1225. }
  1226. // -------------------------------------------------------------------
  1227. // Plugin state
  1228. void CarlaPlugin::reload()
  1229. {
  1230. }
  1231. void CarlaPlugin::reloadPrograms(const bool)
  1232. {
  1233. }
  1234. // -------------------------------------------------------------------
  1235. // Plugin processing
  1236. void CarlaPlugin::activate()
  1237. {
  1238. CARLA_ASSERT(! kData->active);
  1239. }
  1240. void CarlaPlugin::deactivate()
  1241. {
  1242. CARLA_ASSERT(kData->active);
  1243. }
  1244. void CarlaPlugin::process(float** const, float** const, const uint32_t)
  1245. {
  1246. }
  1247. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1248. {
  1249. }
  1250. void CarlaPlugin::sampleRateChanged(const double)
  1251. {
  1252. }
  1253. bool CarlaPlugin::tryLock()
  1254. {
  1255. return kData->masterMutex.tryLock();
  1256. }
  1257. void CarlaPlugin::unlock()
  1258. {
  1259. kData->masterMutex.unlock();
  1260. }
  1261. // -------------------------------------------------------------------
  1262. // Plugin buffers
  1263. void CarlaPlugin::initBuffers()
  1264. {
  1265. kData->audioIn.initBuffers(kData->engine);
  1266. kData->audioOut.initBuffers(kData->engine);
  1267. kData->event.initBuffers(kData->engine);
  1268. }
  1269. void CarlaPlugin::clearBuffers()
  1270. {
  1271. kData->clearBuffers();
  1272. }
  1273. // -------------------------------------------------------------------
  1274. // OSC stuff
  1275. void CarlaPlugin::registerToOscClient()
  1276. {
  1277. #ifdef BUILD_BRIDGE
  1278. if (! kData->engine->isOscBridgeRegistered())
  1279. return;
  1280. #else
  1281. if (! kData->engine->isOscControlRegistered())
  1282. return;
  1283. #endif
  1284. #ifndef BUILD_BRIDGE
  1285. kData->engine->osc_send_control_add_plugin_start(fId, fName);
  1286. #endif
  1287. // Base data
  1288. {
  1289. char bufName[STR_MAX+1] = { '\0' };
  1290. char bufLabel[STR_MAX+1] = { '\0' };
  1291. char bufMaker[STR_MAX+1] = { '\0' };
  1292. char bufCopyright[STR_MAX+1] = { '\0' };
  1293. getRealName(bufName);
  1294. getLabel(bufLabel);
  1295. getMaker(bufMaker);
  1296. getCopyright(bufCopyright);
  1297. #ifdef BUILD_BRIDGE
  1298. kData->engine->osc_send_bridge_plugin_info(category(), fHints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1299. #else
  1300. kData->engine->osc_send_control_set_plugin_data(fId, type(), category(), fHints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1301. #endif
  1302. }
  1303. // Base count
  1304. {
  1305. uint32_t cIns, cOuts, cTotals;
  1306. getParameterCountInfo(&cIns, &cOuts, &cTotals);
  1307. #ifdef BUILD_BRIDGE
  1308. kData->engine->osc_send_bridge_audio_count(audioInCount(), audioOutCount(), audioInCount() + audioOutCount());
  1309. kData->engine->osc_send_bridge_midi_count(midiInCount(), midiOutCount(), midiInCount() + midiOutCount());
  1310. kData->engine->osc_send_bridge_parameter_count(cIns, cOuts, cTotals);
  1311. #else
  1312. kData->engine->osc_send_control_set_plugin_ports(fId, audioInCount(), audioOutCount(), midiInCount(), midiOutCount(), cIns, cOuts, cTotals);
  1313. #endif
  1314. }
  1315. // Plugin Parameters
  1316. if (kData->param.count > 0 && kData->param.count < kData->engine->getOptions().maxParameters)
  1317. {
  1318. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1319. for (uint32_t i=0; i < kData->param.count; ++i)
  1320. {
  1321. carla_fill<char>(bufName, STR_MAX, '\0');
  1322. carla_fill<char>(bufUnit, STR_MAX, '\0');
  1323. getParameterName(i, bufName);
  1324. getParameterUnit(i, bufUnit);
  1325. const ParameterData& paramData(kData->param.data[i]);
  1326. const ParameterRanges& paramRanges(kData->param.ranges[i]);
  1327. #ifdef BUILD_BRIDGE
  1328. kData->engine->osc_send_bridge_parameter_info(i, bufName, bufUnit);
  1329. kData->engine->osc_send_bridge_parameter_data(i, paramData.type, paramData.rindex, paramData.hints, paramData.midiChannel, paramData.midiCC);
  1330. kData->engine->osc_send_bridge_parameter_ranges(i, paramRanges.def, paramRanges.min, paramRanges.max, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1331. kData->engine->osc_send_bridge_set_parameter_value(i, getParameterValue(i));
  1332. #else
  1333. kData->engine->osc_send_control_set_parameter_data(fId, i, paramData.type, paramData.hints, bufName, bufUnit, getParameterValue(i));
  1334. kData->engine->osc_send_control_set_parameter_ranges(fId, i, paramRanges.min, paramRanges.max, paramRanges.def, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1335. kData->engine->osc_send_control_set_parameter_midi_cc(fId, i, paramData.midiCC);
  1336. kData->engine->osc_send_control_set_parameter_midi_channel(fId, i, paramData.midiChannel);
  1337. kData->engine->osc_send_control_set_parameter_value(fId, i, getParameterValue(i));
  1338. #endif
  1339. }
  1340. }
  1341. // Programs
  1342. if (kData->prog.count > 0)
  1343. {
  1344. #ifdef BUILD_BRIDGE
  1345. kData->engine->osc_send_bridge_program_count(kData->prog.count);
  1346. for (uint32_t i=0; i < kData->prog.count; ++i)
  1347. kData->engine->osc_send_bridge_program_info(i, kData->prog.names[i]);
  1348. kData->engine->osc_send_bridge_set_program(kData->prog.current);
  1349. #else
  1350. kData->engine->osc_send_control_set_program_count(fId, kData->prog.count);
  1351. for (uint32_t i=0; i < kData->prog.count; ++i)
  1352. kData->engine->osc_send_control_set_program_name(fId, i, kData->prog.names[i]);
  1353. kData->engine->osc_send_control_set_program(fId, kData->prog.current);
  1354. #endif
  1355. }
  1356. // MIDI Programs
  1357. if (kData->midiprog.count > 0)
  1358. {
  1359. #ifdef BUILD_BRIDGE
  1360. kData->engine->osc_send_bridge_midi_program_count(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_bridge_midi_program_info(i, mpData.bank, mpData.program, mpData.name);
  1365. }
  1366. kData->engine->osc_send_bridge_set_midi_program(kData->midiprog.current);
  1367. #else
  1368. kData->engine->osc_send_control_set_midi_program_count(fId, kData->midiprog.count);
  1369. for (uint32_t i=0; i < kData->midiprog.count; ++i)
  1370. {
  1371. const MidiProgramData& mpData(kData->midiprog.data[i]);
  1372. kData->engine->osc_send_control_set_midi_program_data(fId, i, mpData.bank, mpData.program, mpData.name);
  1373. }
  1374. kData->engine->osc_send_control_set_midi_program(fId, kData->midiprog.current);
  1375. #endif
  1376. }
  1377. #ifndef BUILD_BRIDGE
  1378. kData->engine->osc_send_control_add_plugin_end(fId);
  1379. // Internal Parameters
  1380. {
  1381. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_DRYWET, kData->postProc.dryWet);
  1382. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_VOLUME, kData->postProc.volume);
  1383. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_LEFT, kData->postProc.balanceLeft);
  1384. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_RIGHT, kData->postProc.balanceRight);
  1385. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_PANNING, kData->postProc.panning);
  1386. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_CTRL_CHANNEL, kData->ctrlChannel);
  1387. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_ACTIVE, kData->active ? 1.0f : 0.0f);
  1388. }
  1389. #endif
  1390. }
  1391. void CarlaPlugin::updateOscData(const lo_address& source, const char* const url)
  1392. {
  1393. // FIXME - remove debug prints later
  1394. carla_stdout("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  1395. kData->osc.data.free();
  1396. const int proto = lo_address_get_protocol(source);
  1397. {
  1398. const char* host = lo_address_get_hostname(source);
  1399. const char* port = lo_address_get_port(source);
  1400. kData->osc.data.source = lo_address_new_with_proto(proto, host, port);
  1401. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1402. }
  1403. {
  1404. char* host = lo_url_get_hostname(url);
  1405. char* port = lo_url_get_port(url);
  1406. kData->osc.data.path = carla_strdup_free(lo_url_get_path(url));
  1407. kData->osc.data.target = lo_address_new_with_proto(proto, host, port);
  1408. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, kData->osc.data.path);
  1409. std::free(host);
  1410. std::free(port);
  1411. }
  1412. #ifndef BUILD_BRIDGE
  1413. if (fHints & PLUGIN_IS_BRIDGE)
  1414. return;
  1415. #endif
  1416. osc_send_sample_rate(&kData->osc.data, kData->engine->getSampleRate());
  1417. for (auto it = kData->custom.begin(); it.valid(); it.next())
  1418. {
  1419. const CustomData& cData(*it);
  1420. CARLA_ASSERT(cData.type != nullptr);
  1421. CARLA_ASSERT(cData.key != nullptr);
  1422. CARLA_ASSERT(cData.value != nullptr);
  1423. #ifdef WANT_LV2
  1424. if (type() == PLUGIN_LV2)
  1425. osc_send_lv2_transfer_event(&kData->osc.data, 0, cData.type, cData.value);
  1426. else
  1427. #endif
  1428. if (std::strcmp(cData.type, CUSTOM_DATA_STRING) == 0)
  1429. osc_send_configure(&kData->osc.data, cData.key, cData.value);
  1430. }
  1431. if (kData->prog.current >= 0)
  1432. osc_send_program(&kData->osc.data, kData->prog.current);
  1433. if (kData->midiprog.current >= 0)
  1434. {
  1435. const MidiProgramData& curMidiProg(kData->midiprog.getCurrent());
  1436. if (type() == PLUGIN_DSSI)
  1437. osc_send_program(&kData->osc.data, curMidiProg.bank, curMidiProg.program);
  1438. else
  1439. osc_send_midi_program(&kData->osc.data, curMidiProg.bank, curMidiProg.program);
  1440. }
  1441. for (uint32_t i=0; i < kData->param.count; ++i)
  1442. osc_send_control(&kData->osc.data, kData->param.data[i].rindex, getParameterValue(i));
  1443. carla_stdout("CarlaPlugin::updateOscData() - done");
  1444. }
  1445. void CarlaPlugin::freeOscData()
  1446. {
  1447. kData->osc.data.free();
  1448. }
  1449. bool CarlaPlugin::waitForOscGuiShow()
  1450. {
  1451. carla_stdout("CarlaPlugin::waitForOscGuiShow()");
  1452. uint i=0, oscUiTimeout = kData->engine->getOptions().oscUiTimeout;
  1453. // wait for UI 'update' call
  1454. for (; i < oscUiTimeout/100; ++i)
  1455. {
  1456. if (kData->osc.data.target != nullptr)
  1457. {
  1458. carla_stdout("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  1459. osc_send_show(&kData->osc.data);
  1460. return true;
  1461. }
  1462. else
  1463. carla_msleep(100);
  1464. }
  1465. carla_stdout("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  1466. return false;
  1467. }
  1468. // -------------------------------------------------------------------
  1469. // MIDI events
  1470. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1471. {
  1472. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1473. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1474. CARLA_ASSERT(velo < MAX_MIDI_VALUE);
  1475. if (! kData->active)
  1476. return;
  1477. ExternalMidiNote extNote;
  1478. extNote.channel = channel;
  1479. extNote.note = note;
  1480. extNote.velo = velo;
  1481. kData->extNotes.append(extNote);
  1482. if (sendGui)
  1483. {
  1484. if (velo > 0)
  1485. uiNoteOn(channel, note, velo);
  1486. else
  1487. uiNoteOff(channel, note);
  1488. }
  1489. #ifndef BUILD_BRIDGE
  1490. if (sendOsc)
  1491. {
  1492. if (velo > 0)
  1493. kData->engine->osc_send_control_note_on(fId, channel, note, velo);
  1494. else
  1495. kData->engine->osc_send_control_note_off(fId, channel, note);
  1496. }
  1497. #else
  1498. // unused
  1499. (void)sendOsc;
  1500. #endif
  1501. if (sendCallback)
  1502. kData->engine->callback((velo > 0) ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, fId, channel, note, velo, nullptr);
  1503. }
  1504. void CarlaPlugin::sendMidiAllNotesOff()
  1505. {
  1506. if (kData->ctrlChannel < 0 || kData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1507. return;
  1508. PluginPostRtEvent postEvent;
  1509. postEvent.type = kPluginPostRtEventNoteOff;
  1510. postEvent.value1 = kData->ctrlChannel;
  1511. postEvent.value2 = 0;
  1512. postEvent.value3 = 0.0f;
  1513. for (unsigned short i=0; i < MAX_MIDI_NOTE; ++i)
  1514. {
  1515. postEvent.value2 = i;
  1516. kData->postRtEvents.appendRT(postEvent);
  1517. }
  1518. }
  1519. // -------------------------------------------------------------------
  1520. // Post-poned events
  1521. void CarlaPlugin::postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3)
  1522. {
  1523. PluginPostRtEvent event;
  1524. event.type = type;
  1525. event.value1 = value1;
  1526. event.value2 = value2;
  1527. event.value3 = value3;
  1528. kData->postRtEvents.appendRT(event);
  1529. }
  1530. void CarlaPlugin::postRtEventsRun()
  1531. {
  1532. const CarlaMutex::ScopedLocker sl(&kData->postRtEvents.mutex);
  1533. while (! kData->postRtEvents.data.isEmpty())
  1534. {
  1535. const PluginPostRtEvent& event = kData->postRtEvents.data.getFirst(true);
  1536. switch (event.type)
  1537. {
  1538. case kPluginPostRtEventNull:
  1539. break;
  1540. case kPluginPostRtEventDebug:
  1541. kData->engine->callback(CALLBACK_DEBUG, fId, event.value1, event.value2, event.value3, nullptr);
  1542. break;
  1543. case kPluginPostRtEventParameterChange:
  1544. // Update UI
  1545. if (event.value1 >= 0)
  1546. uiParameterChange(event.value1, event.value3);
  1547. #ifndef BUILD_BRIDGE
  1548. // Update OSC control client
  1549. if (kData->engine->isOscControlRegistered())
  1550. kData->engine->osc_send_control_set_parameter_value(fId, event.value1, event.value3);
  1551. #endif
  1552. // Update Host
  1553. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, event.value1, 0, event.value3, nullptr);
  1554. break;
  1555. case kPluginPostRtEventProgramChange:
  1556. // Update UI
  1557. if (event.value1 >= 0)
  1558. uiProgramChange(event.value1);
  1559. #ifndef BUILD_BRIDGE
  1560. // Update OSC control client
  1561. if (kData->engine->isOscControlRegistered())
  1562. {
  1563. kData->engine->osc_send_control_set_program(fId, event.value1);
  1564. for (uint32_t j=0; j < kData->param.count; ++j)
  1565. kData->engine->osc_send_control_set_default_value(fId, j, kData->param.ranges[j].def);
  1566. }
  1567. #endif
  1568. // Update Host
  1569. kData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, event.value1, 0, 0.0f, nullptr);
  1570. break;
  1571. case kPluginPostRtEventMidiProgramChange:
  1572. // Update UI
  1573. if (event.value1 >= 0)
  1574. uiMidiProgramChange(event.value1);
  1575. #ifndef BUILD_BRIDGE
  1576. // Update OSC control client
  1577. if (kData->engine->isOscControlRegistered())
  1578. {
  1579. kData->engine->osc_send_control_set_midi_program(fId, event.value1);
  1580. for (uint32_t j=0; j < kData->param.count; ++j)
  1581. kData->engine->osc_send_control_set_default_value(fId, j, kData->param.ranges[j].def);
  1582. }
  1583. #endif
  1584. // Update Host
  1585. kData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, event.value1, 0, 0.0f, nullptr);
  1586. break;
  1587. case kPluginPostRtEventNoteOn:
  1588. // Update UI
  1589. uiNoteOn(event.value1, event.value2, int(event.value3));
  1590. #ifndef BUILD_BRIDGE
  1591. // Update OSC control client
  1592. if (kData->engine->isOscControlRegistered())
  1593. kData->engine->osc_send_control_note_on(fId, event.value1, event.value2, int(event.value3));
  1594. #endif
  1595. // Update Host
  1596. kData->engine->callback(CALLBACK_NOTE_ON, fId, event.value1, event.value2, int(event.value3), nullptr);
  1597. break;
  1598. case kPluginPostRtEventNoteOff:
  1599. // Update UI
  1600. uiNoteOff(event.value1, event.value2);
  1601. #ifndef BUILD_BRIDGE
  1602. // Update OSC control client
  1603. if (kData->engine->isOscControlRegistered())
  1604. kData->engine->osc_send_control_note_off(fId, event.value1, event.value2);
  1605. #endif
  1606. // Update Host
  1607. kData->engine->callback(CALLBACK_NOTE_OFF, fId, event.value1, event.value2, 0.0f, nullptr);
  1608. break;
  1609. }
  1610. }
  1611. }
  1612. // -------------------------------------------------------------------
  1613. // Post-poned UI Stuff
  1614. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value)
  1615. {
  1616. CARLA_ASSERT(index < parameterCount());
  1617. return;
  1618. // unused
  1619. (void)index;
  1620. (void)value;
  1621. }
  1622. void CarlaPlugin::uiProgramChange(const uint32_t index)
  1623. {
  1624. CARLA_ASSERT(index < programCount());
  1625. return;
  1626. // unused
  1627. (void)index;
  1628. }
  1629. void CarlaPlugin::uiMidiProgramChange(const uint32_t index)
  1630. {
  1631. CARLA_ASSERT(index < midiProgramCount());
  1632. return;
  1633. // unused
  1634. (void)index;
  1635. }
  1636. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1637. {
  1638. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1639. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1640. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1641. return;
  1642. // unused
  1643. (void)channel;
  1644. (void)note;
  1645. (void)velo;
  1646. }
  1647. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note)
  1648. {
  1649. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1650. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1651. return;
  1652. // unused
  1653. (void)channel;
  1654. (void)note;
  1655. }
  1656. // -------------------------------------------------------------------
  1657. // Scoped Disabler
  1658. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin)
  1659. : kPlugin(plugin)
  1660. {
  1661. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1662. CARLA_ASSERT(plugin != nullptr);
  1663. CARLA_ASSERT(plugin->kData != nullptr);
  1664. CARLA_ASSERT(plugin->kData->client != nullptr);
  1665. if (plugin == nullptr)
  1666. return;
  1667. if (plugin->kData == nullptr)
  1668. return;
  1669. if (plugin->kData->client == nullptr)
  1670. return;
  1671. plugin->kData->masterMutex.lock();
  1672. if (plugin->fEnabled)
  1673. plugin->fEnabled = false;
  1674. if (plugin->kData->client->isActive())
  1675. plugin->kData->client->deactivate();
  1676. }
  1677. CarlaPlugin::ScopedDisabler::~ScopedDisabler()
  1678. {
  1679. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1680. CARLA_ASSERT(kPlugin != nullptr);
  1681. CARLA_ASSERT(kPlugin->kData != nullptr);
  1682. CARLA_ASSERT(kPlugin->kData->client != nullptr);
  1683. if (kPlugin == nullptr)
  1684. return;
  1685. if (kPlugin->kData == nullptr)
  1686. return;
  1687. if (kPlugin->kData->client == nullptr)
  1688. return;
  1689. kPlugin->fEnabled = true;
  1690. kPlugin->kData->client->activate();
  1691. kPlugin->kData->masterMutex.unlock();
  1692. }
  1693. // -------------------------------------------------------------------
  1694. // Scoped Process Locker
  1695. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block)
  1696. : kPlugin(plugin),
  1697. kBlock(block)
  1698. {
  1699. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1700. CARLA_ASSERT(kPlugin != nullptr && kPlugin->kData != nullptr);
  1701. if (kPlugin == nullptr)
  1702. return;
  1703. if (kPlugin->kData == nullptr)
  1704. return;
  1705. if (kBlock)
  1706. plugin->kData->singleMutex.lock();
  1707. }
  1708. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker()
  1709. {
  1710. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1711. CARLA_ASSERT(kPlugin != nullptr && kPlugin->kData != nullptr);
  1712. if (kPlugin == nullptr)
  1713. return;
  1714. if (kPlugin->kData == nullptr)
  1715. return;
  1716. if (kBlock)
  1717. {
  1718. if (kPlugin->kData->singleMutex.wasTryLockCalled())
  1719. kPlugin->kData->needsReset = true;
  1720. kPlugin->kData->singleMutex.unlock();
  1721. }
  1722. }
  1723. // -------------------------------------------------------------------
  1724. CARLA_BACKEND_END_NAMESPACE