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.

1652 lines
46KB

  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 <QtGui/QtEvents>
  21. // TODO - save&load options
  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. // 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. kData->ctrlChannel = 0;
  61. break;
  62. case PROCESS_MODE_CONTINUOUS_RACK:
  63. CARLA_ASSERT(id < MAX_RACK_PLUGINS && id < MAX_MIDI_CHANNELS);
  64. if (id < MAX_RACK_PLUGINS && id < MAX_MIDI_CHANNELS)
  65. kData->ctrlChannel = id;
  66. break;
  67. case PROCESS_MODE_PATCHBAY:
  68. case PROCESS_MODE_BRIDGE:
  69. break;
  70. }
  71. if (engine->getOptions().forceStereo)
  72. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  73. }
  74. CarlaPlugin::~CarlaPlugin()
  75. {
  76. carla_debug("CarlaPlugin::~CarlaPlugin()");
  77. // Remove client and ports
  78. if (kData->client != nullptr)
  79. {
  80. if (kData->client->isActive())
  81. kData->client->deactivate();
  82. // can't call virtual functions in destructor
  83. CarlaPlugin::deleteBuffers();
  84. delete kData->client;
  85. }
  86. if (kData->latencyBuffers != nullptr)
  87. {
  88. for (uint32_t i=0; i < kData->audioIn.count; i++)
  89. delete[] kData->latencyBuffers[i];
  90. delete[] kData->latencyBuffers;
  91. }
  92. kData->prog.clear();
  93. kData->midiprog.clear();
  94. kData->custom.clear();
  95. libClose();
  96. delete kData;
  97. }
  98. // -------------------------------------------------------------------
  99. // Information (base)
  100. uint32_t CarlaPlugin::latency() const
  101. {
  102. return kData->latency;
  103. }
  104. // -------------------------------------------------------------------
  105. // Information (count)
  106. uint32_t CarlaPlugin::audioInCount() const
  107. {
  108. return kData->audioIn.count;
  109. }
  110. uint32_t CarlaPlugin::audioOutCount() const
  111. {
  112. return kData->audioOut.count;
  113. }
  114. uint32_t CarlaPlugin::midiInCount() const
  115. {
  116. return (kData->extraHints & PLUGIN_HINT_HAS_MIDI_IN) ? 1 : 0;
  117. }
  118. uint32_t CarlaPlugin::midiOutCount() const
  119. {
  120. return (kData->extraHints & PLUGIN_HINT_HAS_MIDI_OUT) ? 1 : 0;
  121. }
  122. uint32_t CarlaPlugin::parameterCount() const
  123. {
  124. return kData->param.count;
  125. }
  126. uint32_t CarlaPlugin::parameterScalePointCount(const uint32_t parameterId) const
  127. {
  128. CARLA_ASSERT(parameterId < kData->param.count);
  129. return 0;
  130. // unused
  131. (void)parameterId;
  132. }
  133. uint32_t CarlaPlugin::programCount() const
  134. {
  135. return kData->prog.count;
  136. }
  137. uint32_t CarlaPlugin::midiProgramCount() const
  138. {
  139. return kData->midiprog.count;
  140. }
  141. uint32_t CarlaPlugin::customDataCount() const
  142. {
  143. return kData->custom.count();
  144. }
  145. // -------------------------------------------------------------------
  146. // Information (current data)
  147. int32_t CarlaPlugin::currentProgram() const
  148. {
  149. return kData->prog.current;
  150. }
  151. int32_t CarlaPlugin::currentMidiProgram() const
  152. {
  153. return kData->midiprog.current;
  154. }
  155. const ParameterData& CarlaPlugin::parameterData(const uint32_t parameterId) const
  156. {
  157. CARLA_ASSERT(parameterId < kData->param.count);
  158. return (parameterId < kData->param.count) ? kData->param.data[parameterId] : kParameterDataNull;
  159. }
  160. const ParameterRanges& CarlaPlugin::parameterRanges(const uint32_t parameterId) const
  161. {
  162. CARLA_ASSERT(parameterId < kData->param.count);
  163. return (parameterId < kData->param.count) ? kData->param.ranges[parameterId] : kParameterRangesNull;
  164. }
  165. bool CarlaPlugin::parameterIsOutput(const uint32_t parameterId) const
  166. {
  167. CARLA_ASSERT(parameterId < kData->param.count);
  168. return (parameterId < kData->param.count) ? (kData->param.data[parameterId].type == PARAMETER_OUTPUT) : false;
  169. }
  170. const MidiProgramData& CarlaPlugin::midiProgramData(const uint32_t index) const
  171. {
  172. CARLA_ASSERT(index < kData->midiprog.count);
  173. return (index < kData->midiprog.count) ? kData->midiprog.data[index] : kMidiProgramDataNull;
  174. }
  175. const CustomData& CarlaPlugin::customData(const size_t index) const
  176. {
  177. CARLA_ASSERT(index < kData->custom.count());
  178. return (index < kData->custom.count()) ? kData->custom.getAt(index) : kCustomDataNull;
  179. }
  180. int32_t CarlaPlugin::chunkData(void** const dataPtr)
  181. {
  182. CARLA_ASSERT(dataPtr != nullptr);
  183. return 0;
  184. // unused
  185. (void)dataPtr;
  186. }
  187. // -------------------------------------------------------------------
  188. // Information (per-plugin data)
  189. unsigned int CarlaPlugin::availableOptions()
  190. {
  191. return 0x0;
  192. }
  193. float CarlaPlugin::getParameterValue(const uint32_t parameterId)
  194. {
  195. CARLA_ASSERT(parameterId < parameterCount());
  196. return 0.0f;
  197. // unused
  198. (void)parameterId;
  199. }
  200. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  201. {
  202. CARLA_ASSERT(parameterId < parameterCount());
  203. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  204. return 0.0f;
  205. // unused
  206. (void)parameterId;
  207. (void)scalePointId;
  208. }
  209. void CarlaPlugin::getLabel(char* const strBuf)
  210. {
  211. *strBuf = 0;
  212. }
  213. void CarlaPlugin::getMaker(char* const strBuf)
  214. {
  215. *strBuf = 0;
  216. }
  217. void CarlaPlugin::getCopyright(char* const strBuf)
  218. {
  219. *strBuf = 0;
  220. }
  221. void CarlaPlugin::getRealName(char* const strBuf)
  222. {
  223. *strBuf = 0;
  224. }
  225. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf)
  226. {
  227. CARLA_ASSERT(parameterId < parameterCount());
  228. *strBuf = 0;
  229. return;
  230. // unused
  231. (void)parameterId;
  232. }
  233. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf)
  234. {
  235. CARLA_ASSERT(parameterId < parameterCount());
  236. *strBuf = 0;
  237. return;
  238. // unused
  239. (void)parameterId;
  240. }
  241. void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf)
  242. {
  243. CARLA_ASSERT(parameterId < parameterCount());
  244. *strBuf = 0;
  245. return;
  246. // unused
  247. (void)parameterId;
  248. }
  249. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf)
  250. {
  251. CARLA_ASSERT(parameterId < parameterCount());
  252. *strBuf = 0;
  253. return;
  254. // unused
  255. (void)parameterId;
  256. }
  257. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  258. {
  259. CARLA_ASSERT(parameterId < parameterCount());
  260. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  261. *strBuf = 0;
  262. return;
  263. // unused
  264. (void)parameterId;
  265. (void)scalePointId;
  266. }
  267. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf)
  268. {
  269. CARLA_ASSERT(index < kData->prog.count);
  270. CARLA_ASSERT(kData->prog.names[index] != nullptr);
  271. if (index < kData->prog.count && kData->prog.names[index])
  272. std::strncpy(strBuf, kData->prog.names[index], STR_MAX);
  273. else
  274. *strBuf = 0;
  275. }
  276. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf)
  277. {
  278. CARLA_ASSERT(index < kData->midiprog.count);
  279. CARLA_ASSERT(kData->midiprog.data[index].name != nullptr);
  280. if (index < kData->midiprog.count && kData->midiprog.data[index].name)
  281. std::strncpy(strBuf, kData->midiprog.data[index].name, STR_MAX);
  282. else
  283. *strBuf = 0;
  284. }
  285. void CarlaPlugin::getParameterCountInfo(uint32_t* const ins, uint32_t* const outs, uint32_t* const total)
  286. {
  287. CARLA_ASSERT(ins != nullptr);
  288. CARLA_ASSERT(outs != nullptr);
  289. CARLA_ASSERT(total != nullptr);
  290. if (ins == nullptr || outs == nullptr || total == nullptr)
  291. return;
  292. *ins = 0;
  293. *outs = 0;
  294. *total = kData->param.count;
  295. for (uint32_t i=0; i < kData->param.count; i++)
  296. {
  297. if (kData->param.data[i].type == PARAMETER_INPUT)
  298. *ins += 1;
  299. else if (kData->param.data[i].type == PARAMETER_OUTPUT)
  300. *outs += 1;
  301. }
  302. }
  303. // -------------------------------------------------------------------
  304. // Set data (state)
  305. #if 0
  306. const SaveState& CarlaPlugin::getSaveState()
  307. {
  308. static SaveState saveState;
  309. // TODO
  310. return saveState;
  311. }
  312. void CarlaPlugin::loadSaveState(const SaveState& saveState)
  313. {
  314. // TODO
  315. Q_UNUSED(saveState);
  316. }
  317. #endif
  318. // -------------------------------------------------------------------
  319. // Set data (internal stuff)
  320. void CarlaPlugin::setId(const unsigned int id)
  321. {
  322. fId = id;
  323. if (kData->engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK)
  324. {
  325. CARLA_ASSERT(id < MAX_RACK_PLUGINS);
  326. if (id >= MAX_RACK_PLUGINS || kData->ctrlChannel == static_cast<int8_t>(id))
  327. return;
  328. kData->ctrlChannel = id;
  329. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_CTRL_CHANNEL, 0, id, nullptr);
  330. }
  331. }
  332. void CarlaPlugin::setOption(const unsigned int option, const bool yesNo)
  333. {
  334. if (yesNo)
  335. fOptions |= option;
  336. else
  337. fOptions &= ~option;
  338. }
  339. void CarlaPlugin::setEnabled(const bool yesNo)
  340. {
  341. fEnabled = yesNo;
  342. }
  343. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback)
  344. {
  345. kData->active = active;
  346. const float value = active ? 1.0f : 0.0f;
  347. #ifndef BUILD_BRIDGE
  348. if (sendOsc)
  349. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_ACTIVE, value);
  350. #else
  351. // unused
  352. (void)sendOsc;
  353. #endif
  354. if (sendCallback)
  355. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_ACTIVE, 0, value, nullptr);
  356. #ifndef BUILD_BRIDGE
  357. else if (fHints & PLUGIN_IS_BRIDGE)
  358. osc_send_control(&kData->osc.data, PARAMETER_ACTIVE, value);
  359. #endif
  360. }
  361. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback)
  362. {
  363. CARLA_ASSERT(value >= 0.0f && value <= 1.0f);
  364. const float fixedValue = carla_fixValue<float>(0.0f, 1.0f, value);
  365. kData->postProc.dryWet = fixedValue;
  366. #ifndef BUILD_BRIDGE
  367. if (sendOsc)
  368. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_DRYWET, fixedValue);
  369. #else
  370. // unused
  371. (void)sendOsc;
  372. #endif
  373. if (sendCallback)
  374. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  375. #ifndef BUILD_BRIDGE
  376. else if (fHints & PLUGIN_IS_BRIDGE)
  377. osc_send_control(&kData->osc.data, PARAMETER_DRYWET, fixedValue);
  378. #endif
  379. }
  380. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback)
  381. {
  382. CARLA_ASSERT(value >= 0.0f && value <= 1.27f);
  383. const float fixedValue = carla_fixValue<float>(0.0f, 1.27f, value);
  384. kData->postProc.volume = fixedValue;
  385. #ifndef BUILD_BRIDGE
  386. if (sendOsc)
  387. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_VOLUME, fixedValue);
  388. #else
  389. // unused
  390. (void)sendOsc;
  391. #endif
  392. if (sendCallback)
  393. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  394. #ifndef BUILD_BRIDGE
  395. else if (fHints & PLUGIN_IS_BRIDGE)
  396. osc_send_control(&kData->osc.data, PARAMETER_VOLUME, fixedValue);
  397. #endif
  398. }
  399. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback)
  400. {
  401. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  402. const float fixedValue = carla_fixValue<float>(-1.0f, 1.0f, value);
  403. kData->postProc.balanceLeft = fixedValue;
  404. #ifndef BUILD_BRIDGE
  405. if (sendOsc)
  406. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_LEFT, fixedValue);
  407. #else
  408. // unused
  409. (void)sendOsc;
  410. #endif
  411. if (sendCallback)
  412. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  413. #ifndef BUILD_BRIDGE
  414. else if (fHints & PLUGIN_IS_BRIDGE)
  415. osc_send_control(&kData->osc.data, PARAMETER_BALANCE_LEFT, fixedValue);
  416. #endif
  417. }
  418. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback)
  419. {
  420. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  421. const float fixedValue = carla_fixValue<float>(-1.0f, 1.0f, value);
  422. kData->postProc.balanceRight = fixedValue;
  423. #ifndef BUILD_BRIDGE
  424. if (sendOsc)
  425. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_RIGHT, fixedValue);
  426. #else
  427. // unused
  428. (void)sendOsc;
  429. #endif
  430. if (sendCallback)
  431. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  432. #ifndef BUILD_BRIDGE
  433. else if (fHints & PLUGIN_IS_BRIDGE)
  434. osc_send_control(&kData->osc.data, PARAMETER_BALANCE_RIGHT, fixedValue);
  435. #endif
  436. }
  437. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback)
  438. {
  439. CARLA_ASSERT(value >= -1.0f && value <= 1.0f);
  440. const float fixedValue = carla_fixValue<float>(-1.0f, 1.0f, value);
  441. kData->postProc.panning = fixedValue;
  442. #ifndef BUILD_BRIDGE
  443. if (sendOsc)
  444. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_PANNING, fixedValue);
  445. #else
  446. // unused
  447. (void)sendOsc;
  448. #endif
  449. if (sendCallback)
  450. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_PANNING, 0, fixedValue, nullptr);
  451. #ifndef BUILD_BRIDGE
  452. else if (fHints & PLUGIN_IS_BRIDGE)
  453. osc_send_control(&kData->osc.data, PARAMETER_PANNING, fixedValue);
  454. #endif
  455. }
  456. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback)
  457. {
  458. CARLA_SAFE_ASSERT(kData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK);
  459. if (kData->engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK)
  460. return;
  461. if (kData->ctrlChannel != channel)
  462. {
  463. {
  464. const ScopedProcessLocker spl(this, true);
  465. kData->ctrlChannel = channel;
  466. }
  467. #ifndef BUILD_BRIDGE
  468. const float ctrlf = channel;
  469. if (sendOsc)
  470. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_CTRL_CHANNEL, ctrlf);
  471. #else
  472. // unused
  473. (void)sendOsc;
  474. #endif
  475. if (sendCallback)
  476. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, PARAMETER_CTRL_CHANNEL, 0, channel, nullptr);
  477. #ifndef BUILD_BRIDGE
  478. else if (fHints & PLUGIN_IS_BRIDGE)
  479. osc_send_control(&kData->osc.data, PARAMETER_CTRL_CHANNEL, ctrlf);
  480. #endif
  481. }
  482. }
  483. // -------------------------------------------------------------------
  484. // Set data (plugin-specific stuff)
  485. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  486. {
  487. CARLA_ASSERT(parameterId < kData->param.count);
  488. if (sendGui)
  489. uiParameterChange(parameterId, value);
  490. #ifndef BUILD_BRIDGE
  491. if (sendOsc)
  492. kData->engine->osc_send_control_set_parameter_value(fId, parameterId, value);
  493. #else
  494. // unused
  495. (void)sendOsc;
  496. #endif
  497. if (sendCallback)
  498. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, parameterId, 0, value, nullptr);
  499. }
  500. void CarlaPlugin::setParameterValueByRIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  501. {
  502. CARLA_ASSERT(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL);
  503. if (rindex <= PARAMETER_MAX)
  504. return;
  505. if (rindex == PARAMETER_NULL)
  506. return;
  507. if (rindex == PARAMETER_ACTIVE)
  508. return setActive(value > 0.0, sendOsc, sendCallback);
  509. if (rindex == PARAMETER_DRYWET)
  510. return setDryWet(value, sendOsc, sendCallback);
  511. if (rindex == PARAMETER_VOLUME)
  512. return setVolume(value, sendOsc, sendCallback);
  513. if (rindex == PARAMETER_BALANCE_LEFT)
  514. return setBalanceLeft(value, sendOsc, sendCallback);
  515. if (rindex == PARAMETER_BALANCE_RIGHT)
  516. return setBalanceRight(value, sendOsc, sendCallback);
  517. if (rindex == PARAMETER_PANNING)
  518. return setPanning(value, sendOsc, sendCallback);
  519. if (rindex == PARAMETER_CTRL_CHANNEL)
  520. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  521. for (uint32_t i=0; i < kData->param.count; i++)
  522. {
  523. if (kData->param.data[i].rindex == rindex)
  524. return setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  525. }
  526. }
  527. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback)
  528. {
  529. CARLA_ASSERT(parameterId < kData->param.count);
  530. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  531. if (channel >= MAX_MIDI_CHANNELS)
  532. channel = MAX_MIDI_CHANNELS;
  533. kData->param.data[parameterId].midiChannel = channel;
  534. #ifndef BUILD_BRIDGE
  535. if (sendOsc)
  536. kData->engine->osc_send_control_set_parameter_midi_channel(fId, parameterId, channel);
  537. #else
  538. // unused
  539. (void)sendOsc;
  540. #endif
  541. if (sendCallback)
  542. kData->engine->callback(CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, fId, parameterId, channel, 0.0f, nullptr);
  543. }
  544. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback)
  545. {
  546. CARLA_ASSERT(parameterId < kData->param.count);
  547. CARLA_ASSERT(cc >= -1);
  548. if (cc < -1 || cc > 0x5F)
  549. cc = -1;
  550. kData->param.data[parameterId].midiCC = cc;
  551. #ifndef BUILD_BRIDGE
  552. if (sendOsc)
  553. kData->engine->osc_send_control_set_parameter_midi_cc(fId, parameterId, cc);
  554. #else
  555. // unused
  556. (void)sendOsc;
  557. #endif
  558. if (sendCallback)
  559. kData->engine->callback(CALLBACK_PARAMETER_MIDI_CC_CHANGED, fId, parameterId, cc, 0.0f, nullptr);
  560. }
  561. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  562. {
  563. CARLA_ASSERT(type != nullptr);
  564. CARLA_ASSERT(key != nullptr);
  565. CARLA_ASSERT(value != nullptr);
  566. if (type == nullptr)
  567. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  568. if (key == nullptr)
  569. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  570. if (value == nullptr)
  571. return carla_stderr2("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  572. bool saveData = true;
  573. if (std::strcmp(type, CUSTOM_DATA_STRING) == 0)
  574. {
  575. // Ignore some keys
  576. if (std::strncmp(key, "OSC:", 4) == 0 || std::strcmp(key, "guiVisible") == 0)
  577. saveData = false;
  578. //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)
  579. // saveData = false;
  580. }
  581. if (saveData)
  582. {
  583. #if 0
  584. // Check if we already have this key
  585. for (size_t i=0; i < kData->custom.count(); i++)
  586. {
  587. if (std::strcmp(custom[i].key, key) == 0)
  588. {
  589. delete[] custom[i].value;
  590. custom[i].value = carla_strdup(value);
  591. return;
  592. }
  593. }
  594. #endif
  595. // Otherwise store it
  596. CustomData newData;
  597. newData.type = carla_strdup(type);
  598. newData.key = carla_strdup(key);
  599. newData.value = carla_strdup(value);
  600. kData->custom.append(newData);
  601. }
  602. }
  603. void CarlaPlugin::setChunkData(const char* const stringData)
  604. {
  605. CARLA_ASSERT(stringData != nullptr);
  606. return;
  607. // unused
  608. (void)stringData;
  609. }
  610. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  611. {
  612. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->prog.count));
  613. if (index > static_cast<int32_t>(kData->prog.count))
  614. return;
  615. const int32_t fixedIndex = carla_fixValue<int32_t>(-1, kData->prog.count, index);
  616. kData->prog.current = fixedIndex;
  617. // Change default parameter values
  618. if (fixedIndex >= 0)
  619. {
  620. if (sendGui)
  621. uiProgramChange(fixedIndex);
  622. for (uint32_t i=0; i < kData->param.count; i++)
  623. {
  624. // FIXME?
  625. kData->param.ranges[i].def = getParameterValue(i);
  626. kData->param.ranges[i].fixDefault();
  627. if (sendOsc)
  628. {
  629. #ifndef BUILD_BRIDGE
  630. kData->engine->osc_send_control_set_default_value(fId, i, kData->param.ranges[i].def);
  631. kData->engine->osc_send_control_set_parameter_value(fId, i, kData->param.ranges[i].def);
  632. #endif
  633. }
  634. }
  635. }
  636. #ifndef BUILD_BRIDGE
  637. if (sendOsc)
  638. kData->engine->osc_send_control_set_program(fId, fixedIndex);
  639. #endif
  640. if (sendCallback)
  641. kData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, fixedIndex, 0, 0.0f, nullptr);
  642. }
  643. void CarlaPlugin::setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  644. {
  645. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  646. if (index > static_cast<int32_t>(kData->midiprog.count))
  647. return;
  648. const int32_t fixedIndex = carla_fixValue<int32_t>(-1, kData->midiprog.count, index);
  649. kData->midiprog.current = fixedIndex;
  650. if (fixedIndex >= 0)
  651. {
  652. if (sendGui)
  653. uiMidiProgramChange(fixedIndex);
  654. // Change default parameter values (sound banks never change defaults)
  655. #ifndef BUILD_BRIDGE // FIXME
  656. if (type() != PLUGIN_GIG && type() != PLUGIN_SF2 && type() != PLUGIN_SFZ)
  657. #endif
  658. {
  659. for (uint32_t i=0; i < kData->param.count; i++)
  660. {
  661. // FIXME?
  662. kData->param.ranges[i].def = getParameterValue(i);
  663. kData->param.ranges[i].fixDefault();
  664. if (sendOsc)
  665. {
  666. #ifndef BUILD_BRIDGE
  667. kData->engine->osc_send_control_set_default_value(fId, i, kData->param.ranges[i].def);
  668. kData->engine->osc_send_control_set_parameter_value(fId, i, kData->param.ranges[i].def);
  669. #endif
  670. }
  671. }
  672. }
  673. }
  674. #ifndef BUILD_BRIDGE
  675. if (sendOsc)
  676. kData->engine->osc_send_control_set_midi_program(fId, fixedIndex);
  677. #endif
  678. if (sendCallback)
  679. kData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, fixedIndex, 0, 0.0f, nullptr);
  680. }
  681. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback)
  682. {
  683. for (uint32_t i=0; i < kData->midiprog.count; i++)
  684. {
  685. if (kData->midiprog.data[i].bank == bank && kData->midiprog.data[i].program == program)
  686. return setMidiProgram(i, sendGui, sendOsc, sendCallback);
  687. }
  688. }
  689. // -------------------------------------------------------------------
  690. // Set gui stuff
  691. void CarlaPlugin::showGui(const bool yesNo)
  692. {
  693. return;
  694. // unused
  695. (void)yesNo;
  696. }
  697. void CarlaPlugin::idleGui()
  698. {
  699. if (! fEnabled)
  700. return;
  701. if (fHints & PLUGIN_USES_SINGLE_THREAD)
  702. {
  703. // Process postponed events
  704. postRtEventsRun();
  705. // Update parameter outputs
  706. for (uint32_t i=0; i < kData->param.count; i++)
  707. {
  708. if (kData->param.data[i].type == PARAMETER_OUTPUT)
  709. uiParameterChange(i, getParameterValue(i));
  710. }
  711. }
  712. }
  713. // -------------------------------------------------------------------
  714. // Plugin state
  715. void CarlaPlugin::reload()
  716. {
  717. }
  718. void CarlaPlugin::reloadPrograms(const bool)
  719. {
  720. }
  721. void CarlaPlugin::prepareForSave()
  722. {
  723. }
  724. // -------------------------------------------------------------------
  725. // Plugin processing
  726. void CarlaPlugin::process(float** const, float** const, const uint32_t)
  727. {
  728. }
  729. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  730. {
  731. }
  732. void CarlaPlugin::sampleRateChanged(const double)
  733. {
  734. }
  735. void CarlaPlugin::recreateLatencyBuffers()
  736. {
  737. #if 0
  738. if (kData->latencyBuffers)
  739. {
  740. for (uint32_t i=0; i < kData->audioIn.count; i++)
  741. delete[] kData->latencyBuffers[i];
  742. delete[] kData->latencyBuffers;
  743. }
  744. if (kData->audioIn.count > 0 && kData->latency > 0)
  745. {
  746. kData->latencyBuffers = new float*[kData->audioIn.count];
  747. for (uint32_t i=0; i < kData->audioIn.count; i++)
  748. kData->latencyBuffers[i] = new float[kData->latency];
  749. }
  750. else
  751. kData->latencyBuffers = nullptr;
  752. #endif
  753. }
  754. // -------------------------------------------------------------------
  755. // OSC stuff
  756. void CarlaPlugin::registerToOscClient()
  757. {
  758. #ifdef BUILD_BRIDGE
  759. if (! kData->engine->isOscBridgeRegistered())
  760. return;
  761. #else
  762. if (! kData->engine->isOscControlRegistered())
  763. return;
  764. #endif
  765. #ifndef BUILD_BRIDGE
  766. kData->engine->osc_send_control_add_plugin_start(fId, fName);
  767. #endif
  768. // Base data
  769. {
  770. char bufName[STR_MAX] = { 0 };
  771. char bufLabel[STR_MAX] = { 0 };
  772. char bufMaker[STR_MAX] = { 0 };
  773. char bufCopyright[STR_MAX] = { 0 };
  774. getRealName(bufName);
  775. getLabel(bufLabel);
  776. getMaker(bufMaker);
  777. getCopyright(bufCopyright);
  778. #ifdef BUILD_BRIDGE
  779. kData->engine->osc_send_bridge_plugin_info(category(), fHints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  780. #else
  781. kData->engine->osc_send_control_set_plugin_data(fId, type(), category(), fHints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  782. #endif
  783. }
  784. // Base count
  785. {
  786. uint32_t cIns, cOuts, cTotals;
  787. getParameterCountInfo(&cIns, &cOuts, &cTotals);
  788. #ifdef BUILD_BRIDGE
  789. kData->engine->osc_send_bridge_audio_count(audioInCount(), audioOutCount(), audioInCount() + audioOutCount());
  790. kData->engine->osc_send_bridge_midi_count(midiInCount(), midiOutCount(), midiInCount() + midiOutCount());
  791. kData->engine->osc_send_bridge_parameter_count(cIns, cOuts, cTotals);
  792. #else
  793. kData->engine->osc_send_control_set_plugin_ports(fId, audioInCount(), audioOutCount(), midiInCount(), midiOutCount(), cIns, cOuts, cTotals);
  794. #endif
  795. }
  796. // Plugin Parameters
  797. if (kData->param.count > 0 && kData->param.count < kData->engine->getOptions().maxParameters)
  798. {
  799. char bufName[STR_MAX], bufUnit[STR_MAX];
  800. for (uint32_t i=0; i < kData->param.count; i++)
  801. {
  802. getParameterName(i, bufName);
  803. getParameterUnit(i, bufUnit);
  804. const ParameterData& paramData(kData->param.data[i]);
  805. const ParameterRanges& paramRanges(kData->param.ranges[i]);
  806. #ifdef BUILD_BRIDGE
  807. kData->engine->osc_send_bridge_parameter_info(i, bufName, bufUnit);
  808. kData->engine->osc_send_bridge_parameter_data(i, paramData.type, paramData.rindex, paramData.hints, paramData.midiChannel, paramData.midiCC);
  809. kData->engine->osc_send_bridge_parameter_ranges(i, paramRanges.def, paramRanges.min, paramRanges.max, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  810. kData->engine->osc_send_bridge_set_parameter_value(i, getParameterValue(i));
  811. #else
  812. kData->engine->osc_send_control_set_parameter_data(fId, i, paramData.type, paramData.hints, bufName, bufUnit, getParameterValue(i));
  813. kData->engine->osc_send_control_set_parameter_ranges(fId, i, paramRanges.min, paramRanges.max, paramRanges.def, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  814. kData->engine->osc_send_control_set_parameter_midi_cc(fId, i, paramData.midiCC);
  815. kData->engine->osc_send_control_set_parameter_midi_channel(fId, i, paramData.midiChannel);
  816. kData->engine->osc_send_control_set_parameter_value(fId, i, getParameterValue(i));
  817. #endif
  818. }
  819. }
  820. // Programs
  821. if (kData->prog.count > 0)
  822. {
  823. #ifdef BUILD_BRIDGE
  824. kData->engine->osc_send_bridge_program_count(kData->prog.count);
  825. for (uint32_t i=0; i < kData->prog.count; i++)
  826. kData->engine->osc_send_bridge_program_info(i, kData->prog.names[i]);
  827. kData->engine->osc_send_bridge_set_program(kData->prog.current);
  828. #else
  829. kData->engine->osc_send_control_set_program_count(fId, kData->prog.count);
  830. for (uint32_t i=0; i < kData->prog.count; i++)
  831. kData->engine->osc_send_control_set_program_name(fId, i, kData->prog.names[i]);
  832. kData->engine->osc_send_control_set_program(fId, kData->prog.current);
  833. #endif
  834. }
  835. // MIDI Programs
  836. if (kData->midiprog.count > 0)
  837. {
  838. #ifdef BUILD_BRIDGE
  839. kData->engine->osc_send_bridge_midi_program_count(kData->midiprog.count);
  840. for (uint32_t i=0; i < kData->midiprog.count; i++)
  841. {
  842. const MidiProgramData& mpData(kData->midiprog.data[i]);
  843. kData->engine->osc_send_bridge_midi_program_info(i, mpData.bank, mpData.program, mpData.name);
  844. }
  845. kData->engine->osc_send_bridge_set_midi_program(kData->midiprog.current);
  846. #else
  847. kData->engine->osc_send_control_set_midi_program_count(fId, kData->midiprog.count);
  848. for (uint32_t i=0; i < kData->midiprog.count; i++)
  849. {
  850. const MidiProgramData& mpData(kData->midiprog.data[i]);
  851. kData->engine->osc_send_control_set_midi_program_data(fId, i, mpData.bank, mpData.program, mpData.name);
  852. }
  853. kData->engine->osc_send_control_set_midi_program(fId, kData->midiprog.current);
  854. #endif
  855. }
  856. #ifndef BUILD_BRIDGE
  857. kData->engine->osc_send_control_add_plugin_end(fId);
  858. // Internal Parameters
  859. {
  860. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_ACTIVE, kData->active ? 1.0 : 0.0);
  861. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_DRYWET, kData->postProc.dryWet);
  862. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_VOLUME, kData->postProc.volume);
  863. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_LEFT, kData->postProc.balanceLeft);
  864. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_BALANCE_RIGHT, kData->postProc.balanceRight);
  865. kData->engine->osc_send_control_set_parameter_value(fId, PARAMETER_PANNING, kData->postProc.panning);
  866. }
  867. #endif
  868. }
  869. void CarlaPlugin::updateOscData(const lo_address& source, const char* const url)
  870. {
  871. // FIXME - remove debug prints later
  872. carla_stdout("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  873. kData->osc.data.free();
  874. const int proto = lo_address_get_protocol(source);
  875. {
  876. const char* host = lo_address_get_hostname(source);
  877. const char* port = lo_address_get_port(source);
  878. kData->osc.data.source = lo_address_new_with_proto(proto, host, port);
  879. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  880. }
  881. {
  882. char* host = lo_url_get_hostname(url);
  883. char* port = lo_url_get_port(url);
  884. kData->osc.data.path = carla_strdup_free(lo_url_get_path(url));
  885. kData->osc.data.target = lo_address_new_with_proto(proto, host, port);
  886. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, kData->osc.data.path);
  887. std::free(host);
  888. std::free(port);
  889. }
  890. #ifndef BUILD_BRIDGE
  891. if (fHints & PLUGIN_IS_BRIDGE)
  892. return;
  893. #endif
  894. osc_send_sample_rate(&kData->osc.data, kData->engine->getSampleRate());
  895. #if 0
  896. for (size_t i=0; i < kData->custom.count(); i++)
  897. {
  898. // TODO
  899. //if (m_type == PLUGIN_LV2)
  900. //osc_send_lv2_transfer_event(&osc.data, getCustomDataTypeString(custom[i].type), /*custom[i].key,*/ custom[i].value);
  901. //else
  902. if (custom[i].type == CUSTOM_DATA_STRING)
  903. osc_send_configure(&osc.data, custom[i].key, custom[i].value);
  904. }
  905. #endif
  906. if (kData->prog.current >= 0)
  907. osc_send_program(&kData->osc.data, kData->prog.current);
  908. if (kData->midiprog.current >= 0)
  909. {
  910. const MidiProgramData& curMidiProg(kData->midiprog.getCurrent());
  911. if (type() == PLUGIN_DSSI)
  912. osc_send_program(&kData->osc.data, curMidiProg.bank, curMidiProg.program);
  913. else
  914. osc_send_midi_program(&kData->osc.data, curMidiProg.bank, curMidiProg.program);
  915. }
  916. for (uint32_t i=0; i < kData->param.count; i++)
  917. osc_send_control(&kData->osc.data, kData->param.data[i].rindex, getParameterValue(i));
  918. carla_stdout("CarlaPlugin::updateOscData() - done");
  919. }
  920. void CarlaPlugin::freeOscData()
  921. {
  922. kData->osc.data.free();
  923. }
  924. bool CarlaPlugin::waitForOscGuiShow()
  925. {
  926. carla_stdout("CarlaPlugin::waitForOscGuiShow()");
  927. // wait for UI 'update' call
  928. for (uint i=0, oscUiTimeout = kData->engine->getOptions().oscUiTimeout; i < oscUiTimeout; i++)
  929. {
  930. if (kData->osc.data.target)
  931. {
  932. carla_stdout("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  933. osc_send_show(&kData->osc.data);
  934. return true;
  935. }
  936. else
  937. carla_msleep(100);
  938. }
  939. carla_stdout("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", kData->engine->getOptions().oscUiTimeout);
  940. return false;
  941. }
  942. // -------------------------------------------------------------------
  943. // MIDI events
  944. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  945. {
  946. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  947. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  948. CARLA_ASSERT(velo < MAX_MIDI_VALUE);
  949. if (! kData->active)
  950. return;
  951. ExternalMidiNote extNote;
  952. extNote.channel = channel;
  953. extNote.note = note;
  954. extNote.velo = velo;
  955. kData->extNotes.append(extNote);
  956. if (sendGui)
  957. {
  958. if (velo > 0)
  959. uiNoteOn(channel, note, velo);
  960. else
  961. uiNoteOff(channel, note);
  962. }
  963. #ifndef BUILD_BRIDGE
  964. if (sendOsc)
  965. {
  966. if (velo > 0)
  967. kData->engine->osc_send_control_note_on(fId, channel, note, velo);
  968. else
  969. kData->engine->osc_send_control_note_off(fId, channel, note);
  970. }
  971. #else
  972. // unused
  973. (void)sendOsc;
  974. #endif
  975. if (sendCallback)
  976. kData->engine->callback(velo ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, fId, channel, note, velo, nullptr);
  977. }
  978. void CarlaPlugin::sendMidiAllNotesOff()
  979. {
  980. kData->postRtEvents.mutex.lock();
  981. PluginPostRtEvent postEvent;
  982. postEvent.type = kPluginPostRtEventNoteOff;
  983. postEvent.value1 = kData->ctrlChannel;
  984. postEvent.value2 = 0;
  985. postEvent.value3 = 0.0;
  986. for (unsigned short i=0; i < MAX_MIDI_NOTE; i++)
  987. {
  988. postEvent.value2 = i;
  989. kData->postRtEvents.data.append(postEvent);
  990. }
  991. kData->postRtEvents.mutex.unlock();
  992. }
  993. // -------------------------------------------------------------------
  994. // Post-poned events
  995. void CarlaPlugin::postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3)
  996. {
  997. PluginPostRtEvent event;
  998. event.type = type;
  999. event.value1 = value1;
  1000. event.value2 = value2;
  1001. event.value3 = value3;
  1002. kData->postRtEvents.appendRT(event);
  1003. }
  1004. void CarlaPlugin::postRtEventsRun()
  1005. {
  1006. const CarlaMutex::ScopedLocker sl(&kData->postRtEvents.mutex);
  1007. while (! kData->postRtEvents.data.isEmpty())
  1008. {
  1009. const PluginPostRtEvent& event = kData->postRtEvents.data.getFirst(true);
  1010. switch (event.type)
  1011. {
  1012. case kPluginPostRtEventNull:
  1013. break;
  1014. case kPluginPostRtEventDebug:
  1015. kData->engine->callback(CALLBACK_DEBUG, fId, event.value1, event.value2, event.value3, nullptr);
  1016. break;
  1017. case kPluginPostRtEventParameterChange:
  1018. // Update UI
  1019. if (event.value1 >= 0)
  1020. uiParameterChange(event.value1, event.value3);
  1021. #ifndef BUILD_BRIDGE
  1022. // Update OSC control client
  1023. if (kData->engine->isOscControlRegistered())
  1024. kData->engine->osc_send_control_set_parameter_value(fId, event.value1, event.value3);
  1025. #endif
  1026. // Update Host
  1027. kData->engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, fId, event.value1, 0, event.value3, nullptr);
  1028. break;
  1029. case kPluginPostRtEventProgramChange:
  1030. // Update UI
  1031. if (event.value1 >= 0)
  1032. uiProgramChange(event.value1);
  1033. #ifndef BUILD_BRIDGE
  1034. // Update OSC control client
  1035. if (kData->engine->isOscControlRegistered())
  1036. {
  1037. kData->engine->osc_send_control_set_program(fId, event.value1);
  1038. for (uint32_t j=0; j < kData->param.count; j++)
  1039. kData->engine->osc_send_control_set_default_value(fId, j, kData->param.ranges[j].def);
  1040. }
  1041. #endif
  1042. // Update Host
  1043. kData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, event.value1, 0, 0.0, nullptr);
  1044. break;
  1045. case kPluginPostRtEventMidiProgramChange:
  1046. // Update UI
  1047. if (event.value1 >= 0)
  1048. uiMidiProgramChange(event.value1);
  1049. #ifndef BUILD_BRIDGE
  1050. // Update OSC control client
  1051. if (kData->engine->isOscControlRegistered())
  1052. {
  1053. kData->engine->osc_send_control_set_midi_program(fId, event.value1);
  1054. for (uint32_t j=0; j < kData->param.count; j++)
  1055. kData->engine->osc_send_control_set_default_value(fId, j, kData->param.ranges[j].def);
  1056. }
  1057. #endif
  1058. // Update Host
  1059. kData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, event.value1, 0, 0.0, nullptr);
  1060. break;
  1061. case kPluginPostRtEventNoteOn:
  1062. // Update UI
  1063. uiNoteOn(event.value1, event.value2, int(event.value3));
  1064. #ifndef BUILD_BRIDGE
  1065. // Update OSC control client
  1066. if (kData->engine->isOscControlRegistered())
  1067. kData->engine->osc_send_control_note_on(fId, event.value1, event.value2, int(event.value3));
  1068. #endif
  1069. // Update Host
  1070. kData->engine->callback(CALLBACK_NOTE_ON, fId, event.value1, event.value2, int(event.value3), nullptr);
  1071. break;
  1072. case kPluginPostRtEventNoteOff:
  1073. // Update UI
  1074. uiNoteOff(event.value1, event.value2);
  1075. #ifndef BUILD_BRIDGE
  1076. // Update OSC control client
  1077. if (kData->engine->isOscControlRegistered())
  1078. kData->engine->osc_send_control_note_off(fId, event.value1, event.value2);
  1079. #endif
  1080. // Update Host
  1081. kData->engine->callback(CALLBACK_NOTE_OFF, fId, event.value1, event.value2, 0.0, nullptr);
  1082. break;
  1083. }
  1084. }
  1085. }
  1086. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value)
  1087. {
  1088. CARLA_ASSERT(index < parameterCount());
  1089. return;
  1090. // unused
  1091. (void)index;
  1092. (void)value;
  1093. }
  1094. void CarlaPlugin::uiProgramChange(const uint32_t index)
  1095. {
  1096. CARLA_ASSERT(index < programCount());
  1097. return;
  1098. // unused
  1099. (void)index;
  1100. }
  1101. void CarlaPlugin::uiMidiProgramChange(const uint32_t index)
  1102. {
  1103. CARLA_ASSERT(index < midiProgramCount());
  1104. return;
  1105. // unused
  1106. (void)index;
  1107. }
  1108. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1109. {
  1110. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1111. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1112. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1113. return;
  1114. // unused
  1115. (void)channel;
  1116. (void)note;
  1117. (void)velo;
  1118. }
  1119. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note)
  1120. {
  1121. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1122. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1123. return;
  1124. // unused
  1125. (void)channel;
  1126. (void)note;
  1127. }
  1128. // -------------------------------------------------------------------
  1129. // Cleanup
  1130. void CarlaPlugin::initBuffers()
  1131. {
  1132. kData->audioIn.initBuffers(kData->engine);
  1133. kData->audioOut.initBuffers(kData->engine);
  1134. kData->event.initBuffers(kData->engine);
  1135. }
  1136. void CarlaPlugin::deleteBuffers()
  1137. {
  1138. carla_debug("CarlaPlugin::deleteBuffers() - start");
  1139. kData->audioIn.clear();
  1140. kData->audioOut.clear();
  1141. kData->param.clear();
  1142. kData->event.clear();
  1143. carla_debug("CarlaPlugin::deleteBuffers() - end");
  1144. }
  1145. // -------------------------------------------------------------------
  1146. // Library functions
  1147. bool CarlaPlugin::libOpen(const char* const filename)
  1148. {
  1149. kData->lib = lib_open(filename);
  1150. return bool(kData->lib);
  1151. }
  1152. bool CarlaPlugin::libClose()
  1153. {
  1154. if (kData->lib == nullptr)
  1155. return false;
  1156. const bool ret = lib_close(kData->lib);
  1157. kData->lib = nullptr;
  1158. return ret;
  1159. }
  1160. void* CarlaPlugin::libSymbol(const char* const symbol)
  1161. {
  1162. return lib_symbol(kData->lib, symbol);
  1163. }
  1164. const char* CarlaPlugin::libError(const char* const filename)
  1165. {
  1166. return lib_error(filename);
  1167. }
  1168. // -------------------------------------------------------------------
  1169. // Scoped Disabler
  1170. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin)
  1171. : kPlugin(plugin)
  1172. {
  1173. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1174. CARLA_ASSERT(plugin != nullptr);
  1175. if (plugin->fEnabled)
  1176. {
  1177. plugin->fEnabled = false;
  1178. plugin->kData->engine->waitForProccessEnd(plugin->id());
  1179. }
  1180. if (plugin->kData->client->isActive())
  1181. plugin->kData->client->deactivate();
  1182. }
  1183. CarlaPlugin::ScopedDisabler::~ScopedDisabler()
  1184. {
  1185. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1186. kPlugin->fEnabled = true;
  1187. kPlugin->kData->client->activate();
  1188. }
  1189. // -------------------------------------------------------------------
  1190. // Scoped Process Locker
  1191. CarlaPlugin::ScopedProcessLocker::ScopedProcessLocker(CarlaPlugin* const plugin, const bool block)
  1192. : kPlugin(plugin),
  1193. kBlock(block)
  1194. {
  1195. carla_debug("CarlaPlugin::ScopedProcessLocker(%p, %s)", plugin, bool2str(block));
  1196. CARLA_ASSERT(plugin != nullptr);
  1197. if (block)
  1198. plugin->kData->mutex.lock();
  1199. }
  1200. CarlaPlugin::ScopedProcessLocker::~ScopedProcessLocker()
  1201. {
  1202. carla_debug("CarlaPlugin::~ScopedProcessLocker()");
  1203. if (kBlock)
  1204. {
  1205. if (kPlugin->kData->mutex.wasTryLockCalled())
  1206. kPlugin->kData->needsReset = true;
  1207. kPlugin->kData->mutex.unlock();
  1208. }
  1209. }
  1210. // -------------------------------------------------------------------
  1211. // CarlaPluginGUI
  1212. #if 0
  1213. CarlaPluginGUI::CarlaPluginGUI(QWidget* const parent, Callback* const callback)
  1214. : QMainWindow(parent),
  1215. kCallback(callback)
  1216. {
  1217. carla_debug("CarlaPluginGUI::CarlaPluginGUI(%p)", parent);
  1218. //CARLA_ASSERT(callback);
  1219. //m_container = new GuiContainer(this);
  1220. //setCentralWidget(m_container);
  1221. //adjustSize();
  1222. //m_container->setParent(this);
  1223. //m_container->show();
  1224. //m_resizable = true;
  1225. //setNewSize(50, 50);
  1226. QMainWindow::setVisible(false);
  1227. }
  1228. CarlaPluginGUI::~CarlaPluginGUI()
  1229. {
  1230. carla_debug("CarlaPluginGUI::~CarlaPluginGUI()");
  1231. //CARLA_ASSERT(m_container);
  1232. // FIXME, automatically deleted by parent ?
  1233. //delete m_container;
  1234. }
  1235. #endif
  1236. #if 0
  1237. // -------------------------------------------------------------------
  1238. GuiContainer* CarlaPluginGUI::getContainer() const
  1239. {
  1240. return m_container;
  1241. }
  1242. WId CarlaPluginGUI::getWinId() const
  1243. {
  1244. return m_container->winId();
  1245. }
  1246. // -------------------------------------------------------------------
  1247. void CarlaPluginGUI::setNewSize(int width, int height)
  1248. {
  1249. carla_debug("CarlaPluginGUI::setNewSize(%i, %i)", width, height);
  1250. if (width < 30)
  1251. width = 30;
  1252. if (height < 30)
  1253. height = 30;
  1254. if (m_resizable)
  1255. {
  1256. resize(width, height);
  1257. }
  1258. else
  1259. {
  1260. setFixedSize(width, height);
  1261. m_container->setFixedSize(width, height);
  1262. }
  1263. }
  1264. void CarlaPluginGUI::setResizable(bool resizable)
  1265. {
  1266. m_resizable = resizable;
  1267. setNewSize(width(), height());
  1268. #ifdef Q_OS_WIN
  1269. if (! resizable)
  1270. setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  1271. #endif
  1272. }
  1273. void CarlaPluginGUI::setTitle(const char* const title)
  1274. {
  1275. CARLA_ASSERT(title);
  1276. setWindowTitle(QString("%1 (GUI)").arg(title));
  1277. }
  1278. void CarlaPluginGUI::setVisible(const bool yesNo)
  1279. {
  1280. carla_debug("CarlaPluginGUI::setVisible(%s)", bool2str(yesNo));
  1281. if (yesNo)
  1282. {
  1283. if (! m_geometry.isNull())
  1284. restoreGeometry(m_geometry);
  1285. }
  1286. else
  1287. m_geometry = saveGeometry();
  1288. QMainWindow::setVisible(yesNo);
  1289. }
  1290. // -------------------------------------------------------------------
  1291. void CarlaPluginGUI::hideEvent(QHideEvent* const event)
  1292. {
  1293. carla_debug("CarlaPluginGUI::hideEvent(%p)", event);
  1294. CARLA_ASSERT(event);
  1295. event->accept();
  1296. close();
  1297. }
  1298. void CarlaPluginGUI::closeEvent(QCloseEvent* const event)
  1299. {
  1300. carla_debug("CarlaPluginGUI::closeEvent(%p)", event);
  1301. CARLA_ASSERT(event);
  1302. if (event->spontaneous())
  1303. {
  1304. if (m_callback)
  1305. m_callback->guiClosedCallback();
  1306. QMainWindow::closeEvent(event);
  1307. return;
  1308. }
  1309. event->ignore();
  1310. }
  1311. #endif
  1312. // -------------------------------------------------------------------
  1313. CARLA_BACKEND_END_NAMESPACE