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.

1230 lines
40KB

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