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.

2269 lines
65KB

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