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.

1798 lines
61KB

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