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

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