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.

1152 lines
37KB

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