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.

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