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.

1137 lines
36KB

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