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.

1333 lines
44KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2020 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-base.cpp"
  19. #include "CarlaLv2Utils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaPipeUtils.hpp"
  22. #include "CarlaString.hpp"
  23. #if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  24. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  25. # pragma GCC diagnostic push
  26. # pragma GCC diagnostic ignored "-Wconversion"
  27. # pragma GCC diagnostic ignored "-Weffc++"
  28. # pragma GCC diagnostic ignored "-Wsign-conversion"
  29. # pragma GCC diagnostic ignored "-Wundef"
  30. # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  31. # endif
  32. # include "AppConfig.h"
  33. # include "juce_events/juce_events.h"
  34. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  35. # pragma GCC diagnostic pop
  36. # endif
  37. #endif
  38. #include "water/files/File.h"
  39. static const char* const kPathForCarlaFiles = "carlafiles";
  40. template<>
  41. void Lv2PluginBaseClass<NativeTimeInfo>::clearTimeData() noexcept
  42. {
  43. fLastPositionData.clear();
  44. carla_zeroStruct(fTimeInfo);
  45. }
  46. // --------------------------------------------------------------------------------------------------------------------
  47. // Carla Internal Plugin API exposed as LV2 plugin
  48. class NativePlugin : public Lv2PluginBaseClass<NativeTimeInfo>
  49. {
  50. public:
  51. static const uint32_t kMaxMidiEvents = 512;
  52. NativePlugin(const NativePluginDescriptor* const desc,
  53. const double sampleRate,
  54. const char* const bundlePath,
  55. const LV2_Feature* const* const features)
  56. : Lv2PluginBaseClass<NativeTimeInfo>(sampleRate, features),
  57. fHandle(nullptr),
  58. fHost(),
  59. fDescriptor(desc),
  60. #ifdef CARLA_PROPER_CPP11_SUPPORT
  61. fProgramDesc({0, 0, nullptr}),
  62. #endif
  63. kIgnoreParameters(std::strncmp(desc->label, "carla", 5) == 0),
  64. fMidiEventCount(0),
  65. #if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  66. fJuceInitialiser(),
  67. #endif
  68. fLastProjectPath(nullptr),
  69. fLoadedFile(),
  70. fWorkerUISignal(0)
  71. {
  72. carla_zeroStruct(fHost);
  73. #ifndef CARLA_PROPER_CPP11_SUPPORT
  74. carla_zeroStruct(fProgramDesc);
  75. #endif
  76. if (! loadedInProperHost())
  77. return;
  78. using water::File;
  79. using water::String;
  80. String resourceDir(water::File(bundlePath).getChildFile("resources").getFullPathName());
  81. fHost.handle = this;
  82. fHost.resourceDir = carla_strdup(resourceDir.toRawUTF8());
  83. fHost.uiName = nullptr;
  84. fHost.uiParentId = 0;
  85. fHost.get_buffer_size = host_get_buffer_size;
  86. fHost.get_sample_rate = host_get_sample_rate;
  87. fHost.is_offline = host_is_offline;
  88. fHost.get_time_info = host_get_time_info;
  89. fHost.write_midi_event = host_write_midi_event;
  90. fHost.ui_parameter_changed = host_ui_parameter_changed;
  91. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  92. fHost.ui_closed = host_ui_closed;
  93. fHost.ui_open_file = host_ui_open_file;
  94. fHost.ui_save_file = host_ui_save_file;
  95. fHost.dispatcher = host_dispatcher;
  96. #if 0
  97. // NOTE: a few hosts crash with this :(
  98. if (fMakePath != nullptr && fMakePath->path != nullptr)
  99. fLastProjectPath = fMakePath->path(fMakePath->handle, kPathForCarlaFiles);
  100. #endif
  101. }
  102. ~NativePlugin()
  103. {
  104. CARLA_SAFE_ASSERT(fHandle == nullptr);
  105. if (fHost.resourceDir != nullptr)
  106. {
  107. delete[] fHost.resourceDir;
  108. fHost.resourceDir = nullptr;
  109. }
  110. if (fHost.uiName != nullptr)
  111. {
  112. delete[] fHost.uiName;
  113. fHost.uiName = nullptr;
  114. }
  115. if (fLastProjectPath != nullptr)
  116. {
  117. if (fFreePath != nullptr && fFreePath->free_path != nullptr)
  118. fFreePath->free_path(fFreePath->handle, fLastProjectPath);
  119. #ifndef CARLA_OS_WIN
  120. // this is not safe to call under windows
  121. else
  122. std::free(fLastProjectPath);
  123. #endif
  124. fLastProjectPath = nullptr;
  125. }
  126. }
  127. // ----------------------------------------------------------------------------------------------------------------
  128. bool init()
  129. {
  130. if (fHost.resourceDir == nullptr)
  131. return false;
  132. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  133. {
  134. carla_stderr("Plugin is missing something...");
  135. return false;
  136. }
  137. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  138. fHandle = fDescriptor->instantiate(&fHost);
  139. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  140. fPorts.hasUI = fDescriptor->hints & NATIVE_PLUGIN_HAS_UI;
  141. fPorts.usesTime = fDescriptor->hints & NATIVE_PLUGIN_USES_TIME;
  142. fPorts.numAudioIns = fDescriptor->audioIns;
  143. fPorts.numAudioOuts = fDescriptor->audioOuts;
  144. fPorts.numCVIns = fDescriptor->cvIns;
  145. fPorts.numCVOuts = fDescriptor->cvOuts;
  146. fPorts.numMidiIns = fDescriptor->midiIns;
  147. fPorts.numMidiOuts = fDescriptor->midiOuts;
  148. if (fDescriptor->get_parameter_count != nullptr &&
  149. fDescriptor->get_parameter_info != nullptr &&
  150. fDescriptor->get_parameter_value != nullptr &&
  151. fDescriptor->set_parameter_value != nullptr &&
  152. ! kIgnoreParameters)
  153. {
  154. fPorts.numParams = fDescriptor->get_parameter_count(fHandle);
  155. }
  156. fPorts.init();
  157. if (fPorts.numParams > 0)
  158. {
  159. for (uint32_t i=0; i < fPorts.numParams; ++i)
  160. {
  161. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  162. fPorts.paramsOut [i] = fDescriptor->get_parameter_info(fHandle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT;
  163. }
  164. }
  165. return true;
  166. }
  167. // ----------------------------------------------------------------------------------------------------------------
  168. // LV2 functions
  169. void lv2_activate()
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(! fIsActive,);
  172. resetTimeInfo();
  173. if (fDescriptor->activate != nullptr)
  174. fDescriptor->activate(fHandle);
  175. fIsActive = true;
  176. }
  177. void lv2_deactivate()
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(fIsActive,);
  180. fIsActive = false;
  181. if (fDescriptor->deactivate != nullptr)
  182. fDescriptor->deactivate(fHandle);
  183. }
  184. void lv2_cleanup()
  185. {
  186. if (fIsActive)
  187. {
  188. carla_stderr("Warning: Host forgot to call deactivate!");
  189. fIsActive = false;
  190. if (fDescriptor->deactivate != nullptr)
  191. fDescriptor->deactivate(fHandle);
  192. }
  193. if (fDescriptor->cleanup != nullptr)
  194. fDescriptor->cleanup(fHandle);
  195. fHandle = nullptr;
  196. }
  197. // ----------------------------------------------------------------------------------------------------------------
  198. void lv2_run(const uint32_t frames)
  199. {
  200. if (! lv2_pre_run(frames))
  201. {
  202. updateParameterOutputs();
  203. return;
  204. }
  205. if (fPorts.numMidiIns > 0 || fPorts.hasUI)
  206. {
  207. uint32_t numEventsIn;
  208. if (fPorts.numMidiIns > 0)
  209. {
  210. numEventsIn = fPorts.numMidiIns;
  211. fMidiEventCount = 0;
  212. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  213. }
  214. else
  215. {
  216. numEventsIn = 1;
  217. }
  218. for (uint32_t i=0; i < numEventsIn; ++i)
  219. {
  220. const LV2_Atom_Sequence* const eventsIn(fPorts.eventsIn[i]);
  221. CARLA_SAFE_ASSERT_CONTINUE(eventsIn != nullptr);
  222. LV2_ATOM_SEQUENCE_FOREACH(eventsIn, event)
  223. {
  224. if (event == nullptr)
  225. continue;
  226. if (event->body.type == fURIs.uiEvents && fWorkerUISignal != -1)
  227. {
  228. CARLA_SAFE_ASSERT_CONTINUE((fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE) == 0);
  229. if (fWorker != nullptr)
  230. {
  231. // worker is supported by the host, we can continue
  232. fWorkerUISignal = 1;
  233. const char* const msg((const char*)(event + 1));
  234. const size_t msgSize = std::strlen(msg);
  235. fWorker->schedule_work(fWorker->handle, static_cast<uint32_t>(msgSize + 1U), msg);
  236. }
  237. else
  238. {
  239. // worker is not supported, cancel
  240. fWorkerUISignal = -1;
  241. }
  242. continue;
  243. }
  244. if (event->body.type == fURIs.atomObject)
  245. {
  246. const LV2_Atom_Object* const obj = (const LV2_Atom_Object*)(&event->body);
  247. if (obj->body.otype == fURIs.patchSet) {
  248. // Get property URI.
  249. const LV2_Atom* property = nullptr;
  250. lv2_atom_object_get(obj, fURIs.patchProperty, &property, 0);
  251. CARLA_SAFE_ASSERT_CONTINUE(property != nullptr);
  252. CARLA_SAFE_ASSERT_CONTINUE(property->type == fURIs.atomURID);
  253. const LV2_URID urid = ((const LV2_Atom_URID*)property)->body;
  254. /* */ if (std::strcmp(fDescriptor->label, "audiofile") == 0) {
  255. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFileAudio);
  256. } else if (std::strcmp(fDescriptor->label, "midifile") == 0) {
  257. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFileMIDI);
  258. } else {
  259. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFile);
  260. }
  261. // Get value.
  262. const LV2_Atom* fileobj = nullptr;
  263. lv2_atom_object_get(obj, fURIs.patchValue, &fileobj, 0);
  264. CARLA_SAFE_ASSERT_CONTINUE(fileobj != nullptr);
  265. CARLA_SAFE_ASSERT_CONTINUE(fileobj->type == fURIs.atomPath);
  266. const char* const filepath((const char*)(fileobj + 1));
  267. fWorker->schedule_work(fWorker->handle,
  268. static_cast<uint32_t>(std::strlen(filepath) + 1U),
  269. filepath);
  270. }
  271. continue;
  272. }
  273. if (event->body.type != fURIs.midiEvent)
  274. continue;
  275. if (event->body.size > 4)
  276. continue;
  277. if (event->time.frames >= frames)
  278. break;
  279. const uint8_t* const data((const uint8_t*)(event + 1));
  280. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  281. nativeEvent.port = (uint8_t)i;
  282. nativeEvent.size = (uint8_t)event->body.size;
  283. nativeEvent.time = (uint32_t)event->time.frames;
  284. uint32_t j=0;
  285. for (uint32_t size=event->body.size; j<size; ++j)
  286. nativeEvent.data[j] = data[j];
  287. for (; j<4; ++j)
  288. nativeEvent.data[j] = 0;
  289. if (fMidiEventCount >= kMaxMidiEvents)
  290. break;
  291. }
  292. }
  293. }
  294. fDescriptor->process(fHandle, fPorts.audioCVIns, fPorts.audioCVOuts, frames, fMidiEvents, fMidiEventCount);
  295. if (fWorkerUISignal == -1 && fPorts.hasUI)
  296. {
  297. const char* const msg = "quit";
  298. const size_t msgSize = 5;
  299. LV2_Atom_Sequence* const seq(fPorts.eventsOut[0]);
  300. Ports::EventsOutData& mData(fPorts.eventsOutData[0]);
  301. if (sizeof(LV2_Atom_Event) + msgSize <= mData.capacity - mData.offset)
  302. {
  303. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  304. aev->time.frames = 0;
  305. aev->body.size = msgSize;
  306. aev->body.type = fURIs.uiEvents;
  307. std::memcpy(LV2_ATOM_BODY(&aev->body), msg, msgSize);
  308. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + msgSize));
  309. mData.offset += size;
  310. seq->atom.size += size;
  311. fWorkerUISignal = 0;
  312. }
  313. }
  314. lv2_post_run(frames);
  315. updateParameterOutputs();
  316. }
  317. // ----------------------------------------------------------------------------------------------------------------
  318. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  319. {
  320. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  321. return nullptr;
  322. if (fDescriptor->get_midi_program_count == nullptr)
  323. return nullptr;
  324. if (fDescriptor->get_midi_program_info == nullptr)
  325. return nullptr;
  326. if (index >= fDescriptor->get_midi_program_count(fHandle))
  327. return nullptr;
  328. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  329. if (midiProg == nullptr)
  330. return nullptr;
  331. fProgramDesc.bank = midiProg->bank;
  332. fProgramDesc.program = midiProg->program;
  333. fProgramDesc.name = midiProg->name;
  334. return &fProgramDesc;
  335. }
  336. void lv2_select_program(uint32_t bank, uint32_t program)
  337. {
  338. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  339. return;
  340. if (fDescriptor->set_midi_program == nullptr)
  341. return;
  342. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  343. for (uint32_t i=0; i < fPorts.numParams; ++i)
  344. {
  345. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  346. if (fPorts.paramsPtr[i] != nullptr)
  347. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  348. }
  349. }
  350. // ----------------------------------------------------------------------------------------------------------------
  351. void saveLastProjectPathIfPossible(const LV2_Feature* const* const features)
  352. {
  353. char* const last = fLastProjectPath;
  354. if (fMakePath != nullptr && fMakePath->path != nullptr)
  355. {
  356. fLastProjectPath = fMakePath->path(fMakePath->handle, kPathForCarlaFiles);
  357. }
  358. else if (features != nullptr)
  359. {
  360. const LV2_State_Make_Path* makePath = nullptr;
  361. for (int i=0; features[i] != nullptr; ++i)
  362. {
  363. if (std::strcmp(features[i]->URI, LV2_STATE__makePath) == 0)
  364. {
  365. makePath = (const LV2_State_Make_Path*)features[i]->data;
  366. break;
  367. }
  368. }
  369. if (makePath != nullptr && makePath->path != nullptr)
  370. fLastProjectPath = makePath->path(makePath->handle, kPathForCarlaFiles);
  371. else
  372. fLastProjectPath = nullptr;
  373. }
  374. else
  375. {
  376. fLastProjectPath = nullptr;
  377. }
  378. if (fFreePath != nullptr && fFreePath->free_path != nullptr)
  379. fFreePath->free_path(fFreePath->handle, last);
  380. #ifndef CARLA_OS_WIN
  381. // this is not safe to call under windows
  382. else
  383. std::free(last);
  384. #endif
  385. }
  386. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle,
  387. const uint32_t /*flags*/, const LV2_Feature* const* const features)
  388. {
  389. saveLastProjectPathIfPossible(features);
  390. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  391. {
  392. store(handle,
  393. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/file"),
  394. fLoadedFile.buffer(),
  395. fLoadedFile.length()+1,
  396. fURIs.atomPath,
  397. LV2_STATE_IS_POD);
  398. return LV2_STATE_SUCCESS;
  399. }
  400. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  401. return LV2_STATE_ERR_NO_FEATURE;
  402. if (char* const state = fDescriptor->get_state(fHandle))
  403. {
  404. store(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"),
  405. state, std::strlen(state)+1, fURIs.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  406. std::free(state);
  407. return LV2_STATE_SUCCESS;
  408. }
  409. return LV2_STATE_ERR_UNKNOWN;
  410. }
  411. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle,
  412. uint32_t flags, const LV2_Feature* const* const features)
  413. {
  414. saveLastProjectPathIfPossible(features);
  415. size_t size = 0;
  416. uint32_t type = 0;
  417. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  418. {
  419. size = type = 0;
  420. const void* const data = retrieve(handle,
  421. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/file"),
  422. &size, &type, &flags);
  423. CARLA_SAFE_ASSERT_RETURN(type == fURIs.atomPath, LV2_STATE_ERR_UNKNOWN);
  424. const char* const filename = (const char*)data;
  425. fLoadedFile = filename;
  426. fDescriptor->set_custom_data(fHandle, "file", filename);
  427. return LV2_STATE_SUCCESS;
  428. }
  429. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  430. return LV2_STATE_ERR_NO_FEATURE;
  431. size = type = 0;
  432. const void* const data = retrieve(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), &size, &type, &flags);
  433. if (size == 0)
  434. return LV2_STATE_ERR_UNKNOWN;
  435. if (type == 0)
  436. return LV2_STATE_ERR_UNKNOWN;
  437. if (data == nullptr)
  438. return LV2_STATE_ERR_UNKNOWN;
  439. if (type != fURIs.atomString)
  440. return LV2_STATE_ERR_BAD_TYPE;
  441. fDescriptor->set_state(fHandle, (const char*)data);
  442. return LV2_STATE_SUCCESS;
  443. }
  444. // ----------------------------------------------------------------------------------------------------------------
  445. LV2_Worker_Status lv2_work(LV2_Worker_Respond_Function, LV2_Worker_Respond_Handle, uint32_t, const void* data)
  446. {
  447. const char* const msg = (const char*)data;
  448. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  449. {
  450. fLoadedFile = msg;
  451. fDescriptor->set_custom_data(fHandle, "file", msg);
  452. return LV2_WORKER_SUCCESS;
  453. }
  454. /**/ if (std::strncmp(msg, "control ", 8) == 0)
  455. {
  456. if (fDescriptor->ui_set_parameter_value == nullptr)
  457. return LV2_WORKER_SUCCESS;
  458. if (const char* const msgSplit = std::strstr(msg+8, " "))
  459. {
  460. const char* const msgIndex = msg+8;
  461. CARLA_SAFE_ASSERT_RETURN(msgSplit - msgIndex < 8, LV2_WORKER_ERR_UNKNOWN);
  462. CARLA_SAFE_ASSERT_RETURN(msgSplit[0] != '\0', LV2_WORKER_ERR_UNKNOWN);
  463. char strBufIndex[8];
  464. carla_zeroChars(strBufIndex, 8);
  465. std::strncpy(strBufIndex, msgIndex, static_cast<size_t>(msgSplit - msgIndex));
  466. const int index = std::atoi(msgIndex) - static_cast<int>(fPorts.indexOffset);
  467. CARLA_SAFE_ASSERT_RETURN(index >= 0, LV2_WORKER_ERR_UNKNOWN);
  468. float value;
  469. {
  470. const CarlaScopedLocale csl;
  471. value = static_cast<float>(std::atof(msgSplit+1));
  472. }
  473. fDescriptor->ui_set_parameter_value(fHandle, static_cast<uint32_t>(index), value);
  474. }
  475. }
  476. else if (std::strcmp(msg, "show") == 0)
  477. {
  478. handleUiShow();
  479. }
  480. else if (std::strcmp(msg, "hide") == 0)
  481. {
  482. handleUiHide();
  483. }
  484. else if (std::strcmp(msg, "idle") == 0)
  485. {
  486. handleUiRun();
  487. }
  488. else if (std::strcmp(msg, "quit") == 0)
  489. {
  490. handleUiClosed();
  491. }
  492. else
  493. {
  494. carla_stdout("lv2_work unknown msg '%s'", msg);
  495. return LV2_WORKER_ERR_UNKNOWN;
  496. }
  497. return LV2_WORKER_SUCCESS;
  498. }
  499. LV2_Worker_Status lv2_work_resp(uint32_t /*size*/, const void* /*body*/)
  500. {
  501. return LV2_WORKER_SUCCESS;
  502. }
  503. // ----------------------------------------------------------------------------------------------------------------
  504. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  505. LV2UI_Widget* widget, const LV2_Feature* const* features)
  506. {
  507. fUI.writeFunction = writeFunction;
  508. fUI.controller = controller;
  509. if (fHost.uiName != nullptr)
  510. {
  511. delete[] fHost.uiName;
  512. fHost.uiName = nullptr;
  513. }
  514. // ---------------------------------------------------------------
  515. // see if the host supports external-ui
  516. for (int i=0; features[i] != nullptr; ++i)
  517. {
  518. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  519. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  520. {
  521. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  522. }
  523. if (std::strcmp(features[i]->URI, LV2_UI__touch) == 0)
  524. {
  525. fUI.touch = (const LV2UI_Touch*)features[i]->data;
  526. }
  527. }
  528. if (fUI.host != nullptr)
  529. {
  530. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  531. *widget = (LV2_External_UI_Widget_Compat*)this;
  532. return;
  533. }
  534. // ---------------------------------------------------------------
  535. // no external-ui support, use showInterface
  536. for (int i=0; features[i] != nullptr; ++i)
  537. {
  538. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) != 0)
  539. continue;
  540. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  541. CARLA_SAFE_ASSERT_BREAK(options != nullptr);
  542. for (int j=0; options[j].key != 0; ++j)
  543. {
  544. if (options[j].key != fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  545. continue;
  546. const char* const title((const char*)options[j].value);
  547. CARLA_SAFE_ASSERT_BREAK(title != nullptr && title[0] != '\0');
  548. fHost.uiName = carla_strdup(title);
  549. break;
  550. }
  551. break;
  552. }
  553. if (fHost.uiName == nullptr)
  554. fHost.uiName = carla_strdup(fDescriptor->name);
  555. *widget = nullptr;
  556. return;
  557. }
  558. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  559. {
  560. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  561. return;
  562. if (portIndex < fPorts.indexOffset || ! fUI.isVisible)
  563. return;
  564. if (fDescriptor->ui_set_parameter_value == nullptr)
  565. return;
  566. const float value(*(const float*)buffer);
  567. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fPorts.indexOffset, value);
  568. }
  569. // ----------------------------------------------------------------------------------------------------------------
  570. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  571. {
  572. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  573. return;
  574. if (fDescriptor->ui_set_midi_program == nullptr)
  575. return;
  576. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  577. }
  578. // ----------------------------------------------------------------------------------------------------------------
  579. protected:
  580. void handleUiRun() const override
  581. {
  582. if (fDescriptor->ui_idle != nullptr)
  583. fDescriptor->ui_idle(fHandle);
  584. }
  585. void handleUiShow() override
  586. {
  587. if (fDescriptor->ui_show != nullptr)
  588. fDescriptor->ui_show(fHandle, true);
  589. fUI.isVisible = true;
  590. }
  591. void handleUiHide() override
  592. {
  593. if (fDescriptor->ui_show != nullptr)
  594. fDescriptor->ui_show(fHandle, false);
  595. fUI.isVisible = false;
  596. }
  597. // ----------------------------------------------------------------------------------------------------------------
  598. void handleParameterValueChanged(const uint32_t index, const float value) override
  599. {
  600. fDescriptor->set_parameter_value(fHandle, index, value);
  601. }
  602. void handleBufferSizeChanged(const uint32_t bufferSize) override
  603. {
  604. if (fDescriptor->dispatcher == nullptr)
  605. return;
  606. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, bufferSize, nullptr, 0.0f);
  607. }
  608. void handleSampleRateChanged(const double sampleRate) override
  609. {
  610. if (fDescriptor->dispatcher == nullptr)
  611. return;
  612. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
  613. }
  614. // ----------------------------------------------------------------------------------------------------------------
  615. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  616. {
  617. CARLA_SAFE_ASSERT_RETURN(fPorts.numMidiOuts > 0, false);
  618. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  619. CARLA_SAFE_ASSERT_RETURN(event->size > 0, false);
  620. const uint8_t port(event->port);
  621. CARLA_SAFE_ASSERT_RETURN(port < fPorts.numMidiOuts, false);
  622. LV2_Atom_Sequence* const seq(fPorts.eventsOut[port]);
  623. CARLA_SAFE_ASSERT_RETURN(seq != nullptr, false);
  624. Ports::EventsOutData& mData(fPorts.eventsOutData[port]);
  625. if (sizeof(LV2_Atom_Event) + event->size > mData.capacity - mData.offset)
  626. return false;
  627. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  628. aev->time.frames = event->time;
  629. aev->body.size = event->size;
  630. aev->body.type = fURIs.midiEvent;
  631. std::memcpy(LV2_ATOM_BODY(&aev->body), event->data, event->size);
  632. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + event->size));
  633. mData.offset += size;
  634. seq->atom.size += size;
  635. return true;
  636. }
  637. void handleUiParameterChanged(const uint32_t index, const float value) const
  638. {
  639. if (kIgnoreParameters || fWorkerUISignal)
  640. return;
  641. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  642. fUI.writeFunction(fUI.controller, index+fPorts.indexOffset, sizeof(float), 0, &value);
  643. }
  644. void handleUiParameterTouch(const uint32_t index, const bool touch) const
  645. {
  646. if (kIgnoreParameters)
  647. return;
  648. if (fUI.touch != nullptr && fUI.touch->touch != nullptr)
  649. fUI.touch->touch(fUI.touch->handle, index+fPorts.indexOffset, touch);
  650. }
  651. void handleUiResize(const uint32_t, const uint32_t) const
  652. {
  653. // nothing here
  654. }
  655. void handleUiCustomDataChanged(const char* const key, const char* const value) const
  656. {
  657. carla_stdout("TODO: handleUiCustomDataChanged %s %s", key, value);
  658. //storeCustomData(key, value);
  659. if (fUI.writeFunction == nullptr || fUI.controller == nullptr)
  660. return;
  661. }
  662. void handleUiClosed()
  663. {
  664. fUI.isVisible = false;
  665. if (fWorkerUISignal)
  666. fWorkerUISignal = -1;
  667. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  668. fUI.host->ui_closed(fUI.controller);
  669. fUI.host = nullptr;
  670. fUI.touch = nullptr;
  671. fUI.writeFunction = nullptr;
  672. fUI.controller = nullptr;
  673. }
  674. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  675. {
  676. // TODO
  677. return nullptr;
  678. }
  679. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  680. {
  681. // TODO
  682. return nullptr;
  683. }
  684. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  685. {
  686. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
  687. opcode, index, value, ptr, static_cast<double>(opt));
  688. intptr_t ret = 0;
  689. switch (opcode)
  690. {
  691. case NATIVE_HOST_OPCODE_NULL:
  692. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  693. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  694. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  695. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  696. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  697. case NATIVE_HOST_OPCODE_HOST_IDLE:
  698. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  699. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  700. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  701. // nothing
  702. break;
  703. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  704. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  705. if (fLastProjectPath != nullptr)
  706. return static_cast<intptr_t>((uintptr_t)fLastProjectPath);
  707. break;
  708. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  709. handleUiClosed();
  710. break;
  711. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  712. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  713. handleUiParameterTouch(static_cast<uint32_t>(index), value != 0);
  714. break;
  715. case NATIVE_HOST_OPCODE_UI_RESIZE:
  716. CARLA_SAFE_ASSERT_RETURN(index > 0, 0);
  717. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  718. handleUiResize(static_cast<uint32_t>(index), static_cast<uint32_t>(value));
  719. break;
  720. }
  721. return ret;
  722. // unused for now
  723. (void)index;
  724. (void)value;
  725. (void)ptr;
  726. (void)opt;
  727. }
  728. void updateParameterOutputs()
  729. {
  730. float value;
  731. for (uint32_t i=0; i < fPorts.numParams; ++i)
  732. {
  733. if (! fPorts.paramsOut[i])
  734. continue;
  735. fPorts.paramsLast[i] = value = fDescriptor->get_parameter_value(fHandle, i);
  736. if (fPorts.paramsPtr[i] != nullptr)
  737. *fPorts.paramsPtr[i] = value;
  738. }
  739. }
  740. // -------------------------------------------------------------------
  741. private:
  742. // Native data
  743. NativePluginHandle fHandle;
  744. NativeHostDescriptor fHost;
  745. const NativePluginDescriptor* const fDescriptor;
  746. LV2_Program_Descriptor fProgramDesc;
  747. // carla as plugin does not implement lv2 parameter API yet, needed for feedback
  748. const bool kIgnoreParameters;
  749. uint32_t fMidiEventCount;
  750. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  751. #if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  752. juce::SharedResourcePointer<juce::ScopedJuceInitialiser_GUI> fJuceInitialiser;
  753. #endif
  754. char* fLastProjectPath;
  755. CarlaString fLoadedFile;
  756. int fWorkerUISignal;
  757. // -------------------------------------------------------------------
  758. #define handlePtr ((NativePlugin*)handle)
  759. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  760. {
  761. return handlePtr->fBufferSize;
  762. }
  763. static double host_get_sample_rate(NativeHostHandle handle)
  764. {
  765. return handlePtr->fSampleRate;
  766. }
  767. static bool host_is_offline(NativeHostHandle handle)
  768. {
  769. return handlePtr->fIsOffline;
  770. }
  771. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  772. {
  773. return &(handlePtr->fTimeInfo);
  774. }
  775. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  776. {
  777. return handlePtr->handleWriteMidiEvent(event);
  778. }
  779. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  780. {
  781. handlePtr->handleUiParameterChanged(index, value);
  782. }
  783. static void host_ui_parameter_touch(NativeHostHandle handle, uint32_t index, bool touch)
  784. {
  785. handlePtr->handleUiParameterTouch(index, touch);
  786. }
  787. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  788. {
  789. handlePtr->handleUiCustomDataChanged(key, value);
  790. }
  791. static void host_ui_closed(NativeHostHandle handle)
  792. {
  793. handlePtr->handleUiClosed();
  794. }
  795. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  796. {
  797. return handlePtr->handleUiOpenFile(isDir, title, filter);
  798. }
  799. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  800. {
  801. return handlePtr->handleUiSaveFile(isDir, title, filter);
  802. }
  803. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  804. {
  805. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  806. }
  807. #undef handlePtr
  808. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  809. };
  810. // -----------------------------------------------------------------------
  811. // LV2 plugin descriptor functions
  812. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  813. {
  814. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  815. const NativePluginDescriptor* pluginDesc = nullptr;
  816. const char* pluginLabel = nullptr;
  817. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  818. pluginLabel = lv2Descriptor->URI+37;
  819. if (pluginLabel == nullptr)
  820. {
  821. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  822. return nullptr;
  823. }
  824. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  825. PluginListManager& plm(PluginListManager::getInstance());
  826. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  827. {
  828. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  829. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  830. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  831. {
  832. pluginDesc = tmpDesc;
  833. break;
  834. }
  835. }
  836. if (pluginDesc == nullptr)
  837. {
  838. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  839. return nullptr;
  840. }
  841. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  842. if (! plugin->init())
  843. {
  844. carla_stderr("Failed to init plugin");
  845. delete plugin;
  846. return nullptr;
  847. }
  848. return (LV2_Handle)plugin;
  849. }
  850. #define instancePtr ((NativePlugin*)instance)
  851. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  852. {
  853. instancePtr->lv2_connect_port(port, dataLocation);
  854. }
  855. static void lv2_activate(LV2_Handle instance)
  856. {
  857. carla_debug("lv2_activate(%p)", instance);
  858. instancePtr->lv2_activate();
  859. }
  860. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  861. {
  862. instancePtr->lv2_run(sampleCount);
  863. }
  864. static void lv2_deactivate(LV2_Handle instance)
  865. {
  866. carla_debug("lv2_deactivate(%p)", instance);
  867. instancePtr->lv2_deactivate();
  868. }
  869. static void lv2_cleanup(LV2_Handle instance)
  870. {
  871. carla_debug("lv2_cleanup(%p)", instance);
  872. instancePtr->lv2_cleanup();
  873. delete instancePtr;
  874. }
  875. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  876. {
  877. carla_debug("lv2_get_options(%p, %p)", instance, options);
  878. return instancePtr->lv2_get_options(options);
  879. }
  880. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  881. {
  882. carla_debug("lv2_set_options(%p, %p)", instance, options);
  883. return instancePtr->lv2_set_options(options);
  884. }
  885. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  886. {
  887. carla_debug("lv2_get_program(%p, %i)", instance, index);
  888. return instancePtr->lv2_get_program(index);
  889. }
  890. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  891. {
  892. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  893. return instancePtr->lv2_select_program(bank, program);
  894. }
  895. 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)
  896. {
  897. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  898. return instancePtr->lv2_save(store, handle, flags, features);
  899. }
  900. 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)
  901. {
  902. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  903. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  904. }
  905. static LV2_Worker_Status lv2_work(LV2_Handle instance, LV2_Worker_Respond_Function respond, LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  906. {
  907. carla_debug("work(%p, %p, %p, %u, %p)", instance, respond, handle, size, data);
  908. return instancePtr->lv2_work(respond, handle, size, data);
  909. }
  910. static LV2_Worker_Status lv2_work_resp(LV2_Handle instance, uint32_t size, const void* body)
  911. {
  912. carla_debug("work_resp(%p, %u, %p)", instance, size, body);
  913. return instancePtr->lv2_work_resp(size, body);
  914. }
  915. static const void* lv2_extension_data(const char* uri)
  916. {
  917. carla_debug("lv2_extension_data(\"%s\")", uri);
  918. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  919. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  920. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  921. static const LV2_Worker_Interface worker = { lv2_work, lv2_work_resp, nullptr };
  922. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  923. return &options;
  924. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  925. return &programs;
  926. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  927. return &state;
  928. if (std::strcmp(uri, LV2_WORKER__interface) == 0)
  929. return &worker;
  930. return nullptr;
  931. }
  932. #undef instancePtr
  933. #ifdef HAVE_PYQT
  934. // -----------------------------------------------------------------------
  935. // LV2 UI descriptor functions
  936. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*,
  937. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  938. LV2UI_Widget* widget, const LV2_Feature* const* features)
  939. {
  940. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  941. NativePlugin* plugin = nullptr;
  942. for (int i=0; features[i] != nullptr; ++i)
  943. {
  944. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  945. {
  946. plugin = (NativePlugin*)features[i]->data;
  947. break;
  948. }
  949. }
  950. if (plugin == nullptr)
  951. {
  952. carla_stderr("Host doesn't support instance-access, cannot show UI");
  953. return nullptr;
  954. }
  955. plugin->lv2ui_instantiate(writeFunction, controller, widget, features);
  956. return (LV2UI_Handle)plugin;
  957. }
  958. #define uiPtr ((NativePlugin*)ui)
  959. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  960. {
  961. carla_debug("lv2ui_port_eventxx(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  962. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  963. }
  964. static void lv2ui_cleanup(LV2UI_Handle ui)
  965. {
  966. carla_debug("lv2ui_cleanup(%p)", ui);
  967. uiPtr->lv2ui_cleanup();
  968. }
  969. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  970. {
  971. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  972. uiPtr->lv2ui_select_program(bank, program);
  973. }
  974. static int lv2ui_idle(LV2UI_Handle ui)
  975. {
  976. return uiPtr->lv2ui_idle();
  977. }
  978. static int lv2ui_show(LV2UI_Handle ui)
  979. {
  980. carla_debug("lv2ui_show(%p)", ui);
  981. return uiPtr->lv2ui_show();
  982. }
  983. static int lv2ui_hide(LV2UI_Handle ui)
  984. {
  985. carla_debug("lv2ui_hide(%p)", ui);
  986. return uiPtr->lv2ui_hide();
  987. }
  988. static const void* lv2ui_extension_data(const char* uri)
  989. {
  990. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  991. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  992. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  993. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  994. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  995. return &uiidle;
  996. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  997. return &uishow;
  998. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  999. return &uiprograms;
  1000. return nullptr;
  1001. }
  1002. #endif
  1003. #undef uiPtr
  1004. // -----------------------------------------------------------------------
  1005. // Startup code
  1006. CARLA_EXPORT
  1007. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1008. {
  1009. carla_debug("lv2_descriptor(%i)", index);
  1010. PluginListManager& plm(PluginListManager::getInstance());
  1011. if (index >= plm.descs.count())
  1012. {
  1013. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1014. return nullptr;
  1015. }
  1016. if (index < plm.lv2Descs.count())
  1017. {
  1018. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1019. return plm.lv2Descs.getAt(index, nullptr);
  1020. }
  1021. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  1022. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  1023. CarlaString tmpURI;
  1024. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1025. tmpURI += pluginDesc->label;
  1026. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1027. const LV2_Descriptor lv2DescTmp = {
  1028. /* URI */ carla_strdup(tmpURI),
  1029. /* instantiate */ lv2_instantiate,
  1030. /* connect_port */ lv2_connect_port,
  1031. /* activate */ lv2_activate,
  1032. /* run */ lv2_run,
  1033. /* deactivate */ lv2_deactivate,
  1034. /* cleanup */ lv2_cleanup,
  1035. /* extension_data */ lv2_extension_data
  1036. };
  1037. LV2_Descriptor* lv2Desc;
  1038. try {
  1039. lv2Desc = new LV2_Descriptor;
  1040. } CARLA_SAFE_EXCEPTION_RETURN("new LV2_Descriptor", nullptr);
  1041. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1042. plm.lv2Descs.append(lv2Desc);
  1043. return lv2Desc;
  1044. }
  1045. #ifdef HAVE_PYQT
  1046. CARLA_EXPORT
  1047. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1048. {
  1049. carla_debug("lv2ui_descriptor(%i)", index);
  1050. static const LV2UI_Descriptor lv2UiExtDesc = {
  1051. /* URI */ "http://kxstudio.sf.net/carla/ui-ext",
  1052. /* instantiate */ lv2ui_instantiate,
  1053. /* cleanup */ lv2ui_cleanup,
  1054. /* port_event */ lv2ui_port_event,
  1055. /* extension_data */ lv2ui_extension_data
  1056. };
  1057. return (index == 0) ? &lv2UiExtDesc : nullptr;
  1058. }
  1059. #endif
  1060. // -----------------------------------------------------------------------