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.

1765 lines
59KB

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