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.

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