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.

2220 lines
63KB

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