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.

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