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.

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