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.

1878 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. // nothing
  899. break;
  900. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  901. handleUiClosed();
  902. break;
  903. }
  904. return ret;
  905. // unused for now
  906. (void)index;
  907. (void)value;
  908. (void)ptr;
  909. (void)opt;
  910. }
  911. void updateParameterOutputs()
  912. {
  913. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  914. {
  915. if (! fPorts.paramsOut[i])
  916. continue;
  917. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  918. if (fPorts.paramsPtr[i] != nullptr)
  919. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  920. }
  921. }
  922. // -------------------------------------------------------------------
  923. private:
  924. // Native data
  925. NativePluginHandle fHandle;
  926. NativeHostDescriptor fHost;
  927. const NativePluginDescriptor* const fDescriptor;
  928. LV2_Program_Descriptor fProgramDesc;
  929. uint32_t fMidiEventCount;
  930. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  931. NativeTimeInfo fTimeInfo;
  932. // Lv2 host data
  933. bool fIsOffline;
  934. uint32_t fBufferSize;
  935. double fSampleRate;
  936. bool fUsingNominal;
  937. const LV2_URID_Map* fUridMap;
  938. struct Lv2PositionData {
  939. int32_t bar;
  940. float bar_f;
  941. float barBeat;
  942. uint32_t beatUnit;
  943. float beatsPerBar;
  944. double beatsPerMinute;
  945. uint64_t frame;
  946. double speed;
  947. double ticksPerBeat;
  948. Lv2PositionData()
  949. : bar(-1),
  950. bar_f(-1.0f),
  951. barBeat(-1.0f),
  952. beatUnit(0),
  953. beatsPerBar(0.0f),
  954. beatsPerMinute(-1.0),
  955. frame(0),
  956. speed(0.0),
  957. ticksPerBeat(-1.0) {}
  958. } fLastPositionData;
  959. struct URIDs {
  960. LV2_URID atomBlank;
  961. LV2_URID atomObject;
  962. LV2_URID atomDouble;
  963. LV2_URID atomFloat;
  964. LV2_URID atomInt;
  965. LV2_URID atomLong;
  966. LV2_URID atomSequence;
  967. LV2_URID atomString;
  968. LV2_URID midiEvent;
  969. LV2_URID timePos;
  970. LV2_URID timeBar;
  971. LV2_URID timeBarBeat;
  972. LV2_URID timeBeatsPerBar;
  973. LV2_URID timeBeatsPerMinute;
  974. LV2_URID timeBeatUnit;
  975. LV2_URID timeFrame;
  976. LV2_URID timeSpeed;
  977. LV2_URID timeTicksPerBeat;
  978. URIDs()
  979. : atomBlank(0),
  980. atomObject(0),
  981. atomDouble(0),
  982. atomFloat(0),
  983. atomInt(0),
  984. atomLong(0),
  985. atomSequence(0),
  986. atomString(0),
  987. midiEvent(0),
  988. timePos(0),
  989. timeBar(0),
  990. timeBarBeat(0),
  991. timeBeatsPerBar(0),
  992. timeBeatsPerMinute(0),
  993. timeBeatUnit(0),
  994. timeFrame(0),
  995. timeSpeed(0),
  996. timeTicksPerBeat(0) {}
  997. void map(const LV2_URID_Map* const uridMap)
  998. {
  999. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  1000. atomObject = uridMap->map(uridMap->handle, LV2_ATOM__Object);
  1001. atomDouble = uridMap->map(uridMap->handle, LV2_ATOM__Double);
  1002. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  1003. atomInt = uridMap->map(uridMap->handle, LV2_ATOM__Int);
  1004. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  1005. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  1006. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  1007. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  1008. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  1009. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  1010. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  1011. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  1012. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  1013. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  1014. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  1015. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  1016. timeTicksPerBeat = uridMap->map(uridMap->handle, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat);
  1017. }
  1018. } fURIs;
  1019. struct UI {
  1020. const LV2_External_UI_Host* host;
  1021. LV2UI_Write_Function writeFunction;
  1022. LV2UI_Controller controller;
  1023. uint32_t portOffset;
  1024. bool isEmbed;
  1025. bool isVisible;
  1026. UI()
  1027. : host(nullptr),
  1028. writeFunction(nullptr),
  1029. controller(nullptr),
  1030. portOffset(0),
  1031. isEmbed(false),
  1032. isVisible(false) {}
  1033. } fUI;
  1034. struct Ports {
  1035. // need to save current state
  1036. struct MidiOutData {
  1037. uint32_t capacity;
  1038. uint32_t offset;
  1039. MidiOutData()
  1040. : capacity(0),
  1041. offset(0) {}
  1042. };
  1043. const LV2_Atom_Sequence** eventsIn;
  1044. /* */ LV2_Atom_Sequence** midiOuts;
  1045. /* */ MidiOutData* midiOutData;
  1046. const float** audioIns;
  1047. /* */ float** audioOuts;
  1048. float* freewheel;
  1049. uint32_t paramCount;
  1050. float* paramsLast;
  1051. float** paramsPtr;
  1052. bool* paramsOut;
  1053. Ports()
  1054. : eventsIn(nullptr),
  1055. midiOuts(nullptr),
  1056. midiOutData(nullptr),
  1057. audioIns(nullptr),
  1058. audioOuts(nullptr),
  1059. freewheel(nullptr),
  1060. paramCount(0),
  1061. paramsLast(nullptr),
  1062. paramsPtr(nullptr),
  1063. paramsOut(nullptr) {}
  1064. ~Ports()
  1065. {
  1066. if (eventsIn != nullptr)
  1067. {
  1068. delete[] eventsIn;
  1069. eventsIn = nullptr;
  1070. }
  1071. if (midiOuts != nullptr)
  1072. {
  1073. delete[] midiOuts;
  1074. midiOuts = nullptr;
  1075. }
  1076. if (midiOutData != nullptr)
  1077. {
  1078. delete[] midiOutData;
  1079. midiOutData = nullptr;
  1080. }
  1081. if (audioIns != nullptr)
  1082. {
  1083. delete[] audioIns;
  1084. audioIns = nullptr;
  1085. }
  1086. if (audioOuts != nullptr)
  1087. {
  1088. delete[] audioOuts;
  1089. audioOuts = nullptr;
  1090. }
  1091. if (paramsLast != nullptr)
  1092. {
  1093. delete[] paramsLast;
  1094. paramsLast = nullptr;
  1095. }
  1096. if (paramsPtr != nullptr)
  1097. {
  1098. delete[] paramsPtr;
  1099. paramsPtr = nullptr;
  1100. }
  1101. if (paramsOut != nullptr)
  1102. {
  1103. delete[] paramsOut;
  1104. paramsOut = nullptr;
  1105. }
  1106. }
  1107. void init(const NativePluginDescriptor* const desc, NativePluginHandle handle)
  1108. {
  1109. CARLA_SAFE_ASSERT_RETURN(desc != nullptr && handle != nullptr,)
  1110. if (desc->midiIns > 0)
  1111. {
  1112. eventsIn = new const LV2_Atom_Sequence*[desc->midiIns];
  1113. for (uint32_t i=0; i < desc->midiIns; ++i)
  1114. eventsIn[i] = nullptr;
  1115. }
  1116. else if (desc->hints & NATIVE_PLUGIN_USES_TIME)
  1117. {
  1118. eventsIn = new const LV2_Atom_Sequence*[1];
  1119. eventsIn[0] = nullptr;
  1120. }
  1121. if (desc->midiOuts > 0)
  1122. {
  1123. midiOuts = new LV2_Atom_Sequence*[desc->midiOuts];
  1124. midiOutData = new MidiOutData[desc->midiOuts];
  1125. for (uint32_t i=0; i < desc->midiOuts; ++i)
  1126. midiOuts[i] = nullptr;
  1127. }
  1128. if (desc->audioIns > 0)
  1129. {
  1130. audioIns = new const float*[desc->audioIns];
  1131. for (uint32_t i=0; i < desc->audioIns; ++i)
  1132. audioIns[i] = nullptr;
  1133. }
  1134. if (desc->audioOuts > 0)
  1135. {
  1136. audioOuts = new float*[desc->audioOuts];
  1137. for (uint32_t i=0; i < desc->audioOuts; ++i)
  1138. audioOuts[i] = nullptr;
  1139. }
  1140. if (desc->get_parameter_count != nullptr && desc->get_parameter_info != nullptr && desc->get_parameter_value != nullptr && desc->set_parameter_value != nullptr)
  1141. {
  1142. paramCount = desc->get_parameter_count(handle);
  1143. if (paramCount > 0)
  1144. {
  1145. paramsLast = new float[paramCount];
  1146. paramsPtr = new float*[paramCount];
  1147. paramsOut = new bool[paramCount];
  1148. for (uint32_t i=0; i < paramCount; ++i)
  1149. {
  1150. paramsLast[i] = desc->get_parameter_value(handle, i);
  1151. paramsPtr [i] = nullptr;
  1152. paramsOut [i] = (desc->get_parameter_info(handle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT);
  1153. }
  1154. }
  1155. }
  1156. }
  1157. void connectPort(const NativePluginDescriptor* const desc, const uint32_t port, void* const dataLocation)
  1158. {
  1159. uint32_t index = 0;
  1160. if (desc->midiIns > 0 || (desc->hints & NATIVE_PLUGIN_USES_TIME) != 0)
  1161. {
  1162. if (port == index++)
  1163. {
  1164. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  1165. return;
  1166. }
  1167. }
  1168. for (uint32_t i=1; i < desc->midiIns; ++i)
  1169. {
  1170. if (port == index++)
  1171. {
  1172. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  1173. return;
  1174. }
  1175. }
  1176. for (uint32_t i=0; i < desc->midiOuts; ++i)
  1177. {
  1178. if (port == index++)
  1179. {
  1180. midiOuts[i] = (LV2_Atom_Sequence*)dataLocation;
  1181. return;
  1182. }
  1183. }
  1184. if (port == index++)
  1185. {
  1186. freewheel = (float*)dataLocation;
  1187. return;
  1188. }
  1189. for (uint32_t i=0; i < desc->audioIns; ++i)
  1190. {
  1191. if (port == index++)
  1192. {
  1193. audioIns[i] = (float*)dataLocation;
  1194. return;
  1195. }
  1196. }
  1197. for (uint32_t i=0; i < desc->audioOuts; ++i)
  1198. {
  1199. if (port == index++)
  1200. {
  1201. audioOuts[i] = (float*)dataLocation;
  1202. return;
  1203. }
  1204. }
  1205. for (uint32_t i=0; i < paramCount; ++i)
  1206. {
  1207. if (port == index++)
  1208. {
  1209. paramsPtr[i] = (float*)dataLocation;
  1210. return;
  1211. }
  1212. }
  1213. }
  1214. CARLA_DECLARE_NON_COPY_STRUCT(Ports);
  1215. } fPorts;
  1216. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  1217. // -------------------------------------------------------------------
  1218. #define handlePtr ((NativePlugin*)self)
  1219. static void extui_run(LV2_External_UI_Widget_Compat* self)
  1220. {
  1221. handlePtr->handleUiRun();
  1222. }
  1223. static void extui_show(LV2_External_UI_Widget_Compat* self)
  1224. {
  1225. handlePtr->handleUiShow();
  1226. }
  1227. static void extui_hide(LV2_External_UI_Widget_Compat* self)
  1228. {
  1229. handlePtr->handleUiHide();
  1230. }
  1231. #undef handlePtr
  1232. // -------------------------------------------------------------------
  1233. #define handlePtr ((NativePlugin*)handle)
  1234. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  1235. {
  1236. return handlePtr->fBufferSize;
  1237. }
  1238. static double host_get_sample_rate(NativeHostHandle handle)
  1239. {
  1240. return handlePtr->fSampleRate;
  1241. }
  1242. static bool host_is_offline(NativeHostHandle handle)
  1243. {
  1244. return handlePtr->fIsOffline;
  1245. }
  1246. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  1247. {
  1248. return &(handlePtr->fTimeInfo);
  1249. }
  1250. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1251. {
  1252. return handlePtr->handleWriteMidiEvent(event);
  1253. }
  1254. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1255. {
  1256. handlePtr->handleUiParameterChanged(index, value);
  1257. }
  1258. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1259. {
  1260. handlePtr->handleUiCustomDataChanged(key, value);
  1261. }
  1262. static void host_ui_closed(NativeHostHandle handle)
  1263. {
  1264. handlePtr->handleUiClosed();
  1265. }
  1266. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1267. {
  1268. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1269. }
  1270. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1271. {
  1272. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1273. }
  1274. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1275. {
  1276. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1277. }
  1278. #undef handlePtr
  1279. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1280. };
  1281. // -----------------------------------------------------------------------
  1282. // LV2 plugin descriptor functions
  1283. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  1284. {
  1285. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  1286. const NativePluginDescriptor* pluginDesc = nullptr;
  1287. const char* pluginLabel = nullptr;
  1288. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  1289. pluginLabel = lv2Descriptor->URI+37;
  1290. if (pluginLabel == nullptr)
  1291. {
  1292. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  1293. return nullptr;
  1294. }
  1295. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  1296. PluginListManager& plm(PluginListManager::getInstance());
  1297. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  1298. {
  1299. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  1300. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  1301. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  1302. {
  1303. pluginDesc = tmpDesc;
  1304. break;
  1305. }
  1306. }
  1307. if (pluginDesc == nullptr)
  1308. {
  1309. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  1310. return nullptr;
  1311. }
  1312. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  1313. if (! plugin->init())
  1314. {
  1315. carla_stderr("Failed to init plugin");
  1316. delete plugin;
  1317. return nullptr;
  1318. }
  1319. return (LV2_Handle)plugin;
  1320. }
  1321. #define instancePtr ((NativePlugin*)instance)
  1322. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  1323. {
  1324. instancePtr->lv2_connect_port(port, dataLocation);
  1325. }
  1326. static void lv2_activate(LV2_Handle instance)
  1327. {
  1328. carla_debug("lv2_activate(%p)", instance);
  1329. instancePtr->lv2_activate();
  1330. }
  1331. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  1332. {
  1333. instancePtr->lv2_run(sampleCount);
  1334. }
  1335. static void lv2_deactivate(LV2_Handle instance)
  1336. {
  1337. carla_debug("lv2_deactivate(%p)", instance);
  1338. instancePtr->lv2_deactivate();
  1339. }
  1340. static void lv2_cleanup(LV2_Handle instance)
  1341. {
  1342. carla_debug("lv2_cleanup(%p)", instance);
  1343. instancePtr->lv2_cleanup();
  1344. delete instancePtr;
  1345. }
  1346. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1347. {
  1348. carla_debug("lv2_get_options(%p, %p)", instance, options);
  1349. return instancePtr->lv2_get_options(options);
  1350. }
  1351. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1352. {
  1353. carla_debug("lv2_set_options(%p, %p)", instance, options);
  1354. return instancePtr->lv2_set_options(options);
  1355. }
  1356. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1357. {
  1358. carla_debug("lv2_get_program(%p, %i)", instance, index);
  1359. return instancePtr->lv2_get_program(index);
  1360. }
  1361. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1362. {
  1363. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  1364. return instancePtr->lv2_select_program(bank, program);
  1365. }
  1366. 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)
  1367. {
  1368. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  1369. return instancePtr->lv2_save(store, handle, flags, features);
  1370. }
  1371. 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)
  1372. {
  1373. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  1374. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  1375. }
  1376. static const void* lv2_extension_data(const char* uri)
  1377. {
  1378. carla_debug("lv2_extension_data(\"%s\")", uri);
  1379. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1380. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1381. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1382. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1383. return &options;
  1384. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1385. return &programs;
  1386. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1387. return &state;
  1388. return nullptr;
  1389. }
  1390. #undef instancePtr
  1391. // -----------------------------------------------------------------------
  1392. // LV2 UI descriptor functions
  1393. static LV2UI_Handle lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1394. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  1395. {
  1396. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  1397. #ifndef CARLA_OS_LINUX
  1398. CARLA_SAFE_ASSERT_RETURN(! isEmbed, nullptr);
  1399. #endif
  1400. NativePlugin* plugin = nullptr;
  1401. for (int i=0; features[i] != nullptr; ++i)
  1402. {
  1403. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1404. {
  1405. plugin = (NativePlugin*)features[i]->data;
  1406. break;
  1407. }
  1408. }
  1409. if (plugin == nullptr)
  1410. {
  1411. carla_stderr("Host doesn't support instance-access, cannot show UI");
  1412. return nullptr;
  1413. }
  1414. plugin->lv2ui_instantiate(writeFunction, controller, widget, features, isEmbed);
  1415. return (LV2UI_Handle)plugin;
  1416. }
  1417. #ifdef CARLA_OS_LINUX
  1418. static LV2UI_Handle lv2ui_instantiate_embed(const LV2UI_Descriptor*, const char*, const char*,
  1419. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1420. LV2UI_Widget* widget, const LV2_Feature* const* features)
  1421. {
  1422. return lv2ui_instantiate(writeFunction, controller, widget, features, true);
  1423. }
  1424. #endif
  1425. static LV2UI_Handle lv2ui_instantiate_external(const LV2UI_Descriptor*, const char*, const char*,
  1426. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1427. LV2UI_Widget* widget, const LV2_Feature* const* features)
  1428. {
  1429. return lv2ui_instantiate(writeFunction, controller, widget, features, false);
  1430. }
  1431. #define uiPtr ((NativePlugin*)ui)
  1432. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1433. {
  1434. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  1435. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  1436. }
  1437. static void lv2ui_cleanup(LV2UI_Handle ui)
  1438. {
  1439. carla_debug("lv2ui_cleanup(%p)", ui);
  1440. uiPtr->lv2ui_cleanup();
  1441. }
  1442. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  1443. {
  1444. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  1445. uiPtr->lv2ui_select_program(bank, program);
  1446. }
  1447. static int lv2ui_idle(LV2UI_Handle ui)
  1448. {
  1449. return uiPtr->lv2ui_idle();
  1450. }
  1451. static int lv2ui_show(LV2UI_Handle ui)
  1452. {
  1453. carla_debug("lv2ui_show(%p)", ui);
  1454. return uiPtr->lv2ui_show();
  1455. }
  1456. static int lv2ui_hide(LV2UI_Handle ui)
  1457. {
  1458. carla_debug("lv2ui_hide(%p)", ui);
  1459. return uiPtr->lv2ui_hide();
  1460. }
  1461. static const void* lv2ui_extension_data(const char* uri)
  1462. {
  1463. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  1464. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  1465. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  1466. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  1467. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  1468. return &uiidle;
  1469. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  1470. return &uishow;
  1471. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  1472. return &uiprograms;
  1473. return nullptr;
  1474. }
  1475. #undef uiPtr
  1476. // -----------------------------------------------------------------------
  1477. // Startup code
  1478. CARLA_EXPORT
  1479. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1480. {
  1481. carla_debug("lv2_descriptor(%i)", index);
  1482. PluginListManager& plm(PluginListManager::getInstance());
  1483. if (index >= plm.descs.count())
  1484. {
  1485. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1486. return nullptr;
  1487. }
  1488. if (index < plm.lv2Descs.count())
  1489. {
  1490. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1491. return plm.lv2Descs.getAt(index, nullptr);
  1492. }
  1493. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  1494. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  1495. CarlaString tmpURI;
  1496. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1497. tmpURI += pluginDesc->label;
  1498. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1499. const LV2_Descriptor lv2DescTmp = {
  1500. /* URI */ carla_strdup(tmpURI),
  1501. /* instantiate */ lv2_instantiate,
  1502. /* connect_port */ lv2_connect_port,
  1503. /* activate */ lv2_activate,
  1504. /* run */ lv2_run,
  1505. /* deactivate */ lv2_deactivate,
  1506. /* cleanup */ lv2_cleanup,
  1507. /* extension_data */ lv2_extension_data
  1508. };
  1509. LV2_Descriptor* lv2Desc;
  1510. try {
  1511. lv2Desc = new LV2_Descriptor;
  1512. } CARLA_SAFE_EXCEPTION_RETURN("new LV2_Descriptor", nullptr);
  1513. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1514. plm.lv2Descs.append(lv2Desc);
  1515. return lv2Desc;
  1516. }
  1517. CARLA_EXPORT
  1518. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1519. {
  1520. carla_debug("lv2ui_descriptor(%i)", index);
  1521. #ifdef CARLA_OS_LINUX
  1522. static const LV2UI_Descriptor lv2UiEmbedDesc = {
  1523. /* URI */ "http://kxstudio.sf.net/carla/ui-embed",
  1524. /* instantiate */ lv2ui_instantiate_embed,
  1525. /* cleanup */ lv2ui_cleanup,
  1526. /* port_event */ lv2ui_port_event,
  1527. /* extension_data */ lv2ui_extension_data
  1528. };
  1529. if (index == 0)
  1530. return &lv2UiEmbedDesc;
  1531. else
  1532. --index;
  1533. #endif
  1534. static const LV2UI_Descriptor lv2UiExtDesc = {
  1535. /* URI */ "http://kxstudio.sf.net/carla/ui-ext",
  1536. /* instantiate */ lv2ui_instantiate_external,
  1537. /* cleanup */ lv2ui_cleanup,
  1538. /* port_event */ lv2ui_port_event,
  1539. /* extension_data */ lv2ui_extension_data
  1540. };
  1541. return (index == 0) ? &lv2UiExtDesc : nullptr;
  1542. }
  1543. // -----------------------------------------------------------------------