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.

1585 lines
53KB

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