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.

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