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.

1037 lines
33KB

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