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.

2011 lines
57KB

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