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.

1251 lines
41KB

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