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.

2202 lines
62KB

  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. #ifndef BUILD_BRIDGE
  520. saveState.dryWet = kData->postProc.dryWet;
  521. saveState.volume = kData->postProc.volume;
  522. saveState.balanceLeft = kData->postProc.balanceLeft;
  523. saveState.balanceRight = kData->postProc.balanceRight;
  524. saveState.panning = kData->postProc.panning;
  525. saveState.ctrlChannel = kData->ctrlChannel;
  526. #endif
  527. // ----------------------------
  528. // Chunk
  529. if (fOptions & PLUGIN_OPTION_USE_CHUNKS)
  530. {
  531. void* data = nullptr;
  532. const int32_t dataSize(chunkData(&data));
  533. if (data != nullptr && dataSize > 0)
  534. {
  535. QByteArray chunk(QByteArray((char*)data, dataSize).toBase64());
  536. saveState.chunk = carla_strdup(chunk.constData());
  537. // Don't save anything else if using chunks
  538. return saveState;
  539. }
  540. }
  541. // ----------------------------
  542. // Current Program
  543. if (kData->prog.current >= 0)
  544. {
  545. saveState.currentProgramIndex = kData->prog.current;
  546. saveState.currentProgramName = carla_strdup(kData->prog.names[kData->prog.current]);
  547. }
  548. // ----------------------------
  549. // Current MIDI Program
  550. if (kData->midiprog.current >= 0)
  551. {
  552. const MidiProgramData& mpData(kData->midiprog.getCurrent());
  553. saveState.currentMidiBank = mpData.bank;
  554. saveState.currentMidiProgram = mpData.program;
  555. }
  556. // ----------------------------
  557. // Parameters
  558. const float sampleRate(kData->engine->getSampleRate());
  559. for (uint32_t i=0, count=kData->param.count; i < count; ++i)
  560. {
  561. const ParameterData& paramData(kData->param.data[i]);
  562. if ((paramData.hints & PARAMETER_IS_AUTOMABLE) == 0)
  563. continue;
  564. StateParameter* stateParameter(new StateParameter());
  565. stateParameter->index = paramData.index;
  566. stateParameter->midiCC = paramData.midiCC;
  567. stateParameter->midiChannel = paramData.midiChannel + 1;
  568. getParameterName(i, strBuf);
  569. stateParameter->name = carla_strdup(strBuf);
  570. getParameterSymbol(i, strBuf);
  571. stateParameter->symbol = carla_strdup(strBuf);;
  572. stateParameter->value = getParameterValue(i);
  573. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  574. stateParameter->value /= sampleRate;
  575. saveState.parameters.append(stateParameter);
  576. }
  577. // ----------------------------
  578. // Custom Data
  579. for (auto it = kData->custom.begin(); it.valid(); it.next())
  580. {
  581. const CustomData& cData(*it);
  582. if (cData.type == nullptr)
  583. continue;
  584. StateCustomData* stateCustomData(new StateCustomData());
  585. stateCustomData->type = carla_strdup(cData.type);
  586. stateCustomData->key = carla_strdup(cData.key);
  587. stateCustomData->value = carla_strdup(cData.value);
  588. saveState.customData.append(stateCustomData);
  589. }
  590. return saveState;
  591. }
  592. void CarlaPlugin::loadSaveState(const SaveState& saveState)
  593. {
  594. char strBuf[STR_MAX+1];
  595. // ---------------------------------------------------------------------
  596. // Part 1 - set custom data (except binary/chunks)
  597. for (auto it = saveState.customData.begin(); it.valid(); it.next())
  598. {
  599. const StateCustomData* const stateCustomData(*it);
  600. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_CHUNK) != 0)
  601. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  602. }
  603. // ---------------------------------------------------------------------
  604. // Part 2 - set program
  605. int32_t programId = -1;
  606. if (saveState.currentProgramName != nullptr)
  607. {
  608. getProgramName(saveState.currentProgramIndex, strBuf);
  609. // Program name matches
  610. if (std::strcmp(saveState.currentProgramName, strBuf) == 0)
  611. {
  612. programId = saveState.currentProgramIndex;
  613. }
  614. // index < count
  615. else if (saveState.currentProgramIndex < static_cast<int32_t>(kData->prog.count))
  616. {
  617. programId = saveState.currentProgramIndex;
  618. }
  619. // index not valid, try to find by name
  620. else
  621. {
  622. for (uint32_t i=0; i < kData->prog.count; ++i)
  623. {
  624. getProgramName(i, strBuf);
  625. if (std::strcmp(saveState.currentProgramName, strBuf) == 0)
  626. {
  627. programId = i;
  628. break;
  629. }
  630. }
  631. }
  632. }
  633. // set program now, if valid
  634. if (programId >= 0)
  635. setProgram(programId, true, true, true);
  636. // ---------------------------------------------------------------------
  637. // Part 3 - set midi program
  638. if (saveState.currentMidiBank >= 0 && saveState.currentMidiProgram >= 0 && type() != PLUGIN_SF2)
  639. setMidiProgramById(saveState.currentMidiBank, saveState.currentMidiProgram, true, true, true);
  640. // ---------------------------------------------------------------------
  641. // Part 4a - get plugin parameter symbols
  642. struct ParamSymbol {
  643. uint32_t index;
  644. const char* symbol;
  645. ParamSymbol(uint32_t index_, const char* symbol_)
  646. : index(index_),
  647. symbol(carla_strdup(symbol_)) {}
  648. void free()
  649. {
  650. if (symbol != nullptr)
  651. {
  652. delete[] symbol;
  653. symbol = nullptr;
  654. }
  655. }
  656. ParamSymbol() = delete;
  657. ParamSymbol(ParamSymbol&) = delete;
  658. ParamSymbol(const ParamSymbol&) = delete;
  659. };
  660. NonRtList<ParamSymbol*> paramSymbols;
  661. if (type() == PLUGIN_LADSPA || type() == PLUGIN_LV2)
  662. {
  663. for (uint32_t i=0; i < kData->param.count; ++i)
  664. {
  665. getParameterSymbol(i, strBuf);
  666. if (*strBuf != '\0')
  667. {
  668. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  669. paramSymbols.append(paramSymbol);
  670. }
  671. }
  672. }
  673. // ---------------------------------------------------------------------
  674. // Part 4b - set parameter values (carefully)
  675. const float sampleRate(kData->engine->getSampleRate());
  676. for (auto it = saveState.parameters.begin(); it.valid(); it.next())
  677. {
  678. StateParameter* const stateParameter(*it);
  679. int32_t index = -1;
  680. if (type() == PLUGIN_LADSPA)
  681. {
  682. // Try to set by symbol, otherwise use index
  683. if (stateParameter->symbol != nullptr && *stateParameter->symbol != 0)
  684. {
  685. for (auto it = paramSymbols.begin(); it.valid(); it.next())
  686. {
  687. ParamSymbol* const paramSymbol(*it);
  688. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  689. {
  690. index = paramSymbol->index;
  691. break;
  692. }
  693. }
  694. if (index == -1)
  695. index = stateParameter->index;
  696. }
  697. else
  698. index = stateParameter->index;
  699. }
  700. else if (type() == PLUGIN_LV2)
  701. {
  702. // Symbol only
  703. if (stateParameter->symbol != nullptr && *stateParameter->symbol != 0)
  704. {
  705. for (auto it = paramSymbols.begin(); it.valid(); it.next())
  706. {
  707. ParamSymbol* const paramSymbol(*it);
  708. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  709. {
  710. index = paramSymbol->index;
  711. break;
  712. }
  713. }
  714. if (index == -1)
  715. carla_stderr("Failed to find LV2 parameter symbol for '%s')", stateParameter->symbol);
  716. }
  717. else
  718. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  719. }
  720. else
  721. {
  722. // Index only
  723. index = stateParameter->index;
  724. }
  725. // Now set parameter
  726. if (index >= 0 && index < static_cast<int32_t>(kData->param.count))
  727. {
  728. if (kData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  729. stateParameter->value *= sampleRate;
  730. setParameterValue(index, stateParameter->value, true, true, true);
  731. #ifndef BUILD_BRIDGE
  732. setParameterMidiCC(index, stateParameter->midiCC, true, true);
  733. setParameterMidiChannel(index, stateParameter->midiChannel, true, true);
  734. #endif
  735. }
  736. else
  737. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  738. }
  739. // clear
  740. for (auto it = paramSymbols.begin(); it.valid(); it.next())
  741. {
  742. ParamSymbol* const paramSymbol(*it);
  743. paramSymbol->free();
  744. delete paramSymbol;
  745. }
  746. paramSymbols.clear();
  747. // ---------------------------------------------------------------------
  748. // Part 5 - set chunk data
  749. for (auto it = saveState.customData.begin(); it.valid(); it.next())
  750. {
  751. const StateCustomData* const stateCustomData(*it);
  752. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_CHUNK) == 0)
  753. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  754. }
  755. if (saveState.chunk != nullptr && (fOptions & PLUGIN_OPTION_USE_CHUNKS) != 0)
  756. setChunkData(saveState.chunk);
  757. // ---------------------------------------------------------------------
  758. // Part 6 - set internal stuff
  759. #ifndef BUILD_BRIDGE
  760. setDryWet(saveState.dryWet, true, true);
  761. setVolume(saveState.volume, true, true);
  762. setBalanceLeft(saveState.balanceLeft, true, true);
  763. setBalanceRight(saveState.balanceRight, true, true);
  764. setPanning(saveState.panning, true, true);
  765. setCtrlChannel(saveState.ctrlChannel, true, true);
  766. #endif
  767. setActive(saveState.active, true, true);
  768. }
  769. bool CarlaPlugin::saveStateToFile(const char* const filename)
  770. {
  771. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  772. CARLA_ASSERT(filename != nullptr);
  773. QFile file(filename);
  774. if (! file.open(QIODevice::WriteOnly | QIODevice::Text))
  775. return false;
  776. QTextStream out(&file);
  777. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  778. out << "<!DOCTYPE CARLA-PRESET>\n";
  779. out << "<CARLA-PRESET VERSION='1.0'>\n";
  780. out << getXMLFromSaveState(getSaveState());
  781. out << "</CARLA-PRESET>\n";
  782. file.close();
  783. return true;
  784. }
  785. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  786. {
  787. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  788. CARLA_ASSERT(filename != nullptr);
  789. QFile file(filename);
  790. if (! file.open(QIODevice::ReadOnly | QIODevice::Text))
  791. return false;
  792. QDomDocument xml;
  793. xml.setContent(file.readAll());
  794. file.close();
  795. QDomNode xmlNode(xml.documentElement());
  796. if (xmlNode.toElement().tagName() != "CARLA-PRESET")
  797. {
  798. kData->engine->setLastError("Not a valid Carla preset file");
  799. return false;
  800. }
  801. loadSaveState(getSaveStateDictFromXML(xmlNode));
  802. return true;
  803. }
  804. // -------------------------------------------------------------------
  805. // Set data (internal stuff)
  806. void CarlaPlugin::setId(const unsigned int newId)
  807. {
  808. fId = newId;
  809. }
  810. void CarlaPlugin::setName(const char* const newName)
  811. {
  812. fName = newName;
  813. }
  814. void CarlaPlugin::setOption(const unsigned int option, const bool yesNo)
  815. {
  816. CARLA_ASSERT(availableOptions() & option);
  817. if (yesNo)
  818. fOptions |= option;
  819. else
  820. fOptions &= ~option;
  821. kData->saveSetting(option, yesNo);
  822. }
  823. void CarlaPlugin::setEnabled(const bool yesNo)
  824. {
  825. fEnabled = yesNo;
  826. if (! yesNo)
  827. {
  828. kData->masterMutex.lock();
  829. kData->masterMutex.unlock();
  830. }
  831. }
  832. // -------------------------------------------------------------------
  833. // Set data (internal stuff)
  834. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback)
  835. {
  836. #ifndef BUILD_BRIDGE
  837. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  838. #endif
  839. if (kData->active == active)
  840. return;
  841. {
  842. const ScopedSingleProcessLocker spl(this, true);
  843. if (active)
  844. activate();
  845. else
  846. deactivate();
  847. }
  848. kData->active = active;
  849. #ifndef BUILD_BRIDGE
  850. const float value(active ? 1.0f : 0.0f);
  851. if (sendOsc)
  852. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_ACTIVE, value);
  853. if (sendCallback)
  854. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_ACTIVE, 0, value, nullptr);
  855. #else
  856. return;
  857. // unused
  858. (void)sendOsc;
  859. (void)sendCallback;
  860. #endif
  861. }
  862. #ifndef BUILD_BRIDGE
  863. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback)
  864. {
  865. CARLA_ASSERT(value >= 0.0f && value <= 1.0f);
  866. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  867. if (kData->postProc.dryWet == fixedValue)
  868. return;
  869. kData->postProc.dryWet = fixedValue;
  870. if (sendOsc)
  871. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_DRYWET, fixedValue);
  872. if (sendCallback)
  873. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  874. }
  875. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback)
  876. {
  877. CARLA_ASSERT(value >= 0.0f && value <= 1.27f);
  878. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  879. if (kData->postProc.volume == fixedValue)
  880. return;
  881. kData->postProc.volume = fixedValue;
  882. if (sendOsc)
  883. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_VOLUME, fixedValue);
  884. if (sendCallback)
  885. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  886. }
  887. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback)
  888. {
  889. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  890. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  891. if (kData->postProc.balanceLeft == fixedValue)
  892. return;
  893. kData->postProc.balanceLeft = fixedValue;
  894. if (sendOsc)
  895. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_LEFT, fixedValue);
  896. if (sendCallback)
  897. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  898. }
  899. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback)
  900. {
  901. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  902. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  903. if (kData->postProc.balanceRight == fixedValue)
  904. return;
  905. kData->postProc.balanceRight = fixedValue;
  906. if (sendOsc)
  907. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_RIGHT, fixedValue);
  908. if (sendCallback)
  909. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  910. }
  911. void CarlaPlugin::setPanning(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.panning == fixedValue)
  916. return;
  917. kData->postProc.panning = fixedValue;
  918. if (sendOsc)
  919. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_PANNING, fixedValue);
  920. if (sendCallback)
  921. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_PANNING, 0, fixedValue, nullptr);
  922. }
  923. #endif
  924. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback)
  925. {
  926. #ifndef BUILD_BRIDGE
  927. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  928. #endif
  929. CARLA_ASSERT_INT(channel >= -1 && channel < MAX_MIDI_CHANNELS, channel);
  930. if (kData->ctrlChannel == channel)
  931. return;
  932. kData->ctrlChannel = channel;
  933. #ifndef BUILD_BRIDGE
  934. const float ctrlf(channel);
  935. if (sendOsc)
  936. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_CTRL_CHANNEL, ctrlf);
  937. if (sendCallback)
  938. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_CTRL_CHANNEL, 0, ctrlf, nullptr);
  939. if (fHints & PLUGIN_IS_BRIDGE)
  940. osc_send_control(&kData->osc.data, PARAMETER_CTRL_CHANNEL, ctrlf);
  941. #else
  942. return;
  943. // unused
  944. (void)sendOsc;
  945. (void)sendCallback;
  946. #endif
  947. }
  948. // -------------------------------------------------------------------
  949. // Set data (plugin-specific stuff)
  950. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  951. {
  952. CARLA_ASSERT(parameterId < kData->param.count);
  953. #ifdef BUILD_BRIDGE
  954. CARLA_ASSERT(! sendGui); // this should never happen
  955. #endif
  956. #ifndef BUILD_BRIDGE
  957. if (sendGui)
  958. uiParameterChange(parameterId, value);
  959. if (sendOsc)
  960. kData->engine->osc_send_control_set_parameter_value(fId, parameterId, value);
  961. #endif
  962. if (sendCallback)
  963. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, parameterId, 0, value, nullptr);
  964. #ifdef BUILD_BRIDGE
  965. return;
  966. // unused
  967. (void)sendGui;
  968. (void)sendOsc;
  969. #endif
  970. }
  971. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  972. {
  973. CARLA_ASSERT(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL);
  974. if (rindex <= PARAMETER_MAX)
  975. return;
  976. if (rindex == PARAMETER_NULL)
  977. return;
  978. if (rindex == PARAMETER_ACTIVE)
  979. return setActive((value > 0.0f), sendOsc, sendCallback);
  980. if (rindex == PARAMETER_CTRL_CHANNEL)
  981. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  982. #ifndef BUILD_BRIDGE
  983. if (rindex == PARAMETER_DRYWET)
  984. return setDryWet(value, sendOsc, sendCallback);
  985. if (rindex == PARAMETER_VOLUME)
  986. return setVolume(value, sendOsc, sendCallback);
  987. if (rindex == PARAMETER_BALANCE_LEFT)
  988. return setBalanceLeft(value, sendOsc, sendCallback);
  989. if (rindex == PARAMETER_BALANCE_RIGHT)
  990. return setBalanceRight(value, sendOsc, sendCallback);
  991. if (rindex == PARAMETER_PANNING)
  992. return setPanning(value, sendOsc, sendCallback);
  993. #endif
  994. for (uint32_t i=0; i < kData->param.count; ++i)
  995. {
  996. if (kData->param.data[i].rindex == rindex)
  997. {
  998. if (getParameterValue(i) != value)
  999. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. #ifndef BUILD_BRIDGE
  1005. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback)
  1006. {
  1007. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  1008. CARLA_ASSERT(parameterId < kData->param.count);
  1009. CARLA_ASSERT_INT(channel < MAX_MIDI_CHANNELS, channel);
  1010. if (channel >= MAX_MIDI_CHANNELS)
  1011. channel = MAX_MIDI_CHANNELS;
  1012. kData->param.data[parameterId].midiChannel = channel;
  1013. #ifndef BUILD_BRIDGE
  1014. if (sendOsc)
  1015. kData->engine->osc_send_control_set_parameter_midi_channel(fId, parameterId, channel);
  1016. if (sendCallback)
  1017. kData->engine->callback(CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, fId, parameterId, channel, 0.0f, nullptr);
  1018. if (fHints & PLUGIN_IS_BRIDGE)
  1019. {} // TODO
  1020. #else
  1021. return;
  1022. // unused
  1023. (void)sendOsc;
  1024. (void)sendCallback;
  1025. #endif
  1026. }
  1027. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback)
  1028. {
  1029. CARLA_ASSERT(sendOsc || sendCallback); // never call this from RT
  1030. CARLA_ASSERT(parameterId < kData->param.count);
  1031. CARLA_ASSERT_INT(cc >= -1 && cc <= 0x5F, cc);
  1032. if (cc < -1 || cc > 0x5F)
  1033. cc = -1;
  1034. kData->param.data[parameterId].midiCC = cc;
  1035. #ifndef BUILD_BRIDGE
  1036. if (sendOsc)
  1037. kData->engine->osc_send_control_set_parameter_midi_cc(fId, parameterId, cc);
  1038. if (sendCallback)
  1039. kData->engine->callback(CALLBACK_PARAMETER_MIDI_CC_CHANGED, fId, parameterId, cc, 0.0f, nullptr);
  1040. if (fHints & PLUGIN_IS_BRIDGE)
  1041. {} // TODO
  1042. #else
  1043. return;
  1044. // unused
  1045. (void)sendOsc;
  1046. (void)sendCallback;
  1047. #endif
  1048. }
  1049. #endif
  1050. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  1051. {
  1052. CARLA_ASSERT(type != nullptr);
  1053. CARLA_ASSERT(key != nullptr);
  1054. CARLA_ASSERT(value != nullptr);
  1055. #ifdef BUILD_BRIDGE
  1056. CARLA_ASSERT(! sendGui); // this should never happen
  1057. #endif
  1058. if (type == nullptr)
  1059. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is null", type, key, value, bool2str(sendGui));
  1060. if (key == nullptr)
  1061. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  1062. if (value == nullptr)
  1063. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  1064. bool saveData = true;
  1065. if (std::strcmp(type, CUSTOM_DATA_STRING) == 0)
  1066. {
  1067. // Ignore some keys
  1068. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  1069. saveData = false;
  1070. //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)
  1071. // saveData = false;
  1072. }
  1073. if (! saveData)
  1074. return;
  1075. // Check if we already have this key
  1076. for (auto it = kData->custom.begin(); it.valid(); it.next())
  1077. {
  1078. CustomData& cData(*it);
  1079. CARLA_ASSERT(cData.type != nullptr);
  1080. CARLA_ASSERT(cData.key != nullptr);
  1081. CARLA_ASSERT(cData.value != nullptr);
  1082. if (cData.type == nullptr)
  1083. return;
  1084. if (cData.key == nullptr)
  1085. return;
  1086. if (std::strcmp(cData.key, key) == 0)
  1087. {
  1088. if (cData.value != nullptr)
  1089. delete[] cData.value;
  1090. cData.value = carla_strdup(value);
  1091. return;
  1092. }
  1093. }
  1094. // Otherwise store it
  1095. CustomData newData;
  1096. newData.type = carla_strdup(type);
  1097. newData.key = carla_strdup(key);
  1098. newData.value = carla_strdup(value);
  1099. kData->custom.append(newData);
  1100. }
  1101. void CarlaPlugin::setChunkData(const char* const stringData)
  1102. {
  1103. CARLA_ASSERT(stringData != nullptr);
  1104. CARLA_ASSERT(false); // this should never happen
  1105. return;
  1106. // unused
  1107. (void)stringData;
  1108. }
  1109. void CarlaPlugin::setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1110. {
  1111. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->prog.count));
  1112. #ifdef BUILD_BRIDGE
  1113. CARLA_ASSERT(! sendGui); // this should never happen
  1114. #endif
  1115. if (index > static_cast<int32_t>(kData->prog.count))
  1116. return;
  1117. const int32_t fixedIndex = carla_fixValue<int32_t>(-1, kData->prog.count, index);
  1118. kData->prog.current = fixedIndex;
  1119. // Change default parameter values
  1120. if (fixedIndex >= 0)
  1121. {
  1122. #ifndef BUILD_BRIDGE
  1123. if (sendGui)
  1124. uiProgramChange(fixedIndex);
  1125. #endif
  1126. for (uint32_t i=0; i < kData->param.count; ++i)
  1127. {
  1128. // FIXME?
  1129. kData->param.ranges[i].def = getParameterValue(i);
  1130. kData->param.ranges[i].fixDefault();
  1131. #ifndef BUILD_BRIDGE
  1132. if (sendOsc)
  1133. {
  1134. kData->engine->osc_send_control_set_default_value(fId, i, kData->param.ranges[i].def);
  1135. kData->engine->osc_send_control_set_parameter_value(fId, i, kData->param.ranges[i].def);
  1136. }
  1137. #endif
  1138. }
  1139. }
  1140. #ifndef BUILD_BRIDGE
  1141. if (sendOsc)
  1142. kData->engine->osc_send_control_set_program(fId, fixedIndex);
  1143. #endif
  1144. if (sendCallback)
  1145. kData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, fixedIndex, 0, 0.0f, nullptr);
  1146. #ifdef BUILD_BRIDGE
  1147. return;
  1148. // unused
  1149. (void)sendGui;
  1150. (void)sendOsc;
  1151. #endif
  1152. }
  1153. void CarlaPlugin::setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1154. {
  1155. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  1156. #ifdef BUILD_BRIDGE
  1157. CARLA_ASSERT(! sendGui); // this should never happen
  1158. #endif
  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. #ifndef BUILD_BRIDGE
  1166. if (sendGui)
  1167. uiMidiProgramChange(fixedIndex);
  1168. #endif
  1169. // Change default parameter values (sound banks never change defaults)
  1170. if (type() != PLUGIN_GIG && type() != PLUGIN_SF2 && type() != PLUGIN_SFZ)
  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. #ifndef BUILD_BRIDGE
  1178. if (sendOsc)
  1179. {
  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. }
  1183. #endif
  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. #ifdef BUILD_BRIDGE
  1194. return;
  1195. // unused
  1196. (void)sendGui;
  1197. (void)sendOsc;
  1198. #endif
  1199. }
  1200. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1201. {
  1202. for (uint32_t i=0; i < kData->midiprog.count; ++i)
  1203. {
  1204. if (kData->midiprog.data[i].bank == bank && kData->midiprog.data[i].program == program)
  1205. return setMidiProgram(i, sendGui, sendOsc, sendCallback);
  1206. }
  1207. }
  1208. // -------------------------------------------------------------------
  1209. // Set gui stuff
  1210. void CarlaPlugin::showGui(const bool yesNo)
  1211. {
  1212. CARLA_ASSERT(false);
  1213. return;
  1214. // unused
  1215. (void)yesNo;
  1216. }
  1217. void CarlaPlugin::idleGui()
  1218. {
  1219. if (! fEnabled)
  1220. return;
  1221. if (fHints & PLUGIN_HAS_SINGLE_THREAD)
  1222. {
  1223. // Process postponed events
  1224. postRtEventsRun();
  1225. // Update parameter outputs
  1226. for (uint32_t i=0; i < kData->param.count; ++i)
  1227. {
  1228. if (kData->param.data[i].type == PARAMETER_OUTPUT)
  1229. uiParameterChange(i, getParameterValue(i));
  1230. }
  1231. }
  1232. }
  1233. // -------------------------------------------------------------------
  1234. // Plugin state
  1235. void CarlaPlugin::reload()
  1236. {
  1237. }
  1238. void CarlaPlugin::reloadPrograms(const bool)
  1239. {
  1240. }
  1241. // -------------------------------------------------------------------
  1242. // Plugin processing
  1243. void CarlaPlugin::activate()
  1244. {
  1245. CARLA_ASSERT(! kData->active);
  1246. }
  1247. void CarlaPlugin::deactivate()
  1248. {
  1249. CARLA_ASSERT(kData->active);
  1250. }
  1251. void CarlaPlugin::process(float** const, float** const, const uint32_t)
  1252. {
  1253. }
  1254. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1255. {
  1256. }
  1257. void CarlaPlugin::sampleRateChanged(const double)
  1258. {
  1259. }
  1260. bool CarlaPlugin::tryLock()
  1261. {
  1262. return kData->masterMutex.tryLock();
  1263. }
  1264. void CarlaPlugin::unlock()
  1265. {
  1266. kData->masterMutex.unlock();
  1267. }
  1268. // -------------------------------------------------------------------
  1269. // Plugin buffers
  1270. void CarlaPlugin::initBuffers()
  1271. {
  1272. kData->audioIn.initBuffers(kData->engine);
  1273. kData->audioOut.initBuffers(kData->engine);
  1274. kData->event.initBuffers(kData->engine);
  1275. }
  1276. void CarlaPlugin::clearBuffers()
  1277. {
  1278. kData->clearBuffers();
  1279. }
  1280. // -------------------------------------------------------------------
  1281. // OSC stuff
  1282. void CarlaPlugin::registerToOscClient()
  1283. {
  1284. #ifdef BUILD_BRIDGE
  1285. if (! kData->engine->isOscBridgeRegistered())
  1286. return;
  1287. #else
  1288. if (! kData->engine->isOscControlRegistered())
  1289. return;
  1290. #endif
  1291. #ifndef BUILD_BRIDGE
  1292. kData->engine->osc_send_control_add_plugin_start(fId, fName);
  1293. #endif
  1294. // Base data
  1295. {
  1296. char bufName[STR_MAX+1] = { '\0' };
  1297. char bufLabel[STR_MAX+1] = { '\0' };
  1298. char bufMaker[STR_MAX+1] = { '\0' };
  1299. char bufCopyright[STR_MAX+1] = { '\0' };
  1300. getRealName(bufName);
  1301. getLabel(bufLabel);
  1302. getMaker(bufMaker);
  1303. getCopyright(bufCopyright);
  1304. #ifdef BUILD_BRIDGE
  1305. kData->engine->osc_send_bridge_plugin_info(category(), fHints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1306. #else
  1307. kData->engine->osc_send_control_set_plugin_data(fId, type(), category(), fHints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1308. #endif
  1309. }
  1310. // Base count
  1311. {
  1312. uint32_t cIns, cOuts, cTotals;
  1313. getParameterCountInfo(&cIns, &cOuts, &cTotals);
  1314. #ifdef BUILD_BRIDGE
  1315. kData->engine->osc_send_bridge_audio_count(audioInCount(), audioOutCount(), audioInCount() + audioOutCount());
  1316. kData->engine->osc_send_bridge_midi_count(midiInCount(), midiOutCount(), midiInCount() + midiOutCount());
  1317. kData->engine->osc_send_bridge_parameter_count(cIns, cOuts, cTotals);
  1318. #else
  1319. kData->engine->osc_send_control_set_plugin_ports(fId, audioInCount(), audioOutCount(), midiInCount(), midiOutCount(), cIns, cOuts, cTotals);
  1320. #endif
  1321. }
  1322. // Plugin Parameters
  1323. if (kData->param.count > 0 && kData->param.count < kData->engine->getOptions().maxParameters)
  1324. {
  1325. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1326. for (uint32_t i=0; i < kData->param.count; ++i)
  1327. {
  1328. carla_fill<char>(bufName, STR_MAX, '\0');
  1329. carla_fill<char>(bufUnit, STR_MAX, '\0');
  1330. getParameterName(i, bufName);
  1331. getParameterUnit(i, bufUnit);
  1332. const ParameterData& paramData(kData->param.data[i]);
  1333. const ParameterRanges& paramRanges(kData->param.ranges[i]);
  1334. #ifdef BUILD_BRIDGE
  1335. kData->engine->osc_send_bridge_parameter_info(i, bufName, bufUnit);
  1336. kData->engine->osc_send_bridge_parameter_data(i, paramData.type, paramData.rindex, paramData.hints, paramData.midiChannel, paramData.midiCC);
  1337. kData->engine->osc_send_bridge_parameter_ranges(i, paramRanges.def, paramRanges.min, paramRanges.max, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1338. kData->engine->osc_send_bridge_set_parameter_value(i, getParameterValue(i));
  1339. #else
  1340. kData->engine->osc_send_control_set_parameter_data(fId, i, paramData.type, paramData.hints, bufName, bufUnit, getParameterValue(i));
  1341. kData->engine->osc_send_control_set_parameter_ranges(fId, i, paramRanges.min, paramRanges.max, paramRanges.def, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1342. kData->engine->osc_send_control_set_parameter_midi_cc(fId, i, paramData.midiCC);
  1343. kData->engine->osc_send_control_set_parameter_midi_channel(fId, i, paramData.midiChannel);
  1344. #endif
  1345. }
  1346. }
  1347. // Programs
  1348. if (kData->prog.count > 0)
  1349. {
  1350. #ifdef BUILD_BRIDGE
  1351. kData->engine->osc_send_bridge_program_count(kData->prog.count);
  1352. for (uint32_t i=0; i < kData->prog.count; ++i)
  1353. kData->engine->osc_send_bridge_program_info(i, kData->prog.names[i]);
  1354. kData->engine->osc_send_bridge_set_program(kData->prog.current);
  1355. #else
  1356. kData->engine->osc_send_control_set_program_count(fId, kData->prog.count);
  1357. for (uint32_t i=0; i < kData->prog.count; ++i)
  1358. kData->engine->osc_send_control_set_program_name(fId, i, kData->prog.names[i]);
  1359. kData->engine->osc_send_control_set_program(fId, kData->prog.current);
  1360. #endif
  1361. }
  1362. // MIDI Programs
  1363. if (kData->midiprog.count > 0)
  1364. {
  1365. #ifdef BUILD_BRIDGE
  1366. kData->engine->osc_send_bridge_midi_program_count(kData->midiprog.count);
  1367. for (uint32_t i=0; i < kData->midiprog.count; ++i)
  1368. {
  1369. const MidiProgramData& mpData(kData->midiprog.data[i]);
  1370. kData->engine->osc_send_bridge_midi_program_info(i, mpData.bank, mpData.program, mpData.name);
  1371. }
  1372. kData->engine->osc_send_bridge_set_midi_program(kData->midiprog.current);
  1373. #else
  1374. kData->engine->osc_send_control_set_midi_program_count(fId, kData->midiprog.count);
  1375. for (uint32_t i=0; i < kData->midiprog.count; ++i)
  1376. {
  1377. const MidiProgramData& mpData(kData->midiprog.data[i]);
  1378. kData->engine->osc_send_control_set_midi_program_data(fId, i, mpData.bank, mpData.program, mpData.name);
  1379. }
  1380. kData->engine->osc_send_control_set_midi_program(fId, kData->midiprog.current);
  1381. #endif
  1382. }
  1383. #ifndef BUILD_BRIDGE
  1384. kData->engine->osc_send_control_add_plugin_end(fId);
  1385. // Internal Parameters
  1386. {
  1387. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_DRYWET, kData->postProc.dryWet);
  1388. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_VOLUME, kData->postProc.volume);
  1389. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_LEFT, kData->postProc.balanceLeft);
  1390. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_RIGHT, kData->postProc.balanceRight);
  1391. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_PANNING, kData->postProc.panning);
  1392. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_CTRL_CHANNEL, kData->ctrlChannel);
  1393. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_ACTIVE, kData->active ? 1.0f : 0.0f);
  1394. }
  1395. #endif
  1396. }
  1397. void CarlaPlugin::updateOscData(const lo_address& source, const char* const url)
  1398. {
  1399. // FIXME - remove debug prints later
  1400. carla_stdout("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  1401. kData->osc.data.free();
  1402. const int proto = lo_address_get_protocol(source);
  1403. {
  1404. const char* host = lo_address_get_hostname(source);
  1405. const char* port = lo_address_get_port(source);
  1406. kData->osc.data.source = lo_address_new_with_proto(proto, host, port);
  1407. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1408. }
  1409. {
  1410. char* host = lo_url_get_hostname(url);
  1411. char* port = lo_url_get_port(url);
  1412. kData->osc.data.path = carla_strdup_free(lo_url_get_path(url));
  1413. kData->osc.data.target = lo_address_new_with_proto(proto, host, port);
  1414. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, kData->osc.data.path);
  1415. std::free(host);
  1416. std::free(port);
  1417. }
  1418. #ifndef BUILD_BRIDGE
  1419. if (fHints & PLUGIN_IS_BRIDGE)
  1420. return;
  1421. #endif
  1422. osc_send_sample_rate(&kData->osc.data, kData->engine->getSampleRate());
  1423. for (auto it = kData->custom.begin(); it.valid(); it.next())
  1424. {
  1425. const CustomData& cData(*it);
  1426. CARLA_ASSERT(cData.type != nullptr);
  1427. CARLA_ASSERT(cData.key != nullptr);
  1428. CARLA_ASSERT(cData.value != nullptr);
  1429. #ifdef WANT_LV2
  1430. if (type() == PLUGIN_LV2)
  1431. osc_send_lv2_transfer_event(&kData->osc.data, 0, cData.type, cData.value);
  1432. else
  1433. #endif
  1434. if (std::strcmp(cData.type, CUSTOM_DATA_STRING) == 0)
  1435. osc_send_configure(&kData->osc.data, cData.key, cData.value);
  1436. }
  1437. if (kData->prog.current >= 0)
  1438. osc_send_program(&kData->osc.data, kData->prog.current);
  1439. if (kData->midiprog.current >= 0)
  1440. {
  1441. const MidiProgramData& curMidiProg(kData->midiprog.getCurrent());
  1442. if (type() == PLUGIN_DSSI)
  1443. osc_send_program(&kData->osc.data, curMidiProg.bank, curMidiProg.program);
  1444. else
  1445. osc_send_midi_program(&kData->osc.data, curMidiProg.bank, curMidiProg.program);
  1446. }
  1447. for (uint32_t i=0; i < kData->param.count; ++i)
  1448. osc_send_control(&kData->osc.data, kData->param.data[i].rindex, getParameterValue(i));
  1449. carla_stdout("CarlaPlugin::updateOscData() - done");
  1450. }
  1451. void CarlaPlugin::freeOscData()
  1452. {
  1453. kData->osc.data.free();
  1454. }
  1455. bool CarlaPlugin::waitForOscGuiShow()
  1456. {
  1457. carla_stdout("CarlaPlugin::waitForOscGuiShow()");
  1458. uint i=0, oscUiTimeout = kData->engine->getOptions().oscUiTimeout;
  1459. // wait for UI 'update' call
  1460. for (; i < oscUiTimeout/100; ++i)
  1461. {
  1462. if (kData->osc.data.target != nullptr)
  1463. {
  1464. carla_stdout("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  1465. osc_send_show(&kData->osc.data);
  1466. return true;
  1467. }
  1468. else
  1469. carla_msleep(100);
  1470. }
  1471. carla_stdout("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  1472. return false;
  1473. }
  1474. // -------------------------------------------------------------------
  1475. // MIDI events
  1476. #ifndef BUILD_BRIDGE
  1477. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1478. {
  1479. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1480. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1481. CARLA_ASSERT(velo < MAX_MIDI_VALUE);
  1482. if (! kData->active)
  1483. return;
  1484. ExternalMidiNote extNote;
  1485. extNote.channel = channel;
  1486. extNote.note = note;
  1487. extNote.velo = velo;
  1488. kData->extNotes.append(extNote);
  1489. if (sendGui)
  1490. {
  1491. if (velo > 0)
  1492. uiNoteOn(channel, note, velo);
  1493. else
  1494. uiNoteOff(channel, note);
  1495. }
  1496. if (sendOsc)
  1497. {
  1498. if (velo > 0)
  1499. kData->engine->osc_send_control_note_on(fId, channel, note, velo);
  1500. else
  1501. kData->engine->osc_send_control_note_off(fId, channel, note);
  1502. }
  1503. if (sendCallback)
  1504. kData->engine->callback((velo > 0) ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, fId, channel, note, velo, nullptr);
  1505. }
  1506. #endif
  1507. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1508. {
  1509. if (kData->ctrlChannel < 0 || kData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1510. return;
  1511. PluginPostRtEvent postEvent;
  1512. postEvent.type = kPluginPostRtEventNoteOff;
  1513. postEvent.value1 = kData->ctrlChannel;
  1514. postEvent.value2 = 0;
  1515. postEvent.value3 = 0.0f;
  1516. for (unsigned short i=0; i < MAX_MIDI_NOTE; ++i)
  1517. {
  1518. postEvent.value2 = i;
  1519. kData->postRtEvents.appendRT(postEvent);
  1520. }
  1521. }
  1522. // -------------------------------------------------------------------
  1523. // Post-poned events
  1524. void CarlaPlugin::postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3)
  1525. {
  1526. PluginPostRtEvent event;
  1527. event.type = type;
  1528. event.value1 = value1;
  1529. event.value2 = value2;
  1530. event.value3 = value3;
  1531. kData->postRtEvents.appendRT(event);
  1532. }
  1533. void CarlaPlugin::postRtEventsRun()
  1534. {
  1535. const CarlaMutex::ScopedLocker sl(&kData->postRtEvents.mutex);
  1536. while (! kData->postRtEvents.data.isEmpty())
  1537. {
  1538. const PluginPostRtEvent& event(kData->postRtEvents.data.getFirst(true));
  1539. switch (event.type)
  1540. {
  1541. case kPluginPostRtEventNull:
  1542. break;
  1543. case kPluginPostRtEventDebug:
  1544. #ifndef BUILD_BRIDGE
  1545. kData->engine->callback(CALLBACK_DEBUG, fId, event.value1, event.value2, event.value3, nullptr);
  1546. #endif
  1547. break;
  1548. case kPluginPostRtEventParameterChange:
  1549. // Update UI
  1550. if (event.value1 >= 0)
  1551. uiParameterChange(event.value1, event.value3);
  1552. #ifndef BUILD_BRIDGE
  1553. if (event.value2 != 1)
  1554. {
  1555. // Update OSC control client
  1556. if (kData->engine->isOscControlRegistered())
  1557. kData->engine->osc_send_control_set_parameter_value(fId, event.value1, event.value3);
  1558. // Update Host
  1559. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, event.value1, 0, event.value3, nullptr);
  1560. }
  1561. #endif
  1562. break;
  1563. case kPluginPostRtEventProgramChange:
  1564. // Update UI
  1565. if (event.value1 >= 0)
  1566. uiProgramChange(event.value1);
  1567. #ifndef BUILD_BRIDGE
  1568. // Update OSC control client
  1569. if (kData->engine->isOscControlRegistered())
  1570. {
  1571. kData->engine->osc_send_control_set_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. // Update Host
  1576. kData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, event.value1, 0, 0.0f, nullptr);
  1577. #endif
  1578. break;
  1579. case kPluginPostRtEventMidiProgramChange:
  1580. // Update UI
  1581. if (event.value1 >= 0)
  1582. uiMidiProgramChange(event.value1);
  1583. #ifndef BUILD_BRIDGE
  1584. // Update OSC control client
  1585. if (kData->engine->isOscControlRegistered())
  1586. {
  1587. kData->engine->osc_send_control_set_midi_program(fId, event.value1);
  1588. for (uint32_t j=0; j < kData->param.count; ++j)
  1589. kData->engine->osc_send_control_set_default_value(fId, j, kData->param.ranges[j].def);
  1590. }
  1591. // Update Host
  1592. kData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, event.value1, 0, 0.0f, nullptr);
  1593. #endif
  1594. break;
  1595. case kPluginPostRtEventNoteOn:
  1596. // Update UI
  1597. uiNoteOn(event.value1, event.value2, int(event.value3));
  1598. #ifndef BUILD_BRIDGE
  1599. // Update OSC control client
  1600. if (kData->engine->isOscControlRegistered())
  1601. kData->engine->osc_send_control_note_on(fId, event.value1, event.value2, int(event.value3));
  1602. // Update Host
  1603. kData->engine->callback(CALLBACK_NOTE_ON, fId, event.value1, event.value2, int(event.value3), nullptr);
  1604. #endif
  1605. break;
  1606. case kPluginPostRtEventNoteOff:
  1607. // Update UI
  1608. uiNoteOff(event.value1, event.value2);
  1609. #ifndef BUILD_BRIDGE
  1610. // Update OSC control client
  1611. if (kData->engine->isOscControlRegistered())
  1612. kData->engine->osc_send_control_note_off(fId, event.value1, event.value2);
  1613. // Update Host
  1614. kData->engine->callback(CALLBACK_NOTE_OFF, fId, event.value1, event.value2, 0.0f, nullptr);
  1615. #endif
  1616. break;
  1617. }
  1618. }
  1619. }
  1620. // -------------------------------------------------------------------
  1621. // Post-poned UI Stuff
  1622. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value)
  1623. {
  1624. CARLA_ASSERT(index < parameterCount());
  1625. return;
  1626. // unused
  1627. (void)index;
  1628. (void)value;
  1629. }
  1630. void CarlaPlugin::uiProgramChange(const uint32_t index)
  1631. {
  1632. CARLA_ASSERT(index < programCount());
  1633. return;
  1634. // unused
  1635. (void)index;
  1636. }
  1637. void CarlaPlugin::uiMidiProgramChange(const uint32_t index)
  1638. {
  1639. CARLA_ASSERT(index < midiProgramCount());
  1640. return;
  1641. // unused
  1642. (void)index;
  1643. }
  1644. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1645. {
  1646. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1647. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1648. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1649. return;
  1650. // unused
  1651. (void)channel;
  1652. (void)note;
  1653. (void)velo;
  1654. }
  1655. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note)
  1656. {
  1657. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1658. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1659. return;
  1660. // unused
  1661. (void)channel;
  1662. (void)note;
  1663. }
  1664. // -------------------------------------------------------------------
  1665. // Helpers
  1666. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const
  1667. {
  1668. return kData->event.portIn;
  1669. }
  1670. // -------------------------------------------------------------------
  1671. // Scoped Disabler
  1672. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin)
  1673. : kPlugin(plugin)
  1674. {
  1675. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1676. CARLA_ASSERT(plugin != nullptr);
  1677. CARLA_ASSERT(plugin->kData != nullptr);
  1678. CARLA_ASSERT(plugin->kData->client != nullptr);
  1679. if (plugin == nullptr)
  1680. return;
  1681. if (plugin->kData == nullptr)
  1682. return;
  1683. if (plugin->kData->client == nullptr)
  1684. return;
  1685. plugin->kData->masterMutex.lock();
  1686. if (plugin->fEnabled)
  1687. plugin->fEnabled = false;
  1688. if (plugin->kData->client->isActive())
  1689. plugin->kData->client->deactivate();
  1690. }
  1691. CarlaPlugin::ScopedDisabler::~ScopedDisabler()
  1692. {
  1693. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1694. CARLA_ASSERT(kPlugin != nullptr);
  1695. CARLA_ASSERT(kPlugin->kData != nullptr);
  1696. CARLA_ASSERT(kPlugin->kData->client != nullptr);
  1697. if (kPlugin == nullptr)
  1698. return;
  1699. if (kPlugin->kData == nullptr)
  1700. return;
  1701. if (kPlugin->kData->client == nullptr)
  1702. return;
  1703. kPlugin->fEnabled = true;
  1704. kPlugin->kData->client->activate();
  1705. kPlugin->kData->masterMutex.unlock();
  1706. }
  1707. // -------------------------------------------------------------------
  1708. // Scoped Process Locker
  1709. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block)
  1710. : kPlugin(plugin),
  1711. kBlock(block)
  1712. {
  1713. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1714. CARLA_ASSERT(kPlugin != nullptr && kPlugin->kData != nullptr);
  1715. if (kPlugin == nullptr)
  1716. return;
  1717. if (kPlugin->kData == nullptr)
  1718. return;
  1719. if (kBlock)
  1720. plugin->kData->singleMutex.lock();
  1721. }
  1722. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker()
  1723. {
  1724. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1725. CARLA_ASSERT(kPlugin != nullptr && kPlugin->kData != nullptr);
  1726. if (kPlugin == nullptr)
  1727. return;
  1728. if (kPlugin->kData == nullptr)
  1729. return;
  1730. if (kBlock)
  1731. {
  1732. #ifndef BUILD_BRIDGE
  1733. if (kPlugin->kData->singleMutex.wasTryLockCalled())
  1734. kPlugin->kData->needsReset = true;
  1735. #endif
  1736. kPlugin->kData->singleMutex.unlock();
  1737. }
  1738. }
  1739. // -------------------------------------------------------------------
  1740. CARLA_BACKEND_END_NAMESPACE