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.

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