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 64KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
11 years ago
11 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
11 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #define CARLA_NATIVE_PLUGIN_LV2
  18. #include "carla-base.cpp"
  19. #include "CarlaLv2Utils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaString.hpp"
  22. #include "juce_audio_basics/juce_audio_basics.h"
  23. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  24. # include "juce_gui_basics/juce_gui_basics.h"
  25. #else
  26. namespace juce {
  27. # include "juce_events/messages/juce_Initialisation.h"
  28. } // namespace juce
  29. #endif
  30. using juce::FloatVectorOperations;
  31. using juce::ScopedJuceInitialiser_GUI;
  32. using juce::SharedResourcePointer;
  33. // -----------------------------------------------------------------------
  34. // -Weffc++ compat ext widget
  35. extern "C" {
  36. typedef struct _LV2_External_UI_Widget_Compat {
  37. void (*run )(struct _LV2_External_UI_Widget_Compat*);
  38. void (*show)(struct _LV2_External_UI_Widget_Compat*);
  39. void (*hide)(struct _LV2_External_UI_Widget_Compat*);
  40. _LV2_External_UI_Widget_Compat() noexcept
  41. : run(nullptr), show(nullptr), hide(nullptr) {}
  42. } LV2_External_UI_Widget_Compat;
  43. }
  44. // -----------------------------------------------------------------------
  45. // LV2 descriptor functions
  46. #if defined(__clang__)
  47. # pragma clang diagnostic push
  48. # pragma clang diagnostic ignored "-Weffc++"
  49. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  50. # pragma GCC diagnostic push
  51. # pragma GCC diagnostic ignored "-Weffc++"
  52. #endif
  53. class NativePlugin : public LV2_External_UI_Widget_Compat
  54. {
  55. #if defined(__clang__)
  56. # pragma clang diagnostic pop
  57. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  58. # pragma GCC diagnostic pop
  59. #endif
  60. public:
  61. static const uint32_t kMaxMidiEvents = 512;
  62. NativePlugin(const NativePluginDescriptor* const desc, const double sampleRate, const char* const bundlePath, const LV2_Feature* const* features)
  63. : fHandle(nullptr),
  64. fHost(),
  65. fDescriptor(desc),
  66. #ifdef CARLA_PROPER_CPP11_SUPPORT
  67. fProgramDesc({0, 0, nullptr}),
  68. #endif
  69. fMidiEventCount(0),
  70. fTimeInfo(),
  71. fIsOffline(false),
  72. fBufferSize(0),
  73. fSampleRate(sampleRate),
  74. fUsingNominal(false),
  75. fUridMap(nullptr),
  76. fLastPositionData(),
  77. fURIs(),
  78. fUI(),
  79. fPorts(),
  80. sJuceInitialiser()
  81. {
  82. run = extui_run;
  83. show = extui_show;
  84. hide = extui_hide;
  85. CarlaString resourceDir(bundlePath);
  86. resourceDir += CARLA_OS_SEP_STR "resources" CARLA_OS_SEP_STR;
  87. fHost.handle = this;
  88. fHost.resourceDir = resourceDir.dup();
  89. fHost.uiName = nullptr;
  90. fHost.uiParentId = 0;
  91. fHost.get_buffer_size = host_get_buffer_size;
  92. fHost.get_sample_rate = host_get_sample_rate;
  93. fHost.is_offline = host_is_offline;
  94. fHost.get_time_info = host_get_time_info;
  95. fHost.write_midi_event = host_write_midi_event;
  96. fHost.ui_parameter_changed = host_ui_parameter_changed;
  97. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  98. fHost.ui_closed = host_ui_closed;
  99. fHost.ui_open_file = host_ui_open_file;
  100. fHost.ui_save_file = host_ui_save_file;
  101. fHost.dispatcher = host_dispatcher;
  102. const LV2_Options_Option* options = nullptr;
  103. const LV2_URID_Map* uridMap = nullptr;
  104. const LV2_URID_Unmap* uridUnmap = nullptr;
  105. for (int i=0; features[i] != nullptr; ++i)
  106. {
  107. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  108. options = (const LV2_Options_Option*)features[i]->data;
  109. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  110. uridMap = (const LV2_URID_Map*)features[i]->data;
  111. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  112. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  113. }
  114. if (options == nullptr || uridMap == nullptr)
  115. {
  116. carla_stderr("Host doesn't provide option or urid-map features");
  117. return;
  118. }
  119. for (int i=0; options[i].key != 0; ++i)
  120. {
  121. if (uridUnmap != nullptr)
  122. {
  123. carla_debug("Host option %i:\"%s\"", i, uridUnmap->unmap(uridUnmap->handle, options[i].key));
  124. }
  125. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  126. {
  127. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  128. {
  129. const int value(*(const int*)options[i].value);
  130. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  131. fBufferSize = static_cast<uint32_t>(value);
  132. fUsingNominal = true;
  133. }
  134. else
  135. {
  136. carla_stderr("Host provides nominalBlockLength but has wrong value type");
  137. }
  138. break;
  139. }
  140. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  141. {
  142. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  143. {
  144. const int value(*(const int*)options[i].value);
  145. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  146. fBufferSize = static_cast<uint32_t>(value);
  147. }
  148. else
  149. {
  150. carla_stderr("Host provides maxBlockLength but has wrong value type");
  151. }
  152. // no break, continue in case host supports nominalBlockLength
  153. }
  154. }
  155. fUridMap = uridMap;
  156. }
  157. ~NativePlugin()
  158. {
  159. CARLA_ASSERT(fHandle == nullptr);
  160. if (fHost.resourceDir != nullptr)
  161. {
  162. delete[] fHost.resourceDir;
  163. fHost.resourceDir = nullptr;
  164. }
  165. }
  166. bool init()
  167. {
  168. if (fUridMap == nullptr)
  169. {
  170. // host is missing features
  171. return false;
  172. }
  173. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  174. {
  175. carla_stderr("Plugin is missing something...");
  176. return false;
  177. }
  178. if (fBufferSize == 0)
  179. {
  180. carla_stderr("Host is missing bufferSize feature");
  181. //return false;
  182. // as testing, continue for now
  183. fBufferSize = 1024;
  184. }
  185. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  186. carla_zeroStruct(fTimeInfo);
  187. fHandle = fDescriptor->instantiate(&fHost);
  188. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  189. if (fDescriptor->midiIns > 0)
  190. fUI.portOffset += fDescriptor->midiIns;
  191. else if (fDescriptor->hints & NATIVE_PLUGIN_USES_TIME)
  192. fUI.portOffset += 1;
  193. fUI.portOffset += fDescriptor->midiOuts;
  194. fUI.portOffset += 1; // freewheel
  195. fUI.portOffset += fDescriptor->audioIns;
  196. fUI.portOffset += fDescriptor->audioOuts;
  197. fPorts.init(fDescriptor, fHandle);
  198. fURIs.map(fUridMap);
  199. return true;
  200. }
  201. // -------------------------------------------------------------------
  202. // LV2 functions
  203. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  204. {
  205. fPorts.connectPort(fDescriptor, port, dataLocation);
  206. }
  207. void lv2_activate()
  208. {
  209. if (fDescriptor->activate != nullptr)
  210. fDescriptor->activate(fHandle);
  211. carla_zeroStruct(fTimeInfo);
  212. // hosts may not send all values, resulting on some invalid data
  213. fTimeInfo.bbt.bar = 1;
  214. fTimeInfo.bbt.beat = 1;
  215. fTimeInfo.bbt.tick = 0;
  216. fTimeInfo.bbt.barStartTick = 0;
  217. fTimeInfo.bbt.beatsPerBar = 4;
  218. fTimeInfo.bbt.beatType = 4;
  219. fTimeInfo.bbt.ticksPerBeat = 960.0;
  220. fTimeInfo.bbt.beatsPerMinute = 120.0;
  221. }
  222. void lv2_deactivate()
  223. {
  224. if (fDescriptor->deactivate != nullptr)
  225. fDescriptor->deactivate(fHandle);
  226. }
  227. void lv2_cleanup()
  228. {
  229. if (fDescriptor->cleanup != nullptr)
  230. fDescriptor->cleanup(fHandle);
  231. fHandle = nullptr;
  232. }
  233. void lv2_run(const uint32_t frames)
  234. {
  235. fIsOffline = (fPorts.freewheel != nullptr && *fPorts.freewheel >= 0.5f);
  236. // cache midi events and time information first
  237. if (fDescriptor->midiIns > 0 || (fDescriptor->hints & NATIVE_PLUGIN_USES_TIME) != 0)
  238. {
  239. fMidiEventCount = 0;
  240. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  241. if (fDescriptor->hints & NATIVE_PLUGIN_USES_TIME)
  242. {
  243. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[0], event)
  244. {
  245. if (event == nullptr)
  246. continue;
  247. if (event->body.type != fURIs.atomBlank && event->body.type != fURIs.atomObject)
  248. continue;
  249. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  250. if (obj->body.otype != fURIs.timePos)
  251. continue;
  252. LV2_Atom* bar = nullptr;
  253. LV2_Atom* barBeat = nullptr;
  254. LV2_Atom* beatUnit = nullptr;
  255. LV2_Atom* beatsPerBar = nullptr;
  256. LV2_Atom* beatsPerMinute = nullptr;
  257. LV2_Atom* frame = nullptr;
  258. LV2_Atom* speed = nullptr;
  259. LV2_Atom* ticksPerBeat = nullptr;
  260. lv2_atom_object_get(obj,
  261. fURIs.timeBar, &bar,
  262. fURIs.timeBarBeat, &barBeat,
  263. fURIs.timeBeatUnit, &beatUnit,
  264. fURIs.timeBeatsPerBar, &beatsPerBar,
  265. fURIs.timeBeatsPerMinute, &beatsPerMinute,
  266. fURIs.timeFrame, &frame,
  267. fURIs.timeSpeed, &speed,
  268. fURIs.timeTicksPerBeat, &ticksPerBeat,
  269. 0);
  270. // need to handle this first as other values depend on it
  271. if (ticksPerBeat != nullptr)
  272. {
  273. double ticksPerBeatValue = -1.0;
  274. /**/ if (ticksPerBeat->type == fURIs.atomDouble)
  275. ticksPerBeatValue = ((LV2_Atom_Double*)ticksPerBeat)->body;
  276. else if (ticksPerBeat->type == fURIs.atomFloat)
  277. ticksPerBeatValue = ((LV2_Atom_Float*)ticksPerBeat)->body;
  278. else if (ticksPerBeat->type == fURIs.atomInt)
  279. ticksPerBeatValue = static_cast<double>(((LV2_Atom_Int*)ticksPerBeat)->body);
  280. else if (ticksPerBeat->type == fURIs.atomLong)
  281. ticksPerBeatValue = static_cast<double>(((LV2_Atom_Long*)ticksPerBeat)->body);
  282. else
  283. carla_stderr("Unknown lv2 ticksPerBeat value type");
  284. if (ticksPerBeatValue > 0.0)
  285. fTimeInfo.bbt.ticksPerBeat = fLastPositionData.ticksPerBeat = ticksPerBeatValue;
  286. else
  287. carla_stderr("Invalid lv2 ticksPerBeat value");
  288. }
  289. // same
  290. if (speed != nullptr)
  291. {
  292. /**/ if (speed->type == fURIs.atomDouble)
  293. fLastPositionData.speed = ((LV2_Atom_Double*)speed)->body;
  294. else if (speed->type == fURIs.atomFloat)
  295. fLastPositionData.speed = ((LV2_Atom_Float*)speed)->body;
  296. else if (speed->type == fURIs.atomInt)
  297. fLastPositionData.speed = static_cast<double>(((LV2_Atom_Int*)speed)->body);
  298. else if (speed->type == fURIs.atomLong)
  299. fLastPositionData.speed = static_cast<double>(((LV2_Atom_Long*)speed)->body);
  300. else
  301. carla_stderr("Unknown lv2 speed value type");
  302. fTimeInfo.playing = carla_isNotZero(fLastPositionData.speed);
  303. if (fTimeInfo.playing && fLastPositionData.beatsPerMinute > 0.0f)
  304. {
  305. fTimeInfo.bbt.beatsPerMinute = fLastPositionData.beatsPerMinute*
  306. std::abs(fLastPositionData.speed);
  307. }
  308. }
  309. if (bar != nullptr)
  310. {
  311. int64_t barValue = -1;
  312. /**/ if (bar->type == fURIs.atomDouble)
  313. barValue = static_cast<int64_t>(((LV2_Atom_Double*)bar)->body);
  314. else if (bar->type == fURIs.atomFloat)
  315. barValue = static_cast<int64_t>(((LV2_Atom_Float*)bar)->body);
  316. else if (bar->type == fURIs.atomInt)
  317. barValue = ((LV2_Atom_Int*)bar)->body;
  318. else if (bar->type == fURIs.atomLong)
  319. barValue = ((LV2_Atom_Long*)bar)->body;
  320. else
  321. carla_stderr("Unknown lv2 bar value type");
  322. if (barValue >= 0 && barValue < INT32_MAX)
  323. {
  324. fLastPositionData.bar = static_cast<int32_t>(barValue);
  325. fLastPositionData.bar_f = static_cast<float>(barValue);
  326. fTimeInfo.bbt.bar = fLastPositionData.bar + 1;
  327. }
  328. else
  329. {
  330. carla_stderr("Invalid lv2 bar value");
  331. }
  332. }
  333. if (barBeat != nullptr)
  334. {
  335. double barBeatValue = -1.0;
  336. /**/ if (barBeat->type == fURIs.atomDouble)
  337. barBeatValue = ((LV2_Atom_Double*)barBeat)->body;
  338. else if (barBeat->type == fURIs.atomFloat)
  339. barBeatValue = ((LV2_Atom_Float*)barBeat)->body;
  340. else if (barBeat->type == fURIs.atomInt)
  341. barBeatValue = static_cast<float>(((LV2_Atom_Int*)barBeat)->body);
  342. else if (barBeat->type == fURIs.atomLong)
  343. barBeatValue = static_cast<float>(((LV2_Atom_Long*)barBeat)->body);
  344. else
  345. carla_stderr("Unknown lv2 barBeat value type");
  346. if (barBeatValue >= 0.0)
  347. {
  348. fLastPositionData.barBeat = static_cast<float>(barBeatValue);
  349. const double rest = std::fmod(barBeatValue, 1.0);
  350. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeatValue-rest+1.0);
  351. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  352. }
  353. else
  354. {
  355. carla_stderr("Invalid lv2 barBeat value");
  356. }
  357. }
  358. if (beatUnit != nullptr)
  359. {
  360. int64_t beatUnitValue = -1;
  361. /**/ if (beatUnit->type == fURIs.atomDouble)
  362. beatUnitValue = static_cast<int64_t>(((LV2_Atom_Double*)beatUnit)->body);
  363. else if (beatUnit->type == fURIs.atomFloat)
  364. beatUnitValue = static_cast<int64_t>(((LV2_Atom_Float*)beatUnit)->body);
  365. else if (beatUnit->type == fURIs.atomInt)
  366. beatUnitValue = ((LV2_Atom_Int*)beatUnit)->body;
  367. else if (beatUnit->type == fURIs.atomLong)
  368. beatUnitValue = ((LV2_Atom_Long*)beatUnit)->body;
  369. else
  370. carla_stderr("Unknown lv2 beatUnit value type");
  371. if (beatUnitValue > 0 && beatUnitValue < UINT32_MAX)
  372. {
  373. fLastPositionData.beatUnit = static_cast<uint32_t>(beatUnitValue);
  374. fTimeInfo.bbt.beatType = static_cast<float>(beatUnitValue);
  375. }
  376. else
  377. {
  378. carla_stderr("Invalid lv2 beatUnit value");
  379. }
  380. }
  381. if (beatsPerBar != nullptr)
  382. {
  383. float beatsPerBarValue = -1.0f;
  384. /**/ if (beatsPerBar->type == fURIs.atomDouble)
  385. beatsPerBarValue = static_cast<float>(((LV2_Atom_Double*)beatsPerBar)->body);
  386. else if (beatsPerBar->type == fURIs.atomFloat)
  387. beatsPerBarValue = ((LV2_Atom_Float*)beatsPerBar)->body;
  388. else if (beatsPerBar->type == fURIs.atomInt)
  389. beatsPerBarValue = static_cast<float>(((LV2_Atom_Int*)beatsPerBar)->body);
  390. else if (beatsPerBar->type == fURIs.atomLong)
  391. beatsPerBarValue = static_cast<float>(((LV2_Atom_Long*)beatsPerBar)->body);
  392. else
  393. carla_stderr("Unknown lv2 beatsPerBar value type");
  394. if (beatsPerBarValue > 0.0f)
  395. fTimeInfo.bbt.beatsPerBar = fLastPositionData.beatsPerBar = beatsPerBarValue;
  396. else
  397. carla_stderr("Invalid lv2 beatsPerBar value");
  398. }
  399. if (beatsPerMinute != nullptr)
  400. {
  401. double beatsPerMinuteValue = -1.0;
  402. /**/ if (beatsPerMinute->type == fURIs.atomDouble)
  403. beatsPerMinuteValue = ((LV2_Atom_Double*)beatsPerMinute)->body;
  404. else if (beatsPerMinute->type == fURIs.atomFloat)
  405. beatsPerMinuteValue = ((LV2_Atom_Float*)beatsPerMinute)->body;
  406. else if (beatsPerMinute->type == fURIs.atomInt)
  407. beatsPerMinuteValue = static_cast<double>(((LV2_Atom_Int*)beatsPerMinute)->body);
  408. else if (beatsPerMinute->type == fURIs.atomLong)
  409. beatsPerMinuteValue = static_cast<double>(((LV2_Atom_Long*)beatsPerMinute)->body);
  410. else
  411. carla_stderr("Unknown lv2 beatsPerMinute value type");
  412. if (beatsPerMinuteValue >= 12.0 && beatsPerMinuteValue <= 999.0)
  413. {
  414. fTimeInfo.bbt.beatsPerMinute = fLastPositionData.beatsPerMinute = beatsPerMinuteValue;
  415. if (carla_isNotZero(fLastPositionData.speed))
  416. fTimeInfo.bbt.beatsPerMinute *= std::abs(fLastPositionData.speed);
  417. }
  418. else
  419. {
  420. carla_stderr("Invalid lv2 beatsPerMinute value");
  421. }
  422. }
  423. if (frame != nullptr)
  424. {
  425. int64_t frameValue = -1;
  426. /**/ if (frame->type == fURIs.atomDouble)
  427. frameValue = static_cast<int64_t>(((LV2_Atom_Double*)frame)->body);
  428. else if (frame->type == fURIs.atomFloat)
  429. frameValue = static_cast<int64_t>(((LV2_Atom_Float*)frame)->body);
  430. else if (frame->type == fURIs.atomInt)
  431. frameValue = ((LV2_Atom_Int*)frame)->body;
  432. else if (frame->type == fURIs.atomLong)
  433. frameValue = ((LV2_Atom_Long*)frame)->body;
  434. else
  435. carla_stderr("Unknown lv2 frame value type");
  436. if (frameValue >= 0)
  437. fTimeInfo.frame = fLastPositionData.frame = static_cast<uint64_t>(frameValue);
  438. else
  439. carla_stderr("Invalid lv2 frame value");
  440. }
  441. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*
  442. fTimeInfo.bbt.beatsPerBar*
  443. (fTimeInfo.bbt.bar-1);
  444. fTimeInfo.bbt.valid = (fLastPositionData.beatsPerMinute > 0.0 &&
  445. fLastPositionData.beatUnit > 0 &&
  446. fLastPositionData.beatsPerBar > 0.0f);
  447. }
  448. }
  449. for (uint32_t i=0; i < fDescriptor->midiIns; ++i)
  450. {
  451. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[i], event)
  452. {
  453. if (event == nullptr)
  454. continue;
  455. if (event->body.type != fURIs.midiEvent)
  456. continue;
  457. if (event->body.size > 4)
  458. continue;
  459. if (event->time.frames >= frames)
  460. break;
  461. if (fMidiEventCount >= kMaxMidiEvents)
  462. break;
  463. const uint8_t* const data((const uint8_t*)(event + 1));
  464. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  465. nativeEvent.port = (uint8_t)i;
  466. nativeEvent.size = (uint8_t)event->body.size;
  467. nativeEvent.time = (uint32_t)event->time.frames;
  468. uint32_t j=0;
  469. for (uint32_t size=event->body.size; j<size; ++j)
  470. nativeEvent.data[j] = data[j];
  471. for (; j<4; ++j)
  472. nativeEvent.data[j] = 0;
  473. }
  474. }
  475. }
  476. // init midi out data
  477. if (fDescriptor->midiOuts > 0)
  478. {
  479. for (uint32_t i=0, size=fDescriptor->midiOuts; i<size; ++i)
  480. {
  481. LV2_Atom_Sequence* const seq(fPorts.midiOuts[i]);
  482. CARLA_SAFE_ASSERT_CONTINUE(seq != nullptr);
  483. Ports::MidiOutData& mData(fPorts.midiOutData[i]);
  484. mData.capacity = seq->atom.size;
  485. mData.offset = 0;
  486. seq->atom.size = sizeof(LV2_Atom_Sequence_Body);
  487. seq->atom.type = fURIs.atomSequence;
  488. seq->body.unit = 0;
  489. seq->body.pad = 0;
  490. }
  491. }
  492. // Check for updated parameters
  493. float curValue;
  494. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  495. {
  496. if (fPorts.paramsOut[i])
  497. continue;
  498. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  499. curValue = *fPorts.paramsPtr[i];
  500. if (carla_isEqual(fPorts.paramsLast[i], curValue))
  501. continue;
  502. fPorts.paramsLast[i] = curValue;
  503. fDescriptor->set_parameter_value(fHandle, i, curValue);
  504. }
  505. if (frames == 0)
  506. return updateParameterOutputs();
  507. // FIXME
  508. fDescriptor->process(fHandle,
  509. const_cast<float**>(fPorts.audioIns), fPorts.audioOuts, frames,
  510. fMidiEvents, fMidiEventCount);
  511. // update timePos for next callback
  512. if (carla_isNotZero(fLastPositionData.speed))
  513. {
  514. if (fLastPositionData.speed > 0.0)
  515. {
  516. // playing forwards
  517. fLastPositionData.frame += frames;
  518. }
  519. else
  520. {
  521. // playing backwards
  522. if (frames >= fLastPositionData.frame)
  523. fLastPositionData.frame = 0;
  524. else
  525. fLastPositionData.frame -= frames;
  526. }
  527. fTimeInfo.frame = fLastPositionData.frame;
  528. if (fTimeInfo.bbt.valid)
  529. {
  530. const double beatsPerMinute = fLastPositionData.beatsPerMinute * fLastPositionData.speed;
  531. const double framesPerBeat = 60.0 * fSampleRate / beatsPerMinute;
  532. const double addedBarBeats = double(frames) / framesPerBeat;
  533. if (fLastPositionData.barBeat >= 0.0f)
  534. {
  535. fLastPositionData.barBeat = std::fmod(fLastPositionData.barBeat+static_cast<float>(addedBarBeats),
  536. fLastPositionData.beatsPerBar);
  537. const double rest = std::fmod(fLastPositionData.barBeat, 1.0);
  538. fTimeInfo.bbt.beat = static_cast<int32_t>(fLastPositionData.barBeat-rest+1.0);
  539. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  540. if (fLastPositionData.bar_f >= 0.0f)
  541. {
  542. fLastPositionData.bar_f += std::floor((fLastPositionData.barBeat+static_cast<float>(addedBarBeats))/
  543. fLastPositionData.beatsPerBar);
  544. if (fLastPositionData.bar_f <= 0.0f)
  545. {
  546. fLastPositionData.bar = 0;
  547. fLastPositionData.bar_f = 0.0f;
  548. }
  549. else
  550. {
  551. fLastPositionData.bar = static_cast<int32_t>(fLastPositionData.bar_f+0.5f);
  552. }
  553. fTimeInfo.bbt.bar = fLastPositionData.bar + 1;
  554. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*
  555. fTimeInfo.bbt.beatsPerBar*
  556. (fTimeInfo.bbt.bar-1);
  557. }
  558. }
  559. }
  560. }
  561. updateParameterOutputs();
  562. }
  563. // -------------------------------------------------------------------
  564. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/) const
  565. {
  566. // currently unused
  567. return LV2_OPTIONS_SUCCESS;
  568. }
  569. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  570. {
  571. for (int i=0; options[i].key != 0; ++i)
  572. {
  573. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  574. {
  575. if (options[i].type == fURIs.atomInt)
  576. {
  577. const int value(*(const int*)options[i].value);
  578. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  579. fBufferSize = static_cast<uint32_t>(value);
  580. if (fDescriptor->dispatcher != nullptr)
  581. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  582. }
  583. else
  584. carla_stderr("Host changed nominalBlockLength but with wrong value type");
  585. }
  586. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength) && ! fUsingNominal)
  587. {
  588. if (options[i].type == fURIs.atomInt)
  589. {
  590. const int value(*(const int*)options[i].value);
  591. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  592. fBufferSize = static_cast<uint32_t>(value);
  593. if (fDescriptor->dispatcher != nullptr)
  594. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  595. }
  596. else
  597. carla_stderr("Host changed maxBlockLength but with wrong value type");
  598. }
  599. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_CORE__sampleRate))
  600. {
  601. if (options[i].type == fURIs.atomDouble)
  602. {
  603. const double value(*(const double*)options[i].value);
  604. CARLA_SAFE_ASSERT_CONTINUE(value > 0.0);
  605. fSampleRate = value;
  606. if (fDescriptor->dispatcher != nullptr)
  607. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)fSampleRate);
  608. }
  609. else
  610. carla_stderr("Host changed sampleRate but with wrong value type");
  611. }
  612. }
  613. return LV2_OPTIONS_SUCCESS;
  614. }
  615. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  616. {
  617. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  618. return nullptr;
  619. if (fDescriptor->get_midi_program_count == nullptr)
  620. return nullptr;
  621. if (fDescriptor->get_midi_program_info == nullptr)
  622. return nullptr;
  623. if (index >= fDescriptor->get_midi_program_count(fHandle))
  624. return nullptr;
  625. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  626. if (midiProg == nullptr)
  627. return nullptr;
  628. fProgramDesc.bank = midiProg->bank;
  629. fProgramDesc.program = midiProg->program;
  630. fProgramDesc.name = midiProg->name;
  631. return &fProgramDesc;
  632. }
  633. void lv2_select_program(uint32_t bank, uint32_t program)
  634. {
  635. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  636. return;
  637. if (fDescriptor->set_midi_program == nullptr)
  638. return;
  639. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  640. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  641. {
  642. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  643. if (fPorts.paramsPtr[i] != nullptr)
  644. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  645. }
  646. }
  647. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle, const uint32_t /*flags*/, const LV2_Feature* const* const /*features*/) const
  648. {
  649. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  650. return LV2_STATE_ERR_NO_FEATURE;
  651. if (char* const state = fDescriptor->get_state(fHandle))
  652. {
  653. 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);
  654. std::free(state);
  655. return LV2_STATE_SUCCESS;
  656. }
  657. return LV2_STATE_ERR_UNKNOWN;
  658. }
  659. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* const /*features*/) const
  660. {
  661. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  662. return LV2_STATE_ERR_NO_FEATURE;
  663. size_t size = 0;
  664. uint32_t type = 0;
  665. const void* const data = retrieve(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), &size, &type, &flags);
  666. if (size == 0)
  667. return LV2_STATE_ERR_UNKNOWN;
  668. if (type == 0)
  669. return LV2_STATE_ERR_UNKNOWN;
  670. if (data == nullptr)
  671. return LV2_STATE_ERR_UNKNOWN;
  672. if (type != fURIs.atomString)
  673. return LV2_STATE_ERR_BAD_TYPE;
  674. fDescriptor->set_state(fHandle, (const char*)data);
  675. return LV2_STATE_SUCCESS;
  676. }
  677. // -------------------------------------------------------------------
  678. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  679. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  680. {
  681. fUI.writeFunction = writeFunction;
  682. fUI.controller = controller;
  683. fUI.isEmbed = isEmbed;
  684. #ifdef CARLA_OS_LINUX
  685. // ---------------------------------------------------------------
  686. // show embed UI if needed
  687. if (isEmbed)
  688. {
  689. fUI.isVisible = true;
  690. intptr_t parentId = 0;
  691. for (int i=0; features[i] != nullptr; ++i)
  692. {
  693. if (std::strcmp(features[i]->URI, LV2_UI__parent) == 0)
  694. {
  695. parentId = (intptr_t)features[i]->data;
  696. }
  697. else if (std::strcmp(features[i]->URI, LV2_UI__resize) == 0)
  698. {
  699. const LV2UI_Resize* const uiResize((const LV2UI_Resize*)features[i]->data);
  700. uiResize->ui_resize(uiResize->handle, 740, 512);
  701. }
  702. }
  703. char strBuf[0xff+1];
  704. strBuf[0xff] = '\0';
  705. std::snprintf(strBuf, 0xff, P_INTPTR, parentId);
  706. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  707. fDescriptor->ui_show(fHandle, true);
  708. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  709. }
  710. #endif
  711. // ---------------------------------------------------------------
  712. // see if the host supports external-ui
  713. for (int i=0; features[i] != nullptr; ++i)
  714. {
  715. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  716. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  717. {
  718. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  719. break;
  720. }
  721. }
  722. if (fUI.host != nullptr)
  723. {
  724. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  725. *widget = this;
  726. return;
  727. }
  728. // ---------------------------------------------------------------
  729. // no external-ui support, use showInterface
  730. for (int i=0; features[i] != nullptr; ++i)
  731. {
  732. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  733. {
  734. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  735. for (int j=0; options[j].key != 0; ++j)
  736. {
  737. if (options[j].key == fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  738. {
  739. fHost.uiName = carla_strdup((const char*)options[j].value);
  740. break;
  741. }
  742. }
  743. break;
  744. }
  745. }
  746. if (fHost.uiName == nullptr)
  747. fHost.uiName = carla_strdup(fDescriptor->name);
  748. *widget = nullptr;
  749. }
  750. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  751. {
  752. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  753. return;
  754. if (portIndex >= fUI.portOffset || ! fUI.isVisible)
  755. return;
  756. if (fDescriptor->ui_set_parameter_value == nullptr)
  757. return;
  758. const float value(*(const float*)buffer);
  759. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fUI.portOffset, value);
  760. }
  761. void lv2ui_cleanup()
  762. {
  763. if (fUI.isVisible)
  764. handleUiHide();
  765. fUI.host = nullptr;
  766. fUI.writeFunction = nullptr;
  767. fUI.controller = nullptr;
  768. if (fHost.uiName != nullptr)
  769. {
  770. delete[] fHost.uiName;
  771. fHost.uiName = nullptr;
  772. }
  773. }
  774. // -------------------------------------------------------------------
  775. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  776. {
  777. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  778. return;
  779. if (fDescriptor->ui_set_midi_program == nullptr)
  780. return;
  781. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  782. }
  783. // -------------------------------------------------------------------
  784. int lv2ui_idle() const
  785. {
  786. if (! fUI.isVisible)
  787. return 1;
  788. handleUiRun();
  789. return 0;
  790. }
  791. int lv2ui_show()
  792. {
  793. handleUiShow();
  794. return 0;
  795. }
  796. int lv2ui_hide()
  797. {
  798. handleUiHide();
  799. return 0;
  800. }
  801. // -------------------------------------------------------------------
  802. protected:
  803. void handleUiRun() const
  804. {
  805. if (fDescriptor->ui_idle != nullptr)
  806. fDescriptor->ui_idle(fHandle);
  807. }
  808. void handleUiShow()
  809. {
  810. CARLA_SAFE_ASSERT_RETURN(! fUI.isEmbed,);
  811. if (fDescriptor->ui_show != nullptr)
  812. fDescriptor->ui_show(fHandle, true);
  813. fUI.isVisible = true;
  814. }
  815. void handleUiHide()
  816. {
  817. if (fDescriptor->ui_show != nullptr)
  818. fDescriptor->ui_show(fHandle, false);
  819. fUI.isVisible = false;
  820. }
  821. // -------------------------------------------------------------------
  822. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  823. {
  824. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  825. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  826. CARLA_SAFE_ASSERT_RETURN(event->size > 0, false);
  827. const uint8_t port(event->port);
  828. CARLA_SAFE_ASSERT_RETURN(port < fDescriptor->midiOuts, false);
  829. LV2_Atom_Sequence* const seq(fPorts.midiOuts[port]);
  830. CARLA_SAFE_ASSERT_RETURN(seq != nullptr, false);
  831. Ports::MidiOutData& mData(fPorts.midiOutData[port]);
  832. if (sizeof(LV2_Atom_Event) + event->size > mData.capacity - mData.offset)
  833. return false;
  834. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  835. aev->time.frames = event->time;
  836. aev->body.size = event->size;
  837. aev->body.type = fURIs.midiEvent;
  838. std::memcpy(LV2_ATOM_BODY(&aev->body), event->data, event->size);
  839. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + event->size));
  840. mData.offset += size;
  841. seq->atom.size += size;
  842. return true;
  843. }
  844. void handleUiParameterChanged(const uint32_t index, const float value) const
  845. {
  846. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  847. fUI.writeFunction(fUI.controller, index+fUI.portOffset, sizeof(float), 0, &value);
  848. }
  849. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  850. {
  851. //storeCustomData(key, value);
  852. }
  853. void handleUiClosed()
  854. {
  855. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  856. fUI.host->ui_closed(fUI.controller);
  857. fUI.host = nullptr;
  858. fUI.writeFunction = nullptr;
  859. fUI.controller = nullptr;
  860. fUI.isVisible = false;
  861. }
  862. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  863. {
  864. // TODO
  865. return nullptr;
  866. }
  867. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  868. {
  869. // TODO
  870. return nullptr;
  871. }
  872. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  873. {
  874. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  875. intptr_t ret = 0;
  876. switch (opcode)
  877. {
  878. case NATIVE_HOST_OPCODE_NULL:
  879. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  880. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  881. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  882. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  883. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  884. case NATIVE_HOST_OPCODE_HOST_IDLE:
  885. // nothing
  886. break;
  887. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  888. handleUiClosed();
  889. break;
  890. }
  891. return ret;
  892. // unused for now
  893. (void)index;
  894. (void)value;
  895. (void)ptr;
  896. (void)opt;
  897. }
  898. void updateParameterOutputs()
  899. {
  900. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  901. {
  902. if (! fPorts.paramsOut[i])
  903. continue;
  904. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  905. if (fPorts.paramsPtr[i] != nullptr)
  906. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  907. }
  908. }
  909. // -------------------------------------------------------------------
  910. private:
  911. // Native data
  912. NativePluginHandle fHandle;
  913. NativeHostDescriptor fHost;
  914. const NativePluginDescriptor* const fDescriptor;
  915. LV2_Program_Descriptor fProgramDesc;
  916. uint32_t fMidiEventCount;
  917. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  918. NativeTimeInfo fTimeInfo;
  919. // Lv2 host data
  920. bool fIsOffline;
  921. uint32_t fBufferSize;
  922. double fSampleRate;
  923. bool fUsingNominal;
  924. const LV2_URID_Map* fUridMap;
  925. struct Lv2PositionData {
  926. int32_t bar;
  927. float bar_f;
  928. float barBeat;
  929. uint32_t beatUnit;
  930. float beatsPerBar;
  931. double beatsPerMinute;
  932. uint64_t frame;
  933. double speed;
  934. double ticksPerBeat;
  935. Lv2PositionData()
  936. : bar(-1),
  937. bar_f(-1.0f),
  938. barBeat(-1.0f),
  939. beatUnit(0),
  940. beatsPerBar(0.0f),
  941. beatsPerMinute(-1.0),
  942. frame(0),
  943. speed(0.0),
  944. ticksPerBeat(-1.0) {}
  945. } fLastPositionData;
  946. struct URIDs {
  947. LV2_URID atomBlank;
  948. LV2_URID atomObject;
  949. LV2_URID atomDouble;
  950. LV2_URID atomFloat;
  951. LV2_URID atomInt;
  952. LV2_URID atomLong;
  953. LV2_URID atomSequence;
  954. LV2_URID atomString;
  955. LV2_URID midiEvent;
  956. LV2_URID timePos;
  957. LV2_URID timeBar;
  958. LV2_URID timeBarBeat;
  959. LV2_URID timeBeatsPerBar;
  960. LV2_URID timeBeatsPerMinute;
  961. LV2_URID timeBeatUnit;
  962. LV2_URID timeFrame;
  963. LV2_URID timeSpeed;
  964. LV2_URID timeTicksPerBeat;
  965. URIDs()
  966. : atomBlank(0),
  967. atomObject(0),
  968. atomDouble(0),
  969. atomFloat(0),
  970. atomInt(0),
  971. atomLong(0),
  972. atomSequence(0),
  973. atomString(0),
  974. midiEvent(0),
  975. timePos(0),
  976. timeBar(0),
  977. timeBarBeat(0),
  978. timeBeatsPerBar(0),
  979. timeBeatsPerMinute(0),
  980. timeBeatUnit(0),
  981. timeFrame(0),
  982. timeSpeed(0),
  983. timeTicksPerBeat(0) {}
  984. void map(const LV2_URID_Map* const uridMap)
  985. {
  986. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  987. atomObject = uridMap->map(uridMap->handle, LV2_ATOM__Object);
  988. atomDouble = uridMap->map(uridMap->handle, LV2_ATOM__Double);
  989. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  990. atomInt = uridMap->map(uridMap->handle, LV2_ATOM__Int);
  991. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  992. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  993. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  994. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  995. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  996. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  997. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  998. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  999. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  1000. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  1001. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  1002. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  1003. timeTicksPerBeat = uridMap->map(uridMap->handle, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat);
  1004. }
  1005. } fURIs;
  1006. struct UI {
  1007. const LV2_External_UI_Host* host;
  1008. LV2UI_Write_Function writeFunction;
  1009. LV2UI_Controller controller;
  1010. uint32_t portOffset;
  1011. bool isEmbed;
  1012. bool isVisible;
  1013. UI()
  1014. : host(nullptr),
  1015. writeFunction(nullptr),
  1016. controller(nullptr),
  1017. portOffset(0),
  1018. isEmbed(false),
  1019. isVisible(false) {}
  1020. } fUI;
  1021. struct Ports {
  1022. // need to save current state
  1023. struct MidiOutData {
  1024. uint32_t capacity;
  1025. uint32_t offset;
  1026. MidiOutData()
  1027. : capacity(0),
  1028. offset(0) {}
  1029. };
  1030. const LV2_Atom_Sequence** eventsIn;
  1031. /* */ LV2_Atom_Sequence** midiOuts;
  1032. /* */ MidiOutData* midiOutData;
  1033. const float** audioIns;
  1034. /* */ float** audioOuts;
  1035. float* freewheel;
  1036. uint32_t paramCount;
  1037. float* paramsLast;
  1038. float** paramsPtr;
  1039. bool* paramsOut;
  1040. Ports()
  1041. : eventsIn(nullptr),
  1042. midiOuts(nullptr),
  1043. midiOutData(nullptr),
  1044. audioIns(nullptr),
  1045. audioOuts(nullptr),
  1046. freewheel(nullptr),
  1047. paramCount(0),
  1048. paramsLast(nullptr),
  1049. paramsPtr(nullptr),
  1050. paramsOut(nullptr) {}
  1051. ~Ports()
  1052. {
  1053. if (eventsIn != nullptr)
  1054. {
  1055. delete[] eventsIn;
  1056. eventsIn = nullptr;
  1057. }
  1058. if (midiOuts != nullptr)
  1059. {
  1060. delete[] midiOuts;
  1061. midiOuts = nullptr;
  1062. }
  1063. if (midiOutData != nullptr)
  1064. {
  1065. delete[] midiOutData;
  1066. midiOutData = nullptr;
  1067. }
  1068. if (audioIns != nullptr)
  1069. {
  1070. delete[] audioIns;
  1071. audioIns = nullptr;
  1072. }
  1073. if (audioOuts != nullptr)
  1074. {
  1075. delete[] audioOuts;
  1076. audioOuts = nullptr;
  1077. }
  1078. if (paramsLast != nullptr)
  1079. {
  1080. delete[] paramsLast;
  1081. paramsLast = nullptr;
  1082. }
  1083. if (paramsPtr != nullptr)
  1084. {
  1085. delete[] paramsPtr;
  1086. paramsPtr = nullptr;
  1087. }
  1088. if (paramsOut != nullptr)
  1089. {
  1090. delete[] paramsOut;
  1091. paramsOut = nullptr;
  1092. }
  1093. }
  1094. void init(const NativePluginDescriptor* const desc, NativePluginHandle handle)
  1095. {
  1096. CARLA_SAFE_ASSERT_RETURN(desc != nullptr && handle != nullptr,)
  1097. if (desc->midiIns > 0)
  1098. {
  1099. eventsIn = new const LV2_Atom_Sequence*[desc->midiIns];
  1100. for (uint32_t i=0; i < desc->midiIns; ++i)
  1101. eventsIn[i] = nullptr;
  1102. }
  1103. else if (desc->hints & NATIVE_PLUGIN_USES_TIME)
  1104. {
  1105. eventsIn = new const LV2_Atom_Sequence*[1];
  1106. eventsIn[0] = nullptr;
  1107. }
  1108. if (desc->midiOuts > 0)
  1109. {
  1110. midiOuts = new LV2_Atom_Sequence*[desc->midiOuts];
  1111. midiOutData = new MidiOutData[desc->midiOuts];
  1112. for (uint32_t i=0; i < desc->midiOuts; ++i)
  1113. midiOuts[i] = nullptr;
  1114. }
  1115. if (desc->audioIns > 0)
  1116. {
  1117. audioIns = new const float*[desc->audioIns];
  1118. for (uint32_t i=0; i < desc->audioIns; ++i)
  1119. audioIns[i] = nullptr;
  1120. }
  1121. if (desc->audioOuts > 0)
  1122. {
  1123. audioOuts = new float*[desc->audioOuts];
  1124. for (uint32_t i=0; i < desc->audioOuts; ++i)
  1125. audioOuts[i] = nullptr;
  1126. }
  1127. if (desc->get_parameter_count != nullptr && desc->get_parameter_info != nullptr && desc->get_parameter_value != nullptr && desc->set_parameter_value != nullptr)
  1128. {
  1129. paramCount = desc->get_parameter_count(handle);
  1130. if (paramCount > 0)
  1131. {
  1132. paramsLast = new float[paramCount];
  1133. paramsPtr = new float*[paramCount];
  1134. paramsOut = new bool[paramCount];
  1135. for (uint32_t i=0; i < paramCount; ++i)
  1136. {
  1137. paramsLast[i] = desc->get_parameter_value(handle, i);
  1138. paramsPtr [i] = nullptr;
  1139. paramsOut [i] = (desc->get_parameter_info(handle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT);
  1140. }
  1141. }
  1142. }
  1143. }
  1144. void connectPort(const NativePluginDescriptor* const desc, const uint32_t port, void* const dataLocation)
  1145. {
  1146. uint32_t index = 0;
  1147. if (desc->midiIns > 0 || (desc->hints & NATIVE_PLUGIN_USES_TIME) != 0)
  1148. {
  1149. if (port == index++)
  1150. {
  1151. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  1152. return;
  1153. }
  1154. }
  1155. for (uint32_t i=1; i < desc->midiIns; ++i)
  1156. {
  1157. if (port == index++)
  1158. {
  1159. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  1160. return;
  1161. }
  1162. }
  1163. for (uint32_t i=0; i < desc->midiOuts; ++i)
  1164. {
  1165. if (port == index++)
  1166. {
  1167. midiOuts[i] = (LV2_Atom_Sequence*)dataLocation;
  1168. return;
  1169. }
  1170. }
  1171. if (port == index++)
  1172. {
  1173. freewheel = (float*)dataLocation;
  1174. return;
  1175. }
  1176. for (uint32_t i=0; i < desc->audioIns; ++i)
  1177. {
  1178. if (port == index++)
  1179. {
  1180. audioIns[i] = (float*)dataLocation;
  1181. return;
  1182. }
  1183. }
  1184. for (uint32_t i=0; i < desc->audioOuts; ++i)
  1185. {
  1186. if (port == index++)
  1187. {
  1188. audioOuts[i] = (float*)dataLocation;
  1189. return;
  1190. }
  1191. }
  1192. for (uint32_t i=0; i < paramCount; ++i)
  1193. {
  1194. if (port == index++)
  1195. {
  1196. paramsPtr[i] = (float*)dataLocation;
  1197. return;
  1198. }
  1199. }
  1200. }
  1201. CARLA_DECLARE_NON_COPY_STRUCT(Ports);
  1202. } fPorts;
  1203. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  1204. // -------------------------------------------------------------------
  1205. #define handlePtr ((NativePlugin*)self)
  1206. static void extui_run(LV2_External_UI_Widget_Compat* self)
  1207. {
  1208. handlePtr->handleUiRun();
  1209. }
  1210. static void extui_show(LV2_External_UI_Widget_Compat* self)
  1211. {
  1212. handlePtr->handleUiShow();
  1213. }
  1214. static void extui_hide(LV2_External_UI_Widget_Compat* self)
  1215. {
  1216. handlePtr->handleUiHide();
  1217. }
  1218. #undef handlePtr
  1219. // -------------------------------------------------------------------
  1220. #define handlePtr ((NativePlugin*)handle)
  1221. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  1222. {
  1223. return handlePtr->fBufferSize;
  1224. }
  1225. static double host_get_sample_rate(NativeHostHandle handle)
  1226. {
  1227. return handlePtr->fSampleRate;
  1228. }
  1229. static bool host_is_offline(NativeHostHandle handle)
  1230. {
  1231. return handlePtr->fIsOffline;
  1232. }
  1233. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  1234. {
  1235. return &(handlePtr->fTimeInfo);
  1236. }
  1237. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1238. {
  1239. return handlePtr->handleWriteMidiEvent(event);
  1240. }
  1241. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1242. {
  1243. handlePtr->handleUiParameterChanged(index, value);
  1244. }
  1245. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1246. {
  1247. handlePtr->handleUiCustomDataChanged(key, value);
  1248. }
  1249. static void host_ui_closed(NativeHostHandle handle)
  1250. {
  1251. handlePtr->handleUiClosed();
  1252. }
  1253. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1254. {
  1255. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1256. }
  1257. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1258. {
  1259. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1260. }
  1261. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1262. {
  1263. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1264. }
  1265. #undef handlePtr
  1266. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1267. };
  1268. // -----------------------------------------------------------------------
  1269. // LV2 plugin descriptor functions
  1270. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  1271. {
  1272. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  1273. const NativePluginDescriptor* pluginDesc = nullptr;
  1274. const char* pluginLabel = nullptr;
  1275. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  1276. pluginLabel = lv2Descriptor->URI+37;
  1277. if (pluginLabel == nullptr)
  1278. {
  1279. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  1280. return nullptr;
  1281. }
  1282. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  1283. PluginListManager& plm(PluginListManager::getInstance());
  1284. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  1285. {
  1286. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  1287. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  1288. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  1289. {
  1290. pluginDesc = tmpDesc;
  1291. break;
  1292. }
  1293. }
  1294. if (pluginDesc == nullptr)
  1295. {
  1296. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  1297. return nullptr;
  1298. }
  1299. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  1300. if (! plugin->init())
  1301. {
  1302. carla_stderr("Failed to init plugin");
  1303. delete plugin;
  1304. return nullptr;
  1305. }
  1306. return (LV2_Handle)plugin;
  1307. }
  1308. #define instancePtr ((NativePlugin*)instance)
  1309. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  1310. {
  1311. instancePtr->lv2_connect_port(port, dataLocation);
  1312. }
  1313. static void lv2_activate(LV2_Handle instance)
  1314. {
  1315. carla_debug("lv2_activate(%p)", instance);
  1316. instancePtr->lv2_activate();
  1317. }
  1318. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  1319. {
  1320. instancePtr->lv2_run(sampleCount);
  1321. }
  1322. static void lv2_deactivate(LV2_Handle instance)
  1323. {
  1324. carla_debug("lv2_deactivate(%p)", instance);
  1325. instancePtr->lv2_deactivate();
  1326. }
  1327. static void lv2_cleanup(LV2_Handle instance)
  1328. {
  1329. carla_debug("lv2_cleanup(%p)", instance);
  1330. instancePtr->lv2_cleanup();
  1331. delete instancePtr;
  1332. }
  1333. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1334. {
  1335. carla_debug("lv2_get_options(%p, %p)", instance, options);
  1336. return instancePtr->lv2_get_options(options);
  1337. }
  1338. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1339. {
  1340. carla_debug("lv2_set_options(%p, %p)", instance, options);
  1341. return instancePtr->lv2_set_options(options);
  1342. }
  1343. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1344. {
  1345. carla_debug("lv2_get_program(%p, %i)", instance, index);
  1346. return instancePtr->lv2_get_program(index);
  1347. }
  1348. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1349. {
  1350. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  1351. return instancePtr->lv2_select_program(bank, program);
  1352. }
  1353. 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)
  1354. {
  1355. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  1356. return instancePtr->lv2_save(store, handle, flags, features);
  1357. }
  1358. 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)
  1359. {
  1360. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  1361. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  1362. }
  1363. static const void* lv2_extension_data(const char* uri)
  1364. {
  1365. carla_debug("lv2_extension_data(\"%s\")", uri);
  1366. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1367. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1368. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1369. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1370. return &options;
  1371. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1372. return &programs;
  1373. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1374. return &state;
  1375. return nullptr;
  1376. }
  1377. #undef instancePtr
  1378. // -----------------------------------------------------------------------
  1379. // LV2 UI descriptor functions
  1380. static LV2UI_Handle lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1381. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  1382. {
  1383. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  1384. #ifndef CARLA_OS_LINUX
  1385. CARLA_SAFE_ASSERT_RETURN(! isEmbed, nullptr);
  1386. #endif
  1387. NativePlugin* plugin = nullptr;
  1388. for (int i=0; features[i] != nullptr; ++i)
  1389. {
  1390. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1391. {
  1392. plugin = (NativePlugin*)features[i]->data;
  1393. break;
  1394. }
  1395. }
  1396. if (plugin == nullptr)
  1397. {
  1398. carla_stderr("Host doesn't support instance-access, cannot show UI");
  1399. return nullptr;
  1400. }
  1401. plugin->lv2ui_instantiate(writeFunction, controller, widget, features, isEmbed);
  1402. return (LV2UI_Handle)plugin;
  1403. }
  1404. #ifdef CARLA_OS_LINUX
  1405. static LV2UI_Handle lv2ui_instantiate_embed(const LV2UI_Descriptor*, const char*, const char*,
  1406. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1407. LV2UI_Widget* widget, const LV2_Feature* const* features)
  1408. {
  1409. return lv2ui_instantiate(writeFunction, controller, widget, features, true);
  1410. }
  1411. #endif
  1412. static LV2UI_Handle lv2ui_instantiate_external(const LV2UI_Descriptor*, const char*, const char*,
  1413. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1414. LV2UI_Widget* widget, const LV2_Feature* const* features)
  1415. {
  1416. return lv2ui_instantiate(writeFunction, controller, widget, features, false);
  1417. }
  1418. #define uiPtr ((NativePlugin*)ui)
  1419. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1420. {
  1421. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  1422. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  1423. }
  1424. static void lv2ui_cleanup(LV2UI_Handle ui)
  1425. {
  1426. carla_debug("lv2ui_cleanup(%p)", ui);
  1427. uiPtr->lv2ui_cleanup();
  1428. }
  1429. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  1430. {
  1431. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  1432. uiPtr->lv2ui_select_program(bank, program);
  1433. }
  1434. static int lv2ui_idle(LV2UI_Handle ui)
  1435. {
  1436. return uiPtr->lv2ui_idle();
  1437. }
  1438. static int lv2ui_show(LV2UI_Handle ui)
  1439. {
  1440. carla_debug("lv2ui_show(%p)", ui);
  1441. return uiPtr->lv2ui_show();
  1442. }
  1443. static int lv2ui_hide(LV2UI_Handle ui)
  1444. {
  1445. carla_debug("lv2ui_hide(%p)", ui);
  1446. return uiPtr->lv2ui_hide();
  1447. }
  1448. static const void* lv2ui_extension_data(const char* uri)
  1449. {
  1450. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  1451. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  1452. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  1453. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  1454. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  1455. return &uiidle;
  1456. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  1457. return &uishow;
  1458. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  1459. return &uiprograms;
  1460. return nullptr;
  1461. }
  1462. #undef uiPtr
  1463. // -----------------------------------------------------------------------
  1464. // Startup code
  1465. CARLA_EXPORT
  1466. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1467. {
  1468. carla_debug("lv2_descriptor(%i)", index);
  1469. PluginListManager& plm(PluginListManager::getInstance());
  1470. if (index >= plm.descs.count())
  1471. {
  1472. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1473. return nullptr;
  1474. }
  1475. if (index < plm.lv2Descs.count())
  1476. {
  1477. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1478. return plm.lv2Descs.getAt(index, nullptr);
  1479. }
  1480. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  1481. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  1482. CarlaString tmpURI;
  1483. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1484. tmpURI += pluginDesc->label;
  1485. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1486. const LV2_Descriptor lv2DescTmp = {
  1487. /* URI */ carla_strdup(tmpURI),
  1488. /* instantiate */ lv2_instantiate,
  1489. /* connect_port */ lv2_connect_port,
  1490. /* activate */ lv2_activate,
  1491. /* run */ lv2_run,
  1492. /* deactivate */ lv2_deactivate,
  1493. /* cleanup */ lv2_cleanup,
  1494. /* extension_data */ lv2_extension_data
  1495. };
  1496. LV2_Descriptor* lv2Desc;
  1497. try {
  1498. lv2Desc = new LV2_Descriptor;
  1499. } CARLA_SAFE_EXCEPTION_RETURN("new LV2_Descriptor", nullptr);
  1500. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1501. plm.lv2Descs.append(lv2Desc);
  1502. return lv2Desc;
  1503. }
  1504. CARLA_EXPORT
  1505. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1506. {
  1507. carla_debug("lv2ui_descriptor(%i)", index);
  1508. #ifdef CARLA_OS_LINUX
  1509. static const LV2UI_Descriptor lv2UiEmbedDesc = {
  1510. /* URI */ "http://kxstudio.sf.net/carla/ui-embed",
  1511. /* instantiate */ lv2ui_instantiate_embed,
  1512. /* cleanup */ lv2ui_cleanup,
  1513. /* port_event */ lv2ui_port_event,
  1514. /* extension_data */ lv2ui_extension_data
  1515. };
  1516. if (index == 0)
  1517. return &lv2UiEmbedDesc;
  1518. else
  1519. --index;
  1520. #endif
  1521. static const LV2UI_Descriptor lv2UiExtDesc = {
  1522. /* URI */ "http://kxstudio.sf.net/carla/ui-ext",
  1523. /* instantiate */ lv2ui_instantiate_external,
  1524. /* cleanup */ lv2ui_cleanup,
  1525. /* port_event */ lv2ui_port_event,
  1526. /* extension_data */ lv2ui_extension_data
  1527. };
  1528. return (index == 0) ? &lv2UiExtDesc : nullptr;
  1529. }
  1530. // -----------------------------------------------------------------------