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.

1502 lines
47KB

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