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.

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