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