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.

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