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.

1455 lines
45KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 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 doc/GPL.txt file.
  16. */
  17. #define CARLA_NATIVE_PLUGIN_LV2
  18. #include "carla-native-base.cpp"
  19. #include "CarlaLv2Utils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaString.hpp"
  22. #include "juce_audio_basics.h"
  23. using juce::FloatVectorOperations;
  24. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  25. # include "juce_gui_basics.h"
  26. using juce::ScopedJuceInitialiser_GUI;
  27. using juce::SharedResourcePointer;
  28. #endif
  29. // -----------------------------------------------------------------------
  30. // -Weffc++ compat ext widget
  31. extern "C" {
  32. typedef struct _LV2_External_UI_Widget_Compat {
  33. void (*run )(struct _LV2_External_UI_Widget_Compat*);
  34. void (*show)(struct _LV2_External_UI_Widget_Compat*);
  35. void (*hide)(struct _LV2_External_UI_Widget_Compat*);
  36. _LV2_External_UI_Widget_Compat()
  37. : run(nullptr), show(nullptr), hide(nullptr) {}
  38. } LV2_External_UI_Widget_Compat;
  39. }
  40. // -----------------------------------------------------------------------
  41. // LV2 descriptor functions
  42. #if defined(__clang__)
  43. # pragma clang diagnostic push
  44. # pragma clang diagnostic ignored "-Weffc++"
  45. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  46. # pragma GCC diagnostic push
  47. # pragma GCC diagnostic ignored "-Weffc++"
  48. #endif
  49. class NativePlugin : public LV2_External_UI_Widget_Compat
  50. {
  51. #if defined(__clang__)
  52. # pragma clang diagnostic pop
  53. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  54. # pragma GCC diagnostic pop
  55. #endif
  56. public:
  57. static const uint32_t kMaxMidiEvents = 512;
  58. NativePlugin(const NativePluginDescriptor* const desc, const double sampleRate, const char* const bundlePath, const LV2_Feature* const* features)
  59. : fHandle(nullptr),
  60. fHost(),
  61. fDescriptor(desc),
  62. fProgramDesc({0, 0, nullptr}),
  63. fMidiEventCount(0),
  64. fTimeInfo(),
  65. fIsProcessing(false),
  66. fBufferSize(0),
  67. fSampleRate(sampleRate),
  68. fUridMap(nullptr),
  69. fURIs(),
  70. fUI(),
  71. fPorts(),
  72. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  73. sJuceGUI(),
  74. #endif
  75. leakDetector_NativePlugin()
  76. {
  77. run = extui_run;
  78. show = extui_show;
  79. hide = extui_hide;
  80. CarlaString resourceDir(bundlePath);
  81. #ifdef CARLA_OS_WIN
  82. resourceDir += "\\resources\\";
  83. #else
  84. resourceDir += "/resources/";
  85. #endif
  86. fHost.handle = this;
  87. fHost.resourceDir = resourceDir.dup();
  88. fHost.uiName = nullptr;
  89. fHost.uiParentId = 0;
  90. fHost.get_buffer_size = host_get_buffer_size;
  91. fHost.get_sample_rate = host_get_sample_rate;
  92. fHost.is_offline = host_is_offline;
  93. fHost.get_time_info = host_get_time_info;
  94. fHost.write_midi_event = host_write_midi_event;
  95. fHost.ui_parameter_changed = host_ui_parameter_changed;
  96. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  97. fHost.ui_closed = host_ui_closed;
  98. fHost.ui_open_file = host_ui_open_file;
  99. fHost.ui_save_file = host_ui_save_file;
  100. fHost.dispatcher = host_dispatcher;
  101. const LV2_Options_Option* options = nullptr;
  102. const LV2_URID_Map* uridMap = nullptr;
  103. const LV2_URID_Unmap* uridUnmap = nullptr;
  104. for (int i=0; features[i] != nullptr; ++i)
  105. {
  106. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  107. options = (const LV2_Options_Option*)features[i]->data;
  108. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  109. uridMap = (const LV2_URID_Map*)features[i]->data;
  110. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  111. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  112. }
  113. if (options == nullptr || uridMap == nullptr)
  114. {
  115. carla_stderr("Host doesn't provides option or urid-map features");
  116. return;
  117. }
  118. for (int i=0; options[i].key != 0; ++i)
  119. {
  120. if (uridUnmap != nullptr)
  121. {
  122. carla_debug("Host option %i:\"%s\"", i, uridUnmap->unmap(uridUnmap->handle, options[i].key));
  123. }
  124. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  125. {
  126. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  127. {
  128. const int value(*(const int*)options[i].value);
  129. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  130. fBufferSize = static_cast<uint32_t>(value);
  131. }
  132. else
  133. carla_stderr("Host provides maxBlockLength but has wrong value type");
  134. break;
  135. }
  136. }
  137. fUridMap = uridMap;
  138. if (fDescriptor->midiIns > 0)
  139. fUI.portOffset += desc->midiIns;
  140. else if (fDescriptor->hints & PLUGIN_USES_TIME)
  141. fUI.portOffset += 1;
  142. fUI.portOffset += desc->midiOuts;
  143. fUI.portOffset += 1; // freewheel
  144. fUI.portOffset += desc->audioIns;
  145. fUI.portOffset += desc->audioOuts;
  146. }
  147. ~NativePlugin()
  148. {
  149. CARLA_ASSERT(fHandle == nullptr);
  150. if (fHost.resourceDir != nullptr)
  151. {
  152. delete[] fHost.resourceDir;
  153. fHost.resourceDir = nullptr;
  154. }
  155. }
  156. bool init()
  157. {
  158. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  159. {
  160. carla_stderr("Plugin is missing something...");
  161. return false;
  162. }
  163. if (fBufferSize == 0)
  164. {
  165. carla_stderr("Host is missing bufferSize feature");
  166. //return false;
  167. // as testing, continue for now
  168. fBufferSize = 1024;
  169. }
  170. fHandle = fDescriptor->instantiate(&fHost);
  171. if (fHandle == nullptr)
  172. return false;
  173. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  174. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  175. fPorts.init(fDescriptor, fHandle);
  176. fURIs.map(fUridMap);
  177. return true;
  178. }
  179. // -------------------------------------------------------------------
  180. // LV2 functions
  181. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  182. {
  183. fPorts.connectPort(fDescriptor, port, dataLocation);
  184. }
  185. void lv2_activate()
  186. {
  187. if (fDescriptor->activate != nullptr)
  188. fDescriptor->activate(fHandle);
  189. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  190. }
  191. void lv2_deactivate()
  192. {
  193. if (fDescriptor->deactivate != nullptr)
  194. fDescriptor->deactivate(fHandle);
  195. }
  196. void lv2_cleanup()
  197. {
  198. if (fDescriptor->cleanup != nullptr)
  199. fDescriptor->cleanup(fHandle);
  200. fHandle = nullptr;
  201. }
  202. void lv2_run(const uint32_t frames)
  203. {
  204. if (frames == 0)
  205. {
  206. updateParameterOutputs();
  207. return;
  208. }
  209. // Check for updated parameters
  210. float curValue;
  211. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  212. {
  213. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  214. curValue = *fPorts.paramsPtr[i];
  215. if ((! carla_compareFloats(fPorts.paramsLast[i], curValue)) && (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT) == 0)
  216. {
  217. fPorts.paramsLast[i] = curValue;
  218. fDescriptor->set_parameter_value(fHandle, i, curValue);
  219. }
  220. }
  221. if (fDescriptor->midiIns > 0 || (fDescriptor->hints & PLUGIN_USES_TIME) != 0)
  222. {
  223. fMidiEventCount = 0;
  224. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  225. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[0], iter)
  226. {
  227. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  228. if (event == nullptr)
  229. continue;
  230. if (event->body.size > 4)
  231. continue;
  232. if (event->time.frames >= frames)
  233. break;
  234. if (event->body.type == fURIs.midiEvent)
  235. {
  236. if (fMidiEventCount >= kMaxMidiEvents*2)
  237. continue;
  238. const uint8_t* const data((const uint8_t*)(event + 1));
  239. fMidiEvents[fMidiEventCount].port = 0;
  240. fMidiEvents[fMidiEventCount].time = (uint32_t)event->time.frames;
  241. fMidiEvents[fMidiEventCount].size = (uint8_t)event->body.size;
  242. for (uint32_t i=0; i < event->body.size; ++i)
  243. fMidiEvents[fMidiEventCount].data[i] = data[i];
  244. fMidiEventCount += 1;
  245. continue;
  246. }
  247. if (event->body.type == fURIs.atomBlank)
  248. {
  249. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  250. if (obj->body.otype != fURIs.timePos)
  251. continue;
  252. LV2_Atom* bar = nullptr;
  253. LV2_Atom* barBeat = nullptr;
  254. LV2_Atom* beatsPerBar = nullptr;
  255. LV2_Atom* bpm = nullptr;
  256. LV2_Atom* beatUnit = nullptr;
  257. LV2_Atom* frame = nullptr;
  258. LV2_Atom* speed = nullptr;
  259. lv2_atom_object_get(obj,
  260. fURIs.timeBar, &bar,
  261. fURIs.timeBarBeat, &barBeat,
  262. fURIs.timeBeatsPerBar, &beatsPerBar,
  263. fURIs.timeBeatsPerMinute, &bpm,
  264. fURIs.timeBeatUnit, &beatUnit,
  265. fURIs.timeFrame, &frame,
  266. fURIs.timeSpeed, &speed,
  267. nullptr);
  268. if (bpm != nullptr && bpm->type == fURIs.atomFloat)
  269. {
  270. fTimeInfo.bbt.beatsPerMinute = ((LV2_Atom_Float*)bpm)->body;
  271. fTimeInfo.bbt.valid = true;
  272. }
  273. if (beatsPerBar != nullptr && beatsPerBar->type == fURIs.atomFloat)
  274. {
  275. float beatsPerBarValue = ((LV2_Atom_Float*)beatsPerBar)->body;
  276. fTimeInfo.bbt.beatsPerBar = beatsPerBarValue;
  277. if (bar != nullptr && bar->type == fURIs.atomLong)
  278. {
  279. //float barValue = ((LV2_Atom_Long*)bar)->body;
  280. //curPosInfo.ppqPositionOfLastBarStart = barValue * beatsPerBarValue;
  281. if (barBeat != nullptr && barBeat->type == fURIs.atomFloat)
  282. {
  283. //float barBeatValue = ((LV2_Atom_Float*)barBeat)->body;
  284. //curPosInfo.ppqPosition = curPosInfo.ppqPositionOfLastBarStart + barBeatValue;
  285. }
  286. }
  287. }
  288. if (beatUnit != nullptr && beatUnit->type == fURIs.atomFloat)
  289. fTimeInfo.bbt.beatType = ((LV2_Atom_Float*)beatUnit)->body;
  290. if (frame != nullptr && frame->type == fURIs.atomLong)
  291. {
  292. const int64_t value(((LV2_Atom_Long*)frame)->body);
  293. CARLA_SAFE_ASSERT_CONTINUE(value >= 0);
  294. fTimeInfo.frame = static_cast<uint64_t>(value);
  295. }
  296. if (speed != nullptr && speed->type == fURIs.atomFloat)
  297. fTimeInfo.playing = carla_compareFloats(((LV2_Atom_Float*)speed)->body, 1.0f);
  298. continue;
  299. }
  300. }
  301. for (uint32_t i=1; i < fDescriptor->midiIns; ++i)
  302. {
  303. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[i], iter)
  304. {
  305. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  306. if (event == nullptr)
  307. continue;
  308. if (event->body.type != fURIs.midiEvent)
  309. continue;
  310. if (event->body.size > 4)
  311. continue;
  312. if (event->time.frames >= frames)
  313. break;
  314. if (fMidiEventCount >= kMaxMidiEvents*2)
  315. break;
  316. const uint8_t* const data((const uint8_t*)(event + 1));
  317. fMidiEvents[fMidiEventCount].port = (uint8_t)i;
  318. fMidiEvents[fMidiEventCount].size = (uint8_t)event->body.size;
  319. fMidiEvents[fMidiEventCount].time = (uint32_t)event->time.frames;
  320. for (uint32_t j=0; j < event->body.size; ++j)
  321. fMidiEvents[fMidiEventCount].data[j] = data[j];
  322. fMidiEventCount += 1;
  323. }
  324. }
  325. }
  326. fIsProcessing = true;
  327. fDescriptor->process(fHandle, fPorts.audioIns, fPorts.audioOuts, frames, fMidiEvents, fMidiEventCount);
  328. fIsProcessing = false;
  329. // TODO - midi out
  330. updateParameterOutputs();
  331. }
  332. // -------------------------------------------------------------------
  333. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/) const
  334. {
  335. // currently unused
  336. return LV2_OPTIONS_SUCCESS;
  337. }
  338. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  339. {
  340. for (int i=0; options[i].key != 0; ++i)
  341. {
  342. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  343. {
  344. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Int))
  345. {
  346. const int value(*(const int*)options[i].value);
  347. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  348. fBufferSize = static_cast<uint32_t>(value);
  349. if (fDescriptor->dispatcher != nullptr)
  350. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, fBufferSize, nullptr, 0.0f);
  351. }
  352. else
  353. carla_stderr("Host changed maxBlockLength but with wrong value type");
  354. }
  355. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_CORE__sampleRate))
  356. {
  357. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Double))
  358. {
  359. const double value(*(const double*)options[i].value);
  360. CARLA_SAFE_ASSERT_CONTINUE(value > 0.0);
  361. fSampleRate = value;
  362. if (fDescriptor->dispatcher != nullptr)
  363. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)fSampleRate);
  364. }
  365. else
  366. carla_stderr("Host changed sampleRate but with wrong value type");
  367. }
  368. }
  369. return LV2_OPTIONS_SUCCESS;
  370. }
  371. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  372. {
  373. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  374. return nullptr;
  375. if (fDescriptor->get_midi_program_count == nullptr)
  376. return nullptr;
  377. if (fDescriptor->get_midi_program_info == nullptr)
  378. return nullptr;
  379. if (index >= fDescriptor->get_midi_program_count(fHandle))
  380. return nullptr;
  381. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  382. if (midiProg == nullptr)
  383. return nullptr;
  384. fProgramDesc.bank = midiProg->bank;
  385. fProgramDesc.program = midiProg->program;
  386. fProgramDesc.name = midiProg->name;
  387. return &fProgramDesc;
  388. }
  389. void lv2_select_program(uint32_t bank, uint32_t program)
  390. {
  391. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  392. return;
  393. if (fDescriptor->set_midi_program == nullptr)
  394. return;
  395. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  396. }
  397. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle, const uint32_t /*flags*/, const LV2_Feature* const* const /*features*/) const
  398. {
  399. if ((fDescriptor->hints & PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  400. return LV2_STATE_ERR_NO_FEATURE;
  401. if (char* const state = fDescriptor->get_state(fHandle))
  402. {
  403. store(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), state, std::strlen(state), fURIs.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  404. std::free(state);
  405. return LV2_STATE_SUCCESS;
  406. }
  407. return LV2_STATE_ERR_UNKNOWN;
  408. }
  409. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* const /*features*/) const
  410. {
  411. if ((fDescriptor->hints & PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  412. return LV2_STATE_ERR_NO_FEATURE;
  413. size_t size = 0;
  414. uint32_t type = 0;
  415. const void* data = retrieve(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), &size, &type, &flags);
  416. if (size == 0)
  417. return LV2_STATE_ERR_UNKNOWN;
  418. if (type == 0)
  419. return LV2_STATE_ERR_UNKNOWN;
  420. if (data == nullptr)
  421. return LV2_STATE_ERR_UNKNOWN;
  422. if (type != fURIs.atomString)
  423. return LV2_STATE_ERR_BAD_TYPE;
  424. fDescriptor->set_state(fHandle, (const char*)data);
  425. return LV2_STATE_SUCCESS;
  426. }
  427. // -------------------------------------------------------------------
  428. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  429. {
  430. fUI.writeFunction = writeFunction;
  431. fUI.controller = controller;
  432. // ---------------------------------------------------------------
  433. // see if the host supports external-ui
  434. for (int i=0; features[i] != nullptr; ++i)
  435. {
  436. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  437. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  438. {
  439. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  440. break;
  441. }
  442. }
  443. if (fUI.host != nullptr)
  444. {
  445. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  446. *widget = this;
  447. return;
  448. }
  449. // ---------------------------------------------------------------
  450. // no external-ui support, use showInterface
  451. for (int i=0; features[i] != nullptr; ++i)
  452. {
  453. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  454. {
  455. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  456. for (int j=0; options[j].key != 0; ++j)
  457. {
  458. if (options[j].key == fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  459. {
  460. fHost.uiName = carla_strdup((const char*)options[j].value);
  461. break;
  462. }
  463. }
  464. break;
  465. }
  466. }
  467. if (fHost.uiName == nullptr)
  468. fHost.uiName = carla_strdup(fDescriptor->name);
  469. *widget = nullptr;
  470. }
  471. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  472. {
  473. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  474. return;
  475. if (portIndex >= fUI.portOffset || ! fUI.isVisible)
  476. return;
  477. if (fDescriptor->ui_set_parameter_value == nullptr)
  478. return;
  479. const float value(*(const float*)buffer);
  480. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fUI.portOffset, value);
  481. }
  482. void lv2ui_cleanup()
  483. {
  484. fUI.host = nullptr;
  485. fUI.writeFunction = nullptr;
  486. fUI.controller = nullptr;
  487. if (fHost.uiName != nullptr)
  488. {
  489. delete[] fHost.uiName;
  490. fHost.uiName = nullptr;
  491. }
  492. if (! fUI.isVisible)
  493. return;
  494. if (fDescriptor->ui_show != nullptr)
  495. fDescriptor->ui_show(fHandle, false);
  496. fUI.isVisible = false;
  497. }
  498. // -------------------------------------------------------------------
  499. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  500. {
  501. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  502. return;
  503. if (fDescriptor->ui_set_midi_program == nullptr)
  504. return;
  505. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  506. }
  507. // -------------------------------------------------------------------
  508. int lv2ui_idle() const
  509. {
  510. if (! fUI.isVisible)
  511. return 1;
  512. if (fDescriptor->ui_idle != nullptr)
  513. fDescriptor->ui_idle(fHandle);
  514. return 0;
  515. }
  516. int lv2ui_show()
  517. {
  518. handleUiShow();
  519. return 0;
  520. }
  521. int lv2ui_hide()
  522. {
  523. handleUiHide();
  524. return 0;
  525. }
  526. // -------------------------------------------------------------------
  527. protected:
  528. void handleUiRun() const
  529. {
  530. if (fDescriptor->ui_idle != nullptr)
  531. fDescriptor->ui_idle(fHandle);
  532. }
  533. void handleUiShow()
  534. {
  535. if (fDescriptor->ui_show != nullptr)
  536. fDescriptor->ui_show(fHandle, true);
  537. fUI.isVisible = true;
  538. }
  539. void handleUiHide()
  540. {
  541. if (fDescriptor->ui_show != nullptr)
  542. fDescriptor->ui_show(fHandle, false);
  543. fUI.isVisible = false;
  544. }
  545. // -------------------------------------------------------------------
  546. uint32_t handleGetBufferSize() const
  547. {
  548. return fBufferSize;
  549. }
  550. double handleGetSampleRate() const
  551. {
  552. return fSampleRate;
  553. }
  554. bool handleIsOffline() const
  555. {
  556. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  557. return (fPorts.freewheel != nullptr && *fPorts.freewheel >= 0.5f);
  558. }
  559. const NativeTimeInfo* handleGetTimeInfo() const
  560. {
  561. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  562. return &fTimeInfo;
  563. }
  564. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  565. {
  566. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  567. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  568. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  569. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  570. // reverse-find first free event, and put it there
  571. for (uint32_t i=(kMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  572. {
  573. if (fMidiEvents[i].data[0] == 0)
  574. {
  575. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  576. return true;
  577. }
  578. }
  579. return false;
  580. }
  581. void handleUiParameterChanged(const uint32_t index, const float value) const
  582. {
  583. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  584. fUI.writeFunction(fUI.controller, index+fUI.portOffset, sizeof(float), 0, &value);
  585. }
  586. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  587. {
  588. //storeCustomData(key, value);
  589. }
  590. void handleUiClosed()
  591. {
  592. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  593. fUI.host->ui_closed(fUI.controller);
  594. fUI.host = nullptr;
  595. fUI.writeFunction = nullptr;
  596. fUI.controller = nullptr;
  597. fUI.isVisible = false;
  598. }
  599. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  600. {
  601. // TODO
  602. return nullptr;
  603. }
  604. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  605. {
  606. // TODO
  607. return nullptr;
  608. }
  609. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  610. {
  611. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  612. intptr_t ret = 0;
  613. switch (opcode)
  614. {
  615. case HOST_OPCODE_NULL:
  616. case HOST_OPCODE_UPDATE_PARAMETER:
  617. case HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  618. case HOST_OPCODE_RELOAD_PARAMETERS:
  619. case HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  620. case HOST_OPCODE_RELOAD_ALL:
  621. // nothing
  622. break;
  623. case HOST_OPCODE_UI_UNAVAILABLE:
  624. handleUiClosed();
  625. break;
  626. }
  627. return ret;
  628. // unused for now
  629. (void)index;
  630. (void)value;
  631. (void)ptr;
  632. (void)opt;
  633. }
  634. void updateParameterOutputs()
  635. {
  636. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  637. {
  638. if (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT)
  639. {
  640. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  641. if (fPorts.paramsPtr[i] != nullptr)
  642. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  643. }
  644. }
  645. }
  646. // -------------------------------------------------------------------
  647. private:
  648. // Native data
  649. NativePluginHandle fHandle;
  650. NativeHostDescriptor fHost;
  651. const NativePluginDescriptor* const fDescriptor;
  652. LV2_Program_Descriptor fProgramDesc;
  653. uint32_t fMidiEventCount;
  654. NativeMidiEvent fMidiEvents[kMaxMidiEvents*2];
  655. NativeTimeInfo fTimeInfo;
  656. bool fIsProcessing;
  657. // Lv2 host data
  658. uint32_t fBufferSize;
  659. double fSampleRate;
  660. const LV2_URID_Map* fUridMap;
  661. struct URIDs {
  662. LV2_URID atomBlank;
  663. LV2_URID atomFloat;
  664. LV2_URID atomLong;
  665. LV2_URID atomSequence;
  666. LV2_URID atomString;
  667. LV2_URID midiEvent;
  668. LV2_URID timePos;
  669. LV2_URID timeBar;
  670. LV2_URID timeBarBeat;
  671. LV2_URID timeBeatsPerBar;
  672. LV2_URID timeBeatsPerMinute;
  673. LV2_URID timeBeatUnit;
  674. LV2_URID timeFrame;
  675. LV2_URID timeSpeed;
  676. URIDs()
  677. : atomBlank(0),
  678. atomFloat(0),
  679. atomLong(0),
  680. atomSequence(0),
  681. atomString(0),
  682. midiEvent(0),
  683. timePos(0),
  684. timeBar(0),
  685. timeBarBeat(0),
  686. timeBeatsPerBar(0),
  687. timeBeatsPerMinute(0),
  688. timeBeatUnit(0),
  689. timeFrame(0),
  690. timeSpeed(0) {}
  691. void map(const LV2_URID_Map* const uridMap)
  692. {
  693. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  694. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  695. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  696. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  697. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  698. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  699. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  700. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  701. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  702. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  703. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  704. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  705. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  706. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  707. }
  708. } fURIs;
  709. struct UI {
  710. const LV2_External_UI_Host* host;
  711. LV2UI_Write_Function writeFunction;
  712. LV2UI_Controller controller;
  713. uint32_t portOffset;
  714. bool isVisible;
  715. UI()
  716. : host(nullptr),
  717. writeFunction(nullptr),
  718. controller(nullptr),
  719. portOffset(0),
  720. isVisible(false) {}
  721. } fUI;
  722. struct Ports {
  723. LV2_Atom_Sequence** eventsIn;
  724. LV2_Atom_Sequence** midiOuts;
  725. float** audioIns;
  726. float** audioOuts;
  727. float* freewheel;
  728. uint32_t paramCount;
  729. float* paramsLast;
  730. float** paramsPtr;
  731. Ports()
  732. : eventsIn(nullptr),
  733. midiOuts(nullptr),
  734. audioIns(nullptr),
  735. audioOuts(nullptr),
  736. freewheel(nullptr),
  737. paramCount(0),
  738. paramsLast(nullptr),
  739. paramsPtr(nullptr) {}
  740. ~Ports()
  741. {
  742. if (eventsIn != nullptr)
  743. {
  744. delete[] eventsIn;
  745. eventsIn = nullptr;
  746. }
  747. if (midiOuts != nullptr)
  748. {
  749. delete[] midiOuts;
  750. midiOuts = nullptr;
  751. }
  752. if (audioIns != nullptr)
  753. {
  754. delete[] audioIns;
  755. audioIns = nullptr;
  756. }
  757. if (audioOuts != nullptr)
  758. {
  759. delete[] audioOuts;
  760. audioOuts = nullptr;
  761. }
  762. if (paramsLast != nullptr)
  763. {
  764. delete[] paramsLast;
  765. paramsLast = nullptr;
  766. }
  767. if (paramsPtr != nullptr)
  768. {
  769. delete[] paramsPtr;
  770. paramsPtr = nullptr;
  771. }
  772. }
  773. void init(const NativePluginDescriptor* const desc, NativePluginHandle handle)
  774. {
  775. CARLA_SAFE_ASSERT_RETURN(desc != nullptr && handle != nullptr,)
  776. if (desc->midiIns > 0)
  777. {
  778. eventsIn = new LV2_Atom_Sequence*[desc->midiIns];
  779. for (uint32_t i=0; i < desc->midiIns; ++i)
  780. eventsIn[i] = nullptr;
  781. }
  782. else if (desc->hints & PLUGIN_USES_TIME)
  783. {
  784. eventsIn = new LV2_Atom_Sequence*[1];
  785. eventsIn[0] = nullptr;
  786. }
  787. if (desc->midiOuts > 0)
  788. {
  789. midiOuts = new LV2_Atom_Sequence*[desc->midiOuts];
  790. for (uint32_t i=0; i < desc->midiOuts; ++i)
  791. midiOuts[i] = nullptr;
  792. }
  793. if (desc->audioIns > 0)
  794. {
  795. audioIns = new float*[desc->audioIns];
  796. for (uint32_t i=0; i < desc->audioIns; ++i)
  797. audioIns[i] = nullptr;
  798. }
  799. if (desc->audioOuts > 0)
  800. {
  801. audioOuts = new float*[desc->audioOuts];
  802. for (uint32_t i=0; i < desc->audioOuts; ++i)
  803. audioOuts[i] = nullptr;
  804. }
  805. if (desc->get_parameter_count != nullptr && desc->get_parameter_info != nullptr && desc->get_parameter_value != nullptr && desc->set_parameter_value != nullptr)
  806. {
  807. paramCount = desc->get_parameter_count(handle);
  808. if (paramCount > 0)
  809. {
  810. paramsLast = new float[paramCount];
  811. paramsPtr = new float*[paramCount];
  812. for (uint32_t i=0; i < paramCount; ++i)
  813. {
  814. paramsLast[i] = desc->get_parameter_value(handle, i);
  815. paramsPtr[i] = nullptr;
  816. }
  817. }
  818. }
  819. }
  820. void connectPort(const NativePluginDescriptor* const desc, const uint32_t port, void* const dataLocation)
  821. {
  822. uint32_t index = 0;
  823. if (desc->midiIns > 0 || (desc->hints & PLUGIN_USES_TIME) != 0)
  824. {
  825. if (port == index++)
  826. {
  827. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  828. return;
  829. }
  830. }
  831. for (uint32_t i=1; i < desc->midiIns; ++i)
  832. {
  833. if (port == index++)
  834. {
  835. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  836. return;
  837. }
  838. }
  839. for (uint32_t i=0; i < desc->midiOuts; ++i)
  840. {
  841. if (port == index++)
  842. {
  843. midiOuts[i] = (LV2_Atom_Sequence*)dataLocation;
  844. return;
  845. }
  846. }
  847. if (port == index++)
  848. {
  849. freewheel = (float*)dataLocation;
  850. return;
  851. }
  852. for (uint32_t i=0; i < desc->audioIns; ++i)
  853. {
  854. if (port == index++)
  855. {
  856. audioIns[i] = (float*)dataLocation;
  857. return;
  858. }
  859. }
  860. for (uint32_t i=0; i < desc->audioOuts; ++i)
  861. {
  862. if (port == index++)
  863. {
  864. audioOuts[i] = (float*)dataLocation;
  865. return;
  866. }
  867. }
  868. for (uint32_t i=0; i < paramCount; ++i)
  869. {
  870. if (port == index++)
  871. {
  872. paramsPtr[i] = (float*)dataLocation;
  873. return;
  874. }
  875. }
  876. }
  877. CARLA_DECLARE_NON_COPY_STRUCT(Ports);
  878. } fPorts;
  879. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  880. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceGUI;
  881. #endif
  882. // -------------------------------------------------------------------
  883. #define handlePtr ((NativePlugin*)self)
  884. static void extui_run(LV2_External_UI_Widget_Compat* self)
  885. {
  886. handlePtr->handleUiRun();
  887. }
  888. static void extui_show(LV2_External_UI_Widget_Compat* self)
  889. {
  890. handlePtr->handleUiShow();
  891. }
  892. static void extui_hide(LV2_External_UI_Widget_Compat* self)
  893. {
  894. handlePtr->handleUiHide();
  895. }
  896. #undef handlePtr
  897. // -------------------------------------------------------------------
  898. #define handlePtr ((NativePlugin*)handle)
  899. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  900. {
  901. return handlePtr->handleGetBufferSize();
  902. }
  903. static double host_get_sample_rate(NativeHostHandle handle)
  904. {
  905. return handlePtr->handleGetSampleRate();
  906. }
  907. static bool host_is_offline(NativeHostHandle handle)
  908. {
  909. return handlePtr->handleIsOffline();
  910. }
  911. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  912. {
  913. return handlePtr->handleGetTimeInfo();
  914. }
  915. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  916. {
  917. return handlePtr->handleWriteMidiEvent(event);
  918. }
  919. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  920. {
  921. handlePtr->handleUiParameterChanged(index, value);
  922. }
  923. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  924. {
  925. handlePtr->handleUiCustomDataChanged(key, value);
  926. }
  927. static void host_ui_closed(NativeHostHandle handle)
  928. {
  929. handlePtr->handleUiClosed();
  930. }
  931. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  932. {
  933. return handlePtr->handleUiOpenFile(isDir, title, filter);
  934. }
  935. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  936. {
  937. return handlePtr->handleUiSaveFile(isDir, title, filter);
  938. }
  939. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  940. {
  941. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  942. }
  943. #undef handlePtr
  944. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  945. };
  946. // -----------------------------------------------------------------------
  947. // LV2 plugin descriptor functions
  948. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  949. {
  950. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  951. const NativePluginDescriptor* pluginDesc = nullptr;
  952. const char* pluginLabel = nullptr;
  953. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  954. pluginLabel = lv2Descriptor->URI+37;
  955. if (pluginLabel == nullptr)
  956. {
  957. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  958. return nullptr;
  959. }
  960. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  961. PluginListManager& plm(PluginListManager::getInstance());
  962. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  963. {
  964. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  965. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  966. {
  967. pluginDesc = tmpDesc;
  968. break;
  969. }
  970. }
  971. if (pluginDesc == nullptr)
  972. {
  973. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  974. return nullptr;
  975. }
  976. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  977. if (! plugin->init())
  978. {
  979. carla_stderr("Failed to init plugin");
  980. delete plugin;
  981. return nullptr;
  982. }
  983. return (LV2_Handle)plugin;
  984. }
  985. #define instancePtr ((NativePlugin*)instance)
  986. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  987. {
  988. instancePtr->lv2_connect_port(port, dataLocation);
  989. }
  990. static void lv2_activate(LV2_Handle instance)
  991. {
  992. carla_debug("lv2_activate(%p)", instance);
  993. instancePtr->lv2_activate();
  994. }
  995. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  996. {
  997. instancePtr->lv2_run(sampleCount);
  998. }
  999. static void lv2_deactivate(LV2_Handle instance)
  1000. {
  1001. carla_debug("lv2_deactivate(%p)", instance);
  1002. instancePtr->lv2_deactivate();
  1003. }
  1004. static void lv2_cleanup(LV2_Handle instance)
  1005. {
  1006. carla_debug("lv2_cleanup(%p)", instance);
  1007. instancePtr->lv2_cleanup();
  1008. delete instancePtr;
  1009. }
  1010. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1011. {
  1012. carla_debug("lv2_get_options(%p, %p)", instance, options);
  1013. return instancePtr->lv2_get_options(options);
  1014. }
  1015. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1016. {
  1017. carla_debug("lv2_set_options(%p, %p)", instance, options);
  1018. return instancePtr->lv2_set_options(options);
  1019. }
  1020. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1021. {
  1022. carla_debug("lv2_get_program(%p, %i)", instance, index);
  1023. return instancePtr->lv2_get_program(index);
  1024. }
  1025. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1026. {
  1027. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  1028. return instancePtr->lv2_select_program(bank, program);
  1029. }
  1030. static LV2_State_Status lv2_save(LV2_Handle instance, LV2_State_Store_Function store, LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* features)
  1031. {
  1032. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  1033. return instancePtr->lv2_save(store, handle, flags, features);
  1034. }
  1035. static LV2_State_Status lv2_restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* features)
  1036. {
  1037. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  1038. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  1039. }
  1040. static const void* lv2_extension_data(const char* uri)
  1041. {
  1042. carla_debug("lv2_extension_data(\"%s\")", uri);
  1043. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1044. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1045. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1046. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1047. return &options;
  1048. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1049. return &programs;
  1050. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1051. return &state;
  1052. return nullptr;
  1053. }
  1054. #undef instancePtr
  1055. // -----------------------------------------------------------------------
  1056. // LV2 UI descriptor functions
  1057. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*, LV2UI_Write_Function writeFunction,
  1058. LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  1059. {
  1060. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  1061. NativePlugin* plugin = nullptr;
  1062. for (int i=0; features[i] != nullptr; ++i)
  1063. {
  1064. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1065. {
  1066. plugin = (NativePlugin*)features[i]->data;
  1067. break;
  1068. }
  1069. }
  1070. if (plugin == nullptr)
  1071. {
  1072. carla_stderr("Host doesn't support instance-access, cannot show UI");
  1073. return nullptr;
  1074. }
  1075. plugin->lv2ui_instantiate(writeFunction, controller, widget, features);
  1076. return (LV2UI_Handle)plugin;
  1077. }
  1078. #define uiPtr ((NativePlugin*)ui)
  1079. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1080. {
  1081. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  1082. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  1083. }
  1084. static void lv2ui_cleanup(LV2UI_Handle ui)
  1085. {
  1086. carla_debug("lv2ui_cleanup(%p)", ui);
  1087. uiPtr->lv2ui_cleanup();
  1088. }
  1089. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  1090. {
  1091. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  1092. uiPtr->lv2ui_select_program(bank, program);
  1093. }
  1094. static int lv2ui_idle(LV2UI_Handle ui)
  1095. {
  1096. return uiPtr->lv2ui_idle();
  1097. }
  1098. static int lv2ui_show(LV2UI_Handle ui)
  1099. {
  1100. carla_debug("lv2ui_show(%p)", ui);
  1101. return uiPtr->lv2ui_show();
  1102. }
  1103. static int lv2ui_hide(LV2UI_Handle ui)
  1104. {
  1105. carla_debug("lv2ui_hide(%p)", ui);
  1106. return uiPtr->lv2ui_hide();
  1107. }
  1108. static const void* lv2ui_extension_data(const char* uri)
  1109. {
  1110. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  1111. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  1112. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  1113. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  1114. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  1115. return &uiidle;
  1116. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  1117. return &uishow;
  1118. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  1119. return &uiprograms;
  1120. return nullptr;
  1121. }
  1122. #undef uiPtr
  1123. // -----------------------------------------------------------------------
  1124. // Startup code
  1125. CARLA_EXPORT
  1126. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1127. {
  1128. carla_debug("lv2_descriptor(%i)", index);
  1129. PluginListManager& plm(PluginListManager::getInstance());
  1130. if (index >= plm.descs.count())
  1131. {
  1132. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1133. return nullptr;
  1134. }
  1135. if (index < plm.lv2Descs.count())
  1136. {
  1137. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1138. return plm.lv2Descs.getAt(index, nullptr);
  1139. }
  1140. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  1141. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  1142. CarlaString tmpURI;
  1143. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1144. tmpURI += pluginDesc->label;
  1145. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1146. const LV2_Descriptor lv2DescTmp = {
  1147. /* URI */ carla_strdup(tmpURI),
  1148. /* instantiate */ lv2_instantiate,
  1149. /* connect_port */ lv2_connect_port,
  1150. /* activate */ lv2_activate,
  1151. /* run */ lv2_run,
  1152. /* deactivate */ lv2_deactivate,
  1153. /* cleanup */ lv2_cleanup,
  1154. /* extension_data */ lv2_extension_data
  1155. };
  1156. LV2_Descriptor* const lv2Desc(new LV2_Descriptor);
  1157. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1158. plm.lv2Descs.append(lv2Desc);
  1159. return lv2Desc;
  1160. }
  1161. CARLA_EXPORT
  1162. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1163. {
  1164. carla_debug("lv2ui_descriptor(%i)", index);
  1165. static const LV2UI_Descriptor lv2UiDesc = {
  1166. /* URI */ "http://kxstudio.sf.net/carla/ui",
  1167. /* instantiate */ lv2ui_instantiate,
  1168. /* cleanup */ lv2ui_cleanup,
  1169. /* port_event */ lv2ui_port_event,
  1170. /* extension_data */ lv2ui_extension_data
  1171. };
  1172. return (index == 0) ? &lv2UiDesc : nullptr;
  1173. }
  1174. // -----------------------------------------------------------------------