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.

1658 lines
56KB

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