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.

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