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.

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