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.

989 lines
28KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013 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 "carla-native-base.cpp"
  18. #include "CarlaString.hpp"
  19. #include "lv2/atom.h"
  20. #include "lv2/buf-size.h"
  21. #include "lv2/instance-access.h"
  22. #include "lv2/options.h"
  23. #include "lv2/state.h"
  24. #include "lv2/ui.h"
  25. #include "lv2/lv2_external_ui.h"
  26. #include <QtCore/Qt>
  27. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  28. # include <QtWidgets/QFileDialog>
  29. #else
  30. # include <QtGui/QFileDialog>
  31. #endif
  32. // -----------------------------------------------------------------------
  33. // LV2 descriptor functions
  34. class NativePlugin : public LV2_External_UI_Widget
  35. {
  36. public:
  37. static const uint32_t kMaxMidiEvents = 512;
  38. NativePlugin(const PluginDescriptor* const desc, const double sampleRate, const char* const bundlePath, const LV2_Feature* const* features)
  39. : fHandle(nullptr),
  40. fDescriptor(desc),
  41. fMidiEventCount(0),
  42. fIsProcessing(false),
  43. fBufferSize(0),
  44. fSampleRate(sampleRate)
  45. {
  46. run = extui_run;
  47. show = extui_show;
  48. hide = extui_hide;
  49. fHost.handle = this;
  50. fHost.resourceDir = carla_strdup(bundlePath);
  51. fHost.uiName = nullptr;
  52. fHost.get_buffer_size = host_get_buffer_size;
  53. fHost.get_sample_rate = host_get_sample_rate;
  54. fHost.is_offline = host_is_offline;
  55. fHost.get_time_info = host_get_time_info;
  56. fHost.write_midi_event = host_write_midi_event;
  57. fHost.ui_parameter_changed = host_ui_parameter_changed;
  58. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  59. fHost.ui_closed = host_ui_closed;
  60. fHost.ui_open_file = host_ui_open_file;
  61. fHost.ui_save_file = host_ui_save_file;
  62. fHost.dispatcher = host_dispatcher;
  63. const LV2_Options_Option* options = nullptr;
  64. const LV2_URID_Map* uridMap = nullptr;
  65. for (int i=0; features[i] != nullptr; ++i)
  66. {
  67. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  68. options = (const LV2_Options_Option*)features[i]->data;
  69. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  70. uridMap = (const LV2_URID_Map*)features[i]->data;
  71. }
  72. if (options == nullptr || uridMap == nullptr)
  73. {
  74. carla_stderr("Host don't provides option or urid-map features");
  75. return;
  76. }
  77. fBufferSize = 1024;
  78. for (int i=0; options[i].key != 0; ++i)
  79. {
  80. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  81. {
  82. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  83. fBufferSize = *(const int*)options[i].value;
  84. else
  85. carla_stderr("Host provides maxBlockLength but has wrong value type");
  86. break;
  87. }
  88. }
  89. fUridMap = uridMap;
  90. fUI.offset += (desc->midiIns > 0) ? desc->midiIns : 1;
  91. fUI.offset += desc->midiOuts;
  92. fUI.offset += 1; // freewheel
  93. fUI.offset += desc->audioIns;
  94. fUI.offset += desc->audioOuts;
  95. }
  96. ~NativePlugin()
  97. {
  98. CARLA_ASSERT(fHandle == nullptr);
  99. if (fHost.resourceDir != nullptr)
  100. {
  101. delete[] fHost.resourceDir;
  102. fHost.resourceDir = nullptr;
  103. }
  104. }
  105. bool init()
  106. {
  107. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  108. {
  109. carla_stderr("Plugin is missing something...");
  110. return false;
  111. }
  112. if (fBufferSize == 0)
  113. {
  114. carla_stderr("Plugin is missing bufferSize value");
  115. return false;
  116. }
  117. fHandle = fDescriptor->instantiate(&fHost);
  118. if (fHandle == nullptr)
  119. return false;
  120. carla_zeroStruct<MidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  121. carla_zeroStruct<TimeInfo>(fTimeInfo);
  122. fPorts.init(fDescriptor, fHandle);
  123. return true;
  124. }
  125. // -------------------------------------------------------------------
  126. // LV2 functions
  127. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  128. {
  129. fPorts.connectPort(fDescriptor, port, dataLocation);
  130. }
  131. void lv2_activate()
  132. {
  133. if (fDescriptor->activate != nullptr)
  134. fDescriptor->activate(fHandle);
  135. }
  136. void lv2_deactivate()
  137. {
  138. if (fDescriptor->deactivate != nullptr)
  139. fDescriptor->deactivate(fHandle);
  140. }
  141. void lv2_cleanup()
  142. {
  143. if (fDescriptor->cleanup != nullptr)
  144. fDescriptor->cleanup(fHandle);
  145. fHandle = nullptr;
  146. }
  147. void lv2_run(const uint32_t frames)
  148. {
  149. if (frames == 0)
  150. {
  151. updateParameterOutputs();
  152. return;
  153. }
  154. // Check for updated parameters
  155. float curValue;
  156. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  157. {
  158. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  159. curValue = *fPorts.paramsPtr[i];
  160. if (fPorts.paramsLast[i] != curValue && (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT) == 0)
  161. {
  162. fPorts.paramsLast[i] = curValue;
  163. fDescriptor->set_parameter_value(fHandle, i, curValue);
  164. }
  165. }
  166. fDescriptor->process(fHandle, fPorts.audioIns, fPorts.audioOuts, frames, fMidiEventCount, fMidiEvents);
  167. updateParameterOutputs();
  168. }
  169. // -------------------------------------------------------------------
  170. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller, LV2UI_Widget* widget,
  171. const LV2_Feature* const* features)
  172. {
  173. for (int i=0; features[i] != nullptr; ++i)
  174. {
  175. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  176. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  177. {
  178. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  179. break;
  180. }
  181. }
  182. if (fUI.host != nullptr)
  183. fHost.uiName = fUI.host->plugin_human_id;
  184. fUI.writeFunction = writeFunction;
  185. fUI.controller = controller;
  186. *widget = this;
  187. }
  188. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  189. {
  190. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  191. return;
  192. if (portIndex >= fUI.offset || ! fUI.isVisible)
  193. return;
  194. if (fDescriptor->ui_set_parameter_value == nullptr)
  195. return;
  196. const float value(*(const float*)buffer);
  197. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fUI.offset, value);
  198. }
  199. void lv2ui_cleanup()
  200. {
  201. fUI.host = nullptr;
  202. fUI.writeFunction = nullptr;
  203. fUI.controller = nullptr;
  204. if (! fUI.isVisible)
  205. return;
  206. if (fDescriptor->ui_show != nullptr)
  207. fDescriptor->ui_show(fHandle, false);
  208. fUI.isVisible = false;
  209. }
  210. // -------------------------------------------------------------------
  211. protected:
  212. void handleUiRun()
  213. {
  214. // TODO - idle Qt if needed
  215. if (fDescriptor->ui_idle != nullptr)
  216. fDescriptor->ui_idle(fHandle);
  217. }
  218. void handleUiShow()
  219. {
  220. if (fDescriptor->ui_show != nullptr)
  221. fDescriptor->ui_show(fHandle, true);
  222. fUI.isVisible = true;
  223. }
  224. void handleUiHide()
  225. {
  226. if (fDescriptor->ui_show != nullptr)
  227. fDescriptor->ui_show(fHandle, false);
  228. fUI.isVisible = false;
  229. }
  230. // -------------------------------------------------------------------
  231. uint32_t handleGetBufferSize()
  232. {
  233. return fBufferSize;
  234. }
  235. double handleGetSampleRate()
  236. {
  237. return fSampleRate;
  238. }
  239. bool handleIsOffline()
  240. {
  241. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  242. return (fPorts.freewheel != nullptr && *fPorts.freewheel > 0.5f);
  243. }
  244. const TimeInfo* handleGetTimeInfo()
  245. {
  246. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  247. return &fTimeInfo;
  248. }
  249. bool handleWriteMidiEvent(const MidiEvent* const event)
  250. {
  251. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  252. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  253. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  254. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  255. if (fMidiEventCount >= kMaxMidiEvents*2)
  256. return false;
  257. // reverse-find first free event, and put it there
  258. for (uint32_t i=(kMaxMidiEvents*2)-1; i >= fMidiEventCount; --i)
  259. {
  260. if (fMidiEvents[i].data[0] == 0)
  261. {
  262. std::memcpy(&fMidiEvents[i], event, sizeof(MidiEvent));
  263. break;
  264. }
  265. }
  266. return true;
  267. }
  268. void handleUiParameterChanged(const uint32_t index, const float value)
  269. {
  270. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  271. fUI.writeFunction(fUI.controller, index+fUI.offset, sizeof(float), 0, &value);
  272. }
  273. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/)
  274. {
  275. //storeCustomData(key, value);
  276. }
  277. void handleUiClosed()
  278. {
  279. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  280. fUI.host->ui_closed(fUI.controller);
  281. fUI.host = nullptr;
  282. fUI.writeFunction = nullptr;
  283. fUI.controller = nullptr;
  284. fUI.isVisible = false;
  285. }
  286. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  287. {
  288. static CarlaString retStr;
  289. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  290. retStr = QFileDialog::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  291. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  292. }
  293. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  294. {
  295. static CarlaString retStr;
  296. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  297. retStr = QFileDialog::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  298. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  299. }
  300. intptr_t handleDispatcher(const ::HostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  301. {
  302. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  303. intptr_t ret = 0;
  304. switch (opcode)
  305. {
  306. case HOST_OPCODE_NULL:
  307. break;
  308. case HOST_OPCODE_SET_VOLUME:
  309. //setVolume(opt, true, true);
  310. break;
  311. case HOST_OPCODE_SET_DRYWET:
  312. //setDryWet(opt, true, true);
  313. break;
  314. case HOST_OPCODE_SET_BALANCE_LEFT:
  315. //setBalanceLeft(opt, true, true);
  316. break;
  317. case HOST_OPCODE_SET_BALANCE_RIGHT:
  318. //setBalanceRight(opt, true, true);
  319. break;
  320. case HOST_OPCODE_SET_PANNING:
  321. //setPanning(opt, true, true);
  322. break;
  323. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  324. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  325. case HOST_OPCODE_SET_PROCESS_PRECISION:
  326. case HOST_OPCODE_UPDATE_PARAMETER:
  327. case HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  328. case HOST_OPCODE_RELOAD_PARAMETERS:
  329. case HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  330. case HOST_OPCODE_RELOAD_ALL:
  331. break;
  332. case HOST_OPCODE_UI_UNAVAILABLE:
  333. handleUiClosed();
  334. break;
  335. }
  336. return ret;
  337. // unused for now
  338. (void)index;
  339. (void)value;
  340. (void)ptr;
  341. }
  342. void updateParameterOutputs()
  343. {
  344. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  345. {
  346. if (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT)
  347. {
  348. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  349. if (fPorts.paramsPtr[i] != nullptr)
  350. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  351. }
  352. }
  353. }
  354. // -------------------------------------------------------------------
  355. private:
  356. // Native data
  357. PluginHandle fHandle;
  358. HostDescriptor fHost;
  359. const PluginDescriptor* const fDescriptor;
  360. uint32_t fMidiEventCount;
  361. MidiEvent fMidiEvents[kMaxMidiEvents*2];
  362. TimeInfo fTimeInfo;
  363. bool fIsProcessing;
  364. // Lv2 host data
  365. uint32_t fBufferSize;
  366. double fSampleRate;
  367. const LV2_URID_Map* fUridMap;
  368. struct UI {
  369. const LV2_External_UI_Host* host;
  370. LV2UI_Write_Function writeFunction;
  371. LV2UI_Controller controller;
  372. bool isVisible;
  373. uint32_t offset;
  374. UI()
  375. : host(nullptr),
  376. writeFunction(nullptr),
  377. controller(nullptr),
  378. isVisible(false),
  379. offset(0) {}
  380. } fUI;
  381. struct Ports {
  382. LV2_Atom_Sequence** eventsIn;
  383. LV2_Atom_Sequence** midiOuts;
  384. float** audioIns;
  385. float** audioOuts;
  386. float* freewheel;
  387. uint32_t paramCount;
  388. float* paramsLast;
  389. float** paramsPtr;
  390. Ports()
  391. : eventsIn(nullptr),
  392. midiOuts(nullptr),
  393. audioIns(nullptr),
  394. audioOuts(nullptr),
  395. freewheel(nullptr),
  396. paramCount(0),
  397. paramsLast(nullptr),
  398. paramsPtr(nullptr) {}
  399. ~Ports()
  400. {
  401. if (eventsIn != nullptr)
  402. {
  403. delete[] eventsIn;
  404. eventsIn = nullptr;
  405. }
  406. if (midiOuts != nullptr)
  407. {
  408. delete[] midiOuts;
  409. midiOuts = nullptr;
  410. }
  411. if (audioIns != nullptr)
  412. {
  413. delete[] audioIns;
  414. audioIns = nullptr;
  415. }
  416. if (audioOuts != nullptr)
  417. {
  418. delete[] audioOuts;
  419. audioOuts = nullptr;
  420. }
  421. if (paramsLast != nullptr)
  422. {
  423. delete[] paramsLast;
  424. paramsLast = nullptr;
  425. }
  426. if (paramsPtr != nullptr)
  427. {
  428. delete[] paramsPtr;
  429. paramsPtr = nullptr;
  430. }
  431. }
  432. void init(const PluginDescriptor* const desc, PluginHandle handle)
  433. {
  434. if (desc->midiIns > 0)
  435. {
  436. eventsIn = new LV2_Atom_Sequence*[desc->midiIns];
  437. for (uint32_t i=0; i < desc->midiIns; ++i)
  438. eventsIn[i] = nullptr;
  439. }
  440. else
  441. {
  442. eventsIn = new LV2_Atom_Sequence*[1];
  443. eventsIn[0] = nullptr;
  444. }
  445. if (desc->midiOuts > 0)
  446. {
  447. midiOuts = new LV2_Atom_Sequence*[desc->midiOuts];
  448. for (uint32_t i=0; i < desc->midiOuts; ++i)
  449. midiOuts[i] = nullptr;
  450. }
  451. if (desc->audioIns > 0)
  452. {
  453. audioIns = new float*[desc->audioIns];
  454. for (uint32_t i=0; i < desc->audioIns; ++i)
  455. audioIns[i] = nullptr;
  456. }
  457. if (desc->audioOuts > 0)
  458. {
  459. audioOuts = new float*[desc->audioOuts];
  460. for (uint32_t i=0; i < desc->audioOuts; ++i)
  461. audioOuts[i] = nullptr;
  462. }
  463. if (desc->get_parameter_count != nullptr && desc->get_parameter_info != nullptr && desc->get_parameter_value != nullptr && desc->set_parameter_value != nullptr)
  464. {
  465. paramCount = desc->get_parameter_count(handle);
  466. if (paramCount > 0)
  467. {
  468. paramsLast = new float[paramCount];
  469. paramsPtr = new float*[paramCount];
  470. for (uint32_t i=0; i < paramCount; ++i)
  471. {
  472. paramsLast[i] = desc->get_parameter_value(handle, i);
  473. paramsPtr[i] = nullptr;
  474. }
  475. }
  476. }
  477. }
  478. void connectPort(const PluginDescriptor* const desc, const uint32_t port, void* const dataLocation)
  479. {
  480. uint32_t index = 0;
  481. if (port == index++)
  482. {
  483. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  484. return;
  485. }
  486. for (uint32_t i=1; i < desc->midiIns; ++i)
  487. {
  488. if (port == index++)
  489. {
  490. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  491. return;
  492. }
  493. }
  494. for (uint32_t i=0; i < desc->midiOuts; ++i)
  495. {
  496. if (port == index++)
  497. {
  498. midiOuts[i] = (LV2_Atom_Sequence*)dataLocation;
  499. return;
  500. }
  501. }
  502. if (port == index++)
  503. {
  504. freewheel = (float*)dataLocation;
  505. return;
  506. }
  507. for (uint32_t i=0; i < desc->audioIns; ++i)
  508. {
  509. if (port == index++)
  510. {
  511. audioIns[i] = (float*)dataLocation;
  512. return;
  513. }
  514. }
  515. for (uint32_t i=0; i < desc->audioOuts; ++i)
  516. {
  517. if (port == index++)
  518. {
  519. audioOuts[i] = (float*)dataLocation;
  520. return;
  521. }
  522. }
  523. for (uint32_t i=0; i < paramCount; ++i)
  524. {
  525. if (port == index++)
  526. {
  527. paramsPtr[i] = (float*)dataLocation;
  528. return;
  529. }
  530. }
  531. }
  532. } fPorts;
  533. // -------------------------------------------------------------------
  534. #define handlePtr ((NativePlugin*)_this_)
  535. static void extui_run(LV2_External_UI_Widget* _this_)
  536. {
  537. handlePtr->handleUiRun();
  538. }
  539. static void extui_show(LV2_External_UI_Widget* _this_)
  540. {
  541. handlePtr->handleUiShow();
  542. }
  543. static void extui_hide(LV2_External_UI_Widget* _this_)
  544. {
  545. handlePtr->handleUiHide();
  546. }
  547. #undef handlePtr
  548. // -------------------------------------------------------------------
  549. #define handlePtr ((NativePlugin*)handle)
  550. static uint32_t host_get_buffer_size(HostHandle handle)
  551. {
  552. return handlePtr->handleGetBufferSize();
  553. }
  554. static double host_get_sample_rate(HostHandle handle)
  555. {
  556. return handlePtr->handleGetSampleRate();
  557. }
  558. static bool host_is_offline(HostHandle handle)
  559. {
  560. return handlePtr->handleIsOffline();
  561. }
  562. static const TimeInfo* host_get_time_info(HostHandle handle)
  563. {
  564. return handlePtr->handleGetTimeInfo();
  565. }
  566. static bool host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  567. {
  568. return handlePtr->handleWriteMidiEvent(event);
  569. }
  570. static void host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  571. {
  572. handlePtr->handleUiParameterChanged(index, value);
  573. }
  574. static void host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  575. {
  576. handlePtr->handleUiCustomDataChanged(key, value);
  577. }
  578. static void host_ui_closed(HostHandle handle)
  579. {
  580. handlePtr->handleUiClosed();
  581. }
  582. static const char* host_ui_open_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  583. {
  584. return handlePtr->handleUiOpenFile(isDir, title, filter);
  585. }
  586. static const char* host_ui_save_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  587. {
  588. return handlePtr->handleUiSaveFile(isDir, title, filter);
  589. }
  590. static intptr_t host_dispatcher(HostHandle handle, HostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  591. {
  592. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  593. }
  594. #undef handlePtr
  595. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  596. };
  597. // -----------------------------------------------------------------------
  598. // LV2 descriptor functions
  599. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  600. {
  601. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  602. const PluginDescriptor* pluginDesc = nullptr;
  603. const char* pluginLabel = nullptr;
  604. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  605. pluginLabel = lv2Descriptor->URI+37;
  606. else if (std::strcmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla") == 0)
  607. pluginLabel = lv2Descriptor->URI+23;
  608. if (pluginLabel == nullptr)
  609. {
  610. carla_stderr("Failed to find carla native plugin with URI: \"%s\"", lv2Descriptor->URI);
  611. return nullptr;
  612. }
  613. carla_debug("lv2_instantiate() - looking up label %s", pluginLabel);
  614. for (NonRtList<const PluginDescriptor*>::Itenerator it = sPluginDescsMgr.descs.begin(); it.valid(); it.next())
  615. {
  616. const PluginDescriptor*& tmpDesc(*it);
  617. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  618. {
  619. pluginDesc = tmpDesc;
  620. break;
  621. }
  622. }
  623. if (pluginDesc == nullptr)
  624. {
  625. carla_stderr("Failed to find carla native plugin with label: \"%s\"", pluginLabel);
  626. return nullptr;
  627. }
  628. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  629. if (! plugin->init())
  630. {
  631. carla_stderr("Failed to init plugin");
  632. delete plugin;
  633. return nullptr;
  634. }
  635. return (LV2_Handle)plugin;
  636. }
  637. #define instancePtr ((NativePlugin*)instance)
  638. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  639. {
  640. instancePtr->lv2_connect_port(port, dataLocation);
  641. }
  642. static void lv2_activate(LV2_Handle instance)
  643. {
  644. carla_debug("lv2_activate(%p)", instance);
  645. instancePtr->lv2_activate();
  646. }
  647. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  648. {
  649. instancePtr->lv2_run(sampleCount);
  650. }
  651. static void lv2_deactivate(LV2_Handle instance)
  652. {
  653. carla_debug("lv2_deactivate(%p)", instance);
  654. instancePtr->lv2_deactivate();
  655. }
  656. static void lv2_cleanup(LV2_Handle instance)
  657. {
  658. carla_debug("lv2_cleanup(%p)", instance);
  659. instancePtr->lv2_cleanup();
  660. delete instancePtr;
  661. }
  662. #if 0
  663. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  664. {
  665. carla_debug("lv2_()", );
  666. return instancePtr->lv2_get_options(options);
  667. }
  668. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  669. {
  670. carla_debug("lv2_()", );
  671. return instancePtr->lv2_set_options(options);
  672. }
  673. 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)
  674. {
  675. carla_debug("lv2_()", );
  676. return instancePtr->lv2_save(store, handle, flags, features);
  677. }
  678. 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)
  679. {
  680. carla_debug("lv2_()", );
  681. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  682. }
  683. #endif
  684. static const void* lv2_extension_data(const char* uri)
  685. {
  686. carla_debug("lv2_extension_data(%s)", uri);
  687. #if 0
  688. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  689. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  690. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  691. return &options;
  692. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  693. return &state;
  694. #endif
  695. return nullptr;
  696. }
  697. #undef instancePtr
  698. // -----------------------------------------------------------------------
  699. // Startup code
  700. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*, LV2UI_Write_Function writeFunction,
  701. LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  702. {
  703. NativePlugin* plugin = nullptr;
  704. for (int i=0; features[i] != nullptr; ++i)
  705. {
  706. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  707. {
  708. plugin = (NativePlugin*)features[i]->data;
  709. break;
  710. }
  711. }
  712. if (plugin == nullptr)
  713. {
  714. carla_stderr("Host doesn't support instance-access, cannot show UI");
  715. return nullptr;
  716. }
  717. plugin->lv2ui_instantiate(writeFunction, controller, widget, features);
  718. return (LV2UI_Handle)plugin;
  719. }
  720. #define uiPtr ((NativePlugin*)ui)
  721. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  722. {
  723. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  724. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  725. }
  726. static void lv2ui_cleanup(LV2UI_Handle ui)
  727. {
  728. carla_debug("lv2ui_cleanup(%p)", ui);
  729. uiPtr->lv2ui_cleanup();
  730. }
  731. #undef uiPtr
  732. // -----------------------------------------------------------------------
  733. // Startup code
  734. CARLA_EXPORT const LV2_Descriptor* lv2_descriptor(uint32_t index)
  735. {
  736. carla_debug("lv2_descriptor(%i)", index);
  737. if (index >= sPluginDescsMgr.descs.count())
  738. {
  739. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  740. return nullptr;
  741. }
  742. if (index < sPluginDescsMgr.lv2Descs.count())
  743. {
  744. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  745. return sPluginDescsMgr.lv2Descs.getAt(index);
  746. }
  747. const PluginDescriptor*& pluginDesc(sPluginDescsMgr.descs.getAt(index));
  748. CarlaString tmpURI;
  749. if (std::strcmp(pluginDesc->label, "carla") == 0)
  750. {
  751. tmpURI = "http://kxstudio.sf.net/carla";
  752. }
  753. else
  754. {
  755. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  756. tmpURI += pluginDesc->label;
  757. }
  758. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri: %s", index, (const char*)tmpURI);
  759. const LV2_Descriptor* const lv2Desc(new const LV2_Descriptor{
  760. /* URI */ carla_strdup(tmpURI),
  761. /* instantiate */ lv2_instantiate,
  762. /* connect_port */ lv2_connect_port,
  763. /* activate */ lv2_activate,
  764. /* run */ lv2_run,
  765. /* deactivate */ lv2_deactivate,
  766. /* cleanup */ lv2_cleanup,
  767. /* extension_data */ lv2_extension_data
  768. });
  769. sPluginDescsMgr.lv2Descs.append(lv2Desc);
  770. return sPluginDescsMgr.lv2Descs.getLast();
  771. }
  772. CARLA_EXPORT const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  773. {
  774. carla_debug("lv2ui_descriptor(%i)", index);
  775. static const LV2UI_Descriptor lv2UiDesc = {
  776. /* URI */ "http://kxstudio.sf.net/carla#UI",
  777. /* instantiate */ lv2ui_instantiate,
  778. /* cleanup */ lv2ui_cleanup,
  779. /* port_event */ lv2ui_port_event,
  780. /* extension_data */ nullptr
  781. };
  782. static const LV2UI_Descriptor lv2UiDescOld = {
  783. /* URI */ "http://kxstudio.sf.net/carla#UIold",
  784. /* instantiate */ lv2ui_instantiate,
  785. /* cleanup */ lv2ui_cleanup,
  786. /* port_event */ lv2ui_port_event,
  787. /* extension_data */ nullptr
  788. };
  789. switch (index)
  790. {
  791. case 0:
  792. return &lv2UiDesc;
  793. case 1:
  794. return &lv2UiDescOld;
  795. default:
  796. return nullptr;
  797. }
  798. }
  799. // -----------------------------------------------------------------------