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.

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