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.

1879 lines
64KB

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