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.

1673 lines
56KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 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 "CarlaString.hpp"
  22. #include "juce_audio_basics.h"
  23. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  24. # include "juce_gui_basics.h"
  25. #else
  26. namespace juce {
  27. # include "juce_events/messages/juce_Initialisation.h"
  28. } // namespace juce
  29. #endif
  30. using juce::FloatVectorOperations;
  31. using juce::ScopedJuceInitialiser_GUI;
  32. using juce::SharedResourcePointer;
  33. // -----------------------------------------------------------------------
  34. // -Weffc++ compat ext widget
  35. extern "C" {
  36. typedef struct _LV2_External_UI_Widget_Compat {
  37. void (*run )(struct _LV2_External_UI_Widget_Compat*);
  38. void (*show)(struct _LV2_External_UI_Widget_Compat*);
  39. void (*hide)(struct _LV2_External_UI_Widget_Compat*);
  40. _LV2_External_UI_Widget_Compat() noexcept
  41. : run(nullptr), show(nullptr), hide(nullptr) {}
  42. } LV2_External_UI_Widget_Compat;
  43. }
  44. // -----------------------------------------------------------------------
  45. // LV2 descriptor functions
  46. #if defined(__clang__)
  47. # pragma clang diagnostic push
  48. # pragma clang diagnostic ignored "-Weffc++"
  49. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  50. # pragma GCC diagnostic push
  51. # pragma GCC diagnostic ignored "-Weffc++"
  52. #endif
  53. class NativePlugin : public LV2_External_UI_Widget_Compat
  54. {
  55. #if defined(__clang__)
  56. # pragma clang diagnostic pop
  57. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  58. # pragma GCC diagnostic pop
  59. #endif
  60. public:
  61. static const uint32_t kMaxMidiEvents = 512;
  62. NativePlugin(const NativePluginDescriptor* const desc, const double sampleRate, const char* const bundlePath, const LV2_Feature* const* features)
  63. : fHandle(nullptr),
  64. fHost(),
  65. fDescriptor(desc),
  66. #ifdef CARLA_PROPER_CPP11_SUPPORT
  67. fProgramDesc({0, 0, nullptr}),
  68. #endif
  69. fMidiEventCount(0),
  70. fTimeInfo(),
  71. fLastTimeSpeed(0.0),
  72. fIsProcessing(false),
  73. fBufferSize(0),
  74. fSampleRate(sampleRate),
  75. fUridMap(nullptr),
  76. fURIs(),
  77. fUI(),
  78. fPorts(),
  79. sJuceInitialiser(),
  80. leakDetector_NativePlugin()
  81. {
  82. run = extui_run;
  83. show = extui_show;
  84. hide = extui_hide;
  85. CarlaString resourceDir(bundlePath);
  86. #ifdef CARLA_OS_WIN
  87. resourceDir += "\\resources\\";
  88. #else
  89. resourceDir += "/resources/";
  90. #endif
  91. fHost.handle = this;
  92. fHost.resourceDir = resourceDir.dup();
  93. fHost.uiName = nullptr;
  94. fHost.uiParentId = 0;
  95. fHost.get_buffer_size = host_get_buffer_size;
  96. fHost.get_sample_rate = host_get_sample_rate;
  97. fHost.is_offline = host_is_offline;
  98. fHost.get_time_info = host_get_time_info;
  99. fHost.write_midi_event = host_write_midi_event;
  100. fHost.ui_parameter_changed = host_ui_parameter_changed;
  101. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  102. fHost.ui_closed = host_ui_closed;
  103. fHost.ui_open_file = host_ui_open_file;
  104. fHost.ui_save_file = host_ui_save_file;
  105. fHost.dispatcher = host_dispatcher;
  106. const LV2_Options_Option* options = nullptr;
  107. const LV2_URID_Map* uridMap = nullptr;
  108. const LV2_URID_Unmap* uridUnmap = nullptr;
  109. for (int i=0; features[i] != nullptr; ++i)
  110. {
  111. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  112. options = (const LV2_Options_Option*)features[i]->data;
  113. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  114. uridMap = (const LV2_URID_Map*)features[i]->data;
  115. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  116. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  117. }
  118. if (options == nullptr || uridMap == nullptr)
  119. {
  120. carla_stderr("Host doesn't provide option or urid-map features");
  121. return;
  122. }
  123. for (int i=0; options[i].key != 0; ++i)
  124. {
  125. if (uridUnmap != nullptr)
  126. {
  127. carla_debug("Host option %i:\"%s\"", i, uridUnmap->unmap(uridUnmap->handle, options[i].key));
  128. }
  129. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  130. {
  131. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  132. {
  133. const int value(*(const int*)options[i].value);
  134. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  135. fBufferSize = static_cast<uint32_t>(value);
  136. }
  137. else
  138. carla_stderr("Host provides maxBlockLength but has wrong value type");
  139. break;
  140. }
  141. }
  142. fUridMap = uridMap;
  143. if (fDescriptor->midiIns > 0)
  144. fUI.portOffset += desc->midiIns;
  145. else if (fDescriptor->hints & NATIVE_PLUGIN_USES_TIME)
  146. fUI.portOffset += 1;
  147. #if 0 // TODO
  148. fUI.portOffset += desc->midiOuts;
  149. #endif
  150. fUI.portOffset += 1; // freewheel
  151. fUI.portOffset += desc->audioIns;
  152. fUI.portOffset += desc->audioOuts;
  153. }
  154. ~NativePlugin()
  155. {
  156. CARLA_ASSERT(fHandle == nullptr);
  157. if (fHost.resourceDir != nullptr)
  158. {
  159. delete[] fHost.resourceDir;
  160. fHost.resourceDir = nullptr;
  161. }
  162. }
  163. bool init()
  164. {
  165. if (fUI.portOffset == 0)
  166. {
  167. // host is missing features
  168. return false;
  169. }
  170. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  171. {
  172. carla_stderr("Plugin is missing something...");
  173. return false;
  174. }
  175. if (fBufferSize == 0)
  176. {
  177. carla_stderr("Host is missing bufferSize feature");
  178. //return false;
  179. // as testing, continue for now
  180. fBufferSize = 1024;
  181. }
  182. fHandle = fDescriptor->instantiate(&fHost);
  183. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  184. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  185. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  186. fPorts.init(fDescriptor, fHandle);
  187. fURIs.map(fUridMap);
  188. return true;
  189. }
  190. // -------------------------------------------------------------------
  191. // LV2 functions
  192. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  193. {
  194. fPorts.connectPort(fDescriptor, port, dataLocation);
  195. }
  196. void lv2_activate()
  197. {
  198. if (fDescriptor->activate != nullptr)
  199. fDescriptor->activate(fHandle);
  200. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  201. }
  202. void lv2_deactivate()
  203. {
  204. if (fDescriptor->deactivate != nullptr)
  205. fDescriptor->deactivate(fHandle);
  206. }
  207. void lv2_cleanup()
  208. {
  209. if (fDescriptor->cleanup != nullptr)
  210. fDescriptor->cleanup(fHandle);
  211. fHandle = nullptr;
  212. }
  213. void lv2_run(const uint32_t frames)
  214. {
  215. if (frames == 0)
  216. {
  217. updateParameterOutputs();
  218. return;
  219. }
  220. // Check for updated parameters
  221. float curValue;
  222. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  223. {
  224. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  225. curValue = *fPorts.paramsPtr[i];
  226. if ((! carla_compareFloats(fPorts.paramsLast[i], curValue)) && (fDescriptor->get_parameter_info(fHandle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT) == 0)
  227. {
  228. fPorts.paramsLast[i] = curValue;
  229. fDescriptor->set_parameter_value(fHandle, i, curValue);
  230. }
  231. }
  232. if (fDescriptor->midiIns > 0 || (fDescriptor->hints & NATIVE_PLUGIN_USES_TIME) != 0)
  233. {
  234. fMidiEventCount = 0;
  235. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  236. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[0], iter)
  237. {
  238. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  239. if (event == nullptr)
  240. continue;
  241. if (event->body.type == fURIs.midiEvent)
  242. {
  243. if (event->body.size > 4)
  244. continue;
  245. if (event->time.frames >= frames)
  246. continue;
  247. if (fMidiEventCount >= kMaxMidiEvents*2)
  248. continue;
  249. const uint8_t* const data((const uint8_t*)(event + 1));
  250. fMidiEvents[fMidiEventCount].port = 0;
  251. fMidiEvents[fMidiEventCount].time = (uint32_t)event->time.frames;
  252. fMidiEvents[fMidiEventCount].size = (uint8_t)event->body.size;
  253. for (uint32_t i=0; i < event->body.size; ++i)
  254. fMidiEvents[fMidiEventCount].data[i] = data[i];
  255. fMidiEventCount += 1;
  256. continue;
  257. }
  258. if (event->body.type == fURIs.atomBlank)
  259. {
  260. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  261. if (obj->body.otype != fURIs.timePos)
  262. continue;
  263. LV2_Atom* bar = nullptr;
  264. LV2_Atom* barBeat = nullptr;
  265. LV2_Atom* beat = nullptr;
  266. LV2_Atom* beatUnit = nullptr;
  267. LV2_Atom* beatsPerBar = nullptr;
  268. LV2_Atom* beatsPerMinute = nullptr;
  269. LV2_Atom* ticksPerBeat = nullptr;
  270. LV2_Atom* frame = nullptr;
  271. LV2_Atom* speed = nullptr;
  272. lv2_atom_object_get(obj,
  273. fURIs.timeBar, &bar,
  274. fURIs.timeBarBeat, &barBeat,
  275. fURIs.timeBeat, &beat,
  276. fURIs.timeBeatUnit, &beatUnit,
  277. fURIs.timeBeatsPerBar, &beatsPerBar,
  278. fURIs.timeBeatsPerMinute, &beatsPerMinute,
  279. fURIs.timeTicksPerBeat, &ticksPerBeat,
  280. fURIs.timeFrame, &frame,
  281. fURIs.timeSpeed, &speed,
  282. nullptr);
  283. if (bar != nullptr)
  284. {
  285. /**/ if (bar->type == fURIs.atomDouble)
  286. fTimeInfo.bbt.bar = static_cast<int32_t>(((LV2_Atom_Double*)bar)->body + 1.0);
  287. else if (bar->type == fURIs.atomFloat)
  288. fTimeInfo.bbt.bar = static_cast<int32_t>(((LV2_Atom_Float*)bar)->body + 1.0f);
  289. else if (bar->type == fURIs.atomInt)
  290. fTimeInfo.bbt.bar = static_cast<int32_t>(((LV2_Atom_Int*)bar)->body + 1);
  291. else if (bar->type == fURIs.atomLong)
  292. fTimeInfo.bbt.bar = static_cast<int32_t>(((LV2_Atom_Long*)bar)->body + 1);
  293. else
  294. carla_stderr("Unknown lv2 bar value type");
  295. }
  296. if (ticksPerBeat != nullptr)
  297. {
  298. /**/ if (ticksPerBeat->type == fURIs.atomDouble)
  299. fTimeInfo.bbt.ticksPerBeat = ((LV2_Atom_Double*)ticksPerBeat)->body;
  300. else if (ticksPerBeat->type == fURIs.atomFloat)
  301. fTimeInfo.bbt.ticksPerBeat = static_cast<double>(((LV2_Atom_Float*)ticksPerBeat)->body);
  302. else if (ticksPerBeat->type == fURIs.atomInt)
  303. fTimeInfo.bbt.ticksPerBeat = static_cast<double>(((LV2_Atom_Int*)ticksPerBeat)->body);
  304. else if (ticksPerBeat->type == fURIs.atomLong)
  305. fTimeInfo.bbt.ticksPerBeat = static_cast<double>(((LV2_Atom_Long*)ticksPerBeat)->body);
  306. else
  307. carla_stderr("Unknown lv2 ticksPerBeat value type");
  308. }
  309. if (barBeat != nullptr)
  310. {
  311. double barBeatValue = 0.0;
  312. /**/ if (barBeat->type == fURIs.atomDouble)
  313. barBeatValue = ((LV2_Atom_Double*)barBeat)->body;
  314. else if (barBeat->type == fURIs.atomFloat)
  315. barBeatValue = static_cast<double>(((LV2_Atom_Float*)barBeat)->body);
  316. else if (barBeat->type == fURIs.atomInt)
  317. barBeatValue = static_cast<double>(((LV2_Atom_Int*)barBeat)->body);
  318. else if (barBeat->type == fURIs.atomLong)
  319. barBeatValue = static_cast<double>(((LV2_Atom_Long*)barBeat)->body);
  320. else
  321. carla_stderr("Unknown lv2 barBeat value type");
  322. const double rest = std::fmod(barBeatValue, 1.0);
  323. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeatValue-rest+1.0);
  324. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  325. }
  326. // barBeat includes beat
  327. else if (beat != nullptr)
  328. {
  329. /**/ if (beat->type == fURIs.atomDouble)
  330. fTimeInfo.bbt.beat = static_cast<int32_t>(((LV2_Atom_Double*)beat)->body + 1.0);
  331. else if (beat->type == fURIs.atomFloat)
  332. fTimeInfo.bbt.beat = static_cast<int32_t>(((LV2_Atom_Float*)beat)->body + 1.0f);
  333. else if (beat->type == fURIs.atomInt)
  334. fTimeInfo.bbt.beat = static_cast<int32_t>(((LV2_Atom_Int*)beat)->body + 1);
  335. else if (beat->type == fURIs.atomLong)
  336. fTimeInfo.bbt.beat = static_cast<int32_t>(((LV2_Atom_Long*)beat)->body + 1);
  337. else
  338. carla_stderr("Unknown lv2 beat value type");
  339. }
  340. if (beatUnit != nullptr)
  341. {
  342. /**/ if (beatUnit->type == fURIs.atomDouble)
  343. fTimeInfo.bbt.beatType = static_cast<float>(((LV2_Atom_Double*)beatUnit)->body);
  344. else if (beatUnit->type == fURIs.atomFloat)
  345. fTimeInfo.bbt.beatType = ((LV2_Atom_Float*)beatUnit)->body;
  346. else if (beatUnit->type == fURIs.atomInt)
  347. fTimeInfo.bbt.beatType = static_cast<float>(((LV2_Atom_Int*)beatUnit)->body);
  348. else if (beatUnit->type == fURIs.atomLong)
  349. fTimeInfo.bbt.beatType = static_cast<float>(((LV2_Atom_Long*)beatUnit)->body);
  350. else
  351. carla_stderr("Unknown lv2 beatUnit value type");
  352. }
  353. if (beatsPerBar != nullptr)
  354. {
  355. /**/ if (beatsPerBar->type == fURIs.atomDouble)
  356. fTimeInfo.bbt.beatsPerBar = static_cast<float>(((LV2_Atom_Double*)beatsPerBar)->body);
  357. else if (beatsPerBar->type == fURIs.atomFloat)
  358. fTimeInfo.bbt.beatsPerBar = ((LV2_Atom_Float*)beatsPerBar)->body;
  359. else if (beatsPerBar->type == fURIs.atomInt)
  360. fTimeInfo.bbt.beatsPerBar = static_cast<float>(((LV2_Atom_Int*)beatsPerBar)->body);
  361. else if (beatsPerBar->type == fURIs.atomLong)
  362. fTimeInfo.bbt.beatsPerBar = static_cast<float>(((LV2_Atom_Long*)beatsPerBar)->body);
  363. else
  364. carla_stderr("Unknown lv2 beatsPerBar value type");
  365. }
  366. if (beatsPerMinute != nullptr)
  367. {
  368. /**/ if (beatsPerMinute->type == fURIs.atomDouble)
  369. fTimeInfo.bbt.beatsPerMinute = ((LV2_Atom_Double*)beatsPerMinute)->body;
  370. else if (beatsPerMinute->type == fURIs.atomFloat)
  371. fTimeInfo.bbt.beatsPerMinute = static_cast<double>(((LV2_Atom_Float*)beatsPerMinute)->body);
  372. else if (beatsPerMinute->type == fURIs.atomInt)
  373. fTimeInfo.bbt.beatsPerMinute = static_cast<double>(((LV2_Atom_Int*)beatsPerMinute)->body);
  374. else if (beatsPerMinute->type == fURIs.atomLong)
  375. fTimeInfo.bbt.beatsPerMinute = static_cast<double>(((LV2_Atom_Long*)beatsPerMinute)->body);
  376. else
  377. carla_stderr("Unknown lv2 beatsPerMinute value type");
  378. }
  379. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*fTimeInfo.bbt.beatsPerBar*(fTimeInfo.bbt.bar-1);
  380. if (frame != nullptr && frame->type == fURIs.atomLong)
  381. fTimeInfo.frame = static_cast<uint64_t>(((LV2_Atom_Long*)frame)->body);
  382. if (speed != nullptr && speed->type == fURIs.atomFloat)
  383. {
  384. fLastTimeSpeed = ((LV2_Atom_Float*)speed)->body;
  385. fTimeInfo.playing = carla_compareFloats(fLastTimeSpeed, 1.0);
  386. }
  387. fTimeInfo.bbt.valid = (beatsPerMinute != nullptr && beatsPerBar != nullptr && beatUnit != nullptr);
  388. continue;
  389. }
  390. }
  391. for (uint32_t i=1; i < fDescriptor->midiIns; ++i)
  392. {
  393. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[i], iter)
  394. {
  395. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  396. if (event == nullptr)
  397. continue;
  398. if (event->body.type != fURIs.midiEvent)
  399. continue;
  400. if (event->body.size > 4)
  401. continue;
  402. if (event->time.frames >= frames)
  403. break;
  404. if (fMidiEventCount >= kMaxMidiEvents*2)
  405. break;
  406. const uint8_t* const data((const uint8_t*)(event + 1));
  407. fMidiEvents[fMidiEventCount].port = (uint8_t)i;
  408. fMidiEvents[fMidiEventCount].size = (uint8_t)event->body.size;
  409. fMidiEvents[fMidiEventCount].time = (uint32_t)event->time.frames;
  410. for (uint32_t j=0; j < event->body.size; ++j)
  411. fMidiEvents[fMidiEventCount].data[j] = data[j];
  412. fMidiEventCount += 1;
  413. }
  414. }
  415. }
  416. fIsProcessing = true;
  417. fDescriptor->process(fHandle, fPorts.audioIns, fPorts.audioOuts, frames, fMidiEvents, fMidiEventCount);
  418. fIsProcessing = false;
  419. // update timePos for next callback
  420. if (! carla_compareFloats(fLastTimeSpeed, 0.0))
  421. {
  422. const double newFrames = fLastTimeSpeed*frames;
  423. fTimeInfo.frame += static_cast<uint64_t>(newFrames);
  424. if (fTimeInfo.bbt.valid)
  425. {
  426. const double samplesPerBeat = 60.0 / fTimeInfo.bbt.beatsPerMinute * fSampleRate;
  427. const double ticksPerSample = fTimeInfo.bbt.ticksPerBeat / samplesPerBeat;
  428. double newTickPos = double(fTimeInfo.bbt.tick) + ticksPerSample*newFrames;
  429. double newBeatPos = double(fTimeInfo.bbt.beat)-1.0;
  430. double newBarPos = double(fTimeInfo.bbt.bar)-1.0;
  431. for (; newTickPos >= fTimeInfo.bbt.ticksPerBeat;)
  432. {
  433. ++newBeatPos;
  434. newTickPos -= fTimeInfo.bbt.ticksPerBeat;
  435. }
  436. for (; newBeatPos >= fTimeInfo.bbt.beatsPerBar;)
  437. {
  438. ++newBarPos;
  439. newBeatPos -= fTimeInfo.bbt.beatsPerBar;
  440. }
  441. fTimeInfo.bbt.bar = static_cast<int32_t>(newBarPos+1.0);
  442. fTimeInfo.bbt.beat = static_cast<int32_t>(newBeatPos+1.0);
  443. fTimeInfo.bbt.tick = static_cast<int32_t>(newTickPos);
  444. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*fTimeInfo.bbt.beatsPerBar*(fTimeInfo.bbt.bar-1);
  445. }
  446. }
  447. // TODO - midi out
  448. updateParameterOutputs();
  449. }
  450. // -------------------------------------------------------------------
  451. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/) const
  452. {
  453. // currently unused
  454. return LV2_OPTIONS_SUCCESS;
  455. }
  456. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  457. {
  458. for (int i=0; options[i].key != 0; ++i)
  459. {
  460. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  461. {
  462. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Int))
  463. {
  464. const int value(*(const int*)options[i].value);
  465. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  466. fBufferSize = static_cast<uint32_t>(value);
  467. if (fDescriptor->dispatcher != nullptr)
  468. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  469. }
  470. else
  471. carla_stderr("Host changed maxBlockLength but with wrong value type");
  472. }
  473. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_CORE__sampleRate))
  474. {
  475. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Double))
  476. {
  477. const double value(*(const double*)options[i].value);
  478. CARLA_SAFE_ASSERT_CONTINUE(value > 0.0);
  479. fSampleRate = value;
  480. if (fDescriptor->dispatcher != nullptr)
  481. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)fSampleRate);
  482. }
  483. else
  484. carla_stderr("Host changed sampleRate but with wrong value type");
  485. }
  486. }
  487. return LV2_OPTIONS_SUCCESS;
  488. }
  489. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  490. {
  491. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  492. return nullptr;
  493. if (fDescriptor->get_midi_program_count == nullptr)
  494. return nullptr;
  495. if (fDescriptor->get_midi_program_info == nullptr)
  496. return nullptr;
  497. if (index >= fDescriptor->get_midi_program_count(fHandle))
  498. return nullptr;
  499. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  500. if (midiProg == nullptr)
  501. return nullptr;
  502. fProgramDesc.bank = midiProg->bank;
  503. fProgramDesc.program = midiProg->program;
  504. fProgramDesc.name = midiProg->name;
  505. return &fProgramDesc;
  506. }
  507. void lv2_select_program(uint32_t bank, uint32_t program)
  508. {
  509. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  510. return;
  511. if (fDescriptor->set_midi_program == nullptr)
  512. return;
  513. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  514. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  515. {
  516. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  517. if (fPorts.paramsPtr[i] != nullptr)
  518. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  519. }
  520. }
  521. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle, const uint32_t /*flags*/, const LV2_Feature* const* const /*features*/) const
  522. {
  523. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  524. return LV2_STATE_ERR_NO_FEATURE;
  525. if (char* const state = fDescriptor->get_state(fHandle))
  526. {
  527. store(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), state, std::strlen(state), fURIs.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  528. std::free(state);
  529. return LV2_STATE_SUCCESS;
  530. }
  531. return LV2_STATE_ERR_UNKNOWN;
  532. }
  533. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* const /*features*/) const
  534. {
  535. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  536. return LV2_STATE_ERR_NO_FEATURE;
  537. size_t size = 0;
  538. uint32_t type = 0;
  539. const void* const data = retrieve(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), &size, &type, &flags);
  540. if (size == 0)
  541. return LV2_STATE_ERR_UNKNOWN;
  542. if (type == 0)
  543. return LV2_STATE_ERR_UNKNOWN;
  544. if (data == nullptr)
  545. return LV2_STATE_ERR_UNKNOWN;
  546. if (type != fURIs.atomString)
  547. return LV2_STATE_ERR_BAD_TYPE;
  548. fDescriptor->set_state(fHandle, (const char*)data);
  549. return LV2_STATE_SUCCESS;
  550. }
  551. // -------------------------------------------------------------------
  552. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  553. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  554. {
  555. fUI.writeFunction = writeFunction;
  556. fUI.controller = controller;
  557. fUI.isEmbed = isEmbed;
  558. #ifdef CARLA_OS_LINUX
  559. // ---------------------------------------------------------------
  560. // show embed UI if needed
  561. if (isEmbed)
  562. {
  563. fUI.isVisible = true;
  564. intptr_t parentId = 0;
  565. for (int i=0; features[i] != nullptr; ++i)
  566. {
  567. if (std::strcmp(features[i]->URI, LV2_UI__parent) == 0)
  568. {
  569. parentId = (intptr_t)features[i]->data;
  570. }
  571. else if (std::strcmp(features[i]->URI, LV2_UI__resize) == 0)
  572. {
  573. const LV2UI_Resize* const uiResize((const LV2UI_Resize*)features[i]->data);
  574. uiResize->ui_resize(uiResize->handle, 740, 512);
  575. }
  576. }
  577. char strBuf[0xff+1];
  578. strBuf[0xff] = '\0';
  579. std::snprintf(strBuf, 0xff, P_INTPTR, parentId);
  580. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  581. fDescriptor->ui_show(fHandle, true);
  582. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  583. }
  584. #endif
  585. // ---------------------------------------------------------------
  586. // see if the host supports external-ui
  587. for (int i=0; features[i] != nullptr; ++i)
  588. {
  589. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  590. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  591. {
  592. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  593. break;
  594. }
  595. }
  596. if (fUI.host != nullptr)
  597. {
  598. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  599. *widget = this;
  600. return;
  601. }
  602. // ---------------------------------------------------------------
  603. // no external-ui support, use showInterface
  604. for (int i=0; features[i] != nullptr; ++i)
  605. {
  606. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  607. {
  608. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  609. for (int j=0; options[j].key != 0; ++j)
  610. {
  611. if (options[j].key == fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  612. {
  613. fHost.uiName = carla_strdup((const char*)options[j].value);
  614. break;
  615. }
  616. }
  617. break;
  618. }
  619. }
  620. if (fHost.uiName == nullptr)
  621. fHost.uiName = carla_strdup(fDescriptor->name);
  622. *widget = nullptr;
  623. }
  624. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  625. {
  626. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  627. return;
  628. if (portIndex >= fUI.portOffset || ! fUI.isVisible)
  629. return;
  630. if (fDescriptor->ui_set_parameter_value == nullptr)
  631. return;
  632. const float value(*(const float*)buffer);
  633. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fUI.portOffset, value);
  634. }
  635. void lv2ui_cleanup()
  636. {
  637. if (fUI.isVisible)
  638. handleUiHide();
  639. fUI.host = nullptr;
  640. fUI.writeFunction = nullptr;
  641. fUI.controller = nullptr;
  642. if (fHost.uiName != nullptr)
  643. {
  644. delete[] fHost.uiName;
  645. fHost.uiName = nullptr;
  646. }
  647. }
  648. // -------------------------------------------------------------------
  649. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  650. {
  651. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  652. return;
  653. if (fDescriptor->ui_set_midi_program == nullptr)
  654. return;
  655. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  656. }
  657. // -------------------------------------------------------------------
  658. int lv2ui_idle() const
  659. {
  660. if (! fUI.isVisible)
  661. return 1;
  662. handleUiRun();
  663. return 0;
  664. }
  665. int lv2ui_show()
  666. {
  667. handleUiShow();
  668. return 0;
  669. }
  670. int lv2ui_hide()
  671. {
  672. handleUiHide();
  673. return 0;
  674. }
  675. // -------------------------------------------------------------------
  676. protected:
  677. void handleUiRun() const
  678. {
  679. if (fDescriptor->ui_idle != nullptr)
  680. fDescriptor->ui_idle(fHandle);
  681. }
  682. void handleUiShow()
  683. {
  684. CARLA_SAFE_ASSERT_RETURN(! fUI.isEmbed,);
  685. if (fDescriptor->ui_show != nullptr)
  686. fDescriptor->ui_show(fHandle, true);
  687. fUI.isVisible = true;
  688. }
  689. void handleUiHide()
  690. {
  691. if (fDescriptor->ui_show != nullptr)
  692. fDescriptor->ui_show(fHandle, false);
  693. fUI.isVisible = false;
  694. }
  695. // -------------------------------------------------------------------
  696. uint32_t handleGetBufferSize() const
  697. {
  698. return fBufferSize;
  699. }
  700. double handleGetSampleRate() const
  701. {
  702. return fSampleRate;
  703. }
  704. bool handleIsOffline() const
  705. {
  706. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  707. return (fPorts.freewheel != nullptr && *fPorts.freewheel >= 0.5f);
  708. }
  709. const NativeTimeInfo* handleGetTimeInfo() const
  710. {
  711. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  712. return &fTimeInfo;
  713. }
  714. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  715. {
  716. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  717. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  718. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  719. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  720. // reverse-find first free event, and put it there
  721. for (uint32_t i=(kMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  722. {
  723. if (fMidiEvents[i].data[0] == 0)
  724. {
  725. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  726. return true;
  727. }
  728. }
  729. return false;
  730. }
  731. void handleUiParameterChanged(const uint32_t index, const float value) const
  732. {
  733. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  734. fUI.writeFunction(fUI.controller, index+fUI.portOffset, sizeof(float), 0, &value);
  735. }
  736. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  737. {
  738. //storeCustomData(key, value);
  739. }
  740. void handleUiClosed()
  741. {
  742. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  743. fUI.host->ui_closed(fUI.controller);
  744. fUI.host = nullptr;
  745. fUI.writeFunction = nullptr;
  746. fUI.controller = nullptr;
  747. fUI.isVisible = false;
  748. }
  749. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  750. {
  751. // TODO
  752. return nullptr;
  753. }
  754. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  755. {
  756. // TODO
  757. return nullptr;
  758. }
  759. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  760. {
  761. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  762. intptr_t ret = 0;
  763. switch (opcode)
  764. {
  765. case NATIVE_HOST_OPCODE_NULL:
  766. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  767. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  768. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  769. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  770. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  771. case NATIVE_HOST_OPCODE_HOST_IDLE:
  772. // nothing
  773. break;
  774. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  775. handleUiClosed();
  776. break;
  777. }
  778. return ret;
  779. // unused for now
  780. (void)index;
  781. (void)value;
  782. (void)ptr;
  783. (void)opt;
  784. }
  785. void updateParameterOutputs()
  786. {
  787. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  788. {
  789. if (fDescriptor->get_parameter_info(fHandle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT)
  790. {
  791. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  792. if (fPorts.paramsPtr[i] != nullptr)
  793. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  794. }
  795. }
  796. }
  797. // -------------------------------------------------------------------
  798. private:
  799. // Native data
  800. NativePluginHandle fHandle;
  801. NativeHostDescriptor fHost;
  802. const NativePluginDescriptor* const fDescriptor;
  803. LV2_Program_Descriptor fProgramDesc;
  804. uint32_t fMidiEventCount;
  805. NativeMidiEvent fMidiEvents[kMaxMidiEvents*2];
  806. NativeTimeInfo fTimeInfo;
  807. double fLastTimeSpeed;
  808. bool fIsProcessing;
  809. // Lv2 host data
  810. uint32_t fBufferSize;
  811. double fSampleRate;
  812. const LV2_URID_Map* fUridMap;
  813. struct URIDs {
  814. LV2_URID atomBlank;
  815. LV2_URID atomDouble;
  816. LV2_URID atomFloat;
  817. LV2_URID atomInt;
  818. LV2_URID atomLong;
  819. LV2_URID atomSequence;
  820. LV2_URID atomString;
  821. LV2_URID midiEvent;
  822. LV2_URID timePos;
  823. LV2_URID timeBar;
  824. LV2_URID timeBeat;
  825. LV2_URID timeBarBeat;
  826. LV2_URID timeBeatsPerBar;
  827. LV2_URID timeBeatsPerMinute;
  828. LV2_URID timeBeatUnit;
  829. LV2_URID timeFrame;
  830. LV2_URID timeSpeed;
  831. LV2_URID timeTicksPerBeat;
  832. URIDs()
  833. : atomBlank(0),
  834. atomDouble(0),
  835. atomFloat(0),
  836. atomInt(0),
  837. atomLong(0),
  838. atomSequence(0),
  839. atomString(0),
  840. midiEvent(0),
  841. timePos(0),
  842. timeBar(0),
  843. timeBeat(0),
  844. timeBarBeat(0),
  845. timeBeatsPerBar(0),
  846. timeBeatsPerMinute(0),
  847. timeBeatUnit(0),
  848. timeFrame(0),
  849. timeSpeed(0),
  850. timeTicksPerBeat(0) {}
  851. void map(const LV2_URID_Map* const uridMap)
  852. {
  853. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  854. atomDouble = uridMap->map(uridMap->handle, LV2_ATOM__Double);
  855. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  856. atomInt = uridMap->map(uridMap->handle, LV2_ATOM__Int);
  857. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  858. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  859. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  860. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  861. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  862. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  863. timeBeat = uridMap->map(uridMap->handle, LV2_TIME__beat);
  864. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  865. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  866. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  867. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  868. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  869. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  870. timeTicksPerBeat = uridMap->map(uridMap->handle, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat);
  871. }
  872. } fURIs;
  873. struct UI {
  874. const LV2_External_UI_Host* host;
  875. LV2UI_Write_Function writeFunction;
  876. LV2UI_Controller controller;
  877. uint32_t portOffset;
  878. bool isEmbed;
  879. bool isVisible;
  880. UI()
  881. : host(nullptr),
  882. writeFunction(nullptr),
  883. controller(nullptr),
  884. portOffset(0),
  885. isEmbed(false),
  886. isVisible(false) {}
  887. } fUI;
  888. struct Ports {
  889. LV2_Atom_Sequence** eventsIn;
  890. LV2_Atom_Sequence** midiOuts;
  891. float** audioIns;
  892. float** audioOuts;
  893. float* freewheel;
  894. uint32_t paramCount;
  895. float* paramsLast;
  896. float** paramsPtr;
  897. Ports()
  898. : eventsIn(nullptr),
  899. midiOuts(nullptr),
  900. audioIns(nullptr),
  901. audioOuts(nullptr),
  902. freewheel(nullptr),
  903. paramCount(0),
  904. paramsLast(nullptr),
  905. paramsPtr(nullptr) {}
  906. ~Ports()
  907. {
  908. if (eventsIn != nullptr)
  909. {
  910. delete[] eventsIn;
  911. eventsIn = nullptr;
  912. }
  913. if (midiOuts != nullptr)
  914. {
  915. delete[] midiOuts;
  916. midiOuts = nullptr;
  917. }
  918. if (audioIns != nullptr)
  919. {
  920. delete[] audioIns;
  921. audioIns = nullptr;
  922. }
  923. if (audioOuts != nullptr)
  924. {
  925. delete[] audioOuts;
  926. audioOuts = nullptr;
  927. }
  928. if (paramsLast != nullptr)
  929. {
  930. delete[] paramsLast;
  931. paramsLast = nullptr;
  932. }
  933. if (paramsPtr != nullptr)
  934. {
  935. delete[] paramsPtr;
  936. paramsPtr = nullptr;
  937. }
  938. }
  939. void init(const NativePluginDescriptor* const desc, NativePluginHandle handle)
  940. {
  941. CARLA_SAFE_ASSERT_RETURN(desc != nullptr && handle != nullptr,)
  942. if (desc->midiIns > 0)
  943. {
  944. eventsIn = new LV2_Atom_Sequence*[desc->midiIns];
  945. for (uint32_t i=0; i < desc->midiIns; ++i)
  946. eventsIn[i] = nullptr;
  947. }
  948. else if (desc->hints & NATIVE_PLUGIN_USES_TIME)
  949. {
  950. eventsIn = new LV2_Atom_Sequence*[1];
  951. eventsIn[0] = nullptr;
  952. }
  953. if (desc->midiOuts > 0)
  954. {
  955. midiOuts = new LV2_Atom_Sequence*[desc->midiOuts];
  956. for (uint32_t i=0; i < desc->midiOuts; ++i)
  957. midiOuts[i] = nullptr;
  958. }
  959. if (desc->audioIns > 0)
  960. {
  961. audioIns = new float*[desc->audioIns];
  962. for (uint32_t i=0; i < desc->audioIns; ++i)
  963. audioIns[i] = nullptr;
  964. }
  965. if (desc->audioOuts > 0)
  966. {
  967. audioOuts = new float*[desc->audioOuts];
  968. for (uint32_t i=0; i < desc->audioOuts; ++i)
  969. audioOuts[i] = nullptr;
  970. }
  971. if (desc->get_parameter_count != nullptr && desc->get_parameter_info != nullptr && desc->get_parameter_value != nullptr && desc->set_parameter_value != nullptr)
  972. {
  973. paramCount = desc->get_parameter_count(handle);
  974. if (paramCount > 0)
  975. {
  976. paramsLast = new float[paramCount];
  977. paramsPtr = new float*[paramCount];
  978. for (uint32_t i=0; i < paramCount; ++i)
  979. {
  980. paramsLast[i] = desc->get_parameter_value(handle, i);
  981. paramsPtr[i] = nullptr;
  982. }
  983. }
  984. }
  985. }
  986. void connectPort(const NativePluginDescriptor* const desc, const uint32_t port, void* const dataLocation)
  987. {
  988. uint32_t index = 0;
  989. if (desc->midiIns > 0 || (desc->hints & NATIVE_PLUGIN_USES_TIME) != 0)
  990. {
  991. if (port == index++)
  992. {
  993. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  994. return;
  995. }
  996. }
  997. for (uint32_t i=1; i < desc->midiIns; ++i)
  998. {
  999. if (port == index++)
  1000. {
  1001. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  1002. return;
  1003. }
  1004. }
  1005. #if 0 // TODO
  1006. for (uint32_t i=0; i < desc->midiOuts; ++i)
  1007. {
  1008. if (port == index++)
  1009. {
  1010. midiOuts[i] = (LV2_Atom_Sequence*)dataLocation;
  1011. return;
  1012. }
  1013. }
  1014. #endif
  1015. if (port == index++)
  1016. {
  1017. freewheel = (float*)dataLocation;
  1018. return;
  1019. }
  1020. for (uint32_t i=0; i < desc->audioIns; ++i)
  1021. {
  1022. if (port == index++)
  1023. {
  1024. audioIns[i] = (float*)dataLocation;
  1025. return;
  1026. }
  1027. }
  1028. for (uint32_t i=0; i < desc->audioOuts; ++i)
  1029. {
  1030. if (port == index++)
  1031. {
  1032. audioOuts[i] = (float*)dataLocation;
  1033. return;
  1034. }
  1035. }
  1036. for (uint32_t i=0; i < paramCount; ++i)
  1037. {
  1038. if (port == index++)
  1039. {
  1040. paramsPtr[i] = (float*)dataLocation;
  1041. return;
  1042. }
  1043. }
  1044. }
  1045. CARLA_DECLARE_NON_COPY_STRUCT(Ports);
  1046. } fPorts;
  1047. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  1048. // -------------------------------------------------------------------
  1049. #define handlePtr ((NativePlugin*)self)
  1050. static void extui_run(LV2_External_UI_Widget_Compat* self)
  1051. {
  1052. handlePtr->handleUiRun();
  1053. }
  1054. static void extui_show(LV2_External_UI_Widget_Compat* self)
  1055. {
  1056. handlePtr->handleUiShow();
  1057. }
  1058. static void extui_hide(LV2_External_UI_Widget_Compat* self)
  1059. {
  1060. handlePtr->handleUiHide();
  1061. }
  1062. #undef handlePtr
  1063. // -------------------------------------------------------------------
  1064. #define handlePtr ((NativePlugin*)handle)
  1065. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  1066. {
  1067. return handlePtr->handleGetBufferSize();
  1068. }
  1069. static double host_get_sample_rate(NativeHostHandle handle)
  1070. {
  1071. return handlePtr->handleGetSampleRate();
  1072. }
  1073. static bool host_is_offline(NativeHostHandle handle)
  1074. {
  1075. return handlePtr->handleIsOffline();
  1076. }
  1077. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  1078. {
  1079. return handlePtr->handleGetTimeInfo();
  1080. }
  1081. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1082. {
  1083. return handlePtr->handleWriteMidiEvent(event);
  1084. }
  1085. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1086. {
  1087. handlePtr->handleUiParameterChanged(index, value);
  1088. }
  1089. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1090. {
  1091. handlePtr->handleUiCustomDataChanged(key, value);
  1092. }
  1093. static void host_ui_closed(NativeHostHandle handle)
  1094. {
  1095. handlePtr->handleUiClosed();
  1096. }
  1097. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1098. {
  1099. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1100. }
  1101. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1102. {
  1103. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1104. }
  1105. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1106. {
  1107. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1108. }
  1109. #undef handlePtr
  1110. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1111. };
  1112. // -----------------------------------------------------------------------
  1113. // LV2 plugin descriptor functions
  1114. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  1115. {
  1116. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  1117. const NativePluginDescriptor* pluginDesc = nullptr;
  1118. const char* pluginLabel = nullptr;
  1119. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  1120. pluginLabel = lv2Descriptor->URI+37;
  1121. if (pluginLabel == nullptr)
  1122. {
  1123. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  1124. return nullptr;
  1125. }
  1126. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  1127. PluginListManager& plm(PluginListManager::getInstance());
  1128. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  1129. {
  1130. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  1131. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  1132. {
  1133. pluginDesc = tmpDesc;
  1134. break;
  1135. }
  1136. }
  1137. if (pluginDesc == nullptr)
  1138. {
  1139. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  1140. return nullptr;
  1141. }
  1142. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  1143. if (! plugin->init())
  1144. {
  1145. carla_stderr("Failed to init plugin");
  1146. delete plugin;
  1147. return nullptr;
  1148. }
  1149. return (LV2_Handle)plugin;
  1150. }
  1151. #define instancePtr ((NativePlugin*)instance)
  1152. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  1153. {
  1154. instancePtr->lv2_connect_port(port, dataLocation);
  1155. }
  1156. static void lv2_activate(LV2_Handle instance)
  1157. {
  1158. carla_debug("lv2_activate(%p)", instance);
  1159. instancePtr->lv2_activate();
  1160. }
  1161. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  1162. {
  1163. instancePtr->lv2_run(sampleCount);
  1164. }
  1165. static void lv2_deactivate(LV2_Handle instance)
  1166. {
  1167. carla_debug("lv2_deactivate(%p)", instance);
  1168. instancePtr->lv2_deactivate();
  1169. }
  1170. static void lv2_cleanup(LV2_Handle instance)
  1171. {
  1172. carla_debug("lv2_cleanup(%p)", instance);
  1173. instancePtr->lv2_cleanup();
  1174. delete instancePtr;
  1175. }
  1176. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1177. {
  1178. carla_debug("lv2_get_options(%p, %p)", instance, options);
  1179. return instancePtr->lv2_get_options(options);
  1180. }
  1181. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1182. {
  1183. carla_debug("lv2_set_options(%p, %p)", instance, options);
  1184. return instancePtr->lv2_set_options(options);
  1185. }
  1186. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1187. {
  1188. carla_debug("lv2_get_program(%p, %i)", instance, index);
  1189. return instancePtr->lv2_get_program(index);
  1190. }
  1191. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1192. {
  1193. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  1194. return instancePtr->lv2_select_program(bank, program);
  1195. }
  1196. 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)
  1197. {
  1198. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  1199. return instancePtr->lv2_save(store, handle, flags, features);
  1200. }
  1201. 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)
  1202. {
  1203. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  1204. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  1205. }
  1206. static const void* lv2_extension_data(const char* uri)
  1207. {
  1208. carla_debug("lv2_extension_data(\"%s\")", uri);
  1209. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1210. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1211. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1212. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1213. return &options;
  1214. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1215. return &programs;
  1216. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1217. return &state;
  1218. return nullptr;
  1219. }
  1220. #undef instancePtr
  1221. // -----------------------------------------------------------------------
  1222. // LV2 UI descriptor functions
  1223. static LV2UI_Handle lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1224. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  1225. {
  1226. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  1227. #ifndef CARLA_OS_LINUX
  1228. CARLA_SAFE_ASSERT_RETURN(! isEmbed, nullptr);
  1229. #endif
  1230. NativePlugin* plugin = nullptr;
  1231. for (int i=0; features[i] != nullptr; ++i)
  1232. {
  1233. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1234. {
  1235. plugin = (NativePlugin*)features[i]->data;
  1236. break;
  1237. }
  1238. }
  1239. if (plugin == nullptr)
  1240. {
  1241. carla_stderr("Host doesn't support instance-access, cannot show UI");
  1242. return nullptr;
  1243. }
  1244. plugin->lv2ui_instantiate(writeFunction, controller, widget, features, isEmbed);
  1245. return (LV2UI_Handle)plugin;
  1246. }
  1247. #ifdef CARLA_OS_LINUX
  1248. static LV2UI_Handle lv2ui_instantiate_embed(const LV2UI_Descriptor*, const char*, const char*,
  1249. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1250. LV2UI_Widget* widget, const LV2_Feature* const* features)
  1251. {
  1252. return lv2ui_instantiate(writeFunction, controller, widget, features, true);
  1253. }
  1254. #endif
  1255. static LV2UI_Handle lv2ui_instantiate_external(const LV2UI_Descriptor*, const char*, const char*,
  1256. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1257. LV2UI_Widget* widget, const LV2_Feature* const* features)
  1258. {
  1259. return lv2ui_instantiate(writeFunction, controller, widget, features, false);
  1260. }
  1261. #define uiPtr ((NativePlugin*)ui)
  1262. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1263. {
  1264. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  1265. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  1266. }
  1267. static void lv2ui_cleanup(LV2UI_Handle ui)
  1268. {
  1269. carla_debug("lv2ui_cleanup(%p)", ui);
  1270. uiPtr->lv2ui_cleanup();
  1271. }
  1272. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  1273. {
  1274. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  1275. uiPtr->lv2ui_select_program(bank, program);
  1276. }
  1277. static int lv2ui_idle(LV2UI_Handle ui)
  1278. {
  1279. return uiPtr->lv2ui_idle();
  1280. }
  1281. static int lv2ui_show(LV2UI_Handle ui)
  1282. {
  1283. carla_debug("lv2ui_show(%p)", ui);
  1284. return uiPtr->lv2ui_show();
  1285. }
  1286. static int lv2ui_hide(LV2UI_Handle ui)
  1287. {
  1288. carla_debug("lv2ui_hide(%p)", ui);
  1289. return uiPtr->lv2ui_hide();
  1290. }
  1291. static const void* lv2ui_extension_data(const char* uri)
  1292. {
  1293. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  1294. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  1295. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  1296. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  1297. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  1298. return &uiidle;
  1299. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  1300. return &uishow;
  1301. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  1302. return &uiprograms;
  1303. return nullptr;
  1304. }
  1305. #undef uiPtr
  1306. // -----------------------------------------------------------------------
  1307. // Startup code
  1308. CARLA_EXPORT
  1309. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1310. {
  1311. carla_debug("lv2_descriptor(%i)", index);
  1312. PluginListManager& plm(PluginListManager::getInstance());
  1313. if (index >= plm.descs.count())
  1314. {
  1315. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1316. return nullptr;
  1317. }
  1318. if (index < plm.lv2Descs.count())
  1319. {
  1320. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1321. return plm.lv2Descs.getAt(index, nullptr);
  1322. }
  1323. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  1324. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  1325. CarlaString tmpURI;
  1326. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1327. tmpURI += pluginDesc->label;
  1328. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1329. const LV2_Descriptor lv2DescTmp = {
  1330. /* URI */ carla_strdup(tmpURI),
  1331. /* instantiate */ lv2_instantiate,
  1332. /* connect_port */ lv2_connect_port,
  1333. /* activate */ lv2_activate,
  1334. /* run */ lv2_run,
  1335. /* deactivate */ lv2_deactivate,
  1336. /* cleanup */ lv2_cleanup,
  1337. /* extension_data */ lv2_extension_data
  1338. };
  1339. LV2_Descriptor* const lv2Desc(new LV2_Descriptor);
  1340. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1341. plm.lv2Descs.append(lv2Desc);
  1342. return lv2Desc;
  1343. }
  1344. CARLA_EXPORT
  1345. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1346. {
  1347. carla_debug("lv2ui_descriptor(%i)", index);
  1348. #ifdef CARLA_OS_LINUX
  1349. static const LV2UI_Descriptor lv2UiEmbedDesc = {
  1350. /* URI */ "http://kxstudio.sf.net/carla/ui-embed",
  1351. /* instantiate */ lv2ui_instantiate_embed,
  1352. /* cleanup */ lv2ui_cleanup,
  1353. /* port_event */ lv2ui_port_event,
  1354. /* extension_data */ lv2ui_extension_data
  1355. };
  1356. if (index == 0)
  1357. return &lv2UiEmbedDesc;
  1358. else
  1359. --index;
  1360. #endif
  1361. static const LV2UI_Descriptor lv2UiExtDesc = {
  1362. /* URI */ "http://kxstudio.sf.net/carla/ui-ext",
  1363. /* instantiate */ lv2ui_instantiate_external,
  1364. /* cleanup */ lv2ui_cleanup,
  1365. /* port_event */ lv2ui_port_event,
  1366. /* extension_data */ lv2ui_extension_data
  1367. };
  1368. return (index == 0) ? &lv2UiExtDesc : nullptr;
  1369. }
  1370. // -----------------------------------------------------------------------