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.

1114 lines
36KB

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