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.

1470 lines
46KB

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