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.

1151 lines
37KB

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