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.

1199 lines
39KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2018 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. // --------------------------------------------------------------------------------------------------------------------
  25. // Carla Internal Plugin API exposed as LV2 plugin
  26. class NativePlugin : public Lv2PluginBaseClass<NativeTimeInfo>
  27. {
  28. public:
  29. static const uint32_t kMaxMidiEvents = 512;
  30. NativePlugin(const NativePluginDescriptor* const desc,
  31. const double sampleRate,
  32. const char* const bundlePath,
  33. const LV2_Feature* const* const features)
  34. : Lv2PluginBaseClass<NativeTimeInfo>(sampleRate, features),
  35. fHandle(nullptr),
  36. fHost(),
  37. fDescriptor(desc),
  38. #ifdef CARLA_PROPER_CPP11_SUPPORT
  39. fProgramDesc({0, 0, nullptr}),
  40. #endif
  41. fMidiEventCount(0),
  42. fWorkerUISignal(0)
  43. {
  44. carla_zeroStruct(fHost);
  45. if (! loadedInProperHost())
  46. return;
  47. using water::File;
  48. using water::String;
  49. String resourceDir(water::File(bundlePath).getChildFile("resources").getFullPathName());
  50. fHost.handle = this;
  51. fHost.resourceDir = carla_strdup(resourceDir.toRawUTF8());
  52. fHost.uiName = nullptr;
  53. fHost.uiParentId = 0;
  54. fHost.get_buffer_size = host_get_buffer_size;
  55. fHost.get_sample_rate = host_get_sample_rate;
  56. fHost.is_offline = host_is_offline;
  57. fHost.get_time_info = host_get_time_info;
  58. fHost.write_midi_event = host_write_midi_event;
  59. fHost.ui_parameter_changed = host_ui_parameter_changed;
  60. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  61. fHost.ui_closed = host_ui_closed;
  62. fHost.ui_open_file = host_ui_open_file;
  63. fHost.ui_save_file = host_ui_save_file;
  64. fHost.dispatcher = host_dispatcher;
  65. }
  66. ~NativePlugin()
  67. {
  68. CARLA_SAFE_ASSERT(fHandle == nullptr);
  69. if (fHost.resourceDir != nullptr)
  70. {
  71. delete[] fHost.resourceDir;
  72. fHost.resourceDir = nullptr;
  73. }
  74. if (fHost.uiName != nullptr)
  75. {
  76. delete[] fHost.uiName;
  77. fHost.uiName = nullptr;
  78. }
  79. }
  80. // ----------------------------------------------------------------------------------------------------------------
  81. bool init()
  82. {
  83. if (fHost.resourceDir == nullptr)
  84. return false;
  85. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  86. {
  87. carla_stderr("Plugin is missing something...");
  88. return false;
  89. }
  90. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  91. fHandle = fDescriptor->instantiate(&fHost);
  92. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  93. fPorts.hasUI = fDescriptor->hints & NATIVE_PLUGIN_HAS_UI;
  94. fPorts.usesTime = fDescriptor->hints & NATIVE_PLUGIN_USES_TIME;
  95. fPorts.numAudioIns = fDescriptor->audioIns;
  96. fPorts.numAudioOuts = fDescriptor->audioOuts;
  97. fPorts.numMidiIns = fDescriptor->midiIns;
  98. fPorts.numMidiOuts = fDescriptor->midiOuts;
  99. if (fDescriptor->get_parameter_count != nullptr &&
  100. fDescriptor->get_parameter_info != nullptr &&
  101. fDescriptor->get_parameter_value != nullptr &&
  102. fDescriptor->set_parameter_value != nullptr)
  103. {
  104. fPorts.numParams = fDescriptor->get_parameter_count(fHandle);
  105. }
  106. fPorts.init();
  107. if (fPorts.numParams > 0)
  108. {
  109. for (uint32_t i=0; i < fPorts.numParams; ++i)
  110. {
  111. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  112. fPorts.paramsOut [i] = fDescriptor->get_parameter_info(fHandle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT;
  113. }
  114. }
  115. return true;
  116. }
  117. // ----------------------------------------------------------------------------------------------------------------
  118. // LV2 functions
  119. void lv2_activate()
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(! fIsActive,);
  122. resetTimeInfo();
  123. if (fDescriptor->activate != nullptr)
  124. fDescriptor->activate(fHandle);
  125. fIsActive = true;
  126. }
  127. void lv2_deactivate()
  128. {
  129. CARLA_SAFE_ASSERT_RETURN(fIsActive,);
  130. fIsActive = false;
  131. if (fDescriptor->deactivate != nullptr)
  132. fDescriptor->deactivate(fHandle);
  133. }
  134. void lv2_cleanup()
  135. {
  136. if (fIsActive)
  137. {
  138. carla_stderr("Warning: Host forgot to call deactivate!");
  139. fIsActive = false;
  140. if (fDescriptor->deactivate != nullptr)
  141. fDescriptor->deactivate(fHandle);
  142. }
  143. if (fDescriptor->cleanup != nullptr)
  144. fDescriptor->cleanup(fHandle);
  145. fHandle = nullptr;
  146. }
  147. // ----------------------------------------------------------------------------------------------------------------
  148. void lv2_run(const uint32_t frames)
  149. {
  150. if (! lv2_pre_run(frames))
  151. {
  152. updateParameterOutputs();
  153. return;
  154. }
  155. if (fPorts.numMidiIns > 0 || fPorts.hasUI)
  156. {
  157. uint32_t numEventsIn;
  158. if (fPorts.numMidiIns > 0)
  159. {
  160. numEventsIn = fPorts.numMidiIns;
  161. fMidiEventCount = 0;
  162. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  163. }
  164. else
  165. {
  166. numEventsIn = 1;
  167. }
  168. for (uint32_t i=0; i < numEventsIn; ++i)
  169. {
  170. const LV2_Atom_Sequence* const eventsIn(fPorts.eventsIn[i]);
  171. CARLA_SAFE_ASSERT_CONTINUE(eventsIn != nullptr);
  172. LV2_ATOM_SEQUENCE_FOREACH(eventsIn, event)
  173. {
  174. if (event == nullptr)
  175. continue;
  176. if (event->body.type == fURIs.uiEvents && fWorkerUISignal != -1)
  177. {
  178. if (fWorker != nullptr)
  179. {
  180. // worker is supported by the host, we can continue
  181. fWorkerUISignal = 1;
  182. const char* const msg((const char*)(event + 1));
  183. const size_t msgSize = std::strlen(msg);
  184. fWorker->schedule_work(fWorker->handle, msgSize+1, msg);
  185. }
  186. else
  187. {
  188. // worker is not supported, cancel
  189. fWorkerUISignal = -1;
  190. }
  191. continue;
  192. }
  193. if (event->body.type != fURIs.midiEvent)
  194. continue;
  195. if (event->body.size > 4)
  196. continue;
  197. if (event->time.frames >= frames)
  198. break;
  199. const uint8_t* const data((const uint8_t*)(event + 1));
  200. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  201. nativeEvent.port = (uint8_t)i;
  202. nativeEvent.size = (uint8_t)event->body.size;
  203. nativeEvent.time = (uint32_t)event->time.frames;
  204. uint32_t j=0;
  205. for (uint32_t size=event->body.size; j<size; ++j)
  206. nativeEvent.data[j] = data[j];
  207. for (; j<4; ++j)
  208. nativeEvent.data[j] = 0;
  209. if (fMidiEventCount >= kMaxMidiEvents)
  210. break;
  211. }
  212. }
  213. }
  214. // FIXME
  215. fDescriptor->process(fHandle,
  216. const_cast<float**>(fPorts.audioIns), fPorts.audioOuts, frames,
  217. fMidiEvents, fMidiEventCount);
  218. if (fWorkerUISignal == -1 && fPorts.hasUI)
  219. {
  220. const char* const msg = "quit";
  221. const size_t msgSize = 5;
  222. LV2_Atom_Sequence* const seq(fPorts.eventsOut[0]);
  223. Ports::EventsOutData& mData(fPorts.eventsOutData[0]);
  224. if (sizeof(LV2_Atom_Event) + msgSize <= mData.capacity - mData.offset)
  225. {
  226. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  227. aev->time.frames = 0;
  228. aev->body.size = msgSize;
  229. aev->body.type = fURIs.uiEvents;
  230. std::memcpy(LV2_ATOM_BODY(&aev->body), msg, msgSize);
  231. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + msgSize));
  232. mData.offset += size;
  233. seq->atom.size += size;
  234. fWorkerUISignal = 0;
  235. }
  236. }
  237. lv2_post_run(frames);
  238. updateParameterOutputs();
  239. }
  240. // ----------------------------------------------------------------------------------------------------------------
  241. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  242. {
  243. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  244. return nullptr;
  245. if (fDescriptor->get_midi_program_count == nullptr)
  246. return nullptr;
  247. if (fDescriptor->get_midi_program_info == nullptr)
  248. return nullptr;
  249. if (index >= fDescriptor->get_midi_program_count(fHandle))
  250. return nullptr;
  251. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  252. if (midiProg == nullptr)
  253. return nullptr;
  254. fProgramDesc.bank = midiProg->bank;
  255. fProgramDesc.program = midiProg->program;
  256. fProgramDesc.name = midiProg->name;
  257. return &fProgramDesc;
  258. }
  259. void lv2_select_program(uint32_t bank, uint32_t program)
  260. {
  261. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  262. return;
  263. if (fDescriptor->set_midi_program == nullptr)
  264. return;
  265. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  266. for (uint32_t i=0; i < fPorts.numParams; ++i)
  267. {
  268. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  269. if (fPorts.paramsPtr[i] != nullptr)
  270. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  271. }
  272. }
  273. // ----------------------------------------------------------------------------------------------------------------
  274. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle,
  275. const uint32_t /*flags*/, const LV2_Feature* const* const /*features*/) const
  276. {
  277. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  278. return LV2_STATE_ERR_NO_FEATURE;
  279. if (char* const state = fDescriptor->get_state(fHandle))
  280. {
  281. store(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), state, std::strlen(state)+1, fURIs.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  282. std::free(state);
  283. return LV2_STATE_SUCCESS;
  284. }
  285. return LV2_STATE_ERR_UNKNOWN;
  286. }
  287. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle,
  288. uint32_t flags, const LV2_Feature* const* const /*features*/) const
  289. {
  290. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  291. return LV2_STATE_ERR_NO_FEATURE;
  292. size_t size = 0;
  293. uint32_t type = 0;
  294. const void* const data = retrieve(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), &size, &type, &flags);
  295. if (size == 0)
  296. return LV2_STATE_ERR_UNKNOWN;
  297. if (type == 0)
  298. return LV2_STATE_ERR_UNKNOWN;
  299. if (data == nullptr)
  300. return LV2_STATE_ERR_UNKNOWN;
  301. if (type != fURIs.atomString)
  302. return LV2_STATE_ERR_BAD_TYPE;
  303. fDescriptor->set_state(fHandle, (const char*)data);
  304. return LV2_STATE_SUCCESS;
  305. }
  306. // ----------------------------------------------------------------------------------------------------------------
  307. LV2_Worker_Status lv2_work(LV2_Worker_Respond_Function, LV2_Worker_Respond_Handle, uint32_t, const void* data)
  308. {
  309. const char* const msg = (const char*)data;
  310. /**/ if (std::strncmp(msg, "control ", 8) == 0)
  311. {
  312. if (fDescriptor->ui_set_parameter_value == nullptr)
  313. return LV2_WORKER_SUCCESS;
  314. if (const char* const msgSplit = std::strstr(msg+8, " "))
  315. {
  316. const char* const msgIndex = msg+8;
  317. CARLA_SAFE_ASSERT_RETURN(msgSplit - msgIndex < 8, LV2_WORKER_ERR_UNKNOWN);
  318. CARLA_SAFE_ASSERT_RETURN(msgSplit[0] != '\0', LV2_WORKER_ERR_UNKNOWN);
  319. char strBufIndex[8];
  320. carla_zeroChars(strBufIndex, 8);
  321. std::strncpy(strBufIndex, msgIndex, msgSplit-msgIndex);
  322. const int index = std::atoi(msgIndex) - fPorts.indexOffset;
  323. CARLA_SAFE_ASSERT_RETURN(index >= 0, LV2_WORKER_ERR_UNKNOWN);
  324. double value;
  325. {
  326. const ScopedLocale csl;
  327. value = std::atof(msgSplit+1);
  328. }
  329. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  330. }
  331. }
  332. else if (std::strcmp(msg, "show") == 0)
  333. {
  334. handleUiShow();
  335. }
  336. else if (std::strcmp(msg, "hide") == 0)
  337. {
  338. handleUiHide();
  339. }
  340. else if (std::strcmp(msg, "idle") == 0)
  341. {
  342. handleUiRun();
  343. }
  344. else if (std::strcmp(msg, "quit") == 0)
  345. {
  346. handleUiClosed();
  347. }
  348. else
  349. {
  350. carla_stdout("lv2_work unknown msg '%s'", msg);
  351. return LV2_WORKER_ERR_UNKNOWN;
  352. }
  353. return LV2_WORKER_SUCCESS;
  354. }
  355. LV2_Worker_Status lv2_work_resp(uint32_t /*size*/, const void* /*body*/)
  356. {
  357. return LV2_WORKER_SUCCESS;
  358. }
  359. // ----------------------------------------------------------------------------------------------------------------
  360. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  361. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  362. {
  363. fUI.writeFunction = writeFunction;
  364. fUI.controller = controller;
  365. fUI.isEmbed = isEmbed;
  366. if (fHost.uiName != nullptr)
  367. {
  368. delete[] fHost.uiName;
  369. fHost.uiName = nullptr;
  370. }
  371. #if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  372. // ---------------------------------------------------------------
  373. // show embed UI if needed
  374. if (isEmbed)
  375. {
  376. intptr_t parentId = 0;
  377. const LV2UI_Resize* uiResize = nullptr;
  378. for (int i=0; features[i] != nullptr; ++i)
  379. {
  380. if (std::strcmp(features[i]->URI, LV2_UI__parent) == 0)
  381. {
  382. parentId = (intptr_t)features[i]->data;
  383. }
  384. else if (std::strcmp(features[i]->URI, LV2_UI__resize) == 0)
  385. {
  386. uiResize = (const LV2UI_Resize*)features[i]->data;
  387. }
  388. }
  389. // -----------------------------------------------------------
  390. // see if the host can really embed the UI
  391. if (parentId != 0)
  392. {
  393. // wait for remote side to be ready
  394. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_NULL, (int32_t)0xDEADF00D, 0xC0C0B00B, nullptr, 0.0f);
  395. if (uiResize && uiResize->ui_resize != nullptr)
  396. uiResize->ui_resize(uiResize->handle, 740, 512);
  397. fHost.uiName = carla_strdup(fDescriptor->name);
  398. fUI.isVisible = true;
  399. char strBuf[0xff+1];
  400. strBuf[0xff] = '\0';
  401. std::snprintf(strBuf, 0xff, P_INTPTR, parentId);
  402. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  403. fDescriptor->ui_show(fHandle, true);
  404. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  405. const intptr_t winId(fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_NULL, (int32_t)0xDEADF00D, 0xC0C0B00B, nullptr, 0.0f));
  406. CARLA_SAFE_ASSERT_RETURN(winId != 0,);
  407. *widget = (LV2UI_Widget)winId;
  408. return;
  409. }
  410. }
  411. #endif
  412. // ---------------------------------------------------------------
  413. // see if the host supports external-ui
  414. for (int i=0; features[i] != nullptr; ++i)
  415. {
  416. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  417. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  418. {
  419. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  420. break;
  421. }
  422. }
  423. if (fUI.host != nullptr)
  424. {
  425. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  426. *widget = (LV2_External_UI_Widget_Compat*)this;
  427. return;
  428. }
  429. // ---------------------------------------------------------------
  430. // no external-ui support, use showInterface
  431. for (int i=0; features[i] != nullptr; ++i)
  432. {
  433. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) != 0)
  434. continue;
  435. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  436. CARLA_SAFE_ASSERT_BREAK(options != nullptr);
  437. for (int j=0; options[j].key != 0; ++j)
  438. {
  439. if (options[j].key != fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  440. continue;
  441. const char* const title((const char*)options[j].value);
  442. CARLA_SAFE_ASSERT_BREAK(title != nullptr && title[0] != '\0');
  443. fHost.uiName = carla_strdup(title);
  444. break;
  445. }
  446. break;
  447. }
  448. if (fHost.uiName == nullptr)
  449. fHost.uiName = carla_strdup(fDescriptor->name);
  450. *widget = nullptr;
  451. return;
  452. // maybe be unused
  453. (void)isEmbed;
  454. }
  455. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  456. {
  457. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  458. return;
  459. if (portIndex < fPorts.indexOffset || ! fUI.isVisible)
  460. return;
  461. if (fDescriptor->ui_set_parameter_value == nullptr)
  462. return;
  463. const float value(*(const float*)buffer);
  464. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fPorts.indexOffset, value);
  465. }
  466. // ----------------------------------------------------------------------------------------------------------------
  467. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  468. {
  469. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  470. return;
  471. if (fDescriptor->ui_set_midi_program == nullptr)
  472. return;
  473. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  474. }
  475. // ----------------------------------------------------------------------------------------------------------------
  476. protected:
  477. void handleUiRun() const override
  478. {
  479. if (fDescriptor->ui_idle != nullptr)
  480. fDescriptor->ui_idle(fHandle);
  481. }
  482. void handleUiShow() override
  483. {
  484. if (fDescriptor->ui_show != nullptr)
  485. fDescriptor->ui_show(fHandle, true);
  486. fUI.isVisible = true;
  487. }
  488. void handleUiHide() override
  489. {
  490. if (fDescriptor->ui_show != nullptr)
  491. fDescriptor->ui_show(fHandle, false);
  492. fUI.isVisible = false;
  493. }
  494. // ----------------------------------------------------------------------------------------------------------------
  495. void handleParameterValueChanged(const uint32_t index, const float value) override
  496. {
  497. fDescriptor->set_parameter_value(fHandle, index, value);
  498. }
  499. void handleBufferSizeChanged(const uint32_t bufferSize) override
  500. {
  501. if (fDescriptor->dispatcher == nullptr)
  502. return;
  503. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, bufferSize, nullptr, 0.0f);
  504. }
  505. void handleSampleRateChanged(const double sampleRate) override
  506. {
  507. if (fDescriptor->dispatcher == nullptr)
  508. return;
  509. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
  510. }
  511. // ----------------------------------------------------------------------------------------------------------------
  512. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  513. {
  514. CARLA_SAFE_ASSERT_RETURN(fPorts.numMidiOuts > 0, false);
  515. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  516. CARLA_SAFE_ASSERT_RETURN(event->size > 0, false);
  517. const uint8_t port(event->port);
  518. CARLA_SAFE_ASSERT_RETURN(port < fPorts.numMidiOuts, false);
  519. LV2_Atom_Sequence* const seq(fPorts.eventsOut[port]);
  520. CARLA_SAFE_ASSERT_RETURN(seq != nullptr, false);
  521. Ports::EventsOutData& mData(fPorts.eventsOutData[port]);
  522. if (sizeof(LV2_Atom_Event) + event->size > mData.capacity - mData.offset)
  523. return false;
  524. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  525. aev->time.frames = event->time;
  526. aev->body.size = event->size;
  527. aev->body.type = fURIs.midiEvent;
  528. std::memcpy(LV2_ATOM_BODY(&aev->body), event->data, event->size);
  529. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + event->size));
  530. mData.offset += size;
  531. seq->atom.size += size;
  532. return true;
  533. }
  534. void handleUiParameterChanged(const uint32_t index, const float value) const
  535. {
  536. if (fWorkerUISignal)
  537. return;
  538. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  539. fUI.writeFunction(fUI.controller, index+fPorts.indexOffset, sizeof(float), 0, &value);
  540. }
  541. void handleUiCustomDataChanged(const char* const key, const char* const value) const
  542. {
  543. carla_stdout("TODO: handleUiCustomDataChanged %s %s", key, value);
  544. //storeCustomData(key, value);
  545. if (fUI.writeFunction == nullptr || fUI.controller == nullptr)
  546. return;
  547. }
  548. void handleUiClosed()
  549. {
  550. fUI.isVisible = false;
  551. if (fWorkerUISignal)
  552. fWorkerUISignal = -1;
  553. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  554. fUI.host->ui_closed(fUI.controller);
  555. fUI.host = nullptr;
  556. fUI.writeFunction = nullptr;
  557. fUI.controller = nullptr;
  558. }
  559. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  560. {
  561. // TODO
  562. return nullptr;
  563. }
  564. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  565. {
  566. // TODO
  567. return nullptr;
  568. }
  569. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  570. {
  571. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  572. intptr_t ret = 0;
  573. switch (opcode)
  574. {
  575. case NATIVE_HOST_OPCODE_NULL:
  576. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  577. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  578. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  579. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  580. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  581. case NATIVE_HOST_OPCODE_HOST_IDLE:
  582. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  583. // nothing
  584. break;
  585. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  586. handleUiClosed();
  587. break;
  588. }
  589. return ret;
  590. // unused for now
  591. (void)index;
  592. (void)value;
  593. (void)ptr;
  594. (void)opt;
  595. }
  596. void updateParameterOutputs()
  597. {
  598. float value;
  599. for (uint32_t i=0; i < fPorts.numParams; ++i)
  600. {
  601. if (! fPorts.paramsOut[i])
  602. continue;
  603. fPorts.paramsLast[i] = value = fDescriptor->get_parameter_value(fHandle, i);
  604. if (fPorts.paramsPtr[i] != nullptr)
  605. *fPorts.paramsPtr[i] = value;
  606. }
  607. }
  608. // -------------------------------------------------------------------
  609. private:
  610. // Native data
  611. NativePluginHandle fHandle;
  612. NativeHostDescriptor fHost;
  613. const NativePluginDescriptor* const fDescriptor;
  614. LV2_Program_Descriptor fProgramDesc;
  615. uint32_t fMidiEventCount;
  616. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  617. int fWorkerUISignal;
  618. // -------------------------------------------------------------------
  619. #define handlePtr ((NativePlugin*)handle)
  620. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  621. {
  622. return handlePtr->fBufferSize;
  623. }
  624. static double host_get_sample_rate(NativeHostHandle handle)
  625. {
  626. return handlePtr->fSampleRate;
  627. }
  628. static bool host_is_offline(NativeHostHandle handle)
  629. {
  630. return handlePtr->fIsOffline;
  631. }
  632. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  633. {
  634. return &(handlePtr->fTimeInfo);
  635. }
  636. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  637. {
  638. return handlePtr->handleWriteMidiEvent(event);
  639. }
  640. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  641. {
  642. handlePtr->handleUiParameterChanged(index, value);
  643. }
  644. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  645. {
  646. handlePtr->handleUiCustomDataChanged(key, value);
  647. }
  648. static void host_ui_closed(NativeHostHandle handle)
  649. {
  650. handlePtr->handleUiClosed();
  651. }
  652. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  653. {
  654. return handlePtr->handleUiOpenFile(isDir, title, filter);
  655. }
  656. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  657. {
  658. return handlePtr->handleUiSaveFile(isDir, title, filter);
  659. }
  660. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  661. {
  662. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  663. }
  664. #undef handlePtr
  665. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  666. };
  667. // -----------------------------------------------------------------------
  668. // LV2 plugin descriptor functions
  669. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  670. {
  671. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  672. const NativePluginDescriptor* pluginDesc = nullptr;
  673. const char* pluginLabel = nullptr;
  674. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  675. pluginLabel = lv2Descriptor->URI+37;
  676. if (pluginLabel == nullptr)
  677. {
  678. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  679. return nullptr;
  680. }
  681. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  682. PluginListManager& plm(PluginListManager::getInstance());
  683. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  684. {
  685. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  686. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  687. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  688. {
  689. pluginDesc = tmpDesc;
  690. break;
  691. }
  692. }
  693. if (pluginDesc == nullptr)
  694. {
  695. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  696. return nullptr;
  697. }
  698. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  699. if (! plugin->init())
  700. {
  701. carla_stderr("Failed to init plugin");
  702. delete plugin;
  703. return nullptr;
  704. }
  705. return (LV2_Handle)plugin;
  706. }
  707. #define instancePtr ((NativePlugin*)instance)
  708. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  709. {
  710. instancePtr->lv2_connect_port(port, dataLocation);
  711. }
  712. static void lv2_activate(LV2_Handle instance)
  713. {
  714. carla_debug("lv2_activate(%p)", instance);
  715. instancePtr->lv2_activate();
  716. }
  717. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  718. {
  719. instancePtr->lv2_run(sampleCount);
  720. }
  721. static void lv2_deactivate(LV2_Handle instance)
  722. {
  723. carla_debug("lv2_deactivate(%p)", instance);
  724. instancePtr->lv2_deactivate();
  725. }
  726. static void lv2_cleanup(LV2_Handle instance)
  727. {
  728. carla_debug("lv2_cleanup(%p)", instance);
  729. instancePtr->lv2_cleanup();
  730. delete instancePtr;
  731. }
  732. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  733. {
  734. carla_debug("lv2_get_options(%p, %p)", instance, options);
  735. return instancePtr->lv2_get_options(options);
  736. }
  737. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  738. {
  739. carla_debug("lv2_set_options(%p, %p)", instance, options);
  740. return instancePtr->lv2_set_options(options);
  741. }
  742. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  743. {
  744. carla_debug("lv2_get_program(%p, %i)", instance, index);
  745. return instancePtr->lv2_get_program(index);
  746. }
  747. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  748. {
  749. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  750. return instancePtr->lv2_select_program(bank, program);
  751. }
  752. 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)
  753. {
  754. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  755. return instancePtr->lv2_save(store, handle, flags, features);
  756. }
  757. 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)
  758. {
  759. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  760. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  761. }
  762. 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)
  763. {
  764. carla_debug("work(%p, %p, %p, %u, %p)", instance, respond, handle, size, data);
  765. return instancePtr->lv2_work(respond, handle, size, data);
  766. }
  767. LV2_Worker_Status lv2_work_resp(LV2_Handle instance, uint32_t size, const void* body)
  768. {
  769. carla_debug("work_resp(%p, %u, %p)", instance, size, body);
  770. return instancePtr->lv2_work_resp(size, body);
  771. }
  772. static const void* lv2_extension_data(const char* uri)
  773. {
  774. carla_debug("lv2_extension_data(\"%s\")", uri);
  775. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  776. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  777. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  778. static const LV2_Worker_Interface worker = { lv2_work, lv2_work_resp, nullptr };
  779. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  780. return &options;
  781. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  782. return &programs;
  783. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  784. return &state;
  785. if (std::strcmp(uri, LV2_WORKER__interface) == 0)
  786. return &worker;
  787. return nullptr;
  788. }
  789. #undef instancePtr
  790. #ifdef HAVE_PYQT
  791. // -----------------------------------------------------------------------
  792. // LV2 UI descriptor functions
  793. static LV2UI_Handle lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  794. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  795. {
  796. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  797. # if ! (defined(CARLA_OS_LINUX) && defined(HAVE_X11))
  798. CARLA_SAFE_ASSERT_RETURN(! isEmbed, nullptr);
  799. # endif
  800. NativePlugin* plugin = nullptr;
  801. for (int i=0; features[i] != nullptr; ++i)
  802. {
  803. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  804. {
  805. plugin = (NativePlugin*)features[i]->data;
  806. break;
  807. }
  808. }
  809. if (plugin == nullptr)
  810. {
  811. carla_stderr("Host doesn't support instance-access, cannot show UI");
  812. return nullptr;
  813. }
  814. plugin->lv2ui_instantiate(writeFunction, controller, widget, features, isEmbed);
  815. return (LV2UI_Handle)plugin;
  816. }
  817. # if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  818. static LV2UI_Handle lv2ui_instantiate_embed(const LV2UI_Descriptor*, const char*, const char*,
  819. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  820. LV2UI_Widget* widget, const LV2_Feature* const* features)
  821. {
  822. return lv2ui_instantiate(writeFunction, controller, widget, features, true);
  823. }
  824. # endif
  825. static LV2UI_Handle lv2ui_instantiate_external(const LV2UI_Descriptor*, const char*, const char*,
  826. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  827. LV2UI_Widget* widget, const LV2_Feature* const* features)
  828. {
  829. return lv2ui_instantiate(writeFunction, controller, widget, features, false);
  830. }
  831. #define uiPtr ((NativePlugin*)ui)
  832. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  833. {
  834. carla_debug("lv2ui_port_eventxx(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  835. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  836. }
  837. static void lv2ui_cleanup(LV2UI_Handle ui)
  838. {
  839. carla_debug("lv2ui_cleanup(%p)", ui);
  840. uiPtr->lv2ui_cleanup();
  841. }
  842. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  843. {
  844. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  845. uiPtr->lv2ui_select_program(bank, program);
  846. }
  847. static int lv2ui_idle(LV2UI_Handle ui)
  848. {
  849. return uiPtr->lv2ui_idle();
  850. }
  851. static int lv2ui_show(LV2UI_Handle ui)
  852. {
  853. carla_debug("lv2ui_show(%p)", ui);
  854. return uiPtr->lv2ui_show();
  855. }
  856. static int lv2ui_hide(LV2UI_Handle ui)
  857. {
  858. carla_debug("lv2ui_hide(%p)", ui);
  859. return uiPtr->lv2ui_hide();
  860. }
  861. static const void* lv2ui_extension_data(const char* uri)
  862. {
  863. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  864. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  865. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  866. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  867. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  868. return &uiidle;
  869. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  870. return &uishow;
  871. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  872. return &uiprograms;
  873. return nullptr;
  874. }
  875. #endif
  876. #undef uiPtr
  877. // -----------------------------------------------------------------------
  878. // Startup code
  879. CARLA_EXPORT
  880. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  881. {
  882. carla_debug("lv2_descriptor(%i)", index);
  883. PluginListManager& plm(PluginListManager::getInstance());
  884. if (index >= plm.descs.count())
  885. {
  886. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  887. return nullptr;
  888. }
  889. if (index < plm.lv2Descs.count())
  890. {
  891. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  892. return plm.lv2Descs.getAt(index, nullptr);
  893. }
  894. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  895. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  896. CarlaString tmpURI;
  897. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  898. tmpURI += pluginDesc->label;
  899. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  900. const LV2_Descriptor lv2DescTmp = {
  901. /* URI */ carla_strdup(tmpURI),
  902. /* instantiate */ lv2_instantiate,
  903. /* connect_port */ lv2_connect_port,
  904. /* activate */ lv2_activate,
  905. /* run */ lv2_run,
  906. /* deactivate */ lv2_deactivate,
  907. /* cleanup */ lv2_cleanup,
  908. /* extension_data */ lv2_extension_data
  909. };
  910. LV2_Descriptor* lv2Desc;
  911. try {
  912. lv2Desc = new LV2_Descriptor;
  913. } CARLA_SAFE_EXCEPTION_RETURN("new LV2_Descriptor", nullptr);
  914. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  915. plm.lv2Descs.append(lv2Desc);
  916. return lv2Desc;
  917. }
  918. #ifdef HAVE_PYQT
  919. CARLA_EXPORT
  920. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  921. {
  922. carla_debug("lv2ui_descriptor(%i)", index);
  923. #if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  924. static const LV2UI_Descriptor lv2UiEmbedDesc = {
  925. /* URI */ "http://kxstudio.sf.net/carla/ui-embed",
  926. /* instantiate */ lv2ui_instantiate_embed,
  927. /* cleanup */ lv2ui_cleanup,
  928. /* port_event */ lv2ui_port_event,
  929. /* extension_data */ lv2ui_extension_data
  930. };
  931. if (index == 0)
  932. return &lv2UiEmbedDesc;
  933. else
  934. --index;
  935. #endif
  936. static const LV2UI_Descriptor lv2UiExtDesc = {
  937. /* URI */ "http://kxstudio.sf.net/carla/ui-ext",
  938. /* instantiate */ lv2ui_instantiate_external,
  939. /* cleanup */ lv2ui_cleanup,
  940. /* port_event */ lv2ui_port_event,
  941. /* extension_data */ lv2ui_extension_data
  942. };
  943. return (index == 0) ? &lv2UiExtDesc : nullptr;
  944. }
  945. #endif
  946. // -----------------------------------------------------------------------