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.

1716 lines
57KB

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