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.

1336 lines
41KB

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