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.

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