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.

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