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.

1768 lines
60KB

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