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.

2258 lines
65KB

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