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.

1979 lines
56KB

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