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.

carla-lv2.cpp 41KB

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