Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

carla-lv2.cpp 53KB

10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
6 years ago
11 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #define CARLA_NATIVE_PLUGIN_LV2
  18. #include "carla-base.cpp"
  19. #include "CarlaLv2Utils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaPipeUtils.hpp"
  22. #include "CarlaString.hpp"
  23. #include "water/files/File.h"
  24. static const char* const kPathForCarlaFiles = "carlafiles";
  25. template<>
  26. void Lv2PluginBaseClass<NativeTimeInfo>::clearTimeData() noexcept
  27. {
  28. fLastPositionData.clear();
  29. carla_zeroStruct(fTimeInfo);
  30. }
  31. struct PreviewData {
  32. char type;
  33. uint32_t size;
  34. const void* buffer;
  35. bool shouldSend;
  36. PreviewData()
  37. : type(0),
  38. size(0),
  39. buffer(nullptr),
  40. shouldSend(false) {}
  41. };
  42. // --------------------------------------------------------------------------------------------------------------------
  43. // Carla Internal Plugin API exposed as LV2 plugin
  44. class NativePlugin : public Lv2PluginBaseClass<NativeTimeInfo>
  45. {
  46. public:
  47. static const uint32_t kMaxMidiEvents = 512;
  48. NativePlugin(const NativePluginDescriptor* const desc,
  49. const double sampleRate,
  50. const char* const bundlePath,
  51. const LV2_Feature* const* const features)
  52. : Lv2PluginBaseClass<NativeTimeInfo>(sampleRate, features),
  53. fHandle(nullptr),
  54. fHost(),
  55. fDescriptor(desc),
  56. #ifdef CARLA_PROPER_CPP11_SUPPORT
  57. fProgramDesc({0, 0, nullptr}),
  58. #endif
  59. kIgnoreParameters(std::strncmp(desc->label, "carla", 5) == 0),
  60. fAtomForge(),
  61. fMidiEventCount(0),
  62. fLastProjectPath(),
  63. fLoadedFile(),
  64. fPreviewData(),
  65. fNeedsNotifyFileChanged(false),
  66. fPluginNeedsIdle(0),
  67. fWorkerUISignal(0)
  68. {
  69. carla_zeroStruct(fHost);
  70. #ifndef CARLA_PROPER_CPP11_SUPPORT
  71. carla_zeroStruct(fProgramDesc);
  72. #endif
  73. if (! loadedInProperHost())
  74. return;
  75. using water::File;
  76. using water::String;
  77. String resourceDir(water::File(bundlePath).getChildFile("resources").getFullPathName());
  78. fHost.handle = this;
  79. fHost.resourceDir = carla_strdup(resourceDir.toRawUTF8());
  80. fHost.uiName = nullptr;
  81. fHost.uiParentId = 0;
  82. fHost.get_buffer_size = host_get_buffer_size;
  83. fHost.get_sample_rate = host_get_sample_rate;
  84. fHost.is_offline = host_is_offline;
  85. fHost.get_time_info = host_get_time_info;
  86. fHost.write_midi_event = host_write_midi_event;
  87. fHost.ui_parameter_changed = host_ui_parameter_changed;
  88. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  89. fHost.ui_closed = host_ui_closed;
  90. fHost.ui_open_file = host_ui_open_file;
  91. fHost.ui_save_file = host_ui_save_file;
  92. fHost.dispatcher = host_dispatcher;
  93. carla_zeroStruct(fAtomForge);
  94. lv2_atom_forge_init(&fAtomForge, fUridMap);
  95. }
  96. ~NativePlugin()
  97. {
  98. CARLA_SAFE_ASSERT(fHandle == nullptr);
  99. if (fHost.resourceDir != nullptr)
  100. {
  101. delete[] fHost.resourceDir;
  102. fHost.resourceDir = nullptr;
  103. }
  104. if (fHost.uiName != nullptr)
  105. {
  106. delete[] fHost.uiName;
  107. fHost.uiName = nullptr;
  108. }
  109. }
  110. // ----------------------------------------------------------------------------------------------------------------
  111. bool init()
  112. {
  113. if (fHost.resourceDir == nullptr)
  114. return false;
  115. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  116. {
  117. carla_stderr("Plugin is missing something...");
  118. return false;
  119. }
  120. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  121. fHandle = fDescriptor->instantiate(&fHost);
  122. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  123. fPorts.hasUI = fDescriptor->hints & NATIVE_PLUGIN_HAS_UI;
  124. fPorts.usesTime = fDescriptor->hints & NATIVE_PLUGIN_USES_TIME;
  125. fPorts.numAudioIns = fDescriptor->audioIns;
  126. fPorts.numAudioOuts = fDescriptor->audioOuts;
  127. fPorts.numCVIns = fDescriptor->cvIns;
  128. fPorts.numCVOuts = fDescriptor->cvOuts;
  129. fPorts.numMidiIns = fDescriptor->midiIns;
  130. fPorts.numMidiOuts = fDescriptor->midiOuts;
  131. if (fDescriptor->get_parameter_count != nullptr &&
  132. fDescriptor->get_parameter_info != nullptr &&
  133. fDescriptor->get_parameter_value != nullptr &&
  134. fDescriptor->set_parameter_value != nullptr &&
  135. ! kIgnoreParameters)
  136. {
  137. fPorts.numParams = fDescriptor->get_parameter_count(fHandle);
  138. }
  139. fPorts.init();
  140. if (fPorts.numParams > 0)
  141. {
  142. for (uint32_t i=0; i < fPorts.numParams; ++i)
  143. {
  144. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  145. fPorts.paramsOut [i] = fDescriptor->get_parameter_info(fHandle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT;
  146. }
  147. }
  148. return true;
  149. }
  150. // ----------------------------------------------------------------------------------------------------------------
  151. // LV2 functions
  152. void lv2_activate()
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(! fIsActive,);
  155. resetTimeInfo();
  156. if (fDescriptor->activate != nullptr)
  157. fDescriptor->activate(fHandle);
  158. fIsActive = true;
  159. }
  160. void lv2_deactivate()
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(fIsActive,);
  163. fIsActive = false;
  164. if (fDescriptor->deactivate != nullptr)
  165. fDescriptor->deactivate(fHandle);
  166. }
  167. void lv2_cleanup()
  168. {
  169. if (fIsActive)
  170. {
  171. carla_stderr("Warning: Host forgot to call deactivate!");
  172. fIsActive = false;
  173. if (fDescriptor->deactivate != nullptr)
  174. fDescriptor->deactivate(fHandle);
  175. }
  176. if (fDescriptor->cleanup != nullptr)
  177. fDescriptor->cleanup(fHandle);
  178. fHandle = nullptr;
  179. }
  180. // ----------------------------------------------------------------------------------------------------------------
  181. void lv2_run(const uint32_t frames)
  182. {
  183. if (! lv2_pre_run(frames))
  184. {
  185. updateParameterOutputs();
  186. return;
  187. }
  188. if (fPorts.numMidiIns > 0 || fPorts.hasUI)
  189. {
  190. uint32_t numEventsIn;
  191. if (fPorts.numMidiIns > 0)
  192. {
  193. numEventsIn = fPorts.numMidiIns;
  194. fMidiEventCount = 0;
  195. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  196. }
  197. else
  198. {
  199. numEventsIn = 1;
  200. }
  201. for (uint32_t i=0; i < numEventsIn; ++i)
  202. {
  203. const LV2_Atom_Sequence* const eventsIn(fPorts.eventsIn[i]);
  204. CARLA_SAFE_ASSERT_CONTINUE(eventsIn != nullptr);
  205. LV2_ATOM_SEQUENCE_FOREACH(eventsIn, event)
  206. {
  207. if (event == nullptr)
  208. continue;
  209. if (event->body.type == fURIs.carlaUiEvents && fWorkerUISignal != -1)
  210. {
  211. CARLA_SAFE_ASSERT_CONTINUE((fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE) == 0);
  212. if (fWorker != nullptr)
  213. {
  214. // worker is supported by the host, we can continue
  215. fWorkerUISignal = 1;
  216. const char* const msg((const char*)(event + 1));
  217. const size_t msgSize = std::strlen(msg);
  218. fWorker->schedule_work(fWorker->handle, static_cast<uint32_t>(msgSize + 1U), msg);
  219. }
  220. else
  221. {
  222. // worker is not supported, cancel
  223. fWorkerUISignal = -1;
  224. }
  225. continue;
  226. }
  227. if (event->body.type == fURIs.atomObject)
  228. {
  229. const LV2_Atom_Object* const obj = (const LV2_Atom_Object*)(&event->body);
  230. if (obj->body.otype == fURIs.patchSet)
  231. {
  232. // Get property URI.
  233. const LV2_Atom* property = nullptr;
  234. lv2_atom_object_get(obj, fURIs.patchProperty, &property, 0);
  235. CARLA_SAFE_ASSERT_CONTINUE(property != nullptr);
  236. CARLA_SAFE_ASSERT_CONTINUE(property->type == fURIs.atomURID);
  237. const LV2_URID urid = ((const LV2_Atom_URID*)property)->body;
  238. /* */ if (std::strcmp(fDescriptor->label, "audiofile") == 0) {
  239. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFileAudio);
  240. } else if (std::strcmp(fDescriptor->label, "midifile") == 0) {
  241. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFileMIDI);
  242. } else {
  243. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFile);
  244. }
  245. // Get value.
  246. const LV2_Atom* fileobj = nullptr;
  247. lv2_atom_object_get(obj, fURIs.patchValue, &fileobj, 0);
  248. CARLA_SAFE_ASSERT_CONTINUE(fileobj != nullptr);
  249. CARLA_SAFE_ASSERT_CONTINUE(fileobj->type == fURIs.atomPath);
  250. const char* const filepath((const char*)(fileobj + 1));
  251. fWorker->schedule_work(fWorker->handle,
  252. static_cast<uint32_t>(std::strlen(filepath) + 1U),
  253. filepath);
  254. }
  255. else if (obj->body.otype == fURIs.patchGet)
  256. {
  257. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  258. fNeedsNotifyFileChanged = true;
  259. if (fPreviewData.buffer != nullptr)
  260. fPreviewData.shouldSend = true;
  261. }
  262. continue;
  263. }
  264. if (event->body.type != fURIs.midiEvent)
  265. continue;
  266. // anything past this point assumes plugin with MIDI input
  267. CARLA_SAFE_ASSERT_CONTINUE(fPorts.numMidiIns > 0);
  268. if (event->body.size > 4)
  269. continue;
  270. if (event->time.frames >= frames)
  271. break;
  272. const uint8_t* const data((const uint8_t*)(event + 1));
  273. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  274. nativeEvent.port = (uint8_t)i;
  275. nativeEvent.size = (uint8_t)event->body.size;
  276. nativeEvent.time = (uint32_t)event->time.frames;
  277. uint32_t j=0;
  278. for (uint32_t size=event->body.size; j<size; ++j)
  279. nativeEvent.data[j] = data[j];
  280. for (; j<4; ++j)
  281. nativeEvent.data[j] = 0;
  282. if (fMidiEventCount >= kMaxMidiEvents)
  283. break;
  284. }
  285. }
  286. if (fNeedsNotifyFileChanged || fPreviewData.shouldSend)
  287. {
  288. uint8_t atomBuf[4096];
  289. LV2_Atom_Forge atomForge = fAtomForge;
  290. lv2_atom_forge_set_buffer(&atomForge, atomBuf, sizeof(atomBuf));
  291. const int numEvents = fNeedsNotifyFileChanged && fPreviewData.shouldSend ? 2 : 1;
  292. if (fNeedsNotifyFileChanged)
  293. {
  294. fNeedsNotifyFileChanged = false;
  295. LV2_Atom_Forge_Frame forgeFrame;
  296. lv2_atom_forge_object(&atomForge, &forgeFrame, 0, fURIs.patchSet);
  297. lv2_atom_forge_key(&atomForge, fURIs.patchProperty);
  298. /* */ if (std::strcmp(fDescriptor->label, "audiofile") == 0) {
  299. lv2_atom_forge_urid(&atomForge, fURIs.carlaFileAudio);
  300. } else if (std::strcmp(fDescriptor->label, "midifile") == 0) {
  301. lv2_atom_forge_urid(&atomForge, fURIs.carlaFileMIDI);
  302. } else {
  303. lv2_atom_forge_urid(&atomForge, fURIs.carlaFile);
  304. }
  305. lv2_atom_forge_key(&atomForge, fURIs.patchValue);
  306. lv2_atom_forge_path(&atomForge,
  307. fLoadedFile.buffer(),
  308. static_cast<uint32_t>(fLoadedFile.length()+1));
  309. lv2_atom_forge_pop(&atomForge, &forgeFrame);
  310. }
  311. if (fPreviewData.shouldSend)
  312. {
  313. const char ptype = fPreviewData.type;
  314. const uint32_t psize = fPreviewData.size;
  315. const void* const pbuffer = fPreviewData.buffer;
  316. fPreviewData.shouldSend = false;
  317. LV2_Atom_Forge_Frame forgeFrame;
  318. lv2_atom_forge_object(&atomForge, &forgeFrame, 0, fURIs.patchSet);
  319. lv2_atom_forge_key(&atomForge, fURIs.patchProperty);
  320. lv2_atom_forge_urid(&atomForge, fURIs.carlaPreview);
  321. lv2_atom_forge_key(&atomForge, fURIs.patchValue);
  322. switch (ptype)
  323. {
  324. case 'b':
  325. lv2_atom_forge_vector(&atomForge, sizeof(int32_t), fURIs.atomBool, psize, pbuffer);
  326. break;
  327. case 'i':
  328. lv2_atom_forge_vector(&atomForge, sizeof(int32_t), fURIs.atomInt, psize, pbuffer);
  329. break;
  330. case 'f':
  331. lv2_atom_forge_vector(&atomForge, sizeof(float), fURIs.atomFloat, psize, pbuffer);
  332. break;
  333. default:
  334. carla_stderr2("Preview data buffer has wrong type '%c' (and size %u)", ptype, psize);
  335. break;
  336. }
  337. lv2_atom_forge_pop(&atomForge, &forgeFrame);
  338. }
  339. LV2_Atom* atom = (LV2_Atom*)atomBuf;
  340. LV2_Atom_Sequence* const seq = fPorts.eventsOut[0];
  341. Ports::EventsOutData& mData(fPorts.eventsOutData[0]);
  342. for (int i=0; i<numEvents; ++i)
  343. {
  344. if (sizeof(LV2_Atom_Event) + atom->size <= mData.capacity - mData.offset)
  345. {
  346. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  347. aev->time.frames = 0;
  348. aev->body.size = atom->size;
  349. aev->body.type = atom->type;
  350. std::memcpy(LV2_ATOM_BODY(&aev->body), atom + 1, atom->size);
  351. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + atom->size));
  352. mData.offset += size;
  353. seq->atom.size += size;
  354. }
  355. atom = (LV2_Atom*)(atomBuf + lv2_atom_total_size(atom));
  356. }
  357. }
  358. }
  359. fDescriptor->process(fHandle, fPorts.audioCVIns, fPorts.audioCVOuts, frames, fMidiEvents, fMidiEventCount);
  360. if (fPluginNeedsIdle == 1)
  361. {
  362. fPluginNeedsIdle = 2;
  363. const char* const msg = "_idle_";
  364. const size_t msgSize = std::strlen(msg);
  365. fWorker->schedule_work(fWorker->handle, static_cast<uint32_t>(msgSize + 1U), msg);
  366. }
  367. if (fWorkerUISignal == -1 && fPorts.hasUI)
  368. {
  369. const char* const msg = "quit";
  370. const size_t msgSize = 5;
  371. LV2_Atom_Sequence* const seq(fPorts.eventsOut[0]);
  372. Ports::EventsOutData& mData(fPorts.eventsOutData[0]);
  373. if (sizeof(LV2_Atom_Event) + msgSize <= mData.capacity - mData.offset)
  374. {
  375. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  376. aev->time.frames = 0;
  377. aev->body.size = msgSize;
  378. aev->body.type = fURIs.carlaUiEvents;
  379. std::memcpy(LV2_ATOM_BODY(&aev->body), msg, msgSize);
  380. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + msgSize));
  381. mData.offset += size;
  382. seq->atom.size += size;
  383. fWorkerUISignal = 0;
  384. }
  385. }
  386. lv2_post_run(frames);
  387. updateParameterOutputs();
  388. }
  389. // ----------------------------------------------------------------------------------------------------------------
  390. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  391. {
  392. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  393. return nullptr;
  394. if (fDescriptor->get_midi_program_count == nullptr)
  395. return nullptr;
  396. if (fDescriptor->get_midi_program_info == nullptr)
  397. return nullptr;
  398. if (index >= fDescriptor->get_midi_program_count(fHandle))
  399. return nullptr;
  400. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  401. if (midiProg == nullptr)
  402. return nullptr;
  403. fProgramDesc.bank = midiProg->bank;
  404. fProgramDesc.program = midiProg->program;
  405. fProgramDesc.name = midiProg->name;
  406. return &fProgramDesc;
  407. }
  408. void lv2_select_program(uint32_t bank, uint32_t program)
  409. {
  410. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  411. return;
  412. if (fDescriptor->set_midi_program == nullptr)
  413. return;
  414. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  415. for (uint32_t i=0; i < fPorts.numParams; ++i)
  416. {
  417. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  418. if (fPorts.paramsPtr[i] != nullptr)
  419. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  420. }
  421. }
  422. // ----------------------------------------------------------------------------------------------------------------
  423. void cleanupLastProjectPath()
  424. {
  425. fLastProjectPath.clear();
  426. }
  427. void saveLastProjectPathIfPossible(const LV2_Feature* const* const features)
  428. {
  429. if (features == nullptr)
  430. return cleanupLastProjectPath();
  431. const LV2_State_Free_Path* freePath = nullptr;
  432. const LV2_State_Make_Path* makePath = nullptr;
  433. for (int i=0; features[i] != nullptr; ++i)
  434. {
  435. /**/ if (freePath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__freePath) == 0)
  436. freePath = (const LV2_State_Free_Path*)features[i]->data;
  437. else if (makePath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__makePath) == 0)
  438. makePath = (const LV2_State_Make_Path*)features[i]->data;
  439. }
  440. if (makePath == nullptr || makePath->path == nullptr)
  441. return cleanupLastProjectPath();
  442. if (freePath == nullptr)
  443. freePath = fFreePath;
  444. char* const newpath = makePath->path(makePath->handle, kPathForCarlaFiles);
  445. if (newpath == nullptr)
  446. return cleanupLastProjectPath();
  447. fLastProjectPath = CarlaString(water::File(newpath).getParentDirectory().getFullPathName().toRawUTF8());
  448. if (freePath != nullptr && freePath->free_path != nullptr)
  449. freePath->free_path(freePath->handle, newpath);
  450. #ifndef CARLA_OS_WIN
  451. // this is not safe to call under windows
  452. else
  453. std::free(newpath);
  454. #endif
  455. }
  456. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle,
  457. const uint32_t /*flags*/, const LV2_Feature* const* const features)
  458. {
  459. saveLastProjectPathIfPossible(features);
  460. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  461. {
  462. if (fLoadedFile.isEmpty())
  463. return LV2_STATE_SUCCESS;
  464. const LV2_State_Free_Path* freePath = nullptr;
  465. const LV2_State_Map_Path* mapPath = nullptr;
  466. if (features != nullptr)
  467. {
  468. for (int i=0; features[i] != nullptr; ++i)
  469. {
  470. /**/ if (freePath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__freePath) == 0)
  471. freePath = (const LV2_State_Free_Path*)features[i]->data;
  472. else if (mapPath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__mapPath) == 0)
  473. mapPath = (const LV2_State_Map_Path*)features[i]->data;
  474. }
  475. }
  476. if (mapPath == nullptr || mapPath->abstract_path == nullptr)
  477. return LV2_STATE_ERR_NO_FEATURE;
  478. char* path = mapPath->abstract_path(mapPath->handle, fLoadedFile.buffer());
  479. store(handle,
  480. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/file"),
  481. path,
  482. std::strlen(path)+1,
  483. fURIs.atomPath,
  484. LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  485. if (freePath != nullptr && freePath->free_path != nullptr)
  486. freePath->free_path(freePath->handle, path);
  487. #ifndef CARLA_OS_WIN
  488. // this is not safe to call under windows
  489. else
  490. std::free(path);
  491. #endif
  492. return LV2_STATE_SUCCESS;
  493. }
  494. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  495. return LV2_STATE_ERR_UNKNOWN;
  496. if (char* const state = fDescriptor->get_state(fHandle))
  497. {
  498. store(handle,
  499. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"),
  500. state,
  501. std::strlen(state)+1,
  502. fURIs.atomString,
  503. LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  504. std::free(state);
  505. return LV2_STATE_SUCCESS;
  506. }
  507. return LV2_STATE_ERR_UNKNOWN;
  508. }
  509. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve,
  510. const LV2_State_Handle handle,
  511. uint32_t flags,
  512. const LV2_Feature* const* const features)
  513. {
  514. saveLastProjectPathIfPossible(features);
  515. size_t size = 0;
  516. uint32_t type = 0;
  517. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  518. {
  519. size = type = 0;
  520. const void* const data = retrieve(handle,
  521. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/file"),
  522. &size, &type, &flags);
  523. if (size <= 1 || type == 0)
  524. return LV2_STATE_ERR_NO_PROPERTY;
  525. CARLA_SAFE_ASSERT_RETURN(type == fURIs.atomPath, LV2_STATE_ERR_UNKNOWN);
  526. const LV2_State_Free_Path* freePath = nullptr;
  527. const LV2_State_Map_Path* mapPath = nullptr;
  528. if (features != nullptr)
  529. {
  530. for (int i=0; features[i] != nullptr; ++i)
  531. {
  532. /**/ if (freePath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__freePath) == 0)
  533. freePath = (const LV2_State_Free_Path*)features[i]->data;
  534. else if (mapPath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__mapPath) == 0)
  535. mapPath = (const LV2_State_Map_Path*)features[i]->data;
  536. }
  537. }
  538. if (mapPath == nullptr || mapPath->absolute_path == nullptr)
  539. return LV2_STATE_ERR_NO_FEATURE;
  540. const char* const filename = (const char*)data;
  541. char* const absolute_filename = mapPath->absolute_path(mapPath->handle, filename);
  542. fLoadedFile = absolute_filename;
  543. if (freePath != nullptr && freePath->free_path != nullptr)
  544. freePath->free_path(freePath->handle, absolute_filename);
  545. #ifndef CARLA_OS_WIN
  546. // this is not safe to call under windows
  547. else
  548. std::free(absolute_filename);
  549. #endif
  550. fNeedsNotifyFileChanged = true;
  551. fDescriptor->set_custom_data(fHandle, "file", fLoadedFile);
  552. return LV2_STATE_SUCCESS;
  553. }
  554. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  555. return LV2_STATE_ERR_UNKNOWN;
  556. size = type = 0;
  557. const void* const data = retrieve(handle,
  558. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"),
  559. &size, &type, &flags);
  560. if (size == 0)
  561. return LV2_STATE_ERR_UNKNOWN;
  562. if (type == 0)
  563. return LV2_STATE_ERR_UNKNOWN;
  564. if (data == nullptr)
  565. return LV2_STATE_ERR_UNKNOWN;
  566. if (type != fURIs.atomString)
  567. return LV2_STATE_ERR_BAD_TYPE;
  568. fDescriptor->set_state(fHandle, (const char*)data);
  569. return LV2_STATE_SUCCESS;
  570. }
  571. // ----------------------------------------------------------------------------------------------------------------
  572. LV2_Worker_Status lv2_work(LV2_Worker_Respond_Function, LV2_Worker_Respond_Handle, uint32_t, const void* data)
  573. {
  574. const char* const msg = (const char*)data;
  575. if (std::strcmp(msg, "_idle_") == 0)
  576. {
  577. if (fDescriptor->hints & NATIVE_PLUGIN_REQUESTS_IDLE)
  578. {
  579. fPluginNeedsIdle = 0;
  580. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_IDLE, 0, 0, nullptr, 0.0f);
  581. return LV2_WORKER_SUCCESS;
  582. }
  583. return LV2_WORKER_ERR_UNKNOWN;
  584. }
  585. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  586. {
  587. fLoadedFile = msg;
  588. fDescriptor->set_custom_data(fHandle, "file", msg);
  589. return LV2_WORKER_SUCCESS;
  590. }
  591. /**/ if (std::strncmp(msg, "control ", 8) == 0)
  592. {
  593. if (fDescriptor->ui_set_parameter_value == nullptr)
  594. return LV2_WORKER_SUCCESS;
  595. if (const char* const msgSplit = std::strstr(msg+8, " "))
  596. {
  597. const char* const msgIndex = msg+8;
  598. CARLA_SAFE_ASSERT_RETURN(msgSplit - msgIndex < 8, LV2_WORKER_ERR_UNKNOWN);
  599. CARLA_SAFE_ASSERT_RETURN(msgSplit[0] != '\0', LV2_WORKER_ERR_UNKNOWN);
  600. char strBufIndex[8];
  601. carla_zeroChars(strBufIndex, 8);
  602. std::strncpy(strBufIndex, msgIndex, static_cast<size_t>(msgSplit - msgIndex));
  603. const int index = std::atoi(msgIndex) - static_cast<int>(fPorts.indexOffset);
  604. CARLA_SAFE_ASSERT_RETURN(index >= 0, LV2_WORKER_ERR_UNKNOWN);
  605. float value;
  606. {
  607. const CarlaScopedLocale csl;
  608. value = static_cast<float>(std::atof(msgSplit+1));
  609. }
  610. fDescriptor->ui_set_parameter_value(fHandle, static_cast<uint32_t>(index), value);
  611. }
  612. }
  613. else if (std::strcmp(msg, "show") == 0)
  614. {
  615. handleUiShow();
  616. }
  617. else if (std::strcmp(msg, "hide") == 0)
  618. {
  619. handleUiHide();
  620. }
  621. else if (std::strcmp(msg, "idle") == 0)
  622. {
  623. handleUiRun();
  624. }
  625. else if (std::strcmp(msg, "quit") == 0)
  626. {
  627. handleUiClosed();
  628. }
  629. else
  630. {
  631. carla_stdout("lv2_work unknown msg '%s'", msg);
  632. return LV2_WORKER_ERR_UNKNOWN;
  633. }
  634. return LV2_WORKER_SUCCESS;
  635. }
  636. LV2_Worker_Status lv2_work_resp(uint32_t /*size*/, const void* /*body*/)
  637. {
  638. return LV2_WORKER_SUCCESS;
  639. }
  640. const LV2_Inline_Display_Image_Surface* lv2_idisp_render(const uint32_t width, const uint32_t height)
  641. {
  642. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY, nullptr);
  643. CARLA_SAFE_ASSERT_RETURN(fDescriptor->render_inline_display, nullptr);
  644. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  645. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  646. const NativeInlineDisplayImageSurface* nsur = fDescriptor->render_inline_display(fHandle, width, height);
  647. CARLA_SAFE_ASSERT_RETURN(nsur != nullptr, nullptr);
  648. return (const LV2_Inline_Display_Image_Surface*)(nsur);
  649. }
  650. // ----------------------------------------------------------------------------------------------------------------
  651. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  652. LV2UI_Widget* widget, const LV2_Feature* const* features)
  653. {
  654. fUI.writeFunction = writeFunction;
  655. fUI.controller = controller;
  656. if (fHost.uiName != nullptr)
  657. {
  658. delete[] fHost.uiName;
  659. fHost.uiName = nullptr;
  660. }
  661. // ---------------------------------------------------------------
  662. // see if the host supports external-ui
  663. for (int i=0; features[i] != nullptr; ++i)
  664. {
  665. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  666. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  667. {
  668. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  669. }
  670. if (std::strcmp(features[i]->URI, LV2_UI__touch) == 0)
  671. {
  672. fUI.touch = (const LV2UI_Touch*)features[i]->data;
  673. }
  674. }
  675. if (fUI.host != nullptr)
  676. {
  677. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  678. *widget = (LV2_External_UI_Widget_Compat*)this;
  679. return;
  680. }
  681. // ---------------------------------------------------------------
  682. // no external-ui support, use showInterface
  683. for (int i=0; features[i] != nullptr; ++i)
  684. {
  685. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) != 0)
  686. continue;
  687. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  688. CARLA_SAFE_ASSERT_BREAK(options != nullptr);
  689. for (int j=0; options[j].key != 0; ++j)
  690. {
  691. if (options[j].key != fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  692. continue;
  693. const char* const title((const char*)options[j].value);
  694. CARLA_SAFE_ASSERT_BREAK(title != nullptr && title[0] != '\0');
  695. fHost.uiName = carla_strdup(title);
  696. break;
  697. }
  698. break;
  699. }
  700. if (fHost.uiName == nullptr)
  701. fHost.uiName = carla_strdup(fDescriptor->name);
  702. *widget = nullptr;
  703. return;
  704. }
  705. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  706. {
  707. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  708. return;
  709. if (portIndex < fPorts.indexOffset || ! fUI.isVisible)
  710. return;
  711. if (fDescriptor->ui_set_parameter_value == nullptr)
  712. return;
  713. const float value(*(const float*)buffer);
  714. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fPorts.indexOffset, value);
  715. }
  716. // ----------------------------------------------------------------------------------------------------------------
  717. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  718. {
  719. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  720. return;
  721. if (fDescriptor->ui_set_midi_program == nullptr)
  722. return;
  723. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  724. }
  725. // ----------------------------------------------------------------------------------------------------------------
  726. protected:
  727. void handleUiRun() const override
  728. {
  729. if (fDescriptor->ui_idle != nullptr)
  730. fDescriptor->ui_idle(fHandle);
  731. }
  732. void handleUiShow() override
  733. {
  734. if (fDescriptor->ui_show != nullptr)
  735. fDescriptor->ui_show(fHandle, true);
  736. fUI.isVisible = true;
  737. }
  738. void handleUiHide() override
  739. {
  740. if (fDescriptor->ui_show != nullptr)
  741. fDescriptor->ui_show(fHandle, false);
  742. fUI.isVisible = false;
  743. }
  744. // ----------------------------------------------------------------------------------------------------------------
  745. void handleParameterValueChanged(const uint32_t index, const float value) override
  746. {
  747. fDescriptor->set_parameter_value(fHandle, index, value);
  748. }
  749. void handleBufferSizeChanged(const uint32_t bufferSize) override
  750. {
  751. if (fDescriptor->dispatcher == nullptr)
  752. return;
  753. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, bufferSize, nullptr, 0.0f);
  754. }
  755. void handleSampleRateChanged(const double sampleRate) override
  756. {
  757. if (fDescriptor->dispatcher == nullptr)
  758. return;
  759. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
  760. }
  761. // ----------------------------------------------------------------------------------------------------------------
  762. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  763. {
  764. CARLA_SAFE_ASSERT_RETURN(fPorts.numMidiOuts > 0, false);
  765. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  766. CARLA_SAFE_ASSERT_RETURN(event->size > 0, false);
  767. const uint8_t port(event->port);
  768. CARLA_SAFE_ASSERT_RETURN(port < fPorts.numMidiOuts, false);
  769. LV2_Atom_Sequence* const seq(fPorts.eventsOut[port]);
  770. CARLA_SAFE_ASSERT_RETURN(seq != nullptr, false);
  771. Ports::EventsOutData& mData(fPorts.eventsOutData[port]);
  772. if (sizeof(LV2_Atom_Event) + event->size > mData.capacity - mData.offset)
  773. return false;
  774. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  775. aev->time.frames = event->time;
  776. aev->body.size = event->size;
  777. aev->body.type = fURIs.midiEvent;
  778. std::memcpy(LV2_ATOM_BODY(&aev->body), event->data, event->size);
  779. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + event->size));
  780. mData.offset += size;
  781. seq->atom.size += size;
  782. return true;
  783. }
  784. void handleUiParameterChanged(const uint32_t index, const float value) const
  785. {
  786. if (kIgnoreParameters || fWorkerUISignal)
  787. return;
  788. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  789. fUI.writeFunction(fUI.controller, index+fPorts.indexOffset, sizeof(float), 0, &value);
  790. }
  791. void handleUiParameterTouch(const uint32_t index, const bool touch) const
  792. {
  793. if (kIgnoreParameters)
  794. return;
  795. if (fUI.touch != nullptr && fUI.touch->touch != nullptr)
  796. fUI.touch->touch(fUI.touch->handle, index+fPorts.indexOffset, touch);
  797. }
  798. void handleUiResize(const uint32_t, const uint32_t) const
  799. {
  800. // nothing here
  801. }
  802. void handlePreviewBufferData(const char type, const uint32_t size, const void* const buffer) noexcept
  803. {
  804. fPreviewData.type = type;
  805. fPreviewData.size = size;
  806. fPreviewData.buffer = buffer;
  807. fPreviewData.shouldSend = true;
  808. }
  809. void handleUiCustomDataChanged(const char* const key, const char* const value) const
  810. {
  811. carla_stdout("TODO: handleUiCustomDataChanged %s %s", key, value);
  812. //storeCustomData(key, value);
  813. if (fUI.writeFunction == nullptr || fUI.controller == nullptr)
  814. return;
  815. }
  816. void handleUiClosed()
  817. {
  818. fUI.isVisible = false;
  819. if (fWorkerUISignal)
  820. fWorkerUISignal = -1;
  821. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  822. fUI.host->ui_closed(fUI.controller);
  823. fUI.host = nullptr;
  824. fUI.touch = nullptr;
  825. fUI.writeFunction = nullptr;
  826. fUI.controller = nullptr;
  827. }
  828. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  829. {
  830. // TODO
  831. return nullptr;
  832. }
  833. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  834. {
  835. // TODO
  836. return nullptr;
  837. }
  838. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  839. {
  840. #ifdef DEBUG
  841. if (opcode != NATIVE_HOST_OPCODE_GET_FILE_PATH)
  842. {
  843. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
  844. opcode, index, value, ptr, static_cast<double>(opt));
  845. }
  846. #endif
  847. intptr_t ret = 0;
  848. switch (opcode)
  849. {
  850. case NATIVE_HOST_OPCODE_NULL:
  851. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  852. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  853. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  854. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  855. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  856. case NATIVE_HOST_OPCODE_HOST_IDLE:
  857. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  858. // nothing
  859. break;
  860. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  861. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_REQUESTS_IDLE, 0);
  862. if (fWorker != nullptr && fPluginNeedsIdle == 0)
  863. {
  864. fPluginNeedsIdle = 1;
  865. return 1;
  866. }
  867. return 0;
  868. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  869. if (fInlineDisplay != nullptr && fInlineDisplay->queue_draw != nullptr)
  870. {
  871. fInlineDisplay->queue_draw(fInlineDisplay->handle);
  872. return 1;
  873. }
  874. return 0;
  875. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  876. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  877. if (std::strcmp((char*)ptr, "carla") == 0 && fLastProjectPath != nullptr)
  878. return static_cast<intptr_t>((uintptr_t)fLastProjectPath.buffer());
  879. break;
  880. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  881. handleUiClosed();
  882. break;
  883. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  884. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  885. handleUiParameterTouch(static_cast<uint32_t>(index), value != 0);
  886. break;
  887. case NATIVE_HOST_OPCODE_UI_RESIZE:
  888. CARLA_SAFE_ASSERT_RETURN(index > 0, 0);
  889. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  890. handleUiResize(static_cast<uint32_t>(index), static_cast<uint32_t>(value));
  891. break;
  892. case NATIVE_HOST_OPCODE_PREVIEW_BUFFER_DATA:
  893. CARLA_SAFE_ASSERT_RETURN(index != 0, 0);
  894. CARLA_SAFE_ASSERT_RETURN(index >= 'a', 0);
  895. CARLA_SAFE_ASSERT_RETURN(index <= 'z', 0);
  896. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  897. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  898. handlePreviewBufferData(static_cast<char>(index), static_cast<uint32_t>(value), (const void*)ptr);
  899. break;
  900. }
  901. return ret;
  902. // unused for now
  903. (void)opt;
  904. }
  905. void updateParameterOutputs()
  906. {
  907. float value;
  908. for (uint32_t i=0; i < fPorts.numParams; ++i)
  909. {
  910. if (! fPorts.paramsOut[i])
  911. continue;
  912. fPorts.paramsLast[i] = value = fDescriptor->get_parameter_value(fHandle, i);
  913. if (fPorts.paramsPtr[i] != nullptr)
  914. *fPorts.paramsPtr[i] = value;
  915. }
  916. }
  917. // -------------------------------------------------------------------
  918. private:
  919. // Native data
  920. NativePluginHandle fHandle;
  921. NativeHostDescriptor fHost;
  922. const NativePluginDescriptor* const fDescriptor;
  923. LV2_Program_Descriptor fProgramDesc;
  924. // carla as plugin does not implement lv2 parameter API yet, needed for feedback
  925. const bool kIgnoreParameters;
  926. LV2_Atom_Forge fAtomForge;
  927. uint32_t fMidiEventCount;
  928. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  929. CarlaString fLastProjectPath;
  930. CarlaString fLoadedFile;
  931. PreviewData fPreviewData;
  932. volatile bool fNeedsNotifyFileChanged;
  933. volatile int fPluginNeedsIdle;
  934. int fWorkerUISignal;
  935. // -1 needs close, 0 idle, 1 stuff is writing??
  936. // -------------------------------------------------------------------
  937. #define handlePtr ((NativePlugin*)handle)
  938. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  939. {
  940. return handlePtr->fBufferSize;
  941. }
  942. static double host_get_sample_rate(NativeHostHandle handle)
  943. {
  944. return handlePtr->fSampleRate;
  945. }
  946. static bool host_is_offline(NativeHostHandle handle)
  947. {
  948. return handlePtr->fIsOffline;
  949. }
  950. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  951. {
  952. return &(handlePtr->fTimeInfo);
  953. }
  954. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  955. {
  956. return handlePtr->handleWriteMidiEvent(event);
  957. }
  958. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  959. {
  960. handlePtr->handleUiParameterChanged(index, value);
  961. }
  962. static void host_ui_parameter_touch(NativeHostHandle handle, uint32_t index, bool touch)
  963. {
  964. handlePtr->handleUiParameterTouch(index, touch);
  965. }
  966. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  967. {
  968. handlePtr->handleUiCustomDataChanged(key, value);
  969. }
  970. static void host_ui_closed(NativeHostHandle handle)
  971. {
  972. handlePtr->handleUiClosed();
  973. }
  974. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  975. {
  976. return handlePtr->handleUiOpenFile(isDir, title, filter);
  977. }
  978. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  979. {
  980. return handlePtr->handleUiSaveFile(isDir, title, filter);
  981. }
  982. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  983. {
  984. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  985. }
  986. #undef handlePtr
  987. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  988. };
  989. // -----------------------------------------------------------------------
  990. // LV2 plugin descriptor functions
  991. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  992. {
  993. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  994. const NativePluginDescriptor* pluginDesc = nullptr;
  995. const char* pluginLabel = nullptr;
  996. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  997. pluginLabel = lv2Descriptor->URI+37;
  998. if (pluginLabel == nullptr)
  999. {
  1000. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  1001. return nullptr;
  1002. }
  1003. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  1004. PluginListManager& plm(PluginListManager::getInstance());
  1005. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  1006. {
  1007. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  1008. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  1009. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  1010. {
  1011. pluginDesc = tmpDesc;
  1012. break;
  1013. }
  1014. }
  1015. if (pluginDesc == nullptr)
  1016. {
  1017. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  1018. return nullptr;
  1019. }
  1020. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  1021. if (! plugin->init())
  1022. {
  1023. carla_stderr("Failed to init plugin");
  1024. delete plugin;
  1025. return nullptr;
  1026. }
  1027. return (LV2_Handle)plugin;
  1028. }
  1029. #define instancePtr ((NativePlugin*)instance)
  1030. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  1031. {
  1032. instancePtr->lv2_connect_port(port, dataLocation);
  1033. }
  1034. static void lv2_activate(LV2_Handle instance)
  1035. {
  1036. carla_debug("lv2_activate(%p)", instance);
  1037. instancePtr->lv2_activate();
  1038. }
  1039. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  1040. {
  1041. instancePtr->lv2_run(sampleCount);
  1042. }
  1043. static void lv2_deactivate(LV2_Handle instance)
  1044. {
  1045. carla_debug("lv2_deactivate(%p)", instance);
  1046. instancePtr->lv2_deactivate();
  1047. }
  1048. static void lv2_cleanup(LV2_Handle instance)
  1049. {
  1050. carla_debug("lv2_cleanup(%p)", instance);
  1051. instancePtr->lv2_cleanup();
  1052. delete instancePtr;
  1053. }
  1054. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1055. {
  1056. carla_debug("lv2_get_options(%p, %p)", instance, options);
  1057. return instancePtr->lv2_get_options(options);
  1058. }
  1059. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1060. {
  1061. carla_debug("lv2_set_options(%p, %p)", instance, options);
  1062. return instancePtr->lv2_set_options(options);
  1063. }
  1064. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1065. {
  1066. carla_debug("lv2_get_program(%p, %i)", instance, index);
  1067. return instancePtr->lv2_get_program(index);
  1068. }
  1069. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1070. {
  1071. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  1072. return instancePtr->lv2_select_program(bank, program);
  1073. }
  1074. 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)
  1075. {
  1076. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  1077. return instancePtr->lv2_save(store, handle, flags, features);
  1078. }
  1079. 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)
  1080. {
  1081. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  1082. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  1083. }
  1084. static LV2_Worker_Status lv2_work(LV2_Handle instance, LV2_Worker_Respond_Function respond, LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  1085. {
  1086. carla_debug("work(%p, %p, %p, %u, %p)", instance, respond, handle, size, data);
  1087. return instancePtr->lv2_work(respond, handle, size, data);
  1088. }
  1089. static LV2_Worker_Status lv2_work_resp(LV2_Handle instance, uint32_t size, const void* body)
  1090. {
  1091. carla_debug("work_resp(%p, %u, %p)", instance, size, body);
  1092. return instancePtr->lv2_work_resp(size, body);
  1093. }
  1094. static const LV2_Inline_Display_Image_Surface* lv2_idisp_render(LV2_Handle instance, uint32_t w, uint32_t h)
  1095. {
  1096. // carla_debug("lv2_idisp_render(%p, %u, %u)", instance, w, h);
  1097. return instancePtr->lv2_idisp_render(w, h);
  1098. }
  1099. static const void* lv2_extension_data(const char* uri)
  1100. {
  1101. carla_debug("lv2_extension_data(\"%s\")", uri);
  1102. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1103. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1104. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1105. static const LV2_Worker_Interface worker = { lv2_work, lv2_work_resp, nullptr };
  1106. static const LV2_Inline_Display_Interface idisp = { lv2_idisp_render };
  1107. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1108. return &options;
  1109. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1110. return &programs;
  1111. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1112. return &state;
  1113. if (std::strcmp(uri, LV2_WORKER__interface) == 0)
  1114. return &worker;
  1115. if (std::strcmp(uri, LV2_INLINEDISPLAY__interface) == 0)
  1116. return &idisp;
  1117. return nullptr;
  1118. }
  1119. #undef instancePtr
  1120. #ifdef HAVE_PYQT
  1121. // -----------------------------------------------------------------------
  1122. // LV2 UI descriptor functions
  1123. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*,
  1124. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1125. LV2UI_Widget* widget, const LV2_Feature* const* features)
  1126. {
  1127. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  1128. NativePlugin* plugin = nullptr;
  1129. for (int i=0; features[i] != nullptr; ++i)
  1130. {
  1131. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1132. {
  1133. plugin = (NativePlugin*)features[i]->data;
  1134. break;
  1135. }
  1136. }
  1137. if (plugin == nullptr)
  1138. {
  1139. carla_stderr("Host doesn't support instance-access, cannot show UI");
  1140. return nullptr;
  1141. }
  1142. plugin->lv2ui_instantiate(writeFunction, controller, widget, features);
  1143. return (LV2UI_Handle)plugin;
  1144. }
  1145. #define uiPtr ((NativePlugin*)ui)
  1146. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1147. {
  1148. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  1149. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  1150. }
  1151. static void lv2ui_cleanup(LV2UI_Handle ui)
  1152. {
  1153. carla_debug("lv2ui_cleanup(%p)", ui);
  1154. uiPtr->lv2ui_cleanup();
  1155. }
  1156. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  1157. {
  1158. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  1159. uiPtr->lv2ui_select_program(bank, program);
  1160. }
  1161. static int lv2ui_idle(LV2UI_Handle ui)
  1162. {
  1163. return uiPtr->lv2ui_idle();
  1164. }
  1165. static int lv2ui_show(LV2UI_Handle ui)
  1166. {
  1167. carla_debug("lv2ui_show(%p)", ui);
  1168. return uiPtr->lv2ui_show();
  1169. }
  1170. static int lv2ui_hide(LV2UI_Handle ui)
  1171. {
  1172. carla_debug("lv2ui_hide(%p)", ui);
  1173. return uiPtr->lv2ui_hide();
  1174. }
  1175. static const void* lv2ui_extension_data(const char* uri)
  1176. {
  1177. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  1178. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  1179. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  1180. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  1181. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  1182. return &uiidle;
  1183. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  1184. return &uishow;
  1185. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  1186. return &uiprograms;
  1187. return nullptr;
  1188. }
  1189. #endif
  1190. #undef uiPtr
  1191. // -----------------------------------------------------------------------
  1192. // Startup code
  1193. CARLA_PLUGIN_EXPORT
  1194. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1195. {
  1196. carla_debug("lv2_descriptor(%i)", index);
  1197. PluginListManager& plm(PluginListManager::getInstance());
  1198. if (index >= plm.descs.count())
  1199. {
  1200. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1201. return nullptr;
  1202. }
  1203. if (index < plm.lv2Descs.count())
  1204. {
  1205. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1206. return plm.lv2Descs.getAt(index, nullptr);
  1207. }
  1208. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  1209. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  1210. CarlaString tmpURI;
  1211. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1212. tmpURI += pluginDesc->label;
  1213. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1214. const LV2_Descriptor lv2DescTmp = {
  1215. /* URI */ carla_strdup(tmpURI),
  1216. /* instantiate */ lv2_instantiate,
  1217. /* connect_port */ lv2_connect_port,
  1218. /* activate */ lv2_activate,
  1219. /* run */ lv2_run,
  1220. /* deactivate */ lv2_deactivate,
  1221. /* cleanup */ lv2_cleanup,
  1222. /* extension_data */ lv2_extension_data
  1223. };
  1224. LV2_Descriptor* lv2Desc;
  1225. try {
  1226. lv2Desc = new LV2_Descriptor;
  1227. } CARLA_SAFE_EXCEPTION_RETURN("new LV2_Descriptor", nullptr);
  1228. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1229. plm.lv2Descs.append(lv2Desc);
  1230. return lv2Desc;
  1231. }
  1232. #ifdef HAVE_PYQT
  1233. CARLA_PLUGIN_EXPORT
  1234. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1235. {
  1236. carla_debug("lv2ui_descriptor(%i)", index);
  1237. static const LV2UI_Descriptor lv2UiExtDesc = {
  1238. /* URI */ "http://kxstudio.sf.net/carla/ui-ext",
  1239. /* instantiate */ lv2ui_instantiate,
  1240. /* cleanup */ lv2ui_cleanup,
  1241. /* port_event */ lv2ui_port_event,
  1242. /* extension_data */ lv2ui_extension_data
  1243. };
  1244. return (index == 0) ? &lv2UiExtDesc : nullptr;
  1245. }
  1246. #endif
  1247. // -----------------------------------------------------------------------