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.

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