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.

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