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.

1108 lines
35KB

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