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.

1257 lines
38KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013 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. #include "carla-native-base.cpp"
  18. #include "CarlaString.hpp"
  19. #include "lv2/atom.h"
  20. #include "lv2/atom-util.h"
  21. #include "lv2/buf-size.h"
  22. #include "lv2/instance-access.h"
  23. #include "lv2/midi.h"
  24. #include "lv2/options.h"
  25. #include "lv2/state.h"
  26. #include "lv2/time.h"
  27. #include "lv2/ui.h"
  28. #include "lv2/lv2_external_ui.h"
  29. #include "lv2/lv2_programs.h"
  30. using juce::FloatVectorOperations;
  31. // -----------------------------------------------------------------------
  32. // LV2 descriptor functions
  33. class NativePlugin : public LV2_External_UI_Widget
  34. {
  35. public:
  36. static const uint32_t kMaxMidiEvents = 512;
  37. NativePlugin(const PluginDescriptor* const desc, const double sampleRate, const char* const bundlePath, const LV2_Feature* const* features)
  38. : fHandle(nullptr),
  39. fDescriptor(desc),
  40. fMidiEventCount(0),
  41. fIsProcessing(false),
  42. fNeedsDryWetFix(false),
  43. fVolume(1.0f),
  44. fBufferSize(0),
  45. fSampleRate(sampleRate),
  46. fUridMap(nullptr)
  47. {
  48. run = extui_run;
  49. show = extui_show;
  50. hide = extui_hide;
  51. fHost.handle = this;
  52. fHost.resourceDir = carla_strdup(bundlePath);
  53. fHost.uiName = nullptr;
  54. fHost.get_buffer_size = host_get_buffer_size;
  55. fHost.get_sample_rate = host_get_sample_rate;
  56. fHost.is_offline = host_is_offline;
  57. fHost.get_time_info = host_get_time_info;
  58. fHost.write_midi_event = host_write_midi_event;
  59. fHost.ui_parameter_changed = host_ui_parameter_changed;
  60. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  61. fHost.ui_closed = host_ui_closed;
  62. fHost.ui_open_file = host_ui_open_file;
  63. fHost.ui_save_file = host_ui_save_file;
  64. fHost.dispatcher = host_dispatcher;
  65. const LV2_Options_Option* options = nullptr;
  66. const LV2_URID_Map* uridMap = nullptr;
  67. for (int i=0; features[i] != nullptr; ++i)
  68. {
  69. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  70. options = (const LV2_Options_Option*)features[i]->data;
  71. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  72. uridMap = (const LV2_URID_Map*)features[i]->data;
  73. }
  74. if (options == nullptr || uridMap == nullptr)
  75. {
  76. carla_stderr("Host don't provides option or urid-map features");
  77. return;
  78. }
  79. for (int i=0; options[i].key != 0; ++i)
  80. {
  81. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  82. {
  83. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  84. fBufferSize = *(const int*)options[i].value;
  85. else
  86. carla_stderr("Host provides maxBlockLength but has wrong value type");
  87. break;
  88. }
  89. }
  90. fUridMap = uridMap;
  91. fUI.portOffset += (desc->midiIns > 0) ? desc->midiIns : 1;
  92. fUI.portOffset += desc->midiOuts;
  93. fUI.portOffset += 1; // freewheel
  94. fUI.portOffset += desc->audioIns;
  95. fUI.portOffset += desc->audioOuts;
  96. }
  97. ~NativePlugin()
  98. {
  99. CARLA_ASSERT(fHandle == nullptr);
  100. if (fHost.resourceDir != nullptr)
  101. {
  102. delete[] fHost.resourceDir;
  103. fHost.resourceDir = nullptr;
  104. }
  105. }
  106. bool init()
  107. {
  108. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  109. {
  110. carla_stderr("Plugin is missing something...");
  111. return false;
  112. }
  113. if (fBufferSize == 0)
  114. {
  115. carla_stderr("Host is missing bufferSize feature");
  116. return false;
  117. }
  118. fHandle = fDescriptor->instantiate(&fHost);
  119. if (fHandle == nullptr)
  120. return false;
  121. carla_zeroStruct<MidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  122. carla_zeroStruct<TimeInfo>(fTimeInfo);
  123. fPorts.init(fDescriptor, fHandle);
  124. fUris.map(fUridMap);
  125. return true;
  126. }
  127. // -------------------------------------------------------------------
  128. // LV2 functions
  129. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  130. {
  131. fPorts.connectPort(fDescriptor, port, dataLocation);
  132. }
  133. void lv2_activate() const
  134. {
  135. if (fDescriptor->activate != nullptr)
  136. fDescriptor->activate(fHandle);
  137. }
  138. void lv2_deactivate() const
  139. {
  140. if (fDescriptor->deactivate != nullptr)
  141. fDescriptor->deactivate(fHandle);
  142. }
  143. void lv2_cleanup()
  144. {
  145. if (fDescriptor->cleanup != nullptr)
  146. fDescriptor->cleanup(fHandle);
  147. fHandle = nullptr;
  148. }
  149. void lv2_run(const uint32_t frames)
  150. {
  151. if (frames == 0)
  152. {
  153. updateParameterOutputs();
  154. return;
  155. }
  156. // Check for updated parameters
  157. float curValue;
  158. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  159. {
  160. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  161. curValue = *fPorts.paramsPtr[i];
  162. if (fPorts.paramsLast[i] != curValue && (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT) == 0)
  163. {
  164. fPorts.paramsLast[i] = curValue;
  165. fDescriptor->set_parameter_value(fHandle, i, curValue);
  166. }
  167. }
  168. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[0], iter)
  169. {
  170. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  171. if (event == nullptr)
  172. continue;
  173. if (event->body.size > 4)
  174. continue;
  175. if (event->time.frames >= frames)
  176. break;
  177. if (event->body.type == fUris.midiEvent)
  178. {
  179. if (fMidiEventCount >= kMaxMidiEvents*2)
  180. continue;
  181. const uint8_t* const data((const uint8_t*)(event + 1));
  182. fMidiEvents[fMidiEventCount].port = 0;
  183. fMidiEvents[fMidiEventCount].time = event->time.frames;
  184. fMidiEvents[fMidiEventCount].size = event->body.size;
  185. for (uint32_t i=0; i < event->body.size; ++i)
  186. fMidiEvents[fMidiEventCount].data[i] = data[i];
  187. fMidiEventCount += 1;
  188. continue;
  189. }
  190. if (event->body.type == fUris.atomBlank)
  191. {
  192. const LV2_Atom_Object* const obj((LV2_Atom_Object*)&event->body);
  193. if (obj->body.otype != fUris.timePos)
  194. continue;
  195. LV2_Atom* bar = nullptr;
  196. LV2_Atom* barBeat = nullptr;
  197. LV2_Atom* beatsPerBar = nullptr;
  198. LV2_Atom* bpm = nullptr;
  199. LV2_Atom* beatUnit = nullptr;
  200. LV2_Atom* frame = nullptr;
  201. LV2_Atom* speed = nullptr;
  202. lv2_atom_object_get(obj,
  203. fUris.timeBar, &bar,
  204. fUris.timeBarBeat, &barBeat,
  205. fUris.timeBeatsPerBar, &beatsPerBar,
  206. fUris.timeBeatsPerMinute, &bpm,
  207. fUris.timeBeatUnit, &beatUnit,
  208. fUris.timeFrame, &frame,
  209. fUris.timeSpeed, &speed,
  210. nullptr);
  211. if (bpm != nullptr && bpm->type == fUris.atomFloat)
  212. {
  213. fTimeInfo.bbt.beatsPerMinute = ((LV2_Atom_Float*)bpm)->body;
  214. fTimeInfo.bbt.valid = true;
  215. }
  216. if (beatsPerBar != nullptr && beatsPerBar->type == fUris.atomFloat)
  217. {
  218. float beatsPerBarValue = ((LV2_Atom_Float*)beatsPerBar)->body;
  219. fTimeInfo.bbt.beatsPerBar = beatsPerBarValue;
  220. if (bar != nullptr && bar->type == fUris.atomLong)
  221. {
  222. //float barValue = ((LV2_Atom_Long*)bar)->body;
  223. //curPosInfo.ppqPositionOfLastBarStart = barValue * beatsPerBarValue;
  224. if (barBeat != nullptr && barBeat->type == fUris.atomFloat)
  225. {
  226. //float barBeatValue = ((LV2_Atom_Float*)barBeat)->body;
  227. //curPosInfo.ppqPosition = curPosInfo.ppqPositionOfLastBarStart + barBeatValue;
  228. }
  229. }
  230. }
  231. if (beatUnit != nullptr && beatUnit->type == fUris.atomFloat)
  232. fTimeInfo.bbt.beatType = ((LV2_Atom_Float*)beatUnit)->body;
  233. if (frame != nullptr && frame->type == fUris.atomLong)
  234. fTimeInfo.frame = ((LV2_Atom_Long*)frame)->body;
  235. if (speed != nullptr && speed->type == fUris.atomFloat)
  236. fTimeInfo.playing = ((LV2_Atom_Float*)speed)->body == 1.0f;
  237. }
  238. }
  239. for (uint32_t i=1; i < fDescriptor->midiIns; ++i)
  240. {
  241. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[i], iter)
  242. {
  243. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  244. if (event == nullptr)
  245. continue;
  246. if (event->body.type != fUris.midiEvent)
  247. continue;
  248. if (event->body.size > 4)
  249. continue;
  250. if (event->time.frames >= frames)
  251. break;
  252. if (fMidiEventCount >= kMaxMidiEvents*2)
  253. break;
  254. const uint8_t* const data((const uint8_t*)(event + 1));
  255. fMidiEvents[fMidiEventCount].port = i;
  256. fMidiEvents[fMidiEventCount].time = event->time.frames;
  257. fMidiEvents[fMidiEventCount].size = event->body.size;
  258. for (uint32_t j=0; j < event->body.size; ++j)
  259. fMidiEvents[fMidiEventCount].data[j] = data[j];
  260. fMidiEventCount += 1;
  261. }
  262. }
  263. fIsProcessing = true;
  264. fDescriptor->process(fHandle, fPorts.audioIns, fPorts.audioOuts, frames, fMidiEvents, fMidiEventCount);
  265. fIsProcessing = false;
  266. if (fVolume != 1.0f)
  267. {
  268. for (uint32_t i=0; i < fDescriptor->audioOuts; ++i)
  269. FloatVectorOperations::multiply(fPorts.audioOuts[i], fVolume, frames);
  270. }
  271. updateParameterOutputs();
  272. }
  273. // -------------------------------------------------------------------
  274. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/) const
  275. {
  276. return 0;
  277. }
  278. uint32_t lv2_set_options(const LV2_Options_Option* const /*options*/)
  279. {
  280. return 0;
  281. }
  282. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index) const
  283. {
  284. if (fDescriptor->get_midi_program_count == nullptr)
  285. return nullptr;
  286. if (fDescriptor->get_midi_program_info == nullptr)
  287. return nullptr;
  288. if (index >= fDescriptor->get_midi_program_count(fHandle))
  289. return nullptr;
  290. const MidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  291. if (midiProg == nullptr)
  292. return nullptr;
  293. static LV2_Program_Descriptor progDesc;
  294. progDesc.bank = midiProg->bank;
  295. progDesc.program = midiProg->program;
  296. progDesc.name = midiProg->name;
  297. return &progDesc;
  298. }
  299. void lv2_select_program(uint32_t bank, uint32_t program) const
  300. {
  301. if (fDescriptor->set_midi_program == nullptr)
  302. return;
  303. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  304. }
  305. 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
  306. {
  307. return LV2_STATE_ERR_UNKNOWN;
  308. }
  309. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function /*retrieve*/, const LV2_State_Handle /*handle*/, const uint32_t /*flags*/, const LV2_Feature* const* const /*features*/) const
  310. {
  311. return LV2_STATE_ERR_UNKNOWN;
  312. }
  313. // -------------------------------------------------------------------
  314. bool lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller, LV2UI_Widget* widget,
  315. const LV2_Feature* const* features)
  316. {
  317. for (int i=0; features[i] != nullptr; ++i)
  318. {
  319. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  320. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  321. {
  322. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  323. break;
  324. }
  325. }
  326. if (fUI.host == nullptr)
  327. return false;
  328. fUI.writeFunction = writeFunction;
  329. fUI.controller = controller;
  330. *widget = this;
  331. fHost.uiName = fUI.host->plugin_human_id;
  332. return true;
  333. }
  334. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  335. {
  336. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  337. return;
  338. if (portIndex >= fUI.portOffset || ! fUI.isVisible)
  339. return;
  340. if (fDescriptor->ui_set_parameter_value == nullptr)
  341. return;
  342. const float value(*(const float*)buffer);
  343. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fUI.portOffset, value);
  344. }
  345. void lv2ui_cleanup()
  346. {
  347. fUI.host = nullptr;
  348. fUI.writeFunction = nullptr;
  349. fUI.controller = nullptr;
  350. if (! fUI.isVisible)
  351. return;
  352. if (fDescriptor->ui_show != nullptr)
  353. fDescriptor->ui_show(fHandle, false);
  354. fUI.isVisible = false;
  355. }
  356. // -------------------------------------------------------------------
  357. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  358. {
  359. if (fDescriptor->ui_set_midi_program == nullptr)
  360. return;
  361. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  362. }
  363. // -------------------------------------------------------------------
  364. protected:
  365. void handleUiRun()
  366. {
  367. if (fDescriptor->ui_idle != nullptr)
  368. fDescriptor->ui_idle(fHandle);
  369. }
  370. void handleUiShow()
  371. {
  372. if (fDescriptor->ui_show != nullptr)
  373. fDescriptor->ui_show(fHandle, true);
  374. fUI.isVisible = true;
  375. }
  376. void handleUiHide()
  377. {
  378. if (fDescriptor->ui_show != nullptr)
  379. fDescriptor->ui_show(fHandle, false);
  380. fUI.isVisible = false;
  381. }
  382. // -------------------------------------------------------------------
  383. uint32_t handleGetBufferSize() const
  384. {
  385. return fBufferSize;
  386. }
  387. double handleGetSampleRate() const
  388. {
  389. return fSampleRate;
  390. }
  391. bool handleIsOffline() const
  392. {
  393. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  394. return (fPorts.freewheel != nullptr && *fPorts.freewheel >= 0.5f);
  395. }
  396. const TimeInfo* handleGetTimeInfo() const
  397. {
  398. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  399. return &fTimeInfo;
  400. }
  401. bool handleWriteMidiEvent(const MidiEvent* const event)
  402. {
  403. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  404. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  405. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  406. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  407. // reverse-find first free event, and put it there
  408. for (uint32_t i=(kMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  409. {
  410. if (fMidiEvents[i].data[0] == 0)
  411. {
  412. std::memcpy(&fMidiEvents[i], event, sizeof(MidiEvent));
  413. return true;
  414. }
  415. }
  416. return false;
  417. }
  418. void handleUiParameterChanged(const uint32_t index, const float value)
  419. {
  420. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  421. fUI.writeFunction(fUI.controller, index+fUI.portOffset, sizeof(float), 0, &value);
  422. }
  423. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/)
  424. {
  425. //storeCustomData(key, value);
  426. }
  427. void handleUiClosed()
  428. {
  429. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  430. fUI.host->ui_closed(fUI.controller);
  431. fUI.host = nullptr;
  432. fUI.writeFunction = nullptr;
  433. fUI.controller = nullptr;
  434. fUI.isVisible = false;
  435. }
  436. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  437. {
  438. // TODO
  439. return nullptr;
  440. }
  441. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  442. {
  443. // TODO
  444. return nullptr;
  445. }
  446. intptr_t handleDispatcher(const HostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  447. {
  448. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  449. intptr_t ret = 0;
  450. switch (opcode)
  451. {
  452. case HOST_OPCODE_NULL:
  453. break;
  454. case HOST_OPCODE_SET_VOLUME:
  455. fVolume = opt;
  456. break;
  457. case HOST_OPCODE_SET_DRYWET:
  458. carla_stdout("Plugin asked dryWet custom value %f", opt);
  459. fNeedsDryWetFix = true;
  460. break;
  461. case HOST_OPCODE_SET_BALANCE_LEFT:
  462. case HOST_OPCODE_SET_BALANCE_RIGHT:
  463. case HOST_OPCODE_SET_PANNING:
  464. // nothing
  465. break;
  466. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  467. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  468. case HOST_OPCODE_SET_PROCESS_PRECISION:
  469. case HOST_OPCODE_UPDATE_PARAMETER:
  470. case HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  471. case HOST_OPCODE_RELOAD_PARAMETERS:
  472. case HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  473. case HOST_OPCODE_RELOAD_ALL:
  474. // nothing
  475. break;
  476. case HOST_OPCODE_UI_UNAVAILABLE:
  477. handleUiClosed();
  478. break;
  479. }
  480. return ret;
  481. // unused for now
  482. (void)index;
  483. (void)value;
  484. (void)ptr;
  485. }
  486. void updateParameterOutputs()
  487. {
  488. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  489. {
  490. if (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT)
  491. {
  492. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  493. if (fPorts.paramsPtr[i] != nullptr)
  494. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  495. }
  496. }
  497. }
  498. // -------------------------------------------------------------------
  499. private:
  500. // Native data
  501. PluginHandle fHandle;
  502. HostDescriptor fHost;
  503. const PluginDescriptor* const fDescriptor;
  504. uint32_t fMidiEventCount;
  505. MidiEvent fMidiEvents[kMaxMidiEvents*2];
  506. TimeInfo fTimeInfo;
  507. bool fIsProcessing;
  508. bool fNeedsDryWetFix;
  509. float fVolume;
  510. // Lv2 host data
  511. uint32_t fBufferSize;
  512. double fSampleRate;
  513. const LV2_URID_Map* fUridMap;
  514. struct URIDs {
  515. LV2_URID atomBlank;
  516. LV2_URID atomFloat;
  517. LV2_URID atomLong;
  518. LV2_URID atomSequence;
  519. LV2_URID midiEvent;
  520. LV2_URID timePos;
  521. LV2_URID timeBar;
  522. LV2_URID timeBarBeat;
  523. LV2_URID timeBeatsPerBar;
  524. LV2_URID timeBeatsPerMinute;
  525. LV2_URID timeBeatUnit;
  526. LV2_URID timeFrame;
  527. LV2_URID timeSpeed;
  528. URIDs()
  529. : atomBlank(0),
  530. atomFloat(0),
  531. atomLong(0),
  532. atomSequence(0),
  533. midiEvent(0),
  534. timePos(0),
  535. timeBar(0),
  536. timeBarBeat(0),
  537. timeBeatsPerBar(0),
  538. timeBeatsPerMinute(0),
  539. timeBeatUnit(0),
  540. timeFrame(0),
  541. timeSpeed(0) {}
  542. void map(const LV2_URID_Map* const uridMap)
  543. {
  544. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  545. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  546. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  547. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  548. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  549. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  550. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  551. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  552. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  553. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  554. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  555. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  556. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  557. }
  558. } fUris;
  559. struct UI {
  560. const LV2_External_UI_Host* host;
  561. LV2UI_Write_Function writeFunction;
  562. LV2UI_Controller controller;
  563. uint32_t portOffset;
  564. bool isVisible;
  565. UI()
  566. : host(nullptr),
  567. writeFunction(nullptr),
  568. controller(nullptr),
  569. portOffset(0),
  570. isVisible(false) {}
  571. } fUI;
  572. struct Ports {
  573. LV2_Atom_Sequence** eventsIn;
  574. LV2_Atom_Sequence** midiOuts;
  575. float** audioIns;
  576. float** audioOuts;
  577. float* freewheel;
  578. uint32_t paramCount;
  579. float* paramsLast;
  580. float** paramsPtr;
  581. Ports()
  582. : eventsIn(nullptr),
  583. midiOuts(nullptr),
  584. audioIns(nullptr),
  585. audioOuts(nullptr),
  586. freewheel(nullptr),
  587. paramCount(0),
  588. paramsLast(nullptr),
  589. paramsPtr(nullptr) {}
  590. ~Ports()
  591. {
  592. if (eventsIn != nullptr)
  593. {
  594. delete[] eventsIn;
  595. eventsIn = nullptr;
  596. }
  597. if (midiOuts != nullptr)
  598. {
  599. delete[] midiOuts;
  600. midiOuts = nullptr;
  601. }
  602. if (audioIns != nullptr)
  603. {
  604. delete[] audioIns;
  605. audioIns = nullptr;
  606. }
  607. if (audioOuts != nullptr)
  608. {
  609. delete[] audioOuts;
  610. audioOuts = nullptr;
  611. }
  612. if (paramsLast != nullptr)
  613. {
  614. delete[] paramsLast;
  615. paramsLast = nullptr;
  616. }
  617. if (paramsPtr != nullptr)
  618. {
  619. delete[] paramsPtr;
  620. paramsPtr = nullptr;
  621. }
  622. }
  623. void init(const PluginDescriptor* const desc, PluginHandle handle)
  624. {
  625. CARLA_SAFE_ASSERT_RETURN(desc != nullptr && handle != nullptr,)
  626. if (desc->midiIns > 0)
  627. {
  628. eventsIn = new LV2_Atom_Sequence*[desc->midiIns];
  629. for (uint32_t i=0; i < desc->midiIns; ++i)
  630. eventsIn[i] = nullptr;
  631. }
  632. else
  633. {
  634. eventsIn = new LV2_Atom_Sequence*[1];
  635. eventsIn[0] = nullptr;
  636. }
  637. if (desc->midiOuts > 0)
  638. {
  639. midiOuts = new LV2_Atom_Sequence*[desc->midiOuts];
  640. for (uint32_t i=0; i < desc->midiOuts; ++i)
  641. midiOuts[i] = nullptr;
  642. }
  643. if (desc->audioIns > 0)
  644. {
  645. audioIns = new float*[desc->audioIns];
  646. for (uint32_t i=0; i < desc->audioIns; ++i)
  647. audioIns[i] = nullptr;
  648. }
  649. if (desc->audioOuts > 0)
  650. {
  651. audioOuts = new float*[desc->audioOuts];
  652. for (uint32_t i=0; i < desc->audioOuts; ++i)
  653. audioOuts[i] = nullptr;
  654. }
  655. if (desc->get_parameter_count != nullptr && desc->get_parameter_info != nullptr && desc->get_parameter_value != nullptr && desc->set_parameter_value != nullptr)
  656. {
  657. paramCount = desc->get_parameter_count(handle);
  658. if (paramCount > 0)
  659. {
  660. paramsLast = new float[paramCount];
  661. paramsPtr = new float*[paramCount];
  662. for (uint32_t i=0; i < paramCount; ++i)
  663. {
  664. paramsLast[i] = desc->get_parameter_value(handle, i);
  665. paramsPtr[i] = nullptr;
  666. }
  667. }
  668. }
  669. }
  670. void connectPort(const PluginDescriptor* const desc, const uint32_t port, void* const dataLocation)
  671. {
  672. uint32_t index = 0;
  673. if (port == index++)
  674. {
  675. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  676. return;
  677. }
  678. for (uint32_t i=1; i < desc->midiIns; ++i)
  679. {
  680. if (port == index++)
  681. {
  682. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  683. return;
  684. }
  685. }
  686. for (uint32_t i=0; i < desc->midiOuts; ++i)
  687. {
  688. if (port == index++)
  689. {
  690. midiOuts[i] = (LV2_Atom_Sequence*)dataLocation;
  691. return;
  692. }
  693. }
  694. if (port == index++)
  695. {
  696. freewheel = (float*)dataLocation;
  697. return;
  698. }
  699. for (uint32_t i=0; i < desc->audioIns; ++i)
  700. {
  701. if (port == index++)
  702. {
  703. audioIns[i] = (float*)dataLocation;
  704. return;
  705. }
  706. }
  707. for (uint32_t i=0; i < desc->audioOuts; ++i)
  708. {
  709. if (port == index++)
  710. {
  711. audioOuts[i] = (float*)dataLocation;
  712. return;
  713. }
  714. }
  715. for (uint32_t i=0; i < paramCount; ++i)
  716. {
  717. if (port == index++)
  718. {
  719. paramsPtr[i] = (float*)dataLocation;
  720. return;
  721. }
  722. }
  723. }
  724. } fPorts;
  725. // -------------------------------------------------------------------
  726. #define handlePtr ((NativePlugin*)_this_)
  727. static void extui_run(LV2_External_UI_Widget* _this_)
  728. {
  729. handlePtr->handleUiRun();
  730. }
  731. static void extui_show(LV2_External_UI_Widget* _this_)
  732. {
  733. handlePtr->handleUiShow();
  734. }
  735. static void extui_hide(LV2_External_UI_Widget* _this_)
  736. {
  737. handlePtr->handleUiHide();
  738. }
  739. #undef handlePtr
  740. // -------------------------------------------------------------------
  741. #define handlePtr ((NativePlugin*)handle)
  742. static uint32_t host_get_buffer_size(HostHandle handle)
  743. {
  744. return handlePtr->handleGetBufferSize();
  745. }
  746. static double host_get_sample_rate(HostHandle handle)
  747. {
  748. return handlePtr->handleGetSampleRate();
  749. }
  750. static bool host_is_offline(HostHandle handle)
  751. {
  752. return handlePtr->handleIsOffline();
  753. }
  754. static const TimeInfo* host_get_time_info(HostHandle handle)
  755. {
  756. return handlePtr->handleGetTimeInfo();
  757. }
  758. static bool host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  759. {
  760. return handlePtr->handleWriteMidiEvent(event);
  761. }
  762. static void host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  763. {
  764. handlePtr->handleUiParameterChanged(index, value);
  765. }
  766. static void host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  767. {
  768. handlePtr->handleUiCustomDataChanged(key, value);
  769. }
  770. static void host_ui_closed(HostHandle handle)
  771. {
  772. handlePtr->handleUiClosed();
  773. }
  774. static const char* host_ui_open_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  775. {
  776. return handlePtr->handleUiOpenFile(isDir, title, filter);
  777. }
  778. static const char* host_ui_save_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  779. {
  780. return handlePtr->handleUiSaveFile(isDir, title, filter);
  781. }
  782. static intptr_t host_dispatcher(HostHandle handle, HostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  783. {
  784. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  785. }
  786. #undef handlePtr
  787. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  788. };
  789. // -----------------------------------------------------------------------
  790. // LV2 descriptor functions
  791. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  792. {
  793. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  794. const PluginDescriptor* pluginDesc = nullptr;
  795. const char* pluginLabel = nullptr;
  796. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  797. pluginLabel = lv2Descriptor->URI+37;
  798. else if (std::strcmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla") == 0)
  799. pluginLabel = lv2Descriptor->URI+23;
  800. if (pluginLabel == nullptr)
  801. {
  802. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  803. return nullptr;
  804. }
  805. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  806. for (NonRtList<const PluginDescriptor*>::Itenerator it = sPluginDescsMgr.descs.begin(); it.valid(); it.next())
  807. {
  808. const PluginDescriptor*& tmpDesc(*it);
  809. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  810. {
  811. pluginDesc = tmpDesc;
  812. break;
  813. }
  814. }
  815. if (pluginDesc == nullptr)
  816. {
  817. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  818. return nullptr;
  819. }
  820. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  821. if (! plugin->init())
  822. {
  823. carla_stderr("Failed to init plugin");
  824. delete plugin;
  825. return nullptr;
  826. }
  827. return (LV2_Handle)plugin;
  828. }
  829. #define instancePtr ((NativePlugin*)instance)
  830. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  831. {
  832. instancePtr->lv2_connect_port(port, dataLocation);
  833. }
  834. static void lv2_activate(LV2_Handle instance)
  835. {
  836. carla_debug("lv2_activate(%p)", instance);
  837. instancePtr->lv2_activate();
  838. }
  839. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  840. {
  841. instancePtr->lv2_run(sampleCount);
  842. }
  843. static void lv2_deactivate(LV2_Handle instance)
  844. {
  845. carla_debug("lv2_deactivate(%p)", instance);
  846. instancePtr->lv2_deactivate();
  847. }
  848. static void lv2_cleanup(LV2_Handle instance)
  849. {
  850. carla_debug("lv2_cleanup(%p)", instance);
  851. instancePtr->lv2_cleanup();
  852. delete instancePtr;
  853. }
  854. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  855. {
  856. carla_debug("lv2_get_options(%p, %p)", instance, options);
  857. return instancePtr->lv2_get_options(options);
  858. }
  859. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  860. {
  861. carla_debug("lv2_set_options(%p, %p)", instance, options);
  862. return instancePtr->lv2_set_options(options);
  863. }
  864. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  865. {
  866. carla_debug("lv2_get_program(%p, %i)", instance, index);
  867. return instancePtr->lv2_get_program(index);
  868. }
  869. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  870. {
  871. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  872. return instancePtr->lv2_select_program(bank, program);
  873. }
  874. 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)
  875. {
  876. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  877. return instancePtr->lv2_save(store, handle, flags, features);
  878. }
  879. 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)
  880. {
  881. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  882. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  883. }
  884. static const void* lv2_extension_data(const char* uri)
  885. {
  886. carla_debug("lv2_extension_data(\"%s\")", uri);
  887. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  888. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  889. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  890. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  891. return &options;
  892. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  893. return &programs;
  894. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  895. return &state;
  896. return nullptr;
  897. }
  898. #undef instancePtr
  899. // -----------------------------------------------------------------------
  900. // Startup code
  901. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*, LV2UI_Write_Function writeFunction,
  902. LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  903. {
  904. NativePlugin* plugin = nullptr;
  905. for (int i=0; features[i] != nullptr; ++i)
  906. {
  907. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  908. {
  909. plugin = (NativePlugin*)features[i]->data;
  910. break;
  911. }
  912. }
  913. if (plugin == nullptr)
  914. {
  915. carla_stderr("Host doesn't support instance-access, cannot show UI");
  916. return nullptr;
  917. }
  918. if (! plugin->lv2ui_instantiate(writeFunction, controller, widget, features))
  919. {
  920. carla_stderr("Host doesn't support external UI");
  921. return nullptr;
  922. }
  923. return (LV2UI_Handle)plugin;
  924. }
  925. #define uiPtr ((NativePlugin*)ui)
  926. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  927. {
  928. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  929. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  930. }
  931. static void lv2ui_cleanup(LV2UI_Handle ui)
  932. {
  933. carla_debug("lv2ui_cleanup(%p)", ui);
  934. uiPtr->lv2ui_cleanup();
  935. }
  936. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  937. {
  938. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  939. uiPtr->lv2ui_select_program(bank, program);
  940. }
  941. static const void* lv2ui_extension_data(const char* uri)
  942. {
  943. carla_debug("lv2ui_extension_data(\"%s\")", uri);
  944. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  945. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  946. return &uiprograms;
  947. return nullptr;
  948. }
  949. #undef uiPtr
  950. // -----------------------------------------------------------------------
  951. // Startup code
  952. CARLA_EXPORT
  953. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  954. {
  955. carla_debug("lv2_descriptor(%i)", index);
  956. if (index >= sPluginDescsMgr.descs.count())
  957. {
  958. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  959. return nullptr;
  960. }
  961. if (index < sPluginDescsMgr.lv2Descs.count())
  962. {
  963. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  964. return sPluginDescsMgr.lv2Descs.getAt(index);
  965. }
  966. const PluginDescriptor*& pluginDesc(sPluginDescsMgr.descs.getAt(index));
  967. CarlaString tmpURI;
  968. if (std::strcmp(pluginDesc->label, "carla") == 0)
  969. {
  970. tmpURI = "http://kxstudio.sf.net/carla";
  971. }
  972. else
  973. {
  974. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  975. tmpURI += pluginDesc->label;
  976. }
  977. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  978. const LV2_Descriptor* const lv2Desc(new const LV2_Descriptor{
  979. /* URI */ carla_strdup(tmpURI),
  980. /* instantiate */ lv2_instantiate,
  981. /* connect_port */ lv2_connect_port,
  982. /* activate */ lv2_activate,
  983. /* run */ lv2_run,
  984. /* deactivate */ lv2_deactivate,
  985. /* cleanup */ lv2_cleanup,
  986. /* extension_data */ lv2_extension_data
  987. });
  988. sPluginDescsMgr.lv2Descs.append(lv2Desc);
  989. return lv2Desc;
  990. }
  991. CARLA_EXPORT
  992. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  993. {
  994. carla_debug("lv2ui_descriptor(%i)", index);
  995. static const LV2UI_Descriptor lv2UiDesc = {
  996. /* URI */ "http://kxstudio.sf.net/carla#UI",
  997. /* instantiate */ lv2ui_instantiate,
  998. /* cleanup */ lv2ui_cleanup,
  999. /* port_event */ lv2ui_port_event,
  1000. /* extension_data */ lv2ui_extension_data
  1001. };
  1002. static const LV2UI_Descriptor lv2UiDescOld = {
  1003. /* URI */ "http://kxstudio.sf.net/carla#UIold",
  1004. /* instantiate */ lv2ui_instantiate,
  1005. /* cleanup */ lv2ui_cleanup,
  1006. /* port_event */ lv2ui_port_event,
  1007. /* extension_data */ lv2ui_extension_data
  1008. };
  1009. switch (index)
  1010. {
  1011. case 0:
  1012. return &lv2UiDesc;
  1013. case 1:
  1014. return &lv2UiDescOld;
  1015. default:
  1016. return nullptr;
  1017. }
  1018. }
  1019. // -----------------------------------------------------------------------