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.

2233 lines
64KB

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