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-native-lv2.cpp 45KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #define CARLA_NATIVE_PLUGIN_LV2
  18. #include "carla-native-base.cpp"
  19. #include "juce_audio_basics.h"
  20. #include "juce_gui_basics.h"
  21. #include "CarlaString.hpp"
  22. #include "lv2/atom.h"
  23. #include "lv2/atom-util.h"
  24. #include "lv2/buf-size.h"
  25. #include "lv2/instance-access.h"
  26. #include "lv2/midi.h"
  27. #include "lv2/options.h"
  28. #include "lv2/state.h"
  29. #include "lv2/time.h"
  30. #include "lv2/ui.h"
  31. #include "lv2/urid.h"
  32. #include "lv2/lv2_external_ui.h"
  33. #include "lv2/lv2_programs.h"
  34. using namespace juce;
  35. // -----------------------------------------------------------------------
  36. // Juce Message Thread
  37. class JuceMessageThread : public Thread
  38. {
  39. public:
  40. JuceMessageThread()
  41. : Thread("JuceMessageThread"),
  42. fInitialised(false)
  43. {
  44. startThread(7);
  45. while (! fInitialised)
  46. sleep(1);
  47. }
  48. ~JuceMessageThread()
  49. {
  50. signalThreadShouldExit();
  51. JUCEApplication::quit();
  52. waitForThreadToExit(5000);
  53. clearSingletonInstance();
  54. }
  55. void run()
  56. {
  57. initialiseJuce_GUI();
  58. fInitialised = true;
  59. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  60. while ((! threadShouldExit()) && MessageManager::getInstance()->runDispatchLoopUntil(250))
  61. {}
  62. }
  63. juce_DeclareSingleton(JuceMessageThread, false);
  64. private:
  65. bool fInitialised;
  66. };
  67. juce_ImplementSingleton(JuceMessageThread)
  68. static Array<void*> gActivePlugins;
  69. // -----------------------------------------------------------------------
  70. // LV2 descriptor functions
  71. class NativePlugin : public LV2_External_UI_Widget
  72. {
  73. public:
  74. static const uint32_t kMaxMidiEvents = 512;
  75. NativePlugin(const NativePluginDescriptor* const desc, const double sampleRate, const char* const bundlePath, const LV2_Feature* const* features)
  76. : fHandle(nullptr),
  77. fDescriptor(desc),
  78. fMidiEventCount(0),
  79. fUiWasShown(false),
  80. fIsProcessing(false),
  81. fVolume(1.0f),
  82. fDryWet(1.0f),
  83. fBufferSize(0),
  84. fSampleRate(sampleRate),
  85. fUridMap(nullptr)
  86. {
  87. run = extui_run;
  88. show = extui_show;
  89. hide = extui_hide;
  90. CarlaString resourceDir(bundlePath);
  91. #ifdef CARLA_OS_WIN
  92. resourceDir += "\\resources\\";
  93. #else
  94. resourceDir += "/resources/";
  95. #endif
  96. fHost.handle = this;
  97. fHost.resourceDir = resourceDir.dup();
  98. fHost.uiName = nullptr;
  99. fHost.get_buffer_size = host_get_buffer_size;
  100. fHost.get_sample_rate = host_get_sample_rate;
  101. fHost.is_offline = host_is_offline;
  102. fHost.get_time_info = host_get_time_info;
  103. fHost.write_midi_event = host_write_midi_event;
  104. fHost.ui_parameter_changed = host_ui_parameter_changed;
  105. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  106. fHost.ui_closed = host_ui_closed;
  107. fHost.ui_open_file = host_ui_open_file;
  108. fHost.ui_save_file = host_ui_save_file;
  109. fHost.dispatcher = host_dispatcher;
  110. const LV2_Options_Option* options = nullptr;
  111. const LV2_URID_Map* uridMap = nullptr;
  112. const LV2_URID_Unmap* uridUnmap = nullptr;
  113. for (int i=0; features[i] != nullptr; ++i)
  114. {
  115. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  116. options = (const LV2_Options_Option*)features[i]->data;
  117. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  118. uridMap = (const LV2_URID_Map*)features[i]->data;
  119. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  120. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  121. }
  122. if (options == nullptr || uridMap == nullptr)
  123. {
  124. carla_stderr("Host doesn't provides option or urid-map features");
  125. return;
  126. }
  127. for (int i=0; options[i].key != 0; ++i)
  128. {
  129. if (uridUnmap != nullptr)
  130. {
  131. carla_debug("Host option %i:\"%s\"", i, uridUnmap->unmap(uridUnmap->handle, options[i].key));
  132. }
  133. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  134. {
  135. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  136. {
  137. fBufferSize = *(const int*)options[i].value;
  138. if (fBufferSize == 0)
  139. carla_stderr("Host provides maxBlockLength but has null value");
  140. }
  141. else
  142. carla_stderr("Host provides maxBlockLength but has wrong value type");
  143. break;
  144. }
  145. }
  146. fUridMap = uridMap;
  147. if (fDescriptor->midiIns > 0)
  148. fUI.portOffset += desc->midiIns;
  149. else if (fDescriptor->hints & PLUGIN_USES_TIME)
  150. fUI.portOffset += 1;
  151. fUI.portOffset += desc->midiOuts;
  152. fUI.portOffset += 1; // freewheel
  153. fUI.portOffset += desc->audioIns;
  154. fUI.portOffset += desc->audioOuts;
  155. }
  156. ~NativePlugin()
  157. {
  158. CARLA_ASSERT(fHandle == nullptr);
  159. CARLA_ASSERT(! fUiWasShown);
  160. if (fHost.resourceDir != nullptr)
  161. {
  162. delete[] fHost.resourceDir;
  163. fHost.resourceDir = nullptr;
  164. }
  165. }
  166. bool init()
  167. {
  168. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  169. {
  170. carla_stderr("Plugin is missing something...");
  171. return false;
  172. }
  173. if (fBufferSize == 0)
  174. {
  175. carla_stderr("Host is missing bufferSize feature");
  176. //return false;
  177. // as testing, continue for now
  178. fBufferSize = 1024;
  179. }
  180. fHandle = fDescriptor->instantiate(&fHost);
  181. if (fHandle == nullptr)
  182. return false;
  183. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  184. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  185. fPorts.init(fDescriptor, fHandle);
  186. fUris.map(fUridMap);
  187. return true;
  188. }
  189. // -------------------------------------------------------------------
  190. // LV2 functions
  191. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  192. {
  193. fPorts.connectPort(fDescriptor, port, dataLocation);
  194. }
  195. void lv2_activate()
  196. {
  197. if (fDescriptor->activate != nullptr)
  198. fDescriptor->activate(fHandle);
  199. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  200. }
  201. void lv2_deactivate()
  202. {
  203. if (fDescriptor->deactivate != nullptr)
  204. fDescriptor->deactivate(fHandle);
  205. }
  206. void lv2_cleanup()
  207. {
  208. if (fDescriptor->cleanup != nullptr)
  209. fDescriptor->cleanup(fHandle);
  210. fHandle = nullptr;
  211. if (fUiWasShown)
  212. {
  213. CARLA_SAFE_ASSERT_RETURN(gActivePlugins.contains(this),);
  214. gActivePlugins.removeFirstMatchingValue(this);
  215. JUCE_AUTORELEASEPOOL
  216. MessageManagerLock mmLock;
  217. if (gActivePlugins.size() == 0)
  218. {
  219. JuceMessageThread::deleteInstance();
  220. shutdownJuce_GUI();
  221. }
  222. fUiWasShown = false;
  223. }
  224. }
  225. void lv2_run(const uint32_t frames)
  226. {
  227. if (frames == 0)
  228. {
  229. updateParameterOutputs();
  230. return;
  231. }
  232. // Check for updated parameters
  233. float curValue;
  234. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  235. {
  236. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  237. curValue = *fPorts.paramsPtr[i];
  238. if (fPorts.paramsLast[i] != curValue && (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT) == 0)
  239. {
  240. fPorts.paramsLast[i] = curValue;
  241. fDescriptor->set_parameter_value(fHandle, i, curValue);
  242. }
  243. }
  244. if (fDescriptor->midiIns > 0 || (fDescriptor->hints & PLUGIN_USES_TIME) != 0)
  245. {
  246. fMidiEventCount = 0;
  247. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  248. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[0], iter)
  249. {
  250. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  251. if (event == nullptr)
  252. continue;
  253. if (event->body.size > 4)
  254. continue;
  255. if (event->time.frames >= frames)
  256. break;
  257. if (event->body.type == fUris.midiEvent)
  258. {
  259. if (fMidiEventCount >= kMaxMidiEvents*2)
  260. continue;
  261. const uint8_t* const data((const uint8_t*)(event + 1));
  262. fMidiEvents[fMidiEventCount].port = 0;
  263. fMidiEvents[fMidiEventCount].time = (uint32_t)event->time.frames;
  264. fMidiEvents[fMidiEventCount].size = (uint8_t)event->body.size;
  265. for (uint32_t i=0; i < event->body.size; ++i)
  266. fMidiEvents[fMidiEventCount].data[i] = data[i];
  267. fMidiEventCount += 1;
  268. continue;
  269. }
  270. if (event->body.type == fUris.atomBlank)
  271. {
  272. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  273. if (obj->body.otype != fUris.timePos)
  274. continue;
  275. LV2_Atom* bar = nullptr;
  276. LV2_Atom* barBeat = nullptr;
  277. LV2_Atom* beatsPerBar = nullptr;
  278. LV2_Atom* bpm = nullptr;
  279. LV2_Atom* beatUnit = nullptr;
  280. LV2_Atom* frame = nullptr;
  281. LV2_Atom* speed = nullptr;
  282. lv2_atom_object_get(obj,
  283. fUris.timeBar, &bar,
  284. fUris.timeBarBeat, &barBeat,
  285. fUris.timeBeatsPerBar, &beatsPerBar,
  286. fUris.timeBeatsPerMinute, &bpm,
  287. fUris.timeBeatUnit, &beatUnit,
  288. fUris.timeFrame, &frame,
  289. fUris.timeSpeed, &speed,
  290. nullptr);
  291. if (bpm != nullptr && bpm->type == fUris.atomFloat)
  292. {
  293. fTimeInfo.bbt.beatsPerMinute = ((LV2_Atom_Float*)bpm)->body;
  294. fTimeInfo.bbt.valid = true;
  295. }
  296. if (beatsPerBar != nullptr && beatsPerBar->type == fUris.atomFloat)
  297. {
  298. float beatsPerBarValue = ((LV2_Atom_Float*)beatsPerBar)->body;
  299. fTimeInfo.bbt.beatsPerBar = beatsPerBarValue;
  300. if (bar != nullptr && bar->type == fUris.atomLong)
  301. {
  302. //float barValue = ((LV2_Atom_Long*)bar)->body;
  303. //curPosInfo.ppqPositionOfLastBarStart = barValue * beatsPerBarValue;
  304. if (barBeat != nullptr && barBeat->type == fUris.atomFloat)
  305. {
  306. //float barBeatValue = ((LV2_Atom_Float*)barBeat)->body;
  307. //curPosInfo.ppqPosition = curPosInfo.ppqPositionOfLastBarStart + barBeatValue;
  308. }
  309. }
  310. }
  311. if (beatUnit != nullptr && beatUnit->type == fUris.atomFloat)
  312. fTimeInfo.bbt.beatType = ((LV2_Atom_Float*)beatUnit)->body;
  313. if (frame != nullptr && frame->type == fUris.atomLong)
  314. fTimeInfo.frame = ((LV2_Atom_Long*)frame)->body;
  315. if (speed != nullptr && speed->type == fUris.atomFloat)
  316. fTimeInfo.playing = ((LV2_Atom_Float*)speed)->body == 1.0f;
  317. continue;
  318. }
  319. }
  320. for (uint32_t i=1; i < fDescriptor->midiIns; ++i)
  321. {
  322. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[i], iter)
  323. {
  324. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  325. if (event == nullptr)
  326. continue;
  327. if (event->body.type != fUris.midiEvent)
  328. continue;
  329. if (event->body.size > 4)
  330. continue;
  331. if (event->time.frames >= frames)
  332. break;
  333. if (fMidiEventCount >= kMaxMidiEvents*2)
  334. break;
  335. const uint8_t* const data((const uint8_t*)(event + 1));
  336. fMidiEvents[fMidiEventCount].port = (uint8_t)i;
  337. fMidiEvents[fMidiEventCount].size = (uint8_t)event->body.size;
  338. fMidiEvents[fMidiEventCount].time = (uint32_t)event->time.frames;
  339. for (uint32_t j=0; j < event->body.size; ++j)
  340. fMidiEvents[fMidiEventCount].data[j] = data[j];
  341. fMidiEventCount += 1;
  342. }
  343. }
  344. }
  345. fIsProcessing = true;
  346. fDescriptor->process(fHandle, fPorts.audioIns, fPorts.audioOuts, frames, fMidiEvents, fMidiEventCount);
  347. fIsProcessing = false;
  348. if (fDryWet != 1.0f && fDescriptor->audioIns == fDescriptor->audioOuts)
  349. {
  350. for (uint32_t i=0; i < fDescriptor->audioOuts; ++i)
  351. {
  352. FloatVectorOperations::multiply(fPorts.audioIns[i], fVolume*(1.0f-fDryWet), frames);
  353. FloatVectorOperations::multiply(fPorts.audioOuts[i], fVolume*fDryWet, frames);
  354. FloatVectorOperations::add(fPorts.audioOuts[i], fPorts.audioIns[i], frames);
  355. }
  356. }
  357. else if (fVolume != 1.0f)
  358. {
  359. for (uint32_t i=0; i < fDescriptor->audioOuts; ++i)
  360. FloatVectorOperations::multiply(fPorts.audioOuts[i], fVolume, frames);
  361. }
  362. // TODO - midi out
  363. updateParameterOutputs();
  364. }
  365. // -------------------------------------------------------------------
  366. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/) const
  367. {
  368. // currently unused
  369. return LV2_OPTIONS_SUCCESS;
  370. }
  371. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  372. {
  373. for (int i=0; options[i].key != 0; ++i)
  374. {
  375. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  376. {
  377. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Int))
  378. {
  379. fBufferSize = *(const int*)options[i].value;
  380. if (fDescriptor->dispatcher != nullptr)
  381. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, fBufferSize, nullptr, 0.0f);
  382. }
  383. else
  384. carla_stderr("Host changed maxBlockLength but with wrong value type");
  385. }
  386. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_CORE__sampleRate))
  387. {
  388. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Double))
  389. {
  390. fSampleRate = *(const double*)options[i].value;
  391. if (fDescriptor->dispatcher != nullptr)
  392. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)fSampleRate);
  393. }
  394. else
  395. carla_stderr("Host changed sampleRate but with wrong value type");
  396. }
  397. }
  398. return LV2_OPTIONS_SUCCESS;
  399. }
  400. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  401. {
  402. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  403. return nullptr;
  404. if (fDescriptor->get_midi_program_count == nullptr)
  405. return nullptr;
  406. if (fDescriptor->get_midi_program_info == nullptr)
  407. return nullptr;
  408. if (index >= fDescriptor->get_midi_program_count(fHandle))
  409. return nullptr;
  410. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  411. if (midiProg == nullptr)
  412. return nullptr;
  413. fProgramDesc.bank = midiProg->bank;
  414. fProgramDesc.program = midiProg->program;
  415. fProgramDesc.name = midiProg->name;
  416. return &fProgramDesc;
  417. }
  418. void lv2_select_program(uint32_t bank, uint32_t program)
  419. {
  420. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  421. return;
  422. if (fDescriptor->set_midi_program == nullptr)
  423. return;
  424. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  425. }
  426. 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
  427. {
  428. if ((fDescriptor->hints & PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  429. return LV2_STATE_ERR_NO_FEATURE;
  430. if (char* const state = fDescriptor->get_state(fHandle))
  431. {
  432. store(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), state, std::strlen(state), fUris.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  433. std::free(state);
  434. return LV2_STATE_SUCCESS;
  435. }
  436. return LV2_STATE_ERR_UNKNOWN;
  437. }
  438. 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
  439. {
  440. if ((fDescriptor->hints & PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  441. return LV2_STATE_ERR_NO_FEATURE;
  442. size_t size = 0;
  443. uint32_t type = 0;
  444. const void* data = retrieve(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), &size, &type, &flags);
  445. if (size == 0)
  446. return LV2_STATE_ERR_UNKNOWN;
  447. if (type == 0)
  448. return LV2_STATE_ERR_UNKNOWN;
  449. if (data == nullptr)
  450. return LV2_STATE_ERR_UNKNOWN;
  451. if (type != fUris.atomString)
  452. return LV2_STATE_ERR_BAD_TYPE;
  453. fDescriptor->set_state(fHandle, (const char*)data);
  454. return LV2_STATE_SUCCESS;
  455. }
  456. // -------------------------------------------------------------------
  457. bool lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  458. {
  459. for (int i=0; features[i] != nullptr; ++i)
  460. {
  461. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  462. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  463. {
  464. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  465. break;
  466. }
  467. }
  468. if (fUI.host == nullptr)
  469. return false;
  470. fUI.writeFunction = writeFunction;
  471. fUI.controller = controller;
  472. *widget = this;
  473. fHost.uiName = fUI.host->plugin_human_id;
  474. return true;
  475. }
  476. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  477. {
  478. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  479. return;
  480. if (portIndex >= fUI.portOffset || ! fUI.isVisible)
  481. return;
  482. if (fDescriptor->ui_set_parameter_value == nullptr)
  483. return;
  484. const float value(*(const float*)buffer);
  485. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fUI.portOffset, value);
  486. }
  487. void lv2ui_cleanup()
  488. {
  489. fUI.host = nullptr;
  490. fUI.writeFunction = nullptr;
  491. fUI.controller = nullptr;
  492. if (! fUI.isVisible)
  493. return;
  494. if (fDescriptor->ui_show != nullptr)
  495. fDescriptor->ui_show(fHandle, false);
  496. fUI.isVisible = false;
  497. }
  498. // -------------------------------------------------------------------
  499. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  500. {
  501. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  502. return;
  503. if (fDescriptor->ui_set_midi_program == nullptr)
  504. return;
  505. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  506. }
  507. // -------------------------------------------------------------------
  508. protected:
  509. void handleUiRun()
  510. {
  511. if (fDescriptor->ui_idle != nullptr)
  512. fDescriptor->ui_idle(fHandle);
  513. }
  514. void handleUiShow()
  515. {
  516. if (fDescriptor->ui_show != nullptr)
  517. {
  518. if (fDescriptor->hints & PLUGIN_NEEDS_UI_JUCE)
  519. {
  520. JUCE_AUTORELEASEPOOL
  521. if (gActivePlugins.size() == 0)
  522. {
  523. initialiseJuce_GUI();
  524. JuceMessageThread::getInstance();
  525. }
  526. fDescriptor->ui_show(fHandle, true);
  527. fUiWasShown = true;
  528. gActivePlugins.add(this);
  529. }
  530. else
  531. fDescriptor->ui_show(fHandle, true);
  532. }
  533. fUI.isVisible = true;
  534. }
  535. void handleUiHide()
  536. {
  537. if (fDescriptor->ui_show != nullptr)
  538. fDescriptor->ui_show(fHandle, false);
  539. fUI.isVisible = false;
  540. }
  541. // -------------------------------------------------------------------
  542. uint32_t handleGetBufferSize() const
  543. {
  544. return fBufferSize;
  545. }
  546. double handleGetSampleRate() const
  547. {
  548. return fSampleRate;
  549. }
  550. bool handleIsOffline() const
  551. {
  552. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  553. return (fPorts.freewheel != nullptr && *fPorts.freewheel >= 0.5f);
  554. }
  555. const NativeTimeInfo* handleGetTimeInfo() const
  556. {
  557. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  558. return &fTimeInfo;
  559. }
  560. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  561. {
  562. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  563. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  564. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  565. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  566. // reverse-find first free event, and put it there
  567. for (uint32_t i=(kMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  568. {
  569. if (fMidiEvents[i].data[0] == 0)
  570. {
  571. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  572. return true;
  573. }
  574. }
  575. return false;
  576. }
  577. void handleUiParameterChanged(const uint32_t index, const float value) const
  578. {
  579. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  580. fUI.writeFunction(fUI.controller, index+fUI.portOffset, sizeof(float), 0, &value);
  581. }
  582. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  583. {
  584. //storeCustomData(key, value);
  585. }
  586. void handleUiClosed()
  587. {
  588. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  589. fUI.host->ui_closed(fUI.controller);
  590. fUI.host = nullptr;
  591. fUI.writeFunction = nullptr;
  592. fUI.controller = nullptr;
  593. fUI.isVisible = false;
  594. }
  595. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  596. {
  597. // TODO
  598. return nullptr;
  599. }
  600. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  601. {
  602. // TODO
  603. return nullptr;
  604. }
  605. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  606. {
  607. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  608. intptr_t ret = 0;
  609. switch (opcode)
  610. {
  611. case HOST_OPCODE_NULL:
  612. break;
  613. case HOST_OPCODE_SET_VOLUME:
  614. fVolume = opt;
  615. break;
  616. case HOST_OPCODE_SET_DRYWET:
  617. fDryWet = opt;
  618. break;
  619. case HOST_OPCODE_SET_BALANCE_LEFT:
  620. case HOST_OPCODE_SET_BALANCE_RIGHT:
  621. case HOST_OPCODE_SET_PANNING:
  622. // nothing
  623. break;
  624. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  625. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  626. case HOST_OPCODE_SET_PROCESS_PRECISION:
  627. case HOST_OPCODE_UPDATE_PARAMETER:
  628. case HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  629. case HOST_OPCODE_RELOAD_PARAMETERS:
  630. case HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  631. case HOST_OPCODE_RELOAD_ALL:
  632. // nothing
  633. break;
  634. case HOST_OPCODE_UI_UNAVAILABLE:
  635. handleUiClosed();
  636. break;
  637. }
  638. return ret;
  639. // unused for now
  640. (void)index;
  641. (void)value;
  642. (void)ptr;
  643. }
  644. void updateParameterOutputs()
  645. {
  646. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  647. {
  648. if (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT)
  649. {
  650. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  651. if (fPorts.paramsPtr[i] != nullptr)
  652. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  653. }
  654. }
  655. }
  656. // -------------------------------------------------------------------
  657. private:
  658. // Native data
  659. NativePluginHandle fHandle;
  660. NativeHostDescriptor fHost;
  661. const NativePluginDescriptor* const fDescriptor;
  662. LV2_Program_Descriptor fProgramDesc;
  663. uint32_t fMidiEventCount;
  664. NativeMidiEvent fMidiEvents[kMaxMidiEvents*2];
  665. NativeTimeInfo fTimeInfo;
  666. bool fUiWasShown;
  667. bool fIsProcessing;
  668. float fVolume;
  669. float fDryWet;
  670. // Lv2 host data
  671. uint32_t fBufferSize;
  672. double fSampleRate;
  673. const LV2_URID_Map* fUridMap;
  674. struct URIDs {
  675. LV2_URID atomBlank;
  676. LV2_URID atomFloat;
  677. LV2_URID atomLong;
  678. LV2_URID atomSequence;
  679. LV2_URID atomString;
  680. LV2_URID midiEvent;
  681. LV2_URID timePos;
  682. LV2_URID timeBar;
  683. LV2_URID timeBarBeat;
  684. LV2_URID timeBeatsPerBar;
  685. LV2_URID timeBeatsPerMinute;
  686. LV2_URID timeBeatUnit;
  687. LV2_URID timeFrame;
  688. LV2_URID timeSpeed;
  689. URIDs()
  690. : atomBlank(0),
  691. atomFloat(0),
  692. atomLong(0),
  693. atomSequence(0),
  694. atomString(0),
  695. midiEvent(0),
  696. timePos(0),
  697. timeBar(0),
  698. timeBarBeat(0),
  699. timeBeatsPerBar(0),
  700. timeBeatsPerMinute(0),
  701. timeBeatUnit(0),
  702. timeFrame(0),
  703. timeSpeed(0) {}
  704. void map(const LV2_URID_Map* const uridMap)
  705. {
  706. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  707. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  708. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  709. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  710. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  711. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  712. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  713. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  714. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  715. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  716. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  717. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  718. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  719. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  720. }
  721. } fUris;
  722. struct UI {
  723. const LV2_External_UI_Host* host;
  724. LV2UI_Write_Function writeFunction;
  725. LV2UI_Controller controller;
  726. uint32_t portOffset;
  727. bool isVisible;
  728. UI()
  729. : host(nullptr),
  730. writeFunction(nullptr),
  731. controller(nullptr),
  732. portOffset(0),
  733. isVisible(false) {}
  734. } fUI;
  735. struct Ports {
  736. LV2_Atom_Sequence** eventsIn;
  737. LV2_Atom_Sequence** midiOuts;
  738. float** audioIns;
  739. float** audioOuts;
  740. float* freewheel;
  741. uint32_t paramCount;
  742. float* paramsLast;
  743. float** paramsPtr;
  744. Ports()
  745. : eventsIn(nullptr),
  746. midiOuts(nullptr),
  747. audioIns(nullptr),
  748. audioOuts(nullptr),
  749. freewheel(nullptr),
  750. paramCount(0),
  751. paramsLast(nullptr),
  752. paramsPtr(nullptr) {}
  753. ~Ports()
  754. {
  755. if (eventsIn != nullptr)
  756. {
  757. delete[] eventsIn;
  758. eventsIn = nullptr;
  759. }
  760. if (midiOuts != nullptr)
  761. {
  762. delete[] midiOuts;
  763. midiOuts = nullptr;
  764. }
  765. if (audioIns != nullptr)
  766. {
  767. delete[] audioIns;
  768. audioIns = nullptr;
  769. }
  770. if (audioOuts != nullptr)
  771. {
  772. delete[] audioOuts;
  773. audioOuts = nullptr;
  774. }
  775. if (paramsLast != nullptr)
  776. {
  777. delete[] paramsLast;
  778. paramsLast = nullptr;
  779. }
  780. if (paramsPtr != nullptr)
  781. {
  782. delete[] paramsPtr;
  783. paramsPtr = nullptr;
  784. }
  785. }
  786. void init(const NativePluginDescriptor* const desc, NativePluginHandle handle)
  787. {
  788. CARLA_SAFE_ASSERT_RETURN(desc != nullptr && handle != nullptr,)
  789. if (desc->midiIns > 0)
  790. {
  791. eventsIn = new LV2_Atom_Sequence*[desc->midiIns];
  792. for (uint32_t i=0; i < desc->midiIns; ++i)
  793. eventsIn[i] = nullptr;
  794. }
  795. else if (desc->hints & PLUGIN_USES_TIME)
  796. {
  797. eventsIn = new LV2_Atom_Sequence*[1];
  798. eventsIn[0] = nullptr;
  799. }
  800. if (desc->midiOuts > 0)
  801. {
  802. midiOuts = new LV2_Atom_Sequence*[desc->midiOuts];
  803. for (uint32_t i=0; i < desc->midiOuts; ++i)
  804. midiOuts[i] = nullptr;
  805. }
  806. if (desc->audioIns > 0)
  807. {
  808. audioIns = new float*[desc->audioIns];
  809. for (uint32_t i=0; i < desc->audioIns; ++i)
  810. audioIns[i] = nullptr;
  811. }
  812. if (desc->audioOuts > 0)
  813. {
  814. audioOuts = new float*[desc->audioOuts];
  815. for (uint32_t i=0; i < desc->audioOuts; ++i)
  816. audioOuts[i] = nullptr;
  817. }
  818. if (desc->get_parameter_count != nullptr && desc->get_parameter_info != nullptr && desc->get_parameter_value != nullptr && desc->set_parameter_value != nullptr)
  819. {
  820. paramCount = desc->get_parameter_count(handle);
  821. if (paramCount > 0)
  822. {
  823. paramsLast = new float[paramCount];
  824. paramsPtr = new float*[paramCount];
  825. for (uint32_t i=0; i < paramCount; ++i)
  826. {
  827. paramsLast[i] = desc->get_parameter_value(handle, i);
  828. paramsPtr[i] = nullptr;
  829. }
  830. }
  831. }
  832. }
  833. void connectPort(const NativePluginDescriptor* const desc, const uint32_t port, void* const dataLocation)
  834. {
  835. uint32_t index = 0;
  836. if (desc->midiIns > 0 || (desc->hints & PLUGIN_USES_TIME) != 0)
  837. {
  838. if (port == index++)
  839. {
  840. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  841. return;
  842. }
  843. }
  844. for (uint32_t i=1; i < desc->midiIns; ++i)
  845. {
  846. if (port == index++)
  847. {
  848. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  849. return;
  850. }
  851. }
  852. for (uint32_t i=0; i < desc->midiOuts; ++i)
  853. {
  854. if (port == index++)
  855. {
  856. midiOuts[i] = (LV2_Atom_Sequence*)dataLocation;
  857. return;
  858. }
  859. }
  860. if (port == index++)
  861. {
  862. freewheel = (float*)dataLocation;
  863. return;
  864. }
  865. for (uint32_t i=0; i < desc->audioIns; ++i)
  866. {
  867. if (port == index++)
  868. {
  869. audioIns[i] = (float*)dataLocation;
  870. return;
  871. }
  872. }
  873. for (uint32_t i=0; i < desc->audioOuts; ++i)
  874. {
  875. if (port == index++)
  876. {
  877. audioOuts[i] = (float*)dataLocation;
  878. return;
  879. }
  880. }
  881. for (uint32_t i=0; i < paramCount; ++i)
  882. {
  883. if (port == index++)
  884. {
  885. paramsPtr[i] = (float*)dataLocation;
  886. return;
  887. }
  888. }
  889. }
  890. } fPorts;
  891. // -------------------------------------------------------------------
  892. #define handlePtr ((NativePlugin*)_this_)
  893. static void extui_run(LV2_External_UI_Widget* _this_)
  894. {
  895. handlePtr->handleUiRun();
  896. }
  897. static void extui_show(LV2_External_UI_Widget* _this_)
  898. {
  899. handlePtr->handleUiShow();
  900. }
  901. static void extui_hide(LV2_External_UI_Widget* _this_)
  902. {
  903. handlePtr->handleUiHide();
  904. }
  905. #undef handlePtr
  906. // -------------------------------------------------------------------
  907. #define handlePtr ((NativePlugin*)handle)
  908. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  909. {
  910. return handlePtr->handleGetBufferSize();
  911. }
  912. static double host_get_sample_rate(NativeHostHandle handle)
  913. {
  914. return handlePtr->handleGetSampleRate();
  915. }
  916. static bool host_is_offline(NativeHostHandle handle)
  917. {
  918. return handlePtr->handleIsOffline();
  919. }
  920. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  921. {
  922. return handlePtr->handleGetTimeInfo();
  923. }
  924. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  925. {
  926. return handlePtr->handleWriteMidiEvent(event);
  927. }
  928. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  929. {
  930. handlePtr->handleUiParameterChanged(index, value);
  931. }
  932. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  933. {
  934. handlePtr->handleUiCustomDataChanged(key, value);
  935. }
  936. static void host_ui_closed(NativeHostHandle handle)
  937. {
  938. handlePtr->handleUiClosed();
  939. }
  940. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  941. {
  942. return handlePtr->handleUiOpenFile(isDir, title, filter);
  943. }
  944. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  945. {
  946. return handlePtr->handleUiSaveFile(isDir, title, filter);
  947. }
  948. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  949. {
  950. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  951. }
  952. #undef handlePtr
  953. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  954. };
  955. // -----------------------------------------------------------------------
  956. // LV2 descriptor functions
  957. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  958. {
  959. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  960. const NativePluginDescriptor* pluginDesc = nullptr;
  961. const char* pluginLabel = nullptr;
  962. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  963. pluginLabel = lv2Descriptor->URI+37;
  964. else if (std::strcmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla") == 0)
  965. pluginLabel = "carla";
  966. if (pluginLabel == nullptr)
  967. {
  968. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  969. return nullptr;
  970. }
  971. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  972. PluginListManager& plm(PluginListManager::getInstance());
  973. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  974. {
  975. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  976. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  977. {
  978. pluginDesc = tmpDesc;
  979. break;
  980. }
  981. }
  982. if (pluginDesc == nullptr)
  983. {
  984. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  985. return nullptr;
  986. }
  987. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  988. if (! plugin->init())
  989. {
  990. carla_stderr("Failed to init plugin");
  991. delete plugin;
  992. return nullptr;
  993. }
  994. return (LV2_Handle)plugin;
  995. }
  996. #define instancePtr ((NativePlugin*)instance)
  997. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  998. {
  999. instancePtr->lv2_connect_port(port, dataLocation);
  1000. }
  1001. static void lv2_activate(LV2_Handle instance)
  1002. {
  1003. carla_debug("lv2_activate(%p)", instance);
  1004. instancePtr->lv2_activate();
  1005. }
  1006. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  1007. {
  1008. instancePtr->lv2_run(sampleCount);
  1009. }
  1010. static void lv2_deactivate(LV2_Handle instance)
  1011. {
  1012. carla_debug("lv2_deactivate(%p)", instance);
  1013. instancePtr->lv2_deactivate();
  1014. }
  1015. static void lv2_cleanup(LV2_Handle instance)
  1016. {
  1017. carla_debug("lv2_cleanup(%p)", instance);
  1018. instancePtr->lv2_cleanup();
  1019. delete instancePtr;
  1020. }
  1021. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1022. {
  1023. carla_debug("lv2_get_options(%p, %p)", instance, options);
  1024. return instancePtr->lv2_get_options(options);
  1025. }
  1026. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1027. {
  1028. carla_debug("lv2_set_options(%p, %p)", instance, options);
  1029. return instancePtr->lv2_set_options(options);
  1030. }
  1031. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1032. {
  1033. carla_debug("lv2_get_program(%p, %i)", instance, index);
  1034. return instancePtr->lv2_get_program(index);
  1035. }
  1036. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1037. {
  1038. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  1039. return instancePtr->lv2_select_program(bank, program);
  1040. }
  1041. 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)
  1042. {
  1043. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  1044. return instancePtr->lv2_save(store, handle, flags, features);
  1045. }
  1046. 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)
  1047. {
  1048. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  1049. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  1050. }
  1051. static const void* lv2_extension_data(const char* uri)
  1052. {
  1053. carla_debug("lv2_extension_data(\"%s\")", uri);
  1054. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1055. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1056. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1057. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1058. return &options;
  1059. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1060. return &programs;
  1061. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1062. return &state;
  1063. return nullptr;
  1064. }
  1065. #undef instancePtr
  1066. // -----------------------------------------------------------------------
  1067. // Startup code
  1068. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*, LV2UI_Write_Function writeFunction,
  1069. LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  1070. {
  1071. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  1072. NativePlugin* plugin = nullptr;
  1073. for (int i=0; features[i] != nullptr; ++i)
  1074. {
  1075. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1076. {
  1077. plugin = (NativePlugin*)features[i]->data;
  1078. break;
  1079. }
  1080. }
  1081. if (plugin == nullptr)
  1082. {
  1083. carla_stderr("Host doesn't support instance-access, cannot show UI");
  1084. return nullptr;
  1085. }
  1086. if (! plugin->lv2ui_instantiate(writeFunction, controller, widget, features))
  1087. {
  1088. carla_stderr("Host doesn't support external UI");
  1089. return nullptr;
  1090. }
  1091. return (LV2UI_Handle)plugin;
  1092. }
  1093. #define uiPtr ((NativePlugin*)ui)
  1094. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1095. {
  1096. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  1097. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  1098. }
  1099. static void lv2ui_cleanup(LV2UI_Handle ui)
  1100. {
  1101. carla_debug("lv2ui_cleanup(%p)", ui);
  1102. uiPtr->lv2ui_cleanup();
  1103. }
  1104. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  1105. {
  1106. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  1107. uiPtr->lv2ui_select_program(bank, program);
  1108. }
  1109. static const void* lv2ui_extension_data(const char* uri)
  1110. {
  1111. carla_debug("lv2ui_extension_data(\"%s\")", uri);
  1112. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  1113. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  1114. return &uiprograms;
  1115. return nullptr;
  1116. }
  1117. #undef uiPtr
  1118. // -----------------------------------------------------------------------
  1119. // Startup code
  1120. CARLA_EXPORT
  1121. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1122. {
  1123. carla_debug("lv2_descriptor(%i)", index);
  1124. PluginListManager& plm(PluginListManager::getInstance());
  1125. if (index >= plm.descs.count())
  1126. {
  1127. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1128. return nullptr;
  1129. }
  1130. if (index < plm.lv2Descs.count())
  1131. {
  1132. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1133. return plm.lv2Descs.getAt(index);
  1134. }
  1135. const NativePluginDescriptor*& pluginDesc(plm.descs.getAt(index));
  1136. CarlaString tmpURI;
  1137. if (std::strcmp(pluginDesc->label, "carla") == 0)
  1138. {
  1139. tmpURI = "http://kxstudio.sf.net/carla";
  1140. }
  1141. else
  1142. {
  1143. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1144. tmpURI += pluginDesc->label;
  1145. }
  1146. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1147. const LV2_Descriptor lv2DescTmp = {
  1148. /* URI */ carla_strdup(tmpURI),
  1149. /* instantiate */ lv2_instantiate,
  1150. /* connect_port */ lv2_connect_port,
  1151. /* activate */ lv2_activate,
  1152. /* run */ lv2_run,
  1153. /* deactivate */ lv2_deactivate,
  1154. /* cleanup */ lv2_cleanup,
  1155. /* extension_data */ lv2_extension_data
  1156. };
  1157. LV2_Descriptor* const lv2Desc(new LV2_Descriptor);
  1158. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1159. plm.lv2Descs.append(lv2Desc);
  1160. return lv2Desc;
  1161. }
  1162. CARLA_EXPORT
  1163. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1164. {
  1165. carla_debug("lv2ui_descriptor(%i)", index);
  1166. static const LV2UI_Descriptor lv2UiDesc = {
  1167. /* URI */ "http://kxstudio.sf.net/carla/ui",
  1168. /* instantiate */ lv2ui_instantiate,
  1169. /* cleanup */ lv2ui_cleanup,
  1170. /* port_event */ lv2ui_port_event,
  1171. /* extension_data */ lv2ui_extension_data
  1172. };
  1173. return (index == 0) ? &lv2UiDesc : nullptr;
  1174. }
  1175. // -----------------------------------------------------------------------